Podcast
Questions and Answers
What is a necessary part of the basic syntax for a for
loop in Python?
What is a necessary part of the basic syntax for a for
loop in Python?
What will be the output of the following code?
for i in range(3, 6):
print(i)
What will be the output of the following code?
for i in range(3, 6):
print(i)
What does the continue
statement do in a for
loop?
What does the continue
statement do in a for
loop?
How can you loop over the keys of a dictionary in Python using a for
loop?
How can you loop over the keys of a dictionary in Python using a for
loop?
Signup and view all the answers
What happens when a break
statement is executed inside a for
loop?
What happens when a break
statement is executed inside a for
loop?
Signup and view all the answers
How can a for
loop iterate over a string in Python?
How can a for
loop iterate over a string in Python?
Signup and view all the answers
What will be printed by the following code?
for i in range(5):
if i == 3:
break
print(i)
What will be printed by the following code?
for i in range(5):
if i == 3:
break
print(i)
Signup and view all the answers
What is the purpose of the else
clause in a for
loop?
What is the purpose of the else
clause in a for
loop?
Signup and view all the answers
Study Notes
Python for
Loops
- A
for
loop iterates over a sequence (e.g., list, tuple, string, range). - It executes a code block repeatedly for each item in the sequence.
Basic Syntax
-
for
keyword followed by a variable name. -
in
keyword. - The sequence to iterate over.
- Colon (
:
) to start the code block. - Indentation for the code block.
Iterating over Lists
- Example iterating over a list of numbers:
numbers = [1, 2, 3, 4, 5]
for number in numbers:
print(number)
- Prints each number on a new line.
Iterating over Strings
- Strings are iterable sequences.
- Access each character:
my_string = "hello"
for character in my_string:
print(character)
Iterating with range()
- Loops a specific number of times.
-
range(start, stop, step)
creates a sequence of numbers. Defaults torange(0, stop, 1)
. - Example looping 5 times:
for i in range(5):
print(i)
- Example looping from 2 to 5 with a step of 2:
for i in range(2, 6, 2):
print(i)
break
Statement
- Terminates the loop prematurely.
- Used when a specific condition is met:
for i in range(10):
if i == 5:
break
print(i)
continue
Statement
- Skips the rest of the code block in the current iteration, moving to the next.
for i in range(5):
if i == 2:
continue
print(i)
Nested Loops
- Loops placed inside another loop.
- Inner loop completes all iterations per outer loop iteration.
rows = 2
cols = 3
for row in range(rows):
for col in range(cols):
print(f"({row},{col})")
else
Clause (with loops)
- Executes a code block when the loop completes normally (without
break
).
for i in range(3):
print(i)
else:
print("Loop completed normally")
Looping over Dictionaries
- Dictionaries are iterable.
- Iteration by default is over the keys.
my_dict = {"a": 1, "b": 2, "c": 3}
for key in my_dict:
print(key)
## Access values:
for key in my_dict:
print(f"Key: {key}, Value: {my_dict[key]}")
Looping over enumerate
-
enumerate()
function provides both index and value of an iterable.
my_list = ["apple", "banana", "cherry"]
for index, item in enumerate(my_list):
print(f"Index: {index}, Item: {item}")
Important Considerations
- Python's indentation is crucial.
- Ensure the sequence being iterated over is valid.
- Avoid infinite loops.
- Choose the correct loop type (e.g.,
for
for sequences,while
for conditions).
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
This quiz covers the basics of 'for' loops in Python. You'll learn how to iterate over sequences like lists and strings, as well as how to use the 'range()' function for specific iterations. Test your understanding of syntax, structure, and practical applications of loops in Python programming.