0% found this document useful (0 votes)
3 views8 pages

Python Looping Techniques Explained

Uploaded by

anudevnt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Python Looping Techniques Explained

Uploaded by

anudevnt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Loops

Loops are control structures that allow you to execute a statement


or group of statements repeatedly

Python has two primitive loop commands:

• while loops
• for loops

while loops in python

In Python, a while loop allows you to execute a set of statements as


long as a specified condition remains true.

Syntax:

while condition:
# Code to execute while the condition is true
# You might need to update the condition inside the loop
# to avoid an infinite loop

Example

i=1
while i < 6:
print(i)
i=i+1

PROBLEMS
Q1. Write the output of code given below
x=0
while x <= 5:
print(x)
x += 1
Q2. How many times 'while' loop in the code given below will be
executed?
x=0
while x < 5:
print(x)
x += 1
Q3. How many times 'while' loop in the code given below will be
executed?

x=0
while x <= 5:
print(x)
x += 1
Q4. Write the output of code given below
count = 0
while count< 4:
print("The count is: ",count)
count +=1
Q5. Write the output of code given below
count = 0
while count<= 4:
print("The count is: ",count)
count +=1
Q6. Write the output of code given below
count = 0
while count< 4:
print("The count is: ",count)
count +=2
Q7. Write a python program to print first five even number
Q8. Write a python program to print first five odd number
Q9. Write a python program to print first n odd number
Q10. Write a python program to find average of 5 numbers using
while loop
Q11. Write a python program to find average of first n numbers
using while loop
Q12. Write the output of code given below
i=1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")

For loop

for loop is used for iterating over a sequence (such as a list, tuple,
dictionary, set, or string).

Syntax:

for iterator_variable in sequence:


# Loop body (code to execute)
# ...

• The iterator_variable represents the current item in the


sequence during each iteration.
• You can replace iterator_variable with any valid variable name.
• The sequence can be any iterable, such as lists, tuples, strings,
or dictionaries.
Example

Here are some examples to illustrate different use cases:


1. Looping through a list:

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


for x in fruits:
print(x)

[Link] through a string:

for char in "banana":


print(char)

3. Using the range() function:

To loop a specified number of times, you can use


the range() function. It returns a sequence of numbers:

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

[Link] a custom starting value and step to range():

for x in range(2, 6):


print(x)

for x in range(2, 30, 3):


print(x)

[Link] else in a for loop:

The else keyword in a for loop specifies a block of code to be


executed when the loop is finished

for x in range(6):
print(x)
else:
print("Finally finished!")
[Link] through a dictionary
Here are a few ways to iterate through a dictionary in Python using
a for loop:
i. Iterating through keys
my_dict = {'a': 1, 'b': 2, 'c': 3}
for key in my_dict:
print(key)
ii. Iterating through keys and accessing values:
my_dict = {'a': 1, 'b': 2, 'c': 3}
for key in my_dict:
print(key, my_dict[key])
iii. Iterating through key-value pairs using .items():
my_dict = {'a': 1, 'b': 2, 'c': 3}
for key, value in my_dict.items():
print(key, value)
iv. Iterating through values using .values():
my_dict = {'a': 1, 'b': 2, 'c': 3}
for value in my_dict.values():
print(value)

PROBLEMS
Q1. Write the output of code given below
my_list = ['C', 9.5, 66, True, 11.32,’GOOD’]
print(my_list[2:4])

Q2. Write the output of code given below

my_list = ['C', 9.5, 66, True, 11.32,’GOOD’]


print(my_list[:4])

Q3. Write the output of code given below

my_list = ['C', 9.5, 66, True, 11.32,’GOOD’]


print(my_list[2:])

Q4. Write the output of code given below

for i in "python":
print(i)

Q5. Write the output of code given below

for j in range(5):

print(j)

Q6. Write the output of code given below

for i in range(1,5):

print(i)

Q7. Write the output of code given below

for k in range(3,12,2):

print(k)

Q8. Write the output of code given below

AnimalList = ['Cat','Dog','Tiger','Cow']

for x in AnimalList:

print(x)

Q9. Write the output of code given below

flowers = ['Jasmine','Lotus','Rose','Sunflower']
for f in flowers:

print(f)

else:

print('Done')

Q10. Write the output of code given below

list1 = [5,10,15,20]

list2 = ['Tomatoes','Potatoes','Carrots','Cucumbers']

for x in list1:

for y in list2:

print(x,y)

Q11. Write the output of code given below

numbers = [12,3,56,67,89,90]

sum = 0

for n in numbers:

sum += n

print(sum)

Q12. Write the output of code given below

numbers = [1,4,50,80,12]
max = 0 for n in numbers:

if(n>max):

max = n

print(max)

Q13. Write the output of code given below

for i in range(1,5):

for j in range(i):

print('*',end='')

print()

Q14. Write the output of code given below

programmingLanguages = {'J':'Java','P':'Python'}

for key in [Link]():

print(key,programmingLanguages[key])

Q15. How many times the 'for' loop in the code given below will be
executed?
list1 = [1, 2, 3]
for x in list1:
print(x)

Q16. Write a python program to print all the letters in the string
“Python programming”

You might also like