0% found this document useful (0 votes)
3 views6 pages

Python Programming Lab

The document contains five Python programming exercises, each with a specific aim and corresponding code. The exercises include displaying the Fibonacci sequence, calculating the factorial of a number, computing total marks, percentage and grade for students, adding two matrices, and calculating the perimeter and area of different shapes using functions. Each exercise is accompanied by sample outputs demonstrating the functionality of the code.

Uploaded by

gopabandhukamila
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Python Programming Lab

The document contains five Python programming exercises, each with a specific aim and corresponding code. The exercises include displaying the Fibonacci sequence, calculating the factorial of a number, computing total marks, percentage and grade for students, adding two matrices, and calculating the perimeter and area of different shapes using functions. Each exercise is accompanied by sample outputs demonstrating the functionality of the code.

Uploaded by

gopabandhukamila
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON PROGRAMMING UNIPOWER

Exp:1

Aim:- Program to display the Fibonacci sequence up to n-th term.


Program:
nterms = int(input("How many terms? "))

# first two terms


n1, n2 = 0, 1
count = 0

# check if the number of terms is valid


if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Output

How many terms? 7


Fibonacci sequence:
0
1
1
2
3
5
8

Exp:2

Aim:Write a program to find factorial of given number


Program:
def factorial(n):

# single line to find factorial


return 1 if (n==1 or n==0) else n * factorial(n - 1)

# Driver Code
num = 5
print("Factorial of",num,"is",factorial(num))

output:
Factorial of 5 is 120
Exp:3

Aim:- Python Program to Calculate Total Marks Percentage and Grade of a Student
Program:
print("Enter the marks of five subjects::")

subject_1 = float (input ())


subject_2 = float (input ())
subject_3 = float (input ())
subject_4 = float (input ())
subject_5 = float (input ())

total, average, percentage, grade = None, None, None, None

# It will calculate the Total, Average and Percentage


total = subject_1 + subject_2 + subject_3 + subject_4 + subject_5
average = total / 5.0
percentage = (total / 500.0) * 100

if average >= 90:


grade = 'A'
elif average >= 80 and average < 90:
grade = 'B'
elif average >= 70 and average < 80:
grade = 'C'
elif average >= 60 and average < 70:
grade = 'D'
else:
grade = 'E'

# It will produce the final output


print ("\nThe Total marks is: \t", total, "/ 500.00")
print ("\nThe Average marks is: \t", average)
print ("\nThe Percentage is: \t", percentage, "%")
print ("\nThe Grade is: \t", grade)

Output:-
Enter the marks of five subjects::
98
92
87
82
75

The Total marks is: 434.0 / 500.00

The Average marks is: 86.8

The Percentage is: 86.8 %

The Grade is: B


Exp:-4

Aim:- Write aProgram to add two matrices using nested loop


Program:
X = [[1,2,3],
[4 ,5,6],
[7 ,8,9]]

Y = [[9,8,7],
[6,5,4],
[3,2,1]]

result = [[0,0,0],
[0,0,0],
[0,0,0]]

# iterate through rows


for i in range(len(X)):
# iterate through columns
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]

for r in result:
print(r)

Output:-
[10, 10, 10]
[10, 10, 10]
[10, 10, 10]

Exp:-5

AIM:-Calculate the Parameter and Area of different Shapes using functions


Program:

# defining functions
def p_circle(radius):
para = 2 * 3.14 * radius
print("Parameter of Circle:", para)

def p_rectangle(height, width):


para = 2 * (height + width)
print("Parameter of Rectangle:", para)

def p_square(side):
para = 4 * side
print("Parameter of Square:", para)

def a_circle(radius):
area = 3.14 * radius * radius
print("Area of Circle:", area)

def a_rectangle(height, width):


area = height * width
print("Area of Rectangle:", area)

def a_square(side):
area = side * side
print("Area of Square:", area)

# printing the starting line


print("WELCOME TO A SIMPLE MENSURATION PROGRAM")

# creating options
while True:
print("\nMAIN MENU")
print("1. Calculate Parameter")
print("2. Calculate Area")
print("3. Exit")
choice1 = int(input("Enter the Choice:"))

if choice1 == 1:
print("\nCALCULATE PARAMETER")
print("1. Circle")
print("2. Rectangle")
print("3. Square")
print("4. Exit")
choice2 = int(input("Enter the Choice:"))

if choice2 == 1:
radius = int(input("Enter Radius of Circle:"))
p_circle(radius)

elif choice2 == 2:
height = int(input("Enter Height of Rectangle:"))
width = int(input("Enter Width of Rectangle:"))
p_rectangle(height, width)

elif choice2 == 3:
side = int(input("Enter Side of Square:"))
p_square(side)

elif choice2 == 4:
break

else:
print("Oops! Incorrect Choice.")

elif choice1 == 2:
print("\nCALCULATE AREA")
print("1. Circle")
print("2. Rectangle")
print("3. Square")
print("4. Exit")
choice3 = int(input("Enter the Choice:"))

if choice3 == 1:
radius = int(input("Enter Radius of Circle:"))
a_circle(radius)

elif choice3 == 2:
height = int(input("Enter Height of Rectangle:"))
width = int(input("Enter Width of Rectangle:"))
a_rectangle(height, width)

elif choice3 == 3:
side = int(input("Enter Side of Square:"))
a_square(side)

elif choice3 == 4:
break

else:
print("Oops! Incorrect Choice.")

elif choice1 == 3:
break

else:
print("Oops! Incorrect Choice.")

Output:

WELCOME TO A SIMPLE MENSURATION PROGRAM

MAIN MENU
1. Calculate Parameter
2. Calculate Area
3. Exit
Enter the Choice:1

CALCULATE PARAMETER
1. Circle
2. Rectangle
3. Square
4. Exit
Enter the Choice:2
Enter Height of Rectangle:4
Enter Width of Rectangle:5
Parameter of Rectangle: 18

MAIN MENU
1. Calculate Parameter
2. Calculate Area
3. Exit
Enter the Choice:2

CALCULATE AREA
1. Circle
2. Rectangle
3. Square
4. Exit
Enter the Choice:1
Enter Radius of Circle:2
Area of Circle: 12.56

MAIN MENU
1. Calculate Parameter
2. Calculate Area
3. Exit
Enter the Choice:5
Oops! Incorrect Choice.

MAIN MENU
1. Calculate Parameter
2. Calculate Area
3. Exit
Enter the Choice:3

You might also like