Python Loops - For Loop
for loop:
For Loop is used for iterating over a sequence. Collections - a List, Tuple, Dictionary, Set, or String. Its syntax is:
for val in sequence:
statement(s)
In the for statement, the variable ‘val’ stores the value of each item on the sequence with every iteration. The loop goes on until all elements are exhausted.
Program: Icecream Shopping - For Loop
cart=['1.Chocobar','2.Funwheel','3.Vanilla','4.Mango','5.Oreo']
#Function Definition
def menu_card():
print("Menu Card")
print("*********")
for i in cart:
print(i)
def shopping():
tot=0
num,Amt=0,0
while(1):
print("--------------------------")
print("What Icecream do you want?")
c=int(input("Type Code: "))
qty=int(input("How many Icecream do you want? "))
print(" ")
num=num+qty
if(c==1):
print("Your Chocobar Icecream Rate is Rs.20")
tot=qty*20
print("Total Amount is Rs.",tot)
elif(c==2):
print("Your Funwheel Icecream Rate is Rs.15")
tot=qty*15
print("Total Amount is Rs.",tot)
elif(c==3):
print("Your Vanilla Icecream Rate is Rs.35")
tot=qty*35
print("Total Amount is Rs.",tot)
elif(c==4):
print("Your Mango Icecream Rate is Rs.15")
tot=qty*15
print("Total Amount is Rs.",tot)
elif(c==5):
print("Your Oreo Rate is Rs.20")
tot=qty*20
print("Total Amount is Rs.",tot)
else:
print("Invalid Choice")
Amt=Amt+tot
print("--------------------------")
ch=input("One More Icecream? y/n: ")
if ch=='n':
print(" ")
print("**************************")
print("SIVA ICECREAM SHOP")
print("------------------")
print("Number of Items:",num)
print("Your Bill Amount: Rs.",Amt)
print("Thank you for purchasing!!")
print("**************************")
break
#Function Call
menu_card()
shopping()
Output:
Program: Different Patterns - Nested For Loop
#Display atleast 5 different patterns using Nested For loop
#Display Reverse Number Pattern
print("1. Reverse Number Pattern:" '\n' )
for row in range(1,6):
for column in range(row,0,-1):
print(column, end=' ')
print("")
print("-------------------------------------")
#Inverted Pyramid Pattern
#This is a downward pattern
#We need to use reverse for loop to print this pattern.
print("2. Inverted Pyramid Pattern:" '\n' )
b=0
for i in range(5,0,-1):
b += 1
for j in range(1, i +1):
print(b, end=' ')
print("")
print("-------------------------------------")
#Display Descending Order
print("3. Descending Order:" '\n' )
rows = 5
for i in range(rows, 0, -1):
num = i
for j in range(0, i):
print(num, end=' ')
print("")
print("-------------------------------------")
#Display Half Pyramid
#The count of numbers on each row is equal to the current row number
#In each row, every next number is incremented by 1
print("4. Half Pyramid:" '\n' )
for i in range(0, 5):
for j in range(0, i +1):
print("*", end=' ')
print("")
print("-------------------------------------")
#Display Two Pyramids
print("5. Display Two Pyramids:" '\n' )
for i in range(0, 5):
for j in range(0, i +1):
print("*", end=' ')
print("")
print("")
for i in range(5 +1, 0, -1):
for j in range(0, i -1):
print("*", end=' ')
print("")
print("-------------------------------------")
Output:
If you have any doubts, feel free to post it in comment Section.
Comments