Python Programming Lab Manual 2024
Python Programming Lab Manual 2024
no:1
Aim:
To write python programs using simple statements and expressions
with exchange the value of two variables
Algorithm:
temp= P
P=Q
Q = temp
print ("The Value of P after swapping: ", P)
Output:
RESULT:
Thus the program to exchange the value of two variables is executed and the output is obtained.
2. b. Write a program to demonstrate variable and different number data types in
Python.
AIM:
To write a python program to demonstrate variable and different number data types in Python.
ALGORITHM:
Step1:Start.
Step2: read the value
Step3: Read B,C value.
Step4: Check the data type in each input
Step5: find out the data type
PROGRAM / SOURCECODE:
a=5
print("The type of a", type(a))
b = 40.5
print("The type of b", type(b))
c = 1+3j
print("The type of c", type(c))
print(" c is a complex number", isinstance(1+3j,complex))
OUTPUT:
RESULT:
Thus the program to demonstrate variable and different number data types in Python is executed
and the output is obtained.
2.C. PROGRAM TO DEMONSTRATE STRING METHODS/FUNCTIONS
AIM:
ALGORITHM :
Step 1: Start
Step 7: Check whether the string contains substring “hello” and print True or false Step8:
Step10: Stop
# Input a string
len(input_string)
cap_string = input_string.capitalize()
print("Capitalized string:",cap_string)
# 3. Convert to uppercase
uppercase_string = input_string.upper()
print("Uppercase:",uppercase_string)
# 4. Convert to lowercase
lowercase_string = input_string.lower()
print("Lowercase:",lowercase_string)
substring = "hello"
in the string:",words)
OUTPUT
hello, world!
RESULT:
Python program to demonstrate the following string methods : len(), capitalize(),upper(),
lower(), replace() and split() is executed and the output is obtained
3. Creating more in depth working of python like inputting the data.
AIM:
To write a python program to Calculate the Area of a Triangle.
Algorithm:
STEP 1: Read the values of three sides of a triangle
STEP 2: Calculate‘s’ value
STEP 3: Calculate the area using the formula (s*(s-a)*(s-b)*(s-c))
STEP 4: Find the square root of the result
STEP 5: Print the result
STEP 6: Stop
Program/source code:
a = float(input('Enter first side: '))
b = float(input('Enter second side: '))
Output:
Enter first side: 5
Enter second side: 4
Enter third side: 2
The area of the triangle is: 3.799671038392666
RESULT:
Thus The program to find the Area of a Triangle is executed and the output is obtained.
3 . (b) Calculation of Simple Interest and Compound Interest
AIM:
To write a python program to calculate the Simple Interest and Compound Interest
Algorithm:
STEP 1: Read the values of principal amount, no of years and rate of interest
STEP 2: Calculate the simple interest using (principal*time*rate)/100
STEP 3: Calculate the compound interest using principal * ( (1+rate/100)**time - 1)
STEP 4: Display the two values
STEP 5: Stop
Program /source code:
principal = float(input('Enter amount: '))
time = float(input('Enter time: '))
rate = float(input('Enter rate: '))
simple_interest = (principal*time*rate)/100
Output:
Enter amount: 20000
Enter time: 5
Enter rate: 6.7
Simple interest is: 6700.0
Compound interest is: 7659.994714602134
RESULT:
Thus The program to find the Calculation of Simple Interest and Compound Interest is
executed and the output is obtained.
4. Understanding and working with Boolean and other statements.
AIM:
To write a python program to find the maximum of a list of numbers.
ALGORITHM :
Step 1: Start.
Step 2: Read the number of element in the list.
Step 3: Read the number until loop n-1.
Step 4: Then Append the all element in list
Step 5: Go to STEP-3 up to n-1.
Step 6: Sort the listed values. Step
Step 7: Print the a[n-1] value.
PROGRAM/SOURCE CODE:
a=[ ]
n=int(input("Enter number of elements:")) for i in
range(1,n+1):
b=int(input("Enter element:"))
a.append(b)
a.sort()
OUTPUT :
Enter number of elements:5
Enter element: 3
Enter element:2
Enter element:1
Enter element:5
Enter element:4
RESULT:
The program to find the Maximum of a List of numbers is executed and the output is obtained.
4. (b) Sum of digits of a number
AIM:
To write a python program to find the sum of digits of a numbers.
Algorithm:
Step 1: Enter the value and store it in a variable.
Step 2: The while loop is used and the last digit of the number is obtained by using the
modulus operator.
Step 3: The digit is added to another variable each time the loop is executed.
Program/source code:
n=int(input("Enter a number:"))
tot=0
while(n>0):
dig=n%10
tot=tot+dig
n=n//10
print("The total sum of digits is:",tot)
Output:
Enter a number:4 5 6
The total sum of digits is: 15
RESULT:
The program to find the sum of digits of a numbers is executed and the output is obtained.
4 (c) Printing Fibonacci Series
AIM:
To write a python program to find the Fibonacci Series of a numbers.
Algorithm:
Step 1: Start
Step 2: Declare variables i, t1,t2 , sum
Step 3: Initialize the variables, t1=0, t2=1, and sum=0
Step 4: Enter the number of terms of Fibonacci series to be printed
Step 5: Print First two terms of series
Step 6: Use loop for the following steps
-> sum=t1+t3
-> t1=t2
-> t2=sum
-> increase value of i each time by 1
-> print the value of show
Step 7: Stop
Program/source code:
n = int(input("Enter the value of 'n': "))
t1 = 0
t2 = 1
sum = 0
count = 1
count += 1
t1 = t2
t2 = sum
sum = t1 + t2
Output:
The program to find the Fibonacci Series of a numbers is executed and the output is obtained.
Algorithm:
Step 1: Start
Step 2: Read lower and upper range
Step 3: Using a for loop, iterate on all the numbers in the range
Step 4 : For each number, check if its prime or not
Step 5 : If the number is prime, print
Else skip
Step 6 : Stop
Output:
Prime numbers between 900 and 1000 are:
907
911
919
929
937
941
947
953
967
971
977
983
991
997
Result:
Thus, the program using conditionals and loops has been executed and verified.
5., Understanding to provide the use of pandas’ library for data analysis.
AIM:
ALGORITHM:
1. Start the Program.
2. Import numpy and pandas
3. Create a dictionary with student name, marks of three subjects
4. Convert the dictionary to a data frame
5. Print the data frame
6. Stop
PROGRAM:
import numpy as np
import pandas as pd
mydictionary = {'names': ['Somu', 'Kiku', 'Amol', 'Lini'],
'
‘physics': [68, 74, 77, 78],
'chemistry': [84, 56, 73, 69],
'algebra': [78, 88, 82, 87]}
OUTPUT:
names physics chemistry algebra
0 Somu 68 84 78
1 Kiku 74 56 88
2 Amol 77 73 82
3 Lini 78 69 87
Result:
Thus, the pandas program has been executed and verified successfully.
6. Understanding how to deal with different type of errors that one can encounter while working
with python.
AIM:
To write a python program to deal with different type of errors that one can encounter
while working with python.
Algorithm:
Step 1: Start
Step 2: Read the values
Step 3: Using a for loop, iterate on all the numbers in the
range
X=10
If x==10
Print(“x is 10”)
Output:
If x == 10
^
SyntaxError: expected ':'
Name error:
x=10
y=15
z = calculate sum(x,w)
print(z)
output:
Z = calculate_sum(x, w)
^
NameError: name 'w' is not defined
Type error:
X =” 10”
Y= 5
Z=X+Y
Print(Z)
Output:
Z = x + y
~~^~~
TypeError: can only concatenate str (not "int") to str
Index error:
my_list[100,200,300,400,500]
Print(my_list[6])
Output:
print(my_list[p
~~^~~
IndexError: list index out of range
Result:
Thus the python program to deal with different type of errors that one can encounter
while working with python successfully.
7.Understanding and finding how to deal with miscellaneous things in python and regression
import sys
import matplotlib
matplotlib.use('Agg')
x = [5,7,8,7,2,17,2,9,4,11,12,9,6]
y = [99,86,87,88,111,86,103,87,94,78,77,85,86]
def myfunc(x):
plt.scatter(x, y)
plt.plot(x, mymodel)
plt.show()
plt.savefig(sys.stdout.buffer)
sys.stdout.flush()
OUTPUT
Result:
Thus, the matplotlib program has been executed and verified successfully
8.Understanding the try, throw, catch, throwing an exception, catching an exception.
AIM:
To write a python program to deal with different exception that one can encounter
while working with python.
Algorithm:
Step 1: Start
Step 2: Read the values
Step 3: Using a for loop, iterate on all the numbers in the
range
class InvalidAgeException(Exception):
pass
number = 18
try:
raise InvalidAgeException
else:
print("Eligible to Vote")
except InvalidAgeException:
print("Exception occurred: Invalid Age")
OUTPUT:
Result:
Thus the program to deal with try, throw, catch, throwing an exception, catching an
using matplotlib.
AIM:
ALGORITHM:
1. Start the Program.
2. Import numpy , matplotlib and pandas.
DATA SETS:
import pandas as pd
import matplotlib.pyplot as plt
correlation_full_health = full_health_data.corr()
axis_corr = sns.heatmap(
correlation_full_health,
square=True