2 Types of loops in python: 1) For Loop: It is designed to repeatedly execute a code block while iterating through a list, tuple, dictionary, or other iterable objects of Python. string = "Python" # Initiating a loop for s in a string: # giving a condition in if block if s == "o": print("If block") # if condition is not satisfied then else block will be executed else: print(s) ---------------------------------------------------------------------------------------------------------------------------- numbers = [3, 5, 23, 6, 5, 1, 2, 9, 8] # initializing a variable that will store the sum sum_ = 0 # using for loop to iterate over the list for num in numbers: sum_ = sum_ + num ** 2 print("The sum of squares is: ", sum_) ----------------------------------------------------------------------------------------------------------------------------- my_list = [3, 5, 6, 8, 4] for iter_var in ra...
Comments
Post a Comment
Please comment