LOOP STATEMENTS ================ FOR LOOP ========= ==> for ==> keyword ==> when we want execute a block of code repeatedly with each element from the collection Syntax: for iteration_variable in collection: block of code ex: for i in [1,2,3,4]: print(i) # 1 2 3 4 # ITERATING ON STRING DATA USING FOR LOOP. str_data = input("Enter a string data:") # string at an index of 0 ==> # string at an index of 1 ==> index = 0 for i in str_data: # print(i) # print("The Character at positive index of",index,"is = ",i) print("The character at negative index of",index-len(str_data),"is = ",i) index = index + 1 # "Python" # positive index ==> 0 to 5 # negative index ==> -1 to -6 # P ==> 0 -6 ==> 0 - 6 ==> -6 index = -1 for j in str_data[::-1]: print("The Character at ",index,"is = ",j) index -= 1 ====================================== # WAP IN PYTHON TO FIND THE PRODUCT OF ALL ODD NUMBERS FROM THE GIVEN LIST list_data = eval(input("Enter a list data:")) result = 1 for i in list_data: if i % 2 != 0: result = result * i print("The Product of all Odd numbers = ",result) ==================================== ASSIGNMENT: =========== 1) WAP TO TAKE A TUPLE DATA FROM THE KEYBOARD AND PRINT THE TUPLE ELEMENTS ALONG WITH POSITIVE INDEX AND ALSO WITH NEGATIVE INDEX. HINT: ==== THE ELEMENT AT INDEX POSITIVE 0 AND NEGATIVE INDEX -7 = 100 2) WRITE A PYTHON TO ACCEPT A LIST AND PRINT ITS SUM OF ALL INDIVIDUAL ELEMENTS USING FOR LOOP. =================================== ==> FOR LOOP CAN ALSO BE ALLOWED TO MAKE EXECUTE THE BLOCK OF CODE ON EACH ELEMENT FROM THE RANGE OF VALUES. range() ===== ==> used to generate a range of values Syntax: range(value) ==> generate a values from '0' to 'value-1' range(start,stop) ==> generate values from 'start' to 'stop-1' range(start,stop,step) ==> generate values from 'start' to 'stop-1' with the difference of 'step' for loop syntax with range() is: for iteration_variable in range(): block of code for i in range(10): print(i,end = "\t") print() for j in range(10,20): print(j,end = "\t") print() for k in range(10,100,20): print(k,end = "\t") print() for p in range(100,1,-10): print(p,end = "\t") =============================== Nested Loops =========== writing of a loop block in another loop block # WAP TO PRINT ALL ARMSTRONG NUMBERS FROM 1000 TO 5000. """ ARMSTRONG NUMBERS THE SUM OF nth POWERS OF INDIVIDUAL DIGITS OF A NUMBER WHICH EQUALS TO ORIGINAL NUMBER """ for arm in range(1000,5001): # define a logic for armstrong number n = arm # initialization for while loop sum_dig = 0 while n != 0: ind_dig = n % 10 powers = ind_dig ** 4 sum_dig = sum_dig + powers n //= 10 if sum_dig == arm: print(arm,end = "\t") print() ===================================== 3) WAP TO FIND THE PALINDROME NUMBERS FROM 1000 TO 3000. 4) WAP TO PRINT ALL PRIME NUMBERS FROM 100 TO 300.