Professional Documents
Culture Documents
Practical 1:: Functions and Lists
Practical 1:: Functions and Lists
Armstrong Program:
def arm(no):
currentNo = no
length = len(str(no))
sum = 0
lastdig = currentNo % 10
sum += lastdig**length
currentNo = int(currentNo/10)
return sum
armSum = arm(no)
if(armSum ==no):
else:
else:
Palindrome Program:
def palindrome(no):
temp = no
rev = 0
dig = no % 10;
if(temp == rev):
else:
palindrome(num)
Output:
(B) Aim: Write a recursive function to print the factorial for a given number.
Code:
def factorial(no):
if no == 1:
return 1
else:
return no*factorial(no-1)
def recursive(n):
n = int(input());
fact = 1;
fact = fact*n
n=n-1
elif(num == 0):
print("Factorial of 0 is 1.")
else:
Jai Hind College Application Form
Page 3 No.:187876
print("Factorial of", num, "is", factorial(num))
Name: Nivedita Patra Subject: Advanced Python Programming
Class: SYIT Date: 07/07/2021
Output:
and write a program that prints out all the elements of the list that are less than 5.
Code:
new_list = []
for i in a:
if i < 5:
new_list.append(i)
print(new_list)
print(list(filter_b))
Output:
(D) Aim: Write a function to take input of two numbers and an operator ( +, - ,*, /, %) from
user. Depending on the users selected operator perform the mathematical calculation.
Code:
following operators:")
result = 0
if num == '+':
else:
Output:
(E) Aim: Write a function to take input of lower and upper limit values and then print the
Armstrong numbers falling within that range.
# initialize sum
sum = 0
temp = j
digit = temp % 10
sum += digit ** 3
temp //= 10
if j == sum:
print(j)
(F) Aim: Write a Python function that prints out the first n rows of Pascal’s triangle. Take
the value of ‘n’ as input from user.
Code:
def pascal(m):
pas = 1
print()
pascal(rows)
Output:
(G) Aim: Write a recursive function to print Fibonacci series. Get the total numbers to be
printed from the user.
Code:
def fibo(num):
if(num == 0):
return 0
elif(num == 1):
return 1
else:
if n <= 0:
else:
print(fibo(n))
Output: