FOR LOOP ========= ==> HERE: for is the keyword ==> TO PERFORM SOME ACTION USING THE BLOCK OF CODE ON EACH INDIVIDUAL ELEMENT OF THE GIVEN/DEFINED COLLECTION, FOR LOOP CAN BE USED. ==> HERE, THE COLLECTION CAN BE: STRING, LIST, TUPLE, SET, BYTES, DICTIONARY ETC. EX: [1,2,3,4,5,6,7,8,9] EVEN ELEMENTS FROM THE ABOVE LIST CONDITION: ELEMENT % 2 == 0 1 % 2 == 0 ==> False 2 % 2 == 0 ==> True 3 % 2 == 0 ==> False Syntax: ===== for iteration_variable in collection_data: statement-1 statement-2 Here: like while loop, no intialization is used no condition is used no update is used. in for loop: first element of the specified collection ==> start point of the for loop last element of the collection ==> end point of the for loop update ==> is not required. len() === ==> used to find the size of any collection Syntax: len(collection_name) # WRITE A PYTHON PROGRAM TO PRINT THE INDIVIDUAL ELEMEMNTS OF THE GIVEN STRING ALONG WITH ITS POSITIVE AND ITS NEGATIVE INDEX ALSO. """ STR_DATA = "PYTHON" EXP_OP: THE CHARACTER AT POSITIVE INDEX 0 AND AT NEGATIVE INDEX -6 = P THE CHARACTER AT POSITIVE INDEX 1 AND AT NEGATIVE INDEX -5 = Y """ str_data = input("Enter a string data:") # using positive index index = 0 for i in str_data: # print("The Character at positive index",index,"is =",i) # print("The Character at negative index",index-len(str_data),"is =",i) print("The Character at positive index",index,"and at negative index",index-len(str_data),"is =",i) index += 1 print() # using negative index index = -1 for j in str_data[::-1]: print("The Character at negative index",index,"is =",j) index = index - 1 ============================== to define a collection in run time: eval() ==== input() ==== Syntax: eval(input()) # WRITE A PROGRAM IN PYTHON TO FIND THE SUM OF ALL ODD ELEMENTS OF THE GIVEN LIST """ 1) DEFINE A LIST 2) FIND ALL ODD ELEMENTS 3) PERFORM SUM ON ODD ELEMENTS 4) PRINT THE SUM """ list_data = eval(input("Enter a list data:")) sum_odd = 0 for i in list_data: if i % 2 != 0: sum_odd = sum_odd + i print("The sum of all odd elements of a list = ",sum_odd) =================================== ==> for loop is also able to define the block of action on individual element/value of the certain range of values. ==> range() ==> we can able to generate range of values Syntax: 1) range(value) ==> generate a values from 0 (by default) to value-1 2) range(start, stop) ==> generate values from 'start' to 'stop-1' 3) range(start, stop, step) ==> generate values from 'start' to 'stop-1' with the difference of 'step' # WAP IN PYTHON TO PRINT ALL FACTORS OF 11 FROM 100 TO 500. """ 1) we should define the range for finding factors of 11 2) we should make a logic for checking factors of 11 """ # print(range(10)) # print(range(10,20)) # print(range(10,20,2)) # a = range(10) # b = range(10,20) # c = range(10,20,2) # # print(a) # print(b) # print(c) for i in range(100,500): if i % 11 == 0: print(i,end = "\t") # WAP TO PRINT ALL EVEN NUMBERS FROM 500 TO 100. # for i in range(500,399,-1): # if i % 2 == 0: # print(i,end = "\t") for i in range(500,399,-2): print(i,end = " ") NESTED LOOPS ============== OUTER_LOOP: INNER_LOOP: ST-1 ST-2 ST_3 ST_4 WHILE: FOR: FOR: WHILE: WHILE: WHILE FOR: FOR # WAP IN PYTHON TO PRINT ALL PALINDROME NUMBERS FROM 1000 TO 2000 """ PALINDROME NUMBER: REVERSE_NUM == NUMBER 1) WE SHOULD DEFINE RANGE FOR 1000 TO 2000 2) WE WRITE A LOGIC FOR FINDING THE REVERSE """ for p in range(1000,2001): n = p # initialization rev_num = 0 while n != 0: ind_dig = n % 10 rev_num = rev_num * 10 + ind_dig n //= 10 # update if rev_num == p: print(p,end = "\t") =========================== # WAP IN PYTHON TO PRINT ALL ARMSTRONG NUMBERS FROM 9999 TO 1000 (4-DIGIT) """ ARMSTRONG NUMBER: SUM OF NTH POWERS OF INDIVIDUAL DIGITS == ORIGINAL NUMBER 9999 ==> 9^4 + 9^4 + 9^4 + 9^4 == 9999 371 ==> 3^3 + 7^3 + 1^3 == 371 """ for arm in range(9999,1000,-1): n = arm sum_powers = 0 while n > 0: ind_dig = n % 10 powers = ind_dig ** 4 sum_powers = sum_powers + powers n //= 10 if sum_powers == arm: print(arm,end = "\t") ======================================== ASSIGNMENTS: ============= 1) WAP IN PYTHON TO PRINT ONLY EVEN POSITIONED CHARACTERS OF THE GIVEN STRING. "PYTHON" P T O 2) WAP TO PRINT ALL THE INDIVIDUAL ELEMENTS OF THE TUPLE ALONG WITH POSITIVE AND NEGATIVE INDEX. 3) WAP IN PYTHON TO PRINT ALL THE PRIME NUMBERS FROM 100 TO 500.