Python For Loop: Basics, Syntax, Range, And Examples

//

Thomas

Affiliate disclosure: As an Amazon Associate, we may earn commissions from qualifying Amazon.com purchases

Discover the power of Python for loop! This post covers everything you need to know about the basics, syntax, and range function of for loop. Explore examples of using for loop to print numbers, generate lists, iterate over strings, and more.

Basics of Python for Loop

Loops are an essential part of every programming language. In Python, the for loop is one of the most commonly used constructs for iterating over sequences. In this section, we will cover the basics of Python for loop, including its syntax, range function, and how to loop through sequences.

Syntax of for loop in Python

The syntax of the for loop in Python is straightforward. Here is the general structure:

for variable in sequence:
# code block to be executed

The variable in the loop header is the variable that will be assigned the value of each item in the sequence. The sequence could be a list, tuple, dictionary, or any other iterable object. The code block following the colon is the set of instructions that will be executed for each item in the sequence.

Range function in Python

The range function in Python is used to generate a sequence of numbers. The function takes three arguments: start, stop, and step. Here is the general syntax:

range(start, stop, step)

The start argument specifies the starting number of the sequence (default is 0), stop argument specifies the ending number of the sequence (exclusive), and step argument specifies the increment between each number (default is 1).

For example, the following code generates a sequence of numbers from 0 to 9:

PYTHON

for i in range(10):
print(i)

The output of the above code will be:

0
1
2
3
4
5
6
7
8
9

Looping through a sequence in Python

The for loop in Python can be used to iterate over any sequence, including lists, tuples, and strings. Here is an example of how to loop through a list:

PYTHON

fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit)

The output of the above code will be:

apple
banana
cherry

You can also loop through a string and print each character:

PYTHON

word = 'Python'
for letter in word:
print(letter)

The output of the above code will be:

P
y
t
h
o
n

Using the for Loop to Print Numbers from 1 to 10

If you’re new to Python, one of the first things you’ll learn is how to use a for loop to print a sequence of numbers. This is a fundamental concept that will set the foundation for more complex programming tasks.

Simple for loop to print numbers

The simplest way to print numbers using a for loop in Python is to define a range of numbers and loop through each number using the “range” function. Here’s an example:

for i in range(1, 11):
print(i)

This code will print the numbers 1 through 10, with each number on a new line.

Using range function to print numbers

The “range” function is a built-in function in Python that generates a sequence of numbers. It takes three arguments: start, stop, and step.

The start argument is the starting number of the sequence (inclusive), the stop argument is the ending number (exclusive), and the step argument is the increment between each number in the sequence.

Here’s an example of using the range function to print every other number between 1 and 10:

for i in range(1, 11, 2):
print(i)

This code will print the numbers 1, 3, 5, 7, and 9.

Customizing the range of numbers to be printed

You can customize the range of numbers to be printed by changing the arguments in the range function. For example, you can print the numbers from 5 to 15:

for i in range(5, 16):
print(i)

This code will print the numbers 5 through 15, with each number on a new line.

You can also print the numbers in reverse order by using a negative step:

for i in range(10, 0, -1):
print(i)

This code will print the numbers 10 through 1, with each number on a new line.


Using the for Loop to Print Even and Odd Numbers

When programming in Python, one of the most commonly used loops is the for loop. This loop is often used to iterate over a sequence of elements and perform some action on each element. One common task that can be performed using the for loop is to print even and odd numbers. In this section, we will explore how to use the for loop to print even and odd numbers in Python.

Using if statement to print even numbers

To print even numbers using the for loop, we can use an if statement to check if the current number is even. If it is, we can print the number. Otherwise, we can skip it. Here is an example:

for i in range(1, 11):
if i % 2 == 0:
print(i)

In this example, we are using the range function to generate a sequence of numbers from 1 to 10. We then use an if statement to check if the current number is even by checking if the remainder of the number divided by 2 is 0. If it is, we print the number.

Using if-else statement to print odd numbers

To print odd numbers using the for loop, we can use an if-else statement to check if the current number is odd or even. If it is odd, we can print the number. Otherwise, we can skip it. Here is an example:

for i in range(1, 11):
if i % 2 != 0:
print(i)
else:
continue

In this example, we are using the same range function to generate a sequence of numbers from 1 to 10. We then use an if-else statement to check if the current number is odd by checking if the remainder of the number divided by 2 is not 0. If it is not, we print the number. Otherwise, we use the continue keyword to skip to the next iteration of the loop.

Printing alternating even and odd numbers

To print alternating even and odd numbers using the for loop, we can use a combination of the if and if-else statements. Here is an example:

for i in range(1, 11):
if i % 2 == 0:
print(i)
else:
print(i+1)

In this example, we are using the same range function to generate a sequence of numbers from 1 to 10. We then use an if statement to check if the current number is even by checking if the remainder of the number divided by 2 is 0. If it is, we print the number. Otherwise, we add 1 to the number and print it.

Using the for loop to print even and odd numbers is a common task in Python programming. By using if and if-else statements, we can easily print even and odd numbers or alternating even and odd numbers. These techniques can be useful in a variety of applications, from generating numerical patterns to analyzing data. With the examples provided here, you should be able to start using the for loop to print even and odd numbers in your own Python programs.


Using the for Loop to Print Multiplication Tables

Multiplication tables are an important part of mathematics, and you can use the for loop in Python to print them out easily. In this section, we will discuss how to use the for loop to print multiplication tables of single and multiple numbers, and also how to customize the range of numbers to be multiplied.

