Loops in Python

### Introduction to `while` loop A while loop will continuously execute code depending on the value of a condition. It begins with the keyword while, followed by a comparison to be evaluated, then a colon. On the next line is the code block to be executed, indented to the right. Similar to an `if` statement, the code in the body will only be executed if the comparison is evaluated to be true. What sets a while loop apart, however, is that this **code block will keep executing as long as the evaluation statement is true. Once the statement is no longer true, the loop exits and the next line of code will be executed.** A while loop executes the body of the loop while a specified condition remains True. They are commonly used when there’s an unknown number of operations to be performed, and a condition needs to be checked at each iteration **Common bugs related with while loop** 1. One of the most common errors is forgetting to initialize variables with the right value. 2. reuse the variabl. 3. Condition is not changing while might lead to infinite loop ```python number = 1 while (number < 4): print(" number is : ", number) number = number + 1 ``` ### Introduction to `for` loop `for` loop is used when we want to loop through list of things, list of words, list of numbers, lines in file etc *(In pyton terms, It could be list, tuple, set or string)* ```python for x in range(5,10): print(x) ``` #### list ```python days = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday'] for day in days: print(day) for index, fruit in enumerate(days): print(index, fruit) ``` #### tuple ```python animals = ('elephant', 'bear', 'bunny', 'dog', 'cat', 'velociraptor' ) for animal in animals: print(animal) ``` #### string ```python for letters in 'pune': print(letters) ``` #### dictionary ```python person = {"name": "Joe", "age": 33, "city": "Paris"} for key, value in person.items(): print(key, ":", value) ``` ### range range function can be used for looping ```python for i in range(1,5,2): print(i) for j in range(5): print(i) ``` if we provide just one element. start is assumed as zero and increment is by one. use positional values are like this: (start, end, step) **Please note that start is included but end is not** ### When to use `while` and `for` loop 1. Use for loop when you have sequence to iterate 2. Use while loop when you want to repeat an action untill condition changes `for` loop is called as definite loop and `while` loop can be called as indefinite loop because it simply loops untill specific condition is met.. ### Additional Controls * `continue` * With the continue statement we can **stop the current iteration of the loop** * and **continue with the next** * `break` Breaks the loop. * This is used to signal that the current loop should stop running. We can use it not only to stop infinite loops but also to stop a loop early if the code has already achieved what's needed * A break statement in Python provides a way to **exit out of a loop before the loop's condition is false**. Once a break statement is encountered, the program's control flow jumps out of the loop and continues executing the code after the loop * `pass` - A pass statement in Python is a placeholder statement which is used when the syntax requires a statement, but you don't want to execute any code or command. * `else` used with while ```python animals = ('elephant', 'bear', 'bunny', 'dog', 'cat', 'velociraptor' ) for pet in animals: if pet == 'bear': continue if pet == 'cat': break print(pet) ```