0% found this document useful (0 votes)
13 views23 pages

Control Structers - Loops

Uploaded by

speedcarsx61
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)
13 views23 pages

Control Structers - Loops

Uploaded by

speedcarsx61
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
You are on page 1/ 23

SE 1115

Introduction to
Programming
Control Structures in Python:
Loops
LOOPS
• Loops are repetition statements in programming
languages
• They are used for iterative calculations
• Why do we need them?
LOOPS
• They actually take a crucial role in programming
• In real life problems and programs, it is rare to make
just one calculation with a single program
LOOPS
• Most of the programs are written for calculations on
complex and huge amount of data
• In order to search, sort, and analyze them we need to
use loops
LOOPS
• Furthermore, when we want to do the same process
for more than one entity, instead of typing the same
code for many entities, we just write it once and run it
multiple times in a loop
LOOPS
• Loops generally contain three
parts in themselves:
• a counter / a conditional variable
initialization,
• conditions
• counter incrementation /
conditional variable calculation
LOOPS
• There are two types of loops in Python:
1. While
2. For
• We will learn both of these types of loops in this
class
While
• The general structure of while in Python is as
following
• Here, the loop continues as long as the
condition is true!
While
Example 1: Let us print the integer numbers from 1
to 10 in Python.
While
Example 2: Let us write a Python program that asks user to
enter 5 different integer numbers and calculates the sum
of these numbers.
While
Example 3: Let us write a Python program that calculates
n! , where .
While
Example 4: Let us write a Python program that calculates
until its value is less than or equal to 0.01
for
While
Example 5: Let us write a Python program that loops until
the user enter the word "stop".
While
• As in if statements, we can use multiple
conditions in while, as well.

Example 6: Let us write a Python program that


gives user three tries to enter his/her username
and password.
While
Example 6:
user_name = "admin"
user_password = "1234"
password = ""
name = ""
tries = 0
while password != user_password or name != user_name and tries < 3:
name = input("Enter name:")
password = input("Enter password:")
tries = tries + 1

if name == user_name and password == user_password:


print("success")
else:
print("incorrect password")
For
• The general structure of for in Python is as
following

• Here, range executes the incrementation or


decrement of the counter
For
• The function range is used for the iteration. The
following are the main variations of range
function
• range(a) means 0, 1, …, a-1
• Example: range(5)  0, 1, 2, 3, 4
• range(a, b) means a, a+1, a+2, …, b-1
• Example: range(3, 9)  3, 4, 5, 6, 7, 8
• range(a, b, c) means a, a+c, a+2c, …, b-1
• Example: range(4, 14, 3)  4, 7, 10, 13
• Example: range(10, 3, -2)  10, 8, 6, 4
For
Example 7: Let us count with "for" in Python
For
Example 8:
For
Example 9: Factorial example with for.
For
Example 10: Write a Python program that calculates
certain number of Fibonacci numbers.
f(0) = f(1) = 1
f(n) = f(n-1) + f(n-2), for n > 1
For
Example 11: Write a Python program that asks the
name and weight of 100 persons, and then find the
average weight of these people.
Practice Questions
1. Write a Python code that finds the minimum of 20
numbers entered by the user
2. Write a Python program that calculates the value of the
following function until the result of f(x,y) is greater than
or equal to 100

where x = 0, 0.1, 0.2, 0.3, … and y = 2, 2.1, 2.2, 2.3, …

You might also like