0% found this document useful (0 votes)
2 views21 pages

Python Lab

Uploaded by

udaya.avcs0102
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)
2 views21 pages

Python Lab

Uploaded by

udaya.avcs0102
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

35021E02 - PYTHON PROGRAMMING LAB MANUAL

1. Python program to add two numbers.


Aim: To develop python program for addition of two numbers
Algorithm:
Step 1: Start the process
Step 2:Assign the value 5 to x
Step 3:Assign the value 10 to y
Step 4:Calculate and print x and y
Step 5: Stop the process

Program:
x=5
y = 10
print(x + y)

OUTPUT:
15

2. Python program to sum of series of N natural numbers


Aim: To develop Python program to sum of series of N natural numbers
Algorithm:
Step 1: Start the process
Step 2: Get input “Enter a number”
Step 3: if(num<0):
Step 4: print “Enter a positive number”
Step 5: else
Step 6: Assign sum=0
Step 7: while(num > 0):
Step 8: Calculate, sum = sum+num
Step 9: calculate num=num-1
Step 10: Print the sum
Step 11: Stop the process

Program:
num = int(input("Enter a number: "))
if num < 0:
print("Enter a positive number")
else:
sum = 0
while(num > 0):
sum += num
num -= 1
print("The sum is",sum)

Output:
Enter a number: 4
The sum is 10

3. Calculate simple interest

Aim: To develop Python program to Calculate simple interest


Algorithm:
Step 1: Start the process
Step 2: Create a variable as P, R, and T
Step 3: Assign the value to the variable.
Step 4: Calculate Simple_interest = (P * R * T) / 100.
Step 5: Print the Simple_interest
Step 6: Stop the process

Program:
P = 300
R=2
T=4
Simple_interest = (P * R * T) / 100
print("The simple interest is:", Simple_interest)

Output:
The simple interest is: 24.0

[Link] generate Fibonacci series using for loop


Aim: To develop Python program to generate Fibonacci series using for loop
Algorithm:
Step 1: Start the process
Step 2: Enter the range number:
Step 3: Assign 0 to First_val
Step 4: Assign 1 to Second_val
Step 5: for loop begins
Step 6: if(n<=1):
Step 7: Assign next=n
Step 8: else
Step 9: Calculate next = First_val + Second_val

Step 10: move Second_val to First_val


Step 11: Second_val = next
Step 12: Print next
Step 13: Stop the process

Program:
num = int(input("Enter the Range Number: "))
First_val = 0
Second_val = 1
for n in range(0, num):
if(n <= 1):
next = n
else:
next = First_val + Second_val
First_val = Second_val
Second_val = next
print(next)
Output:
Enter the Range Number:10
0
1
1
2
3
5
8
13
21
34

[Link] calculate factorial using while loop


Aim: To develop Python program to calculate factorial using while loop
Algorithm:
Step 1: Start the process
Step 2: Get input “Enter a number”
Step 3 : Assign fac=1
Step 4: For loop begins for i in range(1, num + 1):

Step 5: Calculate fac = fac * i


Step 6: Print fac
Step 7: Stop the process

Program:
num = int(input("enter a number: "))
fac = 1
for i in range(1, num + 1):
fac = fac * i
print("factorial of ", num, " is ", fac)

Output:
enter a number:3
factorial of 3 is 6

6. To find the greatest of three numbers using if condition


Aim: To develop Python program to find the greatest of three numbers using if condition
Algorithm:
Step 1: Start the process

Step 2: Get 3 floating point inputs

Step 3 : if (num1 > num2) and (num1 > num3):


Step 4: Assign number 1 as largest

Step 5: elif (num2 > num1) and (num2 > num3):

Step 6: Assign number 2 as largest


Step 7:else: Assign number 3 as largest
Step 8: Print largest number
Step 9: Stop the process

Program:

1. num1 = float(input("Enter first number: "))


2. num2 = float(input("Enter second number: "))
3. num3 = float(input("Enter third number: "))
4. if (num1 > num2) and (num1 > num3):
5. largest = num1
6. elif (num2 > num1) and (num2 > num3):
7. largest = num2
8. else:
9. largest = num3
10. print("The largest number is",largest)

Output:
Enter first number:10
Enter second number:20
Enter third number:30
The largest number is 30
7. Check whether the given number is odd or even:
Aim:To Check whether the given number is odd or even
Algorithm:
Step 1: Start the process

Step 2: Get input from user

Step 3: check if (num % 2) == 0:

Step 4: print “The number is even”

Step 5:else:
Step 6: print “The provided number is odd”
Step 7: Stop the process

Program:

num = int (input (“Enter any number“))

if (num % 2) == 0:

print (“The number is even”)

else:

print (“The provided number is odd”)

Output:

Enter any number to test whether it is odd or even:

887

887 is odd.

[Link] whether the given year is leap year or not


Aim:To check whether the given year is leap year or not
Algorithm:
Step 1: Start the process

Step 2: Read an integer from user, to year variable

Step 3: Check the condition if year is exactly divisible by 4 and 100, or the year is exactly
divisible by 400
Step 4: print “The given year is a LEAP year”
Step 5:else:
Step 6: print “The given year is NOT a LEAP year”
Step 7: Stop the process
Python Program

year = int(input('Enter year : '))

