Python 3.9 For Loop with Example

Python 3.9 For Loop with Example

In this post, you will learn for loop in the python programming language in detail.

Here you will learn what is for loop in python?, why we use for loop in python, what is syntax, definition of for loop in python and all about for loop in python.

What is for loop in Python?

Basically, for loop in any programming language is used to iterate data(array, number list, etc).

In a python programming language, for loop use list, tuple, dictionary, string, to print in sequence. Or used to iterate over other iterated objects.

Syntax of for Loop

for val in sequence:
 block of code

The val parameter passed in the syntax of the for loop can be string, list, truple, dictionary, etc.

Flowchart of Python for Loop

Example: python print numbers 1 to 5

# List of numbers
numbers = [1, 2, 3, 4, 5]

# iterate over the list
for val in numbers:
	print(val)

Output:

1
2
3
4
5

Example: Looping Through a String

for x in "python":
  print(x) 

Output:

p
y
t
h
o
n

Example: Looping Through a Dictionary

# dictionary
d = {'foo': 1, 'bar': 2, 'baz': 3}

# iterate over the list
for val in d:
	print(val)

Output:

foo
bar
baz

The range() function

In Python the range function is used to iterate the loop within a range.

in the range function range start from 0 by default, and increments by 1 (by default), and ends at a specified number to given in range.

and you can also pass range inside the range function and increment the with range according to your requirement

Here,

  • range(n): generates a set of whole numbers starting from 0 to (n-1).
  • range(start, stop): generates a set of whole numbers starting from start to stop-1.
  • range(start, stop, increment): The default increment is 1 which is why when we didn’t

Example: python number in range

for x in range(5):
  print(x)

Output

1
2
3
4
5

Example: range function and for loop with start parameter

for x in range(2, 5):
  print(x)

Output

2
3
4

Example: how to create a range of numbers in python

for x in range(1, 20, 2):
  print(x) 

Output

 1
 3
 5
 7
 9
 11
 13
 15
 17
 19

Example: python program to find the sum of numbers using for loop

# Program to print the sum of first 5 natural numbers

# variable to store the sum
sum = 0

# iterating over natural numbers using range()
for val in range(1, 6):
    # calculating sum
    sum = sum + val

# displaying sum of first 5 natural numbers
print(sum)

Output:

15

for loop with else

python for loop with else, The ‘else’ block executes only when the loop has completed all the iterations.

for val in range(5):
	print(val)
else:
	print("This is completed")

Output:

0
1
2
3
4
This is completed

AuthorTuts Make

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *