0% found this document useful (0 votes)
3 views

7 Python For Loop (1)

The document explains the use of for loops in Python, detailing how they can iterate over various sequences such as lists, tuples, strings, and dictionaries. It also covers the syntax of for loops, the role of the 'in' keyword, and the optional else block that executes when the loop completes normally. Several examples illustrate the application of for loops with different data types and structures.

Uploaded by

rushikeshkonde1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

7 Python For Loop (1)

The document explains the use of for loops in Python, detailing how they can iterate over various sequences such as lists, tuples, strings, and dictionaries. It also covers the syntax of for loops, the role of the 'in' keyword, and the optional else block that executes when the loop completes normally. Several examples illustrate the application of for loops with different data types and structures.

Uploaded by

rushikeshkonde1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Python - For Loops

The for loop in Python provides the ability to loop over the items of any sequence, such as a list,
tuple or a string.

It performs the same action on each item of the sequence.

This loop starts with the for keyword, followed by a variable that represents the current item in the
sequence.

The in keyword links the variable to the sequence you want to iterate over.

A colon (:) is used at the end of the loop header, and the indented block of code beneath it is
executed once for each item in the sequence.

for iterating_var in sequence:

statement(s)

iterating_var is a variable to which the value of each sequence item will be assigned during each
iteration.

Statements represents the block of code that you want to execute repeatedly.

Before the loop starts, the sequence is evaluated.

If it's a list, the expression list (if any) is evaluated first. Then, the first item (at index 0) in the
sequence is assigned to iterating_var variable.

During each iteration, the block of statements is executed with the current value of iterating_var.
After that, the next item in the sequence is assigned to iterating_var, and the loop continues until
the entire sequence is exhausted.

Python for Loop with Strings

for char in "Hello":

print(char)

Python for Loop with Tuples

numbers = (34,54,67,21,78,97,45,44,80,19)

for num in numbers:

print (num)
Python for Loop with Lists

numbers = [34,54,67,21,78,97,45,44,80,19]

for num in numbers:

print (num)

Python for Loop with Range Objects

for num in range(5):

print (num, end=' ')

print()

for num in range(10, 20):

print (num, end=' ')

print()

for num in range(1, 10, 2):

print (num, end=' ')

Python for Loop with Dictionaries

numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}

for x in numbers:

print (x)

numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}

for x in numbers.items():

print (x)

Python - For Else Loop

Python supports an optional else block to be associated with a for loop.

If a else block is used with a for loop, it is executed only when the for loop terminates normally.
The for loop terminates normally when it completes all its iterations without encountering a break
statement, which allows us to exit the loop when a certain condition is met.

for count in range(6):

print ("Iteration no. {}".format(count))

else:

print ("for loop over. Now in else block")

print ("End of for loop")

fruits = ["apple", "banana", "cherry",'a','v','d']

n = len(fruits)

for num in range(n):

print(num)

print(fruits[num])

fruits = ("apple", "banana", "cherry",'a','v','d')

n = len(fruits)

for num in range(n):

print(num)

print(fruits[num])

Some other examples

for i in range(5):

print(i)

fruits = ["apple", "banana", "cherry"]

for fruit in fruits:


print(fruit)

word = "Python"

for letter in word:

print(letter)

for i in range(1, 10, 2): # Start from 1, go till 10, step by 2

print(i)

student = {"name": "John", "age": 20, "grade": "A"}

for key, value in student.items():

print(f"{key}: {value}")

for i in range(3):

for j in range(2):

print(f"i={i}, j={j}")

for i in range(3):

print(i)

else:

print("Loop completed")

You might also like