Looping Statements: =================== also called as "Iterative Statements". ex: print numbers from 1 to 10 start = 1 print(start) start = start + 1 print(start) start = start+1 print(start) Output: 1 2 3 ...... 10 ==> two looping statements: 1) while loop 2) for loop 1) while loop ============= Syntax: initialization while condition: block of code/statements update statement # WAP TO PRINT NUMBERS FROM 1 TO 10. start = 1 while start <= 10: print(start) start += 1 # WAP TO PRINT NUMBERS 20 TO 1 WITH DIFFERENCE OF 3. start = 20 while start >= 1: print(start) start = start - 3 # WAP TO CHECK WHETHER THE GIVEN NUMBER IS ARMSTRONG NUMBER OR NOT. """ the sum of nth powers of individual digits of number == original number ex: 153 1 ^ 3 + 5 ^ 3 + 3 ^ 3 1 + 125 + 27 == 153 1) take number as input 2) we required to find individual digits of the number 3) calculate powers of those digits 4) find the sum of those powers 5) validate: sum_power == number """ number = 193 n = number # initialization s_dig = 0 while n != 0: ind_dig = n % 10 # 3 5 1 powers = ind_dig ** 3 # 27 125 1 s_dig += powers # s_dig = s_dig + powers 27 152 153 n = n // 10 # 15 1 0 if s_dig == number: print("It is an Armstrong number.") else: print("It is not an Armstrong Number.") # WAP TO CHECK WHETHER THE GIVEN NUMBER IS ARMSTRONG NUMBER OR NOT. """ the sum of nth powers of individual digits of number == original number ex: 153 1 ^ 3 + 5 ^ 3 + 3 ^ 3 1 + 125 + 27 == 153 1) take number as input 2) we required to find individual digits of the number 3) calculate powers of those digits 4) find the sum of those powers 5) validate: sum_power == number """ # number = 193 number = int(input("Enter a number:")) length = len(str(number)) # len(number) ==> not work n = number # initialization s_dig = 0 while n != 0: ind_dig = n % 10 # 3 5 1 powers = ind_dig ** length # 27 125 1 s_dig += powers # s_dig = s_dig + powers 27 152 153 n = n // 10 # 15 1 0 if s_dig == number: print("It is an Armstrong number.") else: print("It is not an Armstrong Number.") ================================================ for Loop: ========= range() ==> special operator ==> also the iterable object. to generate numbers from starting value to ending value with some difference Ex: 1 to 10 ==> range(1,11) Syntax: range(value) ==> 0 to value-1 range(start, end) ==> start to end-1 range(start, end, step) ==> start to end - 1 with the difference of "step" ex: range(10) ==> 0 to 9 range(1,11) ==> 1 to 10 range(1,20,3) ==> 1 4 7 10 13 16 19 Syntax: for variable in range(start, end, stop): block of code In Java: ======== for(initialization; condition; update) { block of code; } # WAP TO PRINT NUMBERS FROM 1 TO 10 USING FOR LOOP. for i in range(1,11): print(i) # WAP TO PRINT ALL EVEN NUMBERS FROM 100 TO 150. for i in range(100,151): if i % 2 == 0: print(i,end = "\t") # WAP TO PRINT ALL INDIVIDUAL CHARACTERS OF THE STRING ALONG WITH POSITIVE AND NEGATIVE INDEX. """ "python" The Character at positive index 0 and at negative index -6 = P """ string = input("Enter a string:") index = 0 for ch in string: print("The Character at positive index ",index," and at negative index ",(index - len(string))," is = ",ch) index += 1 ================================================ Transfer Statements: ==================== break ==== use to stop the loop immediately (based on the condition) for i in range(10): if i == 6: break print(i) continue ======== use to stop the current iteration and continue with remaining iterations. for i in range(10): if i == 6: continue print(i)