Loops in Python

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

Python programming language

provides following types of loops to


handle looping requirements. Python
provides three ways for executing the
loops. While all the ways provide
similar basic functionality, they differ
in their syntax and condition checking
time.
 In python, while loop is used
to execute a block of
statements repeatedly until a
given a condition is satisfied.
And when the condition Syntax:
becomes false, the line
immediately after the loop in
program is executed. while expression:

Below parts are main : statement(s)


 Initial Value.
Increment or Decrement
 Condition
 Increment and Decrement
value.
Follow the steps below for Increment loop:

EXAMPLE :
Step1:

We need to assign a value with start value with the a=1


smaller value rather than the conditional value so that
loop will get started or else loop can’t be run through. while(a<=10):
Step 2:

We need to set a condition based on start value as


print a
smaller once the condition gets pass the statements gets
executed till the condition gets failed. a+=1
Step 3:

We need to set an increment value-based the needs of


the function or statement the statement of loop is based
on the increment value.
Follow the steps below for decrement loop:

Step1: EXAMPLE :
We need to assign a value with start value with the bigger
value rather than the conditional value so that loop will get while(a>=1)
started or else loop can’t be run through.
print a
Step 2:

We need to set a condition based on start value as bigger a-=1


value, once the condition gets pass the statements gets
executed till the condition gets failed.
Step 3:

We need to set a decrement value-based on the needs of the


function or statement the statement of loop is based on the
decrement value.
num = 1
# loop will repeat itself as long as
# num < 10 remains true
while num < 10:
print(num)
#incrementing the value of num
num = num + 3
 The else part is executed if the condition in the while loop evaluates to False.

counter = 0

while counter < 3:


print(“Good Morning”)
counter = counter + 1
else:
print(“Good Evening”)
Python break and continue Statements
 The Python break statement
immediately terminates a loop
entirely. Program execution
proceeds to the first statement
following the loop body.

 The Python continue statement


immediately terminates the
current loop iteration.
Execution jumps to the top of
the loop, and the controlling
expression is re-evaluated to
determine whether the loop will
execute again or terminate.
i=1 i=0
while i < 6: while i < 6:
print(i) i += 1
if i == 3: if i == 3:
break continue
i += 1 print(i)
print("1. Addition");
print("2. Subtraction");
print("3. Multiplication");
print("4. Division");
print("5. Exit");
choice = int(input("Enter your choice: "));
if (choice>=1 and choice<=4):
print("Enter two numbers: ");
num1 = int(input());
num2 = int(input());
if choice == 1:
res = num1 + num2;
print("Result = ", res);
elif choice == 2:
res = num1 - num2;
print("Result = ", res);
elif choice == 3:
res = num1 * num2;
print("Result = ", res);
else:
res = num1 / num2;
print("Result = ", res);
elif choice == 5:
exit();
else:
print("Wrong input..!!");
for loops are traditionally used when you have a block of code which
you want to repeat a fixed number of times. The Python for
statement iterates over the members of a sequence in order,
executing the block each time.
Contrast the for statement with the ''while'' loop, used when a
condition needs to be checked each iteration, or to repeat a block of
code forever. For example:
Syntax:

for iterator_var in sequence:


statements(s)
 range(n): generates a set of whole numbers starting from 0 to (n-1).

For example:
range(8) is equivalent to [0, 1, 2, 3, 4, 5, 6, 7]

range(start, stop): generates a set of whole numbers starting from start to stop-1.
For example:
range(5, 9) is equivalent to [5, 6, 7, 8]
EXAMPLE 2 : FACTORIAL OF A NUMBER
EXAMPLE 1
num = 8
n=4
for i in range(0, n):
factorial = 1
print(i)
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
EXAMPLE 4 : FIBONNACI SERIES
EXAMPLE 3 PRINT TABLE
A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8....

num = 12 The first two terms are 0 and 1. All other terms are obtained by adding the
for i in range(1, 11): preceding two terms. This means to say the nth term is the sum of (n-1)th
print(num, 'x', i, '=', num*i) and (n-2)th term.
Algorithm for printing Fibonacci series using a while loop

Step 1:Input the 'n' value until which the Fibonacci series has to be generated

Step 2:Initialize sum = 0, a = 0, b = 1 and count = 1

Step 3:while (count <= n)

Step 4:print sum

Step 5:Increment the count variable

Step 6:swap a and b

Step 7:sum = a + b

Step 8:while (count > n)

Step 9:End the algorithm

Step 10:Else

Step 11:Repeat from steps 4 to 7


EXAMPLE 4 : FIBONNACI SERIES

A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8....

The first two terms are 0 and 1. All other terms are obtained by
adding the preceding two terms. This means to say the nth term is the
sum of (n-1)th and (n-2)th term.

n = int(input("Enter the value of 'n': "))


a=0
SAMPLE 2 :
b=1
sum = 0
count = 1
x,y=0,1
print("Fibonacci Series: ", end = " ")
while(count <= n):
while y<100:
print(sum, end = " ")
print(y)
count += 1
x,y = y,x+y
a=b
b = sum
sum = a + b
EXAMPLE 5 : ITERATION IN FOR LOOP
3. for i in range(10):
1. for i in 'wisdom': print(i)
print(i) if(i==7): break
else: print(“END")

2. for i in {2,3,3,4}: 4. for i in range(6):


print(i) print(i*2)
Python Programming Code to Print Patterns

EXAMPLE 1:

for i in range(0, 5):


for j in range(0, i+1):
print("* ",end="")
print()

You might also like