if (year%4 == 0 and year%100 != 0) or (year%400 == 0) :


print(year, "is a leap year.")
else :
print(year, "is not a leap year.")

Output

Enter year : 2022

2022 is not a leap year.

9. Quadratic Equation :

Aim: To calculate the solutions of the quadratic equation


ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
The solutions of this quadratic equation is given by:
(-b ± (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)

Algorithm:
Step 1: Start the process

Step 2: assign inputs for the variables a,b,c

Step 3: Calculate d [d = (b**2) - (4*a*c)]


Step 4: print solution1: sol1 = (-[Link](d))/(2*a)
Step 5: print solution2: sol2 = (-b+[Link](d))/(2*a)
Step 6: print('The solution are {0} and {1}'.format(sol1,sol2))
Step 7: Stop the process

Program:

import cmath
a=1
b=5
c=6
d = (b**2) - (4*a*c)
sol1 = (-[Link](d))/(2*a)
sol2 = (-b+[Link](d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))

Output

The solution are (-3+0j) and (-2+0j)

[Link] MULTIPLICATION:
Aim: To calculate the Matrix Multiplication using 2 input matrices
Algorithm:
Step1: Get input of two matrix from user.
Step 2: nested for loops to iterate through each row and each column.
Step 3: take one resultant matrix which is initially contains all 0.
Step 4: multiply each row elements of first matrix with each elements of second matrix,
Step 5: add all multiplied value.
Step 6: That is the value of resultant matrix.

Program:
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
for i in range(len(X)):
for j in range(len(Y[0])):

for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
for r in result:
print(r)

OUTPUT:
[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]
[Link] the greatest of three numbers using conditional operator

Aim: To Find the greatest of three numbers using conditional operator


Algorithm:

Step 1: Start the algorithm

Step 2: assign a=10

Step 3: assign b=20

Step 4: Check if, min = a if a < b else b

Step 5: Print min


Step 6: Stop the process

Program:

a=10
b =20
min = a if a < b else b
print(min)

Output:
20

[Link] recursive function

Aim: To implement the program for recursive function

Algorithm:

Step 1: Start the algorithm

Step 2: Define function tri_recursion(k)

Step 3: check (k>0)

Step 4: Calculate result = k + tri_recursion(k - 1)


Step 5: Print result
Step 6: else
Step 7: result=0
Step 8: print result
Step 9: Stop the process
Program:
def tri_recursion(k):
if(k > 0):
result = k + tri_recursion(k - 1)
print(result)
else:
result = 0
return result
print("\n\nRecursion Example Results")
tri_recursion(6)

Output:

Recursion Example Results


1
3
6
10
15
21
[Link] simple calculator program

Aim: To implement the simple calculator program using python

Program:
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
print("Select operation.")
print("[Link]")
print("[Link]")
print("[Link]")
print("[Link]")
while True:
choice = input("Enter choice(1/2/3/4): ")
if choice in ('1', '2', '3', '4'):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))
next_calculation = input("Let's do next calculation? (yes/no): ")
if next_calculation == "no":
break
else:
print("Invalid Input")

Select operation.
[Link]
[Link]
[Link]
[Link]
Enter choice(1/2/3/4): 1
Enter first number: 3
Enter second number: 4
3.0 + 4.0 = 7.0
Let's do next calculation? (yes/no): yes
Enter choice(1/2/3/4): 2
Enter first number: 6
Enter second number: 2
6.0 - 2.0 = 4.0
Let's do next calculation? (yes/no): yes
Enter choice(1/2/3/4): 3
Enter first number: 3
Enter second number: 3
3.0 * 3.0 = 9.0
Let's do next calculation? (yes/no): yes
Enter choice(1/2/3/4): 10
Invalid Input
Enter choice(1/2/3/4): 4
Enter first number: 10
Enter second number: 5
10.0 / 5.0 = 2.0
Let's do next calculation? (yes/no):
14. Calcluate the area of triangle

Aim: To Calcluate the area of triangle

s = (a+b+c)/2
area = √(s(s-a)*(s-b)*(s-c))

Algorithm:

Step 1: Start the algorithm

Step 2: assign a=5

Step 3: assign b=6

Step 4: assign c=7

Step 5: get 3 side inputs

Step 6: Calculate, s = (a + b + c) / 2
Step 7: Calculate, area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
Step 8: Print area
Step 6: Stop the process
PROGRAM:
a=5
b=6
c=7
a = float(input('Enter first side: '))
b = float(input('Enter second side: '))
c = float(input('Enter third side: '))
s = (a + b + c) / 2
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)

OUTPUT:

Enter first side: 2

Enter second side: 4

Enter third side: 5

The area of the triangle is 3.80

15. Swapping of Two variables

Aim: To swap two variables in python

Algorithm:

Step 1: Start the algorithm

Step 2: assign x=5

Step 3: assign y=10

Step 4: assign x to temp

Step 5: assign y to x

Step 6: assign temp to y

Step 7: Print x value


Step 8: Print y value
Step 9: Stop the process
Program:
x=5
y = 10
x = input('Enter value of x: ')
y = input('Enter value of y: ')
temp = x
x=y
y = temp
print('The value of x after swapping: {}'.format(x))
print('The value of y after swapping: {}'.format(y))

OUTPUT:

The value of x after swapping: 10

The value of y after swapping: 5

You might also like