LOOP STATEMENTS =============== LOOP ==> AN ITERATION WHEN A BLOCK OF CODE CAN EXECUTE FOR SEVERAL NUMBER OF TIMES BASED ON THE CERTAIN CONDITION ==> A LOOP THERE ARE TWO TYPES OF LOOP STATEMENTS: 1) WHILE LOOP 2) FOR LOOP 1) WHILE LOOP: ============== NEED THREE THINGS TO WORK: INITIALIZATION ==> LOOP START POINT CONDITION ==> UNTIL WHICH POINT THE LOOP NEED TO CONTINUE UPDATE ==> INITIALIZATION, THE LOOP NEED TO UPDATE TO THE NEXT TILL THE LAST EX: MY LOOP NEED TO EXECUTE FOR 5-TIMES: LOOP-1;LOOP-2;LOOP-3;LOOP-4;LOOP-5 SYNTAX: Initialization while condition: statement-1 statement-2 update statement ex: print hello world for 5 times hello world hello world hello world hello world hello world line = 1 # initialization while line <= 5: # condition print("Hello World!") line += 1 # line = line + 1 print("End of the loop") ===================== # WAP IN PYTHON TO PRINT NUMBERS FROM 1 TO 10 USING WHILE LOOP. """ 1 2 3 4 5 6 7 8 9 10 or 1 2 3 4 5 6 7 8 9 10 to computer: initialization: num = 1 condition: num <= 10 update: num = num + 1 """ num = 1 while num <= 10: print(num,end = '\t') num = num + 1 print() print("Program is complete.") # WAP TO PRINT ALL MULTIPLES OF 5 FROM 100 TO 0 USING WHILE LOOP. """ 5, 10, 15, 20, 25, .... 100 initialization : number = 100 condition: number >= 0 if number % 5 == 0 ==> print(number) number -= 1 """ number = 100 while number > 0: if number % 5 == 0: print(number,end = "\t") number = number - 1 ============================ # WAP TO COUNT THE NUMBER OF DIGITS IN A GIVEN NUMBER. """ 1234 ==> 4-digit 1 ==> 1-digit 12345 ==> 5-digit while performing any operations on numbers like: counting digits, sum of digits etc. number ==> positive or not equals to 0 number sequence: xyz ==> x X 100 + y X 10 + z X 1 ==> x X 10^2 + y X 10^1 + z X 10^0 from xyz ==> individual digits like: x, y, z : floor division with 10 while split the number, we should count the digits """ number = int(input("Enter the positive number:")) # 12345 num = number # num = 12345 1234 count = 0 while num > 0: num = num // 10 # 1234 123 12 1 0 count = count + 1 # 1 2 3 4 5 print("The given number is {} with {} digits".format(number,count)) ============================ len() ==== used to find the size of the collection (string, list, tuple, set, dictionary etc.) Syntax: len(colletion data) here: len() can return an integer, which defines the length or the size or the number of elements in a collection. number = int(input("Enter the positive number:")) # 12345 num = str(number) count = len(num) print("The given number is {} with {} digits".format(number,count)) ASSIGNMENT: =========== 1) WAP TO FIND THE SUM OF INDIVIDUAL DIGITS OF A NUMBER EX: 12345 ==> 1 + 2 + 3 + 4 + 5 ==> 15 2) WAP TO FIND THE REVERSE OF THE NUMBER. EX: 12345 ==> 54321