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
- Practical Applications
- Higher-Order Functions
Flow control
The For loop
A for loop is used to iterate over a sequence (such as a list, tuple, string, or range) and execute a block of code for each element in that sequence. It's a convenient way to loop through a collection of items without needing to manage the loop counter manually (as in a while loop).
1. Basic looping:
1. Basic looping:
for variable in sequence: # code block to be executed
- variable: The loop variable that takes the value of each item in the sequence one by one.
- sequence: The sequence (like a list, tuple, string, or range) that the loop iterates over.
- code bloc: The block of code to be executed for each item in the sequence.
Example:
fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit)
- The list fruits contains three items: "apple", "banana", and "cherry".
- The for loop iterates over this list, and for each iteration, the variable fruit takes the value of the current item in the list.
- The print(fruit) statement outputs the value of fruit for each iteration.
Output:
apple banana cherry
2. Looping through a range():
The range() function generates a sequence of numbers, which is often used with a for loop to iterate a specific number of times.
Example:
for i in range(5): print(i)
- range(5) generates the numbers 0, 1, 2, 3, 4 (the stop value 5 is excluded).
- For each iteration, i takes the next value in the range, and the loop prints the value of i.
Output:
0 1 2 3 4
You can also use range(start, stop, step) to customize the range:
- start: Starting number (included).
- stop: Ending number (excluded).
- step: The amount by which the loop variable is incremented.
Example:
for i in range(2, 10, 2): print(i)
Output:
2 4 6 8
3. Looping with else:
The for loop can also have an optional else clause. The code in the else block is executed when the loop finishes normally (i.e., it is not terminated by a break statement).
Example:
for i in range(5): print(i) else: print("Loop completed!")
Output:
0 1 2 3 4 Loop completed!
If the loop is stopped with break, the else clause will not be executed.
4. When to Use a for Loop:
- When you know the sequence you want to iterate over.
- When you want to repeat a block of code for every item in a sequence.
It's time to take a quiz!
Test your knowledge and see what you've just learned.
What is the basic structure of a for loop in Python?
Afor variable in sequence: code block
Bfor code block: variable in sequence
Cdo for variable in sequence: code block
Dfor each variable in sequence: code block
Check Answer
How does a for loop iterate over a list in Python?
DIt accesses elements by index only
AIt uses a while loop internally
BIt assigns each element to a variable in each iteration
CIt requires a counter variable
Check Answer
What does range(5) generate in a for loop?
A5 numbers from 1 to 5
B5 numbers from 0 to 5
C5 numbers from 0 to 4
D5 numbers from 1 to 4
Check Answer
When is the else block executed in a for loop?
DWhen the loop iterates more than 10 times
AWhen the loop is terminated by break
BWhen the loop finishes normally
CWhen an exception occurs
Check Answer
When should you use a for loop?
AWhen you know the sequence to iterate over
BWhen the number of iterations is unknown
CWhen you want to break out of a loop
DWhen iterating in reverse order only
Check Answer