0% found this document useful (0 votes)
11 views8 pages

Sixthweeknote

The document provides an overview of loops in Python, detailing the structure and syntax of for and while loops. It explains how to iterate over various data structures, the use of control keywords like break and continue, and the concept of nested loops. Additionally, it warns against infinite loops and emphasizes the importance of ensuring loop conditions eventually become false.

Uploaded by

dagiyoni924
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

Sixthweeknote

The document provides an overview of loops in Python, detailing the structure and syntax of for and while loops. It explains how to iterate over various data structures, the use of control keywords like break and continue, and the concept of nested loops. Additionally, it warns against infinite loops and emphasizes the importance of ensuring loop conditions eventually become false.

Uploaded by

dagiyoni924
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

8 Loops in Python

Loops help you repeat a block of code many


times without writing it again and again.
8.1 for Loop
A for loop is used when you want to loop over
a sequence like a list, string, tuple, set,
dictionary, or numbers from range().
Structure of a for Loop in Python
A for loop is used to go through each item in a
sequence (like a list, string, or range).
8.1.1 Syntax:
for variable in sequence:
# code block (indented)
Detailed Explanation of Each Part
1. for
This is the keyword that tells Python you're
starting a for loop.
2. variable
This is a temporary name that will take the
value of each item in the sequence one by
one.
You can name it anything (like item, x, fruit,
etc.), but it should make sense.
Example:
for fruit in ["apple", "banana"]:
print(fruit)
Here, fruit takes "apple" first, then "banana".
3. in
Another keyword that connects the variable to
the sequence.
It means: "for each item in this group".
4. sequence
This is the group of items you want to loop
over. It can be:
A list: ["apple", "banana"]
A string: "hello"
A range: range(5)
A tuple: (1, 2, 3)
A set: {"a", "b"}
A dictionary: {"key": "value"}
5. : (colon)
Marks the start of the block of code inside the
loop.
6. Indented Block
This is the code that runs each time the loop
goes around.
All lines inside the loop must be indented
(usually with 4 spaces).
8.1.2 range(start, stop, step)
for i in range(1, 10, 2):
print(i) # prints 1, 3, 5, 7, 9
start: where to begin.
stop: where to stop (not included).
step: how much to add each time (or subtract
if negative).
8.1.4 Iterating over a string:
for char in "hi":
print(char) # h, i
8.1.5 Iterating over a list:
names = ["Ali", "Sara"]
for name in names:
print(name)
8.1.6 Iterating over a tuple:
nums = (1, 2, 3)
for n in nums:
print(n)
8.1.7 Iterating over a set:
items = {"pen", "book", "bag"}
for item in items:
print(item)
8.1.8 Iterating over a dictionary:
Keys:
data = {"name": "Lina", "age": 22}
for key in data:
print(key)
Values:
for value in data.values():
print(value)
Key & value:
for key, value in data.items():
print(key, value)
8.2 while Loop
Runs as long as the condition is True.
8.2.1 Syntax :
while condition:
# do something
Parts:
while – starts the loop.
condition – must stay True to keep looping.
Inside the loop, you usually change something
to make the condition false eventually.
Example:
x=0
while x < 3:
print("Hi")
x += 1 # add 1 to x each time
8.2.1 Using else with while:
x=0
while x < 3:
print(x)
x += 1
else:
print("Done")
else runs only if the loop finishes normally
(not stopped by break).
8.3 Operators in Loops
You often use these operators in loops:
1,+=:add and assign
2,-=:subtract and assign
3,*=:multiply and assign
4,/=:divide and assign
These help update the value of a variable
inside a loop.
8.4 Control Keywords
Keyword Description
break Stops the loop completely
continue Skips the current lap and
goes to the next

Examples:
for i in range(5):
if i == 3: break
print(i) # prints 0, 1, 2
for i in range(5):
if i == 3: continue
print (i) # prints 0, 1, 2, 4
8.5 Nested Loops
A loop inside another loop.
Example:
for i in range(2):
for j in range(3):
print("i ="+ i+ "j ="+j)
The inner loop runs fully every time the outer
loop runs once.
8.6 Warning: Avoid Infinite Loops!
If a while loop never becomes False, it will
keep running forever.
Example of an infinite loop:
x=1
while x > 0:
print(x)
This loop never ends unless you stop the
program, because x keeps being more than 0.
How to fix:
Always make sure your loop has a condition
that will eventually become False, like:
x=1
while x < 5:
print(x)
x += 1
On Windows:
Alt + F4 – closes the active window or
program.

You might also like