Professional Documents
Culture Documents
112556336-Python 1
112556336-Python 1
While loop
n=5 Hello! (n=5)
Hello!
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:
● 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:
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: C