Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Name: Nivedita Patra Subject: Advanced Python Programming

Class: SYIT Date: 07/07/2021

Practical 1: Functions and Lists


(A) Aim: Write a function to check the input value is Armstrong and also write the function
for Palindrome.
Code:

Armstrong Program:

def arm(no):

currentNo = no

length = len(str(no))

sum = 0

while currentNo > 0:

lastdig = currentNo % 10

sum += lastdig**length

currentNo = int(currentNo/10)

return sum

no=int(input("Enter any positive any number: "))

if(no > 0):

armSum = arm(no)

if(armSum ==no):

print("The number", no, "is an Armstrong number.")

else:

print("The number", no, "is not an Armstrong number")

else:

print("Enter a valid number.")

Jai Hind College Application Form


Page 1 No.:187876
Name: Nivedita Patra Subject: Advanced Python Programming
Class: SYIT Date: 07/07/2021
Output:

Palindrome Program:

def palindrome(no):

temp = no

rev = 0

while(no > 0):

dig = no % 10;

rev = rev*10 + dig;

no = no//10; #the symbol(//) will remove the floating point


numbers

if(temp == rev):

print(temp, "is a Palindrome");

else:

print(temp, "is not a Palindrome");

num = int(input("Enter a number:"))

palindrome(num)

Output:

Jai Hind College Application Form


Page 2 No.:187876
Name: Nivedita Patra Subject: Advanced Python Programming
Class: SYIT Date: 07/07/2021

(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;

while(n > 0):

fact = fact*n

n=n-1

print("The factorial is", recursive)

num = int(input("Enter the number:"))

if(num < 0):

print("Factorial of negative number is not possible.")

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:

(C) Aim: Take a list, say for example this one:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

and write a program that prints out all the elements of the list that are less than 5.

Jai Hind College Application Form


Page 4 No.:187876
Name: Nivedita Patra Subject: Advanced Python Programming
Class: SYIT Date: 07/07/2021

Code:

print("Printing all elements less than 5 with append function:")

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

new_list = []

for i in a:

if i < 5:

new_list.append(i)

print(new_list)

print("Printing all elements less than 5 with Lambda


function:")

b = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

filter_b = filter(lambda x: x < 5, b)

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:

Jai Hind College Application Form


Page 5 No.:187876
Name: Nivedita Patra Subject: Advanced Python Programming
Class: SYIT Date: 07/07/2021

num1 = int(input("Enter First Number: "))

num2 = int(input("Enter Second Number: "))

print("Enter the operation you would like to perform from the

following operators:")

num = input(" Operators are +,-,*,/,%: ")

result = 0

if num == '+':

result = num1 + num2

elif num == '-':

result = num1 - num2

elif num == '*':

result = num1 * num2

elif num == '/':

result = num1 / num2

elif num == '%':

result = num1 / num2

else:

print("Input character is not recognized!")

print(num1, num , num2, ":", result)

Output:

Jai Hind College Application Form


Page 6 No.:187876
Name: Nivedita Patra Subject: Advanced Python Programming
Class: SYIT Date: 07/07/2021

(E) Aim: Write a function to take input of lower and upper limit values and then print the
Armstrong numbers falling within that range.

Jai Hind College Application Form


Page 7 No.:187876
Name: Nivedita Patra Subject: Advanced Python Programming
Class: SYIT Date: 07/07/2021
Code:

low = int(input("Enter lower range: "))

up = int(input("Enter upper range: "))

for j in range(low,up + 1):

# initialize sum

sum = 0

# find the sum of the cube of each digit

temp = j

while temp > 0:

digit = temp % 10

sum += digit ** 3

temp //= 10

if j == sum:

print(j)

print("The above numbers are an Armstrong number.")


Output:

(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:

Jai Hind College Application Form


Page 8 No.:187876
Name: Nivedita Patra Subject: Advanced Python Programming
Class: SYIT Date: 07/07/2021

def pascal(m):

for i in range(0, rows):

pas = 1

for j in range(1, rows-i):

print(" ", end="")

for k in range(0, i+1):

print(" ", pas , end="")

pas = int(pas * (i - k) / (k + 1))

print()

rows = int(input("Enter the number of rows : "))

pascal(rows)

Output:

(G) Aim: Write a recursive function to print Fibonacci series. Get the total numbers to be
printed from the user.

Code:

Jai Hind College Application Form


Page 9 No.:187876
Name: Nivedita Patra Subject: Advanced Python Programming
Class: SYIT Date: 07/07/2021

def fibo(num):

if(num == 0):

return 0

elif(num == 1):

return 1

else:

return (fibo(num - 2) + fibo(num - 1))

n = int(input("Enter a number to generate the Fibonacci series : "))

if n <= 0:

print("Please enter a positive integer")

else:

print("Fibonacci Series of given number", n, "is:",)

for n in range(0, n):

print(fibo(n))

Output:

Jai Hind College Application Form


Page
No.:187876
10

You might also like