Printing multiplication table of a single number

To print the multiplication table of a single number, you can use a for loop to iterate over a range of numbers from 1 to 10, and then multiply each number by the given number. Here is an example code snippet that prints the multiplication table of 5:

PYTHON

num = 5
for i in range(1, 11):
print(num, 'x', i, '=', num*i)

Output:

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

As you can see, the for loop iterates over a range of numbers from 1 to 10, and then multiplies each number by 5 to print the multiplication table.

Using nested for loop to print multiplication tables of multiple numbers

If you want to print multiplication tables of multiple numbers, you can use a nested for loop in Python. Here is an example code snippet that prints the multiplication tables of numbers from 1 to 5:

PYTHON

for i in range(1, 6):
print('Multiplication table of', i)
for j in range(1, 11):
print(i, 'x', j, '=', i*j)
print()

Output:

Multiplication table of 1
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
1 x 10 = 10
Multiplication table of 2
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
Multiplication table of 3
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
Multiplication table of 4
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
Multiplication table of 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

In the above code snippet, the outer for loop iterates over a range of numbers from 1 to 5, and then the inner for loop iterates over a range of numbers from 1 to 10 to print the multiplication table of each number.

Customizing the range of numbers to be multiplied

You can also customize the range of numbers to be multiplied in the multiplication table by using the range function in Python. Here is an example code snippet that prints the multiplication table of 7 from 1 to 15:

PYTHON

num = 7
for i in range(1, 16):
print(num, 'x', i, '=', num*i)

Output:

7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
7 x 11 = 77
7 x 12 = 84
7 x 13 = 91
7 x 14 = 98
7 x 15 = 105

As you can see, the range function is used to iterate over a range of numbers from 1 to 15 to print the multiplication table of 7.


Using the for Loop to Generate Lists

Lists are an essential data structure in Python, and the for loop is a powerful tool that can be used to generate them. In this section, we’ll look at three different ways to use the for loop to create lists: creating an empty list, adding elements to a list, and using list comprehension.

Creating an empty list using for loop

Sometimes, we need to create a new list that doesn’t contain any elements yet. We can use the for loop to generate an empty list quickly. Here’s an example:

my_list = []
for i in range(5):
my_list.append(i)
print(my_list)

In this example, we create an empty list called my_list. We then use a for loop to iterate over the range of numbers from 0 to 4. During each iteration, we append the current value of i to the end of my_list. Finally, we print the contents of my_list, which should be [0, 1, 2, 3, 4].

Adding elements to a list using for loop

We can also use the for loop to add elements to an existing list. Here’s an example:

my_list = [1, 2, 3, 4, 5]
for i in range(6, 11):
my_list.append(i)
print(my_list)

In this example, we start with an existing list called my_list that contains the numbers 1 through 5. We then use a for loop to iterate over the range of numbers from 6 to 10. During each iteration, we append the current value of i to the end of my_list. Finally, we print the contents of my_list, which should be [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].

List comprehension using for loop

List comprehension is a concise way to create lists in Python using the for loop. Here’s an example:

my_list = [i**2 for i in range(5)]
print(my_list)

In this example, we create a new list called my_list using list comprehension. The expression inside the square brackets is evaluated for each value of i in the range of numbers from 0 to 4. During each iteration, we square the current value of i and add it to the end of my_list. Finally, we print the contents of my_list, which should be [0, 1, 4, 9, 16].

Using list comprehension with the for loop is an efficient way to create lists in Python, especially when dealing with large datasets.


Using the for Loop to Iterate Over Strings

Strings are a fundamental data type in Python, and they are used to represent textual data. The for loop is a powerful tool that can be used to iterate over strings in Python. In this section, we will explore how to use the for loop to loop through strings, how to loop through each character and word in a string, and how to reverse a string using the for loop.

Looping through each character in a string

To loop through each character in a string, we can use the for loop. Here is an example:

PYTHON

string = "Hello, World!"
for char in string:
print(char)

Output:

H
e
l
l
o
,
W
o
r
l
d
!

In the above example, we have defined a string variable called string and used the for loop to iterate over each character in the string. The loop assigns each character to the char variable, which we then print using the print() function.

Looping through each word in a string

To loop through each word in a string, we can use the split() method. The split() method splits a string into a list of words based on a delimiter, which is by default a space character. Here is an example:

PYTHON

string = "Hello, World!"
for word in string.split():
print(word)

Output:

Hello,
World!

In the above example, we have used the split() method to split the string into a list of words and then used the for loop to iterate over each word in the list. The loop assigns each word to the word variable, which we then print using the print() function.

Reversing a string using for loop

To reverse a string using the for loop, we can use the range function to loop through the string in reverse order. Here is an example:

PYTHON

string = "Hello, World!"
reversed_string = ""
for i in range(len(string)-1, -1, -1):
reversed_string += string[i]
print(reversed_string)

Output:

!dlroW ,olleH

In the above example, we have defined a string variable called string and an empty string variable called reversed_string. We have used the range function to loop through the string in reverse order and assigned each character to the reversed_string variable using the += operator. Finally, we have printed the reversed string.

In conclusion, the for loop is a powerful tool that can be used to iterate over strings in Python. We can use the for loop to loop through each character and word in a string, and even reverse a string. By using the examples provided in this section, you can start working with strings in Python and create more complex programs.

Leave a Comment

Contact

3418 Emily Drive
Charlotte, SC 28217

+1 803-820-9654
About Us
Contact Us
Privacy Policy

Connect

Subscribe

Join our email list to receive the latest updates.