0% found this document useful (0 votes)
5 views16 pages

Chapter 8 - More About Python

The document provides an overview of loops in Python, specifically the for loop and while loop, including their syntax and usage. It explains how to use the range() function with for loops and introduces the break and continue statements for controlling loop execution. Additionally, it includes examples and tasks for practicing loop implementation.

Uploaded by

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

Chapter 8 - More About Python

The document provides an overview of loops in Python, specifically the for loop and while loop, including their syntax and usage. It explains how to use the range() function with for loops and introduces the break and continue statements for controlling loop execution. Additionally, it includes examples and tasks for practicing loop implementation.

Uploaded by

naomi.chen
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

More about Python

Loops in Python

Sometimes there is a need to repeat a set of statements more than


once based on a certain condition. This process is called loop or
iteration in programming.

There are 2 available loops in Python:

for loop while loop


for loop
For loop structure is used when you want to perform a loop in a specific
number of times.
For loop using counter variable which is incremented or decremented with
each repetition of the loop

Syntax 1 (using range() function)

for counter_variable in(<collection):


statement(s)

Notes: use colon symbol (:) after for statement and the statement below for
must be indented.
for loop

range(start, stop, step)

Example:
for i in range(6):
print(i)

Stop parameter
The range() function is used to create a list containing a sequence of numbers
starting from 0 and end with one less than the stop parameter. Note
that range(6) is not the values of 0 to 6, but the values 0 to 5.
for loop

Start parameter
The range() function defaults to 0 as a starting value, however it is possible to
specify the starting value by adding a start parameter ex: range(2, 6), which
means values from 2 to 6 (but not including 6):

for i in range(2, 6):


print(i)
for loop
Step parameter
The range() function defaults to increment the sequence by 1, however it is
possible to specify the increment value by adding a third parameter (step
parameter).
Example: range(2, 30, 3):

for i in range(2, 30, 3):


print(i)

Calculation with for loop


num=0
for i in range(10):
num = num + i
print(num)
for loop - Task
1. Write a program 2. Accept a number from the
using for loop in user and display the
Python to show even multiplication table from 1 to
numbers until 100. 10 based on that number.
for loop
Syntax 2 using list and else.
for counter_variable in (<collection):
statement(s)
else:
statement(s)

With the for loop we can execute a set of statements, once for each item in a list, tuple, set
etc.

fruits = ["apple", "banana", "cherry"]


for x in fruits:
print(x)
for loop
A for loop can have an optional else block as well. The else part is executed
when the loop is finished.

fruits = ["apple", "banana", "cherry"]


for i in fruits:
print(i)
else:
print(“Done.”)
while loop
while loop executes a block of code repeatedly as long as the test/control
condition of the loop is true. It is ideally suited when the number of
iterations are not known prior to the execution of the loop.

While Loop Syntax 1


while <condition>:
statement(s)

• while is a reserved/keyword
• It will check for true/false. The statements will executed only if
the condition is true.
while loop
Example:
name=input(“Enter your name:”)
while name==“”:
print(“You did not enter your name”)
name=input(“Enter your name:”)

While statement on the second line asks the user to enter their name.
If the condition remains true, third and fourth line will always be executed.
while loop
With the else statement we can run a block of code once when the
condition no longer is true.
name=input(“Enter your name:”)
while name==“”:
print(“You did not enter your name”)
name=input(“Enter your name:”)
else:
print(“Hello”,name)

If the loop terminates in between the else part is not executed.


Jump statement in Python

Python offers two jump statements to be used within loops to jump out of
loop iterations.

Using break
The break statement enables a
program to skip over a part of the
code. This statement terminates
the current loop and resumes
execution at the statement
immediately after the body of the
loop.
Using break
Example: fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break

With the break statement we can stop the loop before it has looped through all
the items.

Exit the loop when x is "banana", but this time the break comes before the
print.
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x)
Using continue
With the continue statement we can stop the current iteration of the loop, and
continue with the next
Example:
Do not print Banana

fruits = ["apple", "banana", "cherry"]


for x in fruits:
if x == "banana":
continue
print(x)
Write a program where the user enters a number. If the number
matches the number, acknowledge the user and stop the
program. If the number does not match, keep asking for the next
number.

secret=123
i=int(input("Enter your guess:"))
while not secret==i:
print("Your guess is wrong. Try again!")
i=int(input("Enter your guess:"))
else:
print("Your guess is correct!")

You might also like