while loop: =========== Q-1: WAP TO ACCEPT AN INTEGER AS AN INPUT AND FIND THE SUM OF INDIVIDUAL DIGITS OF TAHT GIVEN NUMBER. HINT: ==== X = 9876 IND_DIGITS = 9, 8, 7, 6 SUM_DIGITS = 9 + 8 + 7 + 6 = 30 number = int(input("Enter an integer:")) # 9876 # we need to check the number is not equals to 0 or not sumdigits = 0 n = number # n is the loop variabble defining for initialization while n != 0: # condition inddigits = n % 10 # 6 7 8 9 sumdigits = sumdigits + inddigits # 6 13 21 30 n = n // 10 # update 987 98 9 0 print("The Sum of individual digits of",number,"is = ",sumdigits) ================================================================== Q-2: WAP TO ACCEPT AN INTEGER AND CHECK WHETHER THAT NUMBER IS PALINDROME OR NOT. Palindrome number: ================== let us assume: number = x if x == reverse(x): "it is Palindrome" number = int(input("Enter an integer:")) # 1221 n = number reverseNumber = 0 while n > 0: inddig = n % 10 # 1 2 2 1 reverseNumber = reverseNumber * 10 + inddig # 1 12 122 1221 n = n // 10 # 122 12 1 0 if reverseNumber == number: print(number,"is a palindrome number") else: print(number,"is not a palindrome number") ========================================================= Q-3: WAP TO ACCEPT A NUMBER AS AN INPUT AND PRINT ITS MULTIPLICATION TABLE. number = int(input("Enter some number:")) start = 1 while start <= 10: # result = number * start print(number," X ",start," = ",(number * start)) start += 1 ======================================================= Q: WHEN WE NEED TO USE LOOPS? ============================= -> When we want to execute certain group actions more than one time continuously, then we need to use "loops". Q: WHY WE NEED TWO TYPES OF LOOP STATEMENTS IN PYTHON? ====================================================== 1) When we want to repeat a block of code as long as a certain condition is "True", then we can prefer "while loop". The while loop is typically used when we don't know in advance how many times that loop will run. 2) When we want to repeat a block of code over a sequence (collection), for this case we can use "for loop". Here, we can estimate number of iterations in advance before going to start execution. for loop: ========= Syntax: for variable in range(start, stop, step): block of code # WAP IN PYTHON TO PRINT NUMBERS FROM 1 TO 20 USING FOR LOOP. # i = 1 # while i <= 20: # print(i,end = "\t") # i+=1 for i in range(1,21): print(i,end = "\t") ==================================================== # WAP TO PRINT NUMBERS FROM 20 TO 1 USING FOR LOOP. for i in range(20,0,-1): print(i,end = "\t") ============================================= # WAP TO PRINT NUMBERS FROM 1 TO 100 WHICH are MULTIPLES OF 11. for i in range(1,101): if i % 11 == 0: print(i,end = "\t") ============================================ # WAP TO PRINT NUMBERS FROM 1 TO 100 WITH THE SEPARATION OF 6. for i in range(1,101,6): print(i,end = "\t") ======================================================== for i in range(10): print(i,end = "\t") =================================================== for loop with collections: ========================= 1) strings 2) list 3) tuple 4) sets 5) dictionary string = "Python" listdata = [10,20,30,40,50,60] tupledata = (-11,22,-33,44,-55) setdata = {1,3,5,7,9} dictionary = {'a':11,'b':22,'c':33,'d':44} # iteration over string for i in string: print(i,end = "\t") print() for j in listdata: print(j,end = "\t") print() for k in tupledata: print(k,end = "\t") print() for p in setdata: print(p,end = "\t") print() for q in dictionary: print(q,end = "\t")