![]() For example, if a list contains 10 numbers then for loop will execute 10 times to print each number. In the syntax, i is the iterating variable, and the range specifies how many times the loop should run.Syntax of for loop for i in range/sequencee: In this case, you know how many iterations you need. ![]() Fixed number of times: Print the multiplication table of 2.You don’t know how many attempts the user will need to guess correctly. An unknown number of times: For example, Ask the user to guess the lucky number.Let’s understand this with an example.Īs opposed to while loops that execute until a condition is true, for loops are executed a fixed number of times, you need to know how many times to repeat the code. So the bottom line is using the for loop we can repeat the block of statements a fixed number of times. Using a for loops in Python we can automate and repeat tasks in an efficient manner. ![]() With the help of for loop, we can iterate over each item present in the sequence and executes the same set of operations for each item. In Python, the for loop is used to iterate over a sequence such as a list, string, tuple, other iterable objects such as range.
0 Comments
Leave a Reply. |