Learn Python
- Python basic
- Introduction to File Handling
- Basics of List Comprehension
- Introduction to Matplotlib
- Classes and Objects
- Introduction to Functions
- Python Numbers
- Creating Basic Plots
- Opening and closing files
- Function parameters and arguments
- Advanced Techniques
- Attributes and Methods
- Python Strings
- Scope and lifetime of variables
- Advanced Plotting
- Reading from files
- Performance and Limitations
- Encapsulation
- Python List
- Specialized Plots
- Writing to files
- Return statement and output
- Inheritance
- Python Tuple
- Advanced Customization
- Working with different file formats
- Lambda Functions
- Polymorphism
- Python Sets
- File management operations
Flow control
The While loop
A while loop is used to repeatedly execute a block of code as long as a given condition is True. The condition is evaluated before each iteration, and if it is True, the loop's body is executed. If the condition becomes False, the loop stops.
while condition: # code block to be executed
- condition: A boolean expression that is checked before each iteration. If it evaluates to True, the loop continues; otherwise, it stops.
- code block: The set of statements that are executed repeatedly as long as the condition is True.
Example:
i = 1 while i <= 5: print(i) i += 1 # Increment i by 1 in each iteration
Explanation:
1. The variable i is initialized to 1.
2. The loop checks if i is less than or equal to 5. If true, the block of code inside the loop is executed (in this case, print(i)).
3. After printing i, the value of i is incremented by 1 using i += 1.
4. This process repeats until i becomes 6, at which point the condition i <= 5 becomes False and the loop stops.
Output:
1 2 3 4 5
Infinite Loop
A while loop can run indefinitely if the condition never becomes False. For example:
while True: print("This will print forever")
To prevent infinite loops, make sure that something in the loop eventually makes the condition False.
When to use a while Loop
When the number of iterations is unknown: If you need to repeat something an indeterminate number of times until a condition changes, a while loop is suitable.
It's time to take a quiz!
Test your knowledge and see what you've just learned.
What is the basic structure of a while loop in Python?
Dfor condition in range: code block
Awhile condition: code block
Bwhile code block: condition
Cdo while condition: code block
Check Answer
When is the condition evaluated in a while loop?
AAt the end of the loop
BAfter the loop is executed
CBefore each iteration of the loop
DOnly once at the start
Check Answer
What can cause a while loop to run indefinitely?
DThe condition never becomes False
AThe condition becomes True
BThe loop body is empty
CThe loop counter is decremented
Check Answer
How can you prevent an infinite loop in a while statement?
AAlways set the condition to True
BEnsure the loop modifies the condition
CAvoid using break statements
DUse a for loop instead
Check Answer
When should you use a while loop?
CWhen you want to iterate over a sequence
DWhen you want to break out of a loop
AWhen the number of iterations is unknown
BWhen you need a fixed number of iterations
Check Answer