Loop in Python | Shraddha Khapra #Lec 05
# Loops in python
# loops are used to repeate instructions
# while loop
# for loop
# while loop
""" while True:
print("hello") """ #print hello infinite times
"""
count = 1
while(count <= 3):
print("hello everyone")
count += 1
print("your loop is ended","your count value is :",count)
"""
"""
i = 1
while(i <= 10):
print("while loop")
i += 1
"""
#we can print count value also
"""
i= 1
while(i <= 100):
print("this is count value : ", i)
i += 1
"""
# print numbers
"""
num =1
while(num <=10):
print(num)
num +=1
"""
# print loop reverse
"""
num = 5
while(num >= 0):
print(num)
num -=1
"""
"""
i = 100
while(i >= 1):
print(i)
i -= 1"
"""
"""
count =99900
while(count >= 1):
print(count)
count -= 1
"""
# Lets practice
# print number 1 to 100
"""
num =1
while (num <= 100):
print(num)
num += 1
"""
# print numbers 100 t0 1
"""
count = 100
while(count >= 1):
print(count)
count -= 1
"""
#print factorialtiplication table of number n
"""
count = 1
number = int(input("enter a value :"))
while(count <= 10):
print(count*number)
count += 1
"""
"""
count =1
number = int(input("enter a value : "))
while(count <= 30):
print (count*number)
count +=1
print("loop has been ended")
"""
#print element of following list using a loop
"""
lest = [1,4,9,16,25,36,49,64,81,100]
count = 1
while(count <= 10):
print(count*count)
count +=1
"""
#traverse : traveling on each index or variable
"""
lest = [1,4,9,16,25,36,49,64,81,100]
idx = 0
while(idx < len(lest)):
print(lest[idx])
idx +=1
"""
"""
heros = ["batman","iron man","superman","wanda","black vidow"]
idx = 0
while(idx < len(heros)):
print(heros[idx])
idx += 1
"""
"""
disney_movies = ["ratatouile","the good dianosaur","beauty and the beast","moana","ice age"]
idx = 0
while(idx < len(disney_movies)):
print(disney_movies[idx])
idx += 1
"""
#search for a number x in this tuple using loop
"""
num = [1,4,9,16,25,36,49,64,81,100]
idx = 0
user = 100
while(idx < len(num)):
if(num[idx] == user):
print("found it at index : " ,idx)
idx += 1
print("loop has been ended")
"""
"""
number = [1,4,9,16,25,36,49,64,81,100]
user = int(input ("enter a number to find out : "))
index = 0
while(index < len(number)):
if(number[index] == user):
print("number found at index", index)
break
else:
print("finding ........")
index += 1
"""
"""
let = "asadshamsuddin"
count = 0
while(count < len(let)):
print("the digit on index",count,let[count])
count += 1
"""
# break and continue in while loop
# break : used to terminate or stop the loop
"""
i= 1
while(i<= 5):
print(i)
if (i==3):
break
i += 1
"""
# continue : terminates execution in the current itteration & coninues execution of the loop with the next itteration
"""
i = 1
while(i<=5):
if(i == 3):
i+=1
continue
print(i)
i +=1
"""
"""
i = 1
while(i<=5):
if(i == 2):
i +=1
continue
print(i)
i += 1
"""
"""
i = 0
while(i<=5):
if(i == 4):
i +=1
continue #skip the next code
print(i)
i += 1
"""
"""
disney_movies = ["ratatouile","the good dianosaur","beauty and the beast","moana","ice age"]
idx = 1
string = "the good dianosaur"
while(idx <len(disney_movies)):
if(disney_movies[idx] == string):
print("found it at index",idx)
break
else:
print("not found")
idx +=1
"""
# print even or odd numbers using break and continue
i = 0
while(i<=6):
if(i %2 != 0):
i += 1
continue
print(i)
i += 1
# For loop in python
# for loop used for sequential traversal,for traversing list,string,tuple or ect
a = [1,2,3,4,5,9]
for val in a:
print(val)
# on lists
str1 = ["patato","tomato","lady","finger"]
for sr in str1:
print(sr)
# on tupple
tup = (34,78,9,55,34)
for val in tup:
print(val)
#else in for loop (optional usage)
"""
str2 = "asadshamsuddin"
for character in str2:
if(character == "u"):
print("u found")
break
print(character)
else: #execute then when loop will complete
print("end")
"""
# using for loop to solve problem
# print the element of the following list using a loop
num = [1,4,9,16,25,36,49,64,81,100]
for val in num:
print(val)
#search for a number x in this tuple using loop
val2 = (1,4,9,16,25,36,49,36,64,81,100)
num = 36
for el in val2:
if(num == el):
print("found")
continue
else:
print("not found yet")
# Range : important function while using range
# range function returns a sequence of number,starting from 0 by default and increment by 1(by default),and stop before a specified number
# range(start?,stop,step?)
for el in range(5):
print(el)
for el in range (0,5):
print(el)
for el in range (0,10,2):
print(el)
# Lets practice
# print numbers from 1 to 100
for i in range(0,101):
print(i)
# print numbers from 1 to 100
for i in range(100,0,-1):
print(i)
# print the factorialtiplication table of n
num = 7
for i in range(1,11):
factorial = num*i
print(factorial)
# Pass statment
# pass is a null statment that does nothing. it is used as a placeholder for future code
# if we want to do nothing in loop
for el in range (10):
pass
# we can also use it if else statments
# Lets practice
# WAP to find the sum of first n numbers.(using while )
i=1
sum = 0
while(i<=10):
sum = sum + i
print(sum)
i +=1
print("programm ends here")
#WAP to find factorial of first n numbers (using for )
factorial = 1
for el in range(1,11):
factorial*=el
print(factorial)
print("we have done this lecture")
Comments
Post a Comment