for Loop in Python


A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.

Syntax:


for var in sequence:
    statement(s)  must be indented
          

Here, var is the variable that takes the value of the item inside the sequence on each iteration.

Loop continues until we reach the last item in the sequence.

Example 1:

Print each fruit in a fruit list:
fruits = ["apple", "banana", "mango"]
for x in fruits:
  print(x)
OUTPUT
apple
banana                    
mango

Example 2:

Program to find the sum of all numbers stored in a list
# List of numbers
numbers = [1,2,3,10,20,34]

# variable to store the sum
sum = 0

# iterate over the list
for var in numbers:
    sum = sum+var

# Output: The sum is 70
print("The sum is", sum)
OUTPUT
The sum is 70

Looping through a String

Even strings are iterable objects, they contain a sequence of characters:

Example 3:

Loop through the letters in the word "prowess":
for x in "prowess":
  print(x)
OUTPUT
p
r
o
w
e
s
s

The range() function :

We can generate a sequence of numbers using range() function.

Syntax :

1. range(stop) always starts from 0 to stop-1.

    range(10) will will generate numbers from 0 to 9 (10 numbers).

>>> print(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>

2. range(start, stop, step) We can also define the start, stop and step size.Step size defaults to 1 if not provided.

    range(1,10) will will generate numbers from 1 to 9 (10 numbers).

>>> print(range(1,10))
[1, 2, 3, 4, 5, 6, 7, 8, 9]  step is 1

>>> print(range(1,10,2))
[1, 3, 5, 7, 9]  step is 2

>>> print(range(10,1,-1))  step is -1
[10, 9, 8, 7, 6, 5, 4, 3, 2]  #generate the reverse series

We can use the range() function in for loops to iterate through a sequence of numbers.

>>> for i in range(5):
	print(i)
	
0
1
2
3
4

range() function can be combined with the len() function to iterate though a sequence using indexing.

 Program to iterate through a list using indexing

fruit = ['apple', 'banana', 'mango']

# iterate over the list using index
for i in range(len(fruit)):
	print("I like", fruit[i])
OUTPUT
I like apple
I like banana
I like mango

for loop with else :

In python, a for loop can have an optional else block as well. The else block is executed if the items in the sequence used in for loop exhausts.

for x in range(6):
  print(x)
else:
  print("Finally finished!")
OUTPUT
0
1
2
3
4
5
Finally finished!

Nested Loops

A nested loop is a loop inside a loop.

The "inner loop" will be executed one time for each iteration of the "outer loop":

Print each adjective for every fruit:

adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]

for x in adj:
  for y in fruits:
    print(x, y)
OUTPUT
red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry

Next topic is break, continue and pass statements




 

Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved