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

Python Loops and Iteration Basics

Uploaded by

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

Python Loops and Iteration Basics

Uploaded by

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

Lecture #4

ITERATION
Doing the Same Thing Many Times
• It’s possible to do something repeatedly by just writing it all out
• Print ‘hello’ 5 times
>>> print('Hello!')
Hello
>>> print('Hello!')
Count n Hello
times >>> print('Hello!')
Hello
>>> print('Hello!')
Hello
Statements >>> print('Hello!')
Hello
Iteration and Loops
• A loop repeats a sequence of statements
• A definite loop repeats a sequence of statements a predictable number
of times

>>> for x in range(5): print('Hello!')


...
Hello
Count n Hello
times Hello
Hello
Hello
Statements
The for Loop
• Python’s for loop can be used to iterate a definite number of times
for <variable> in range(<number of times>): <statement>
• Use this syntax when you have only one statement to repeat
for <variable> in range(<number of times>):
<statement-1>
<statement-2>

<statement-n> >>> for x in range(3):
... print('Hello!')
... print('goodbye')
•Use indentation to format two or ...
Hello!
more statements below the loop goodbye
Hello!
header goodbye
Hello!
goodbye
Using the Loop Variable
• The loop variable picks up the next value in a sequence on each pass
through the loop
• The expression range(n) generates a sequence of ints from 0
through n - 1
loop variable

>>> for x in range(5): print(x)


...
0
1
2
3
4
>>> list(range(5)) # Show as a list
[0, 1, 2, 3, 4]
Counting from 1 through n

• The expression range(low, high) generates a sequence of ints from low


through high - 1

>>> for x in range(1, 6): print(x)


...
1
2
3
4
5
Counting from n through 1

• The expression range(high, low, step) generates a sequence of ints from


high through low+1.

>>> for x in range(6, 1, -1): print(x)


...
6
5
4
3
2
Skipping Steps in a Sequence

• The expression range(low, high, step) generates a sequence of ints


starting with low and counting by step until high - 1 is reached or
exceeded
>>> for x in range(1, 6, 2): print(x)
...
1
3
5
>>> list(range(1, 6, 2)) # Show as a list
[1, 3, 5]
Using a Loop in a Real Problem
• An investor deposits $10,000 with the Get-Rich-Quick agency and
receives a statement predicting the earnings on an annual percentage
rate (APR) of 6% for a period of 5 years. Write a program that prints the
beginning principal and the interest earned for each year of the period.
The program also prints the total amount earned and the final principal.
• Pseudocode: principal = 10000
rate = .06
term = 5
totalinterest = 0
for each year in term
print principal
interest = principal * rate
print interest
principal = principal + interest
totalinterest = totalinterest + interest
print totalinterest
print principal
Nested for loop in python

Python allows us to nest any number of for loops inside a for loop.
The inner loop is executed n number of times for every iteration of the outer
loop.

for iterating_var1 in sequence: #outer loop


for iterating_var2 in sequence: #inner loop
#block of statements
#Other statements
Using else statement with for loop
Unlike other languages like C, C++, or Java, Python allows us to use the else
statement with the for loop which can be executed only when all the
iterations are exhausted.

for i in range(0,5):
print(i)
else:
print("for loop completely exhausted, since there is no break.")
for i in range(0,5):
print(i)
break;
else:
print("for loop is exhausted");
print("The loop is broken due to break statement...came out of the loop")

•The loop is broken due to the break statement; therefore, the else
statement will not be executed.
•The statement present immediate next to else block will be executed.
for i in range(0,5):
print(i)
break;
else:
print("for loop is exhausted");
print("The loop is broken due to break statement...came out of the loop")

•The loop is broken due to the break statement; therefore, the else
statement will not be executed.
•The statement present immediate next to else block will be executed.
While Loop
A for loop is used when a program knows it needs to repeat a block of
code for a certain number of times.
A while loop is used when a program needs to loop until a particular
condition occurs.
Flow of Execution for WHILE Statement
Looping Until User Wants To Quit
Questions
Q1. Write a Python program to find sum of natural numbers up to a given
number using loop.
Q2. Write a Python program to find sum of square natural numbers up to a
given number using loop.
Q3. Write a Python program to find sum of cube natural numbers up to a given
number using loop.
Q4. Write a Python program to check a number is palindrome or not.
Q5. Write a Python program to get the Fibonacci series between 0 to 50.
Q6. Write a python program to find whether an inputted number is perfect or
not.
Q7. Write a Python Program to Find Factorial of Number Using Loop.
Questions
Q8. Write a Python Program to check if a number is prime or not.
Q9. Write a python Program to check if the entered number is Armstrong or
not.
Q10. Write a python Program to print a simple number right angle triangle
pattern.
12/12/14

You might also like