Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

LOOPS in Python

While loop
n=5 Hello! (n=5)

Syntax: while n > 0: (n=4)

while<expr> print(n) output


Hello! (n=3)

n=n-1 Hello! (n=2)


<statement(s)>
print('Hello!') Hello! (n=1)

Hello!

● When n=0,it will come out of the loop


Infinite Loop
Hello
Hello
Hello
while True: Hello
Hello
print('Hello')
output
Hello
Hello
Hello
Hello

break….continue…
break

while True: Output:


line = input('> > hello there
') hello there
if line == > finished
'done': finished
break > done
print(line) Done!
print('Done!')
Prime number program
break…

while True:
line = input('> ') > hello there
if line[0] == '#': hello there
continue >#
if line == 'done':
break output > print this!
print(line) print this!
print('Done!') > done
Ref:pg_break_continue.py
done!
Excersice
● Write a program to print n natural number in descending order using a while loop
● Write a program to find greatest common divisor (GCD) or highest common factor
(HCF) of two numbers.
● Factorial of any number n is represented by n! and is equal to 1*2*3*....*(n-1)*n.
○ E.g.- 4! = 1*2*3*4 = 24
○ 3! = 3*2*1 = 6
○ 2! = 2*1 = 2
○ Also,
○ 1! = 1
○ 0! = 1
○ Write a program to calculate the factorial of a number.
● Write the python program to take input and check whether it is
○ Prime number-
■ A natural number which has only two factors ( 1 and itself ) is called a prime number.
○ Magic number
■ A number is said to be magic when its digits are added recursive till we get a single digit which is equal
to 1, this approach uses brute force, which keeps on adding the digit until a single digit is obtained.
■ For example: 1234 = 1 + 2 + 3 + 4 = 10
1+0=1
Therefore, 1234 is a magic number.
○ Strong number
■ A strong number is a special number in which where the sum of all digit factorial is equal to the sum
itself. For example, consider 145 = 1! + 4! + 5! = 145,
○ Palindrome number
■ number=reverse(number) ex:131
○ Perfect number
■ A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding
the number itself. For example, we will take 6 as a number and its divisors are 1, 2, and 3
excluding itself so the sum of its divisors i.e., 1+2+3 = 6.
○ Reverse the number
■ reverse(123)=321
○ Armstrong number
■ Armstrong number is a number that when raised to the power of a number of its own digits is
equal to the sum of that number.
■ For example:-
■ (i) Let’s assume a number as 5, the number of digits is 1 so 5 to the power of 1 is 5 there 5 is an
Armstrong number.
■ (ii) 371 – there are 3 digits so, each digit will be raised to 3
3*3*3 + 7*7*7 + 1*1*1 = 371. Therefore 371 is an Armstrong number.
for loop
● Python For loop is used for sequential traversal i.e. it is used for iterating
over an iterable like
● String
● Tuple
● List
● Set or Dictionary.
Syntax:

for var in iterable:


# statements

● iterable is a collection of
objects like lists, and
tuples.
● The indented statements
inside the for loops are
executed once for each
item in an iterable.
● The variable var takes the
value of the next item of
the iterable each time
through the loop.
Ref:py_for1.py
Loop patterns:

These loops are generally constructed by:


● Initializing one or more variables before the loop starts

● Performing some computation on each item in the loop body,


possibly changing the variables in the body of the loop

● Looking at the resulting variables when the loop completes


Counting and Summing Loops

Ref:py_for2.py
Ref:py_for3.py
Maximum and minimum loops:
range() function in Python:
● range() allows the user to generate a series of numbers within a given
range.
● Depending on no.of arguments the user is passing to the function, the
user can decide where that series of numbers will begin and end, as well
as how big the difference will be between one number and the next.
Python range() function takes can be initialized in 3 ways.
a. range (stop) takes one argument.
b. range (start, stop) takes two arguments.
c. range (start, stop, step) takes three arguments.
range(stop)
Ref:py_for6.py
range(start,stop)
Ref:py_for7.py
range(start,stop,step)
Ref:py_for8.py
Points to remember - range()
● The range() function only works with integers, i.e. whole numbers.
● All arguments must be integers. Users can not pass a string or float number
or any other type in a start, stop, and step argument of a range().
● All three arguments can be positive or negative.
● The step value must not be zero. If a step is zero, python raises a ValueError
exception.
● range() is a type in Python.
● Users can access items in a range() by index, just as users do with a list.
Exercise:

1. Write a Python program to print all the even numbers within the given range.
2. Write a Python program to calculate the sum of all numbers from 1 to a
given number
3. Write a Python program to calculate the sum of all the odd numbers within
the given range
4. Write a Python program to count the total number of digits in a number.
a. 2, 1, 0, -1, -2, -3, -4
b. 2,1,0,-1,-2,-3,-4,-5
c. -5,-4,-3,-2,-1,0,1,2
d. -4,-3,-2,-1,0,1

Ans:a. 2, 1, 0, -1, -2, -3, -4


A. 5
B. 4
C. 3
D. 2
E. 0

Ans: C

You might also like