Python for Loops
POSTED ON FEBRUARY 28, 2022
python-for-loop-tutorial-overview

In this guide, we're going to explore Python for loops!

What exactly is a for loop? In Python, these loops are used to iterate over a sequence (for a list, tuple, set, etc.) and other iterable objects.

Python programming language makes it easy to read and run code. And by learning how to use for loops, you'll be that much closer to creating your own fun games, developing websites, analyzing data, and so much more!

If your looking for more hands on help we offer courses for kids that teach Python programming including:

Python Level 1: Graphics Superstar Python Level 2: Problem Solver Python Level 3: Algorithms Master PyGame: Python Game Development Intro to Robotics with Python Data Science in Python

But if you're ready to start our adventure into Python loops, let's jump into it!

Why Learn Python For Loops?

As a high-level programming language, Python is easy to learn for kid coders and beginners of all ages, opening up doors to computational thinking and creative expression.

For loops allow you to execute a series of statements multiple times. In other words, it helps to run over the contents of a Python list, string, and other data types until it's complete. The code is abbreviated, so it makes it easier to repeat similar operations.

With this trick in the bag, an aspiring coder can boost their skills! From video game development to web design, Python prepares you for more advanced programming and a host of careers in development, data science, and more!

What You Need to Start

Python is an open-source programming language free for anybody to use!

Looking for the latest version of Python 3? You can download Python from the official website. It runs on Windows, MacOS, Linux, and so on. The setup process includes links to Python documentation and a handy online Python tutorial.

New to the world of Python? check out the following:

Python Coding 101 for Kids (age 11+)

Python Virtual Zoom Background (beginner level)

What Python Is Used For (major industries)

For more advanced Python tutorials, check out the following:

Python Pet Simulator (intermediate)

Python Rock Paper Scissors Game (intermediate)

How to Graph in Python (intermediate to advanced level)

Coding can be intimidating at first. Don't worry! With the proper practice and instruction, a young programmer can learn Python easily & confidently.

Types of Python Loops

There are technically two main types of Python looping: for loops and while loops. However, there is a third kind called a nested loop. Let's check them out!

For Loops

As we've already mentioned, for loops allow you to iterate through a collection of statements. A key characteristic of this type of loop is that it’s used for sequential iteration. A for loop is handy when you want to iterate something for a known number of times.

For example, if you want to generate a sequence of numbers, you can use a for loop to iterate over each number in the sequence.

While Loops

Next, there's the while loop. This kind of loop is used to repeat a specific block of code a repeated number of times as long as the condition is true. If the condition is false, the line of code prior to the loop is executed by the program.

Along with syntax, the main difference between a while loop and a for loop is that a while loop is used when the number of times of iteration is unknown.

Nested Loops

Last but not least, we have nested loops. This is a loop inside a loop. The inner loop or outer loop can either be a for loop or while loop. The inner loop is executed each time the outer loop is executed.

A nested loop can be used for multidimensional data structures. For example, it can be used to produce a multiplication table.

Python Terms and Concepts

Let's check out some Python functions and terms:

  1. Break statement: Stops a loop from running.

  2. Continue statement: Makes a loop continue to the next iteration.

  3. If else statement: Evaluates whether an expression is true or false.

  4. For loop: Used for iterating over a sequence.

  5. Iteration: The process of repeatedly executing a set of statements.

  6. Loop statement: Allows the execution of a statement or collection of statements.

  7. Loop variable: Set to execute some iterations of a for loop.

  8. Nested loop: A loop within a loop.

  9. Pass statement: This is useful when planning to use a function in the future.

  10. While loop: Used to repeat a block of code repeatedly until a condition is met.

Want to discover more definitions? Refer to the Python glossary.

Python Tutorial: For Loop

In the following examples, you'll learn the basics of for loops!

List

python-loops-list-example

In the above example, we see a list of vegetables. Using for loop syntax—in this example, "for x in vegetables"—we can print out a list of the vegetables.

example-python-list-output

Once you input and run this code, you should be presented with the above output.

String

python-string-example-loops

A string is a sequence of characters. In the above example, we can iterate the letters of the word 'carrots' into a final output.

string-output-example

When you run this code, you'll see the above output with each letter on its own line.

Break Statement

loops-break-statement-code-example

A break statement allows a coder to stop a loop from completely running. In the above example, the code stops at 'onion'.

break-statement-example-output

As you can see in the above output, it omits the third and final item of carrots.

Else Statement

else-statement-example

In this example, the code includes an else clause. The else block of code results in a specific action to occur when the loop is finished.

else-statement-output-example

In the above case, the output window displays a range of 5 numbers. It's then followed by a 'Completed!' message.

Pass Statement

pass-statement-loops-example-code

Since for loops cannot be empty, a pass statement can be handy for code you want to implement or modify in the future. In the above example, we use three odd numbers (1, 3, and 5) to write our pass statement code.

pass-statement-output

As you can see in the above image, the output screen is blank. This is a good thing! If you didn't use a pass statement, it would display an error message.

You’ve Done It! Now Keep Practicing

Great job!

You now know how a for loop works. Continue to learn more Python concepts and techniques. This will help you become a confident coder!

At Juni Learning, we offer Python courses and other popular programming languages (including Scratch, Java, and Javascript) to aspiring programmers ages 8-18. With 1:1 guidance from a friendly instructor, a student can excel at Python and more!

Explore more of our online courses, or if you're not sure where to start contact our Advisors by calling (650) 263-4306 or emailing advisors@learnwithjuni.com to learn which courses are best for your student’s coding journey.


Related Reading