We use cookies. More

Python For Loop: How For Loops in Python Work

For loops are among the most commonly used structures in almost any programming language, but the way they work may show slight difference from one language to another. In this tutorial, we take a look at how Python for loops work by giving some examples.

Advertisement

For loop is one of the type of loops that is used in programming for running the same code block a number of times, which is crucial in many web design and development related tasks. The for statement can be considered as a control flow tool in coding, which dictates how the flow of the code or content will be by executing the same piece of code based on a counter or the number of array or string elements.

If you are familiar with programming with at least one other language, when you think of a for loop, the first thing that comes to your mind is probably an iteration of a code block that starts from a certain number till the target number is reached. For example, the following for statement in JavaScript starts the iteration with i = 1 and continues to iterate till i = 10 by incrementing i by 1 at each iteration, resulting in 10 total iterations:

for (var i = 1; i <= 10; i++) {
  ...
}

Though, it conceptually functions more or less the same in all languages, for loops in Python work slightly differently. Python for loops can not only iterate over a range of progressive numbers (1, 2, ..., 10), but they can also iterate over sequential items such as strings (e.g. 'mystring') or lists (e.g. ['reg', 'green', 'blue']).

TIP: Arrays are called as lists in Python.

This makes for loops in Python even more versatile so that they can easily be used for a number of scenarios without needing any other constructs. Now, let's see some examples, covering different cases.

Python For Loop Examples

Since Python for loops can be used in different types of iterations, we will give some examples for each case to give you a broader perspective of how to use for loops in your Python projects.

Python For Loop with Strings

As we also mentioned above, you can use for loops in Python to iterate over the elements of strings. For example, if you wanted to print all the characters of a string separately, you can use something like the following:

for c in 'summer':
  print c

The purpose of the above code is to print all letters in the word summer. When you run the above code, you will get the following on the screen:

s
u
m
m
e
r

Python For Loop with Lists

Another quite handy way for using for loops in Python is to use a list of items for the loop. For example, if you have a list of numbers and want to print only the even ones, you can use the following for loop:

numbers = [1, 2, 3, 4, 5,]
for n in numbers:
  if n % 2 == 0:
    print n

The above code iterates through each number in the numbers list and prints if it is an even number. The result is:

2
4

Python For Loop with Ranges

Remember the first example we gave about a JavaScript for loop? For loops with ranges in Python work quite similar, except for their syntax. In Python, the range() function is used to specify the range of the loop. For example, if you wanted to print numbers from 0 to 3, you would need to use this code:

for i in range(4):
  print i

The value of the range() function specifies the end value, without including it. The result of the above code will be:

0
1
2
3

Python For Loop with Else Statement

The else statement in programming is usually linked to the if statement, but in Python, you can use it with for statement too. For example, if you wanted to find the prime numbers between 1 and 12, you could use the following code:

for n in range(2, 13):
  for k in range(2, n):
    if n % k == 0:
      break
  else:
    print n

It will output the following:

2
3
5
7
11

Python Nested For Loop

Our final example will cover nested for loops in Python. For example, you can create a nested loop for printing numbers up to j for i times using the following:

for i in range(1,3):
    for j in range(1,4):
        print j,
    print

The result is:

1 2 3
1 2 3

This concludes our tutorial about for loops in Python. These are some of the most commonly used Python for loops and hopefully they will help you in your programming projects.

Also Check:
Fast Domain Name Checker Tools
List of Free FTP Software
How to Create a Basic HTML Website
Free Ways to Advertise Your Site
HTTP Status Codes and Errors

Advertisement

Was this post helpful?

If you found this post to be helpful, please share it! Thank you :)

f t g+ in