PYTHON LAB MANUAL
Experiment-1
Aim:
☛I. Use a web browser to go to the Python
[Link] This page contains information about
Python and links to Python-related pages, and it gives you
the ability to search the Python documentation.
Overview of the [Link] Homepage
The official Python website is the central hub for everything related to the
Python programming language. On the homepage you’ll find:
📥 Downloads – Pages where you can download Python installers and source
code for various platforms.
📘 Documentation – Links to tutorials, library references, and guides on how to
use Python (for beginners and experienced users).
🔍 Search Box – A search tool to find specific content on the Python site
(including docs).
📰 Latest News – Updates about Python releases, events, surveys, and
community announcements.
🧑🤝🧑 Community – Resources for community events, mailing lists, forums, and
special interest groups.
🧩 Other Key Sections
Beyond the homepage, [Link] includes:
Beginner’s Guides: Tutorials and introductions to start learning Python.
Documentation: Comprehensive reference material for the language and its
standard library (searchable).
Help & FAQ: Support pages for common questions and assistance.
PYTHON LAB MANUAL
Community and Events: Information on Python-related events, user groups,
and conferences.
🐍 About Python
Python is a high-level, general-purpose programming language known for being
easy to read and use. It’s widely used in web development, data science,
automation, artificial intelligence, and more.
📌 Summary
Visiting [Link] (which loads [Link] gives you:
✔ A way to download Python for your computer
✔ Documentation and tutorials to learn and use Python
✔ Search tools for finding information on the site
✔ News and community resources for Python programmers
Aim:
☛ [Link] the Python interpreter and type help() to start
the online help utility
1. Start the Python interpreter
Open a terminal (Command Prompt, PowerShell, or macOS/Linux Terminal)
and type:
python
or, on some systems:
python3
You should see something like:
Python 3.x.x (default, ...)
>>>
That >>> means you’re inside the Python interpreter.
PYTHON LAB MANUAL
2. Start the online help utility
At the >>> prompt, type:
help()
and press Enter.
You’ll see the interactive help system start, usually with a message like:
Welcome to Python 3.x's help utility!help>
3. Using help()
Type a topic (e.g., print, list, modules) and press Enter
Type q to quit the help utility
Type quit or press Ctrl+D (macOS/Linux) / Ctrl+Z then Enter (Windows)
to exit Python
Experiment-2
Aim:
☛ Start a Python interpreter and use it as a Calculator.
Solution :
To Start the Python interpreter in your Linux (Ubuntu) system, open terminal by
pressing Ctrl + ALT + T and then type following command press enter.
$ python3
And then type following
Integer to Integer :
PYTHON LAB MANUAL
>>> a = 10
>>> b = 30
>>> print ( type ( a ) )
< class ' int ' >
>>> print ( type ( b ) )
< class ' int ' >
>>> print ( a + b )
40
>>> print ( a - b )
-20
>>> print ( a * b )
300
>>> print ( a / b )
0.3333333333333333
>>> print ( a % b )
10
>>> print ( a ** b )
1000000000000000000000000000000
>>> print ( a // b )
0
Integer to Float :
>>> a = 10
>>> b = 3.25
>>> print (type(a))
< class ' int ' >
>>> print (type(b))
< class ' float ' >
>>> print ( a + b )
PYTHON LAB MANUAL
13.25
>>> print ( a - b )
6.75
>>> print ( a * b )
32.5
>>> print ( a / b )
3.076923076923077
>>> print ( a % b )
0.25
>>> print ( a ** b )
1778.2794100389228
>>> print ( a // b )
3.0
Float to Float :
>>> a = 6.78
>>> b = 2.34
>>> print (type(a))
< class ' float ' >
>>> print ( type ( b ) )
< class ' float ' >
>>> print ( a + b )
9.120000000000001
>>> print ( a - b )
4.44
>>> print ( a * b )
15.8652
PYTHON LAB MANUAL
>>> print ( a / b )
2.897435897435898
>>> print ( a % b )
2.1000000000000005
>>> print ( a ** b )
88.12060773350571
>>> print ( a // b )
2.0
Integer to String :
>>> a = 6
>>> b = " python "
>>> print ( type ( a ) )
< class ' int ' >
>>> print ( type ( b ) )
< class ' str ' >
>>> print ( a + b )
TypeError: unsupported operand type(s) for + : ' int ' and
' str '
>>> print ( a - b )
TypeError: unsupported operand type(s) for - : ' int ' and
' str '
>>> print ( a * b )
python python python python python python
>>> print ( a / b )
TypeError: unsupported operand type(s) for / : ' int ' and
' str '
>>> print ( a % b )
PYTHON LAB MANUAL
TypeError: unsupported operand type(s) for % : ' int ' and
' str '
>>> print ( a ** b )
TypeError: unsupported operand type(s) for ** : ' int '
and ' str '
>>> print ( a // b )
TypeError: unsupported operand type(s) for // : ' int '
and ' str '
String to String :
>>> a = " python "
>>> b = " Programming "
>>> print ( type ( a ) )
< class ' str ' >
>>> print ( type ( b ) )
< class ' str ' >
>>> print ( a + b )
python Programming
>>> print ( a - b )
TypeError: unsupported operand type(s) for - : ' str ' and
' str '
>>> print ( a * b )
TypeError: unsupported operand type(s) for * : ' str ' and
' str '
>>> print ( a / b )
TypeError: unsupported operand type(s) for / : ' str ' and
' str '
>>> print ( a % b )
TypeError: unsupported operand type(s) for % : ' str ' and
' str '
PYTHON LAB MANUAL
>>> print ( a ** b )
TypeError: unsupported operand type(s) for ** : ' str '
and ' str '
>>> print ( a // b )
TypeError: unsupported operand type(s) for // : ' str '
and ' str '
Integer to Complex :
>>> a = 9
>>> b = 3 + 4j
>>> print ( type ( a ) )
< class ' int ' >
>>> print ( type ( b ) )
< class ' complex' >
>>> print ( a + b )
( 12 + 4j )
>>> print ( a - b )
( 6 - 4j )
>>> print ( a * b )
( 27 + 36j )
>>> print ( a / b )
( 1.08 - 1.44j )
>>> print ( a % b )
TypeError: unsupported operand type(s) for % : ' int ' and
' complex '
>>> print ( a ** b )
( - 586.5166552058375 + 432.9425056126467j )
>>> print ( a // b )
PYTHON LAB MANUAL
TypeError: unsupported operand type(s) for //: ' int ' and
' complex '
Float to Complex :
>>> a = 4.83
>>> b = 3 + 4j
>>> print ( type ( a ) )
< class ' float ' >
>>> print ( type ( b ) )
< class ' complex' >
>>> print ( a + b )
( 7.83 + 4j )
>>> print ( a - b )
( 1.83 - 4j )
>>> print ( a * b )
( 14.49 + 19.32j )
>>> print ( a / b )
TypeError: unsupported operand type(s) for / : ' int ' and
' complex '
>>> print ( a % b )
TypeError: unsupported operand type(s) for % : ' int ' and
' complex '
>>> print ( a ** b )
( 112.66380061063158 + 1.8253767513645904j )
>>> print ( a // b )
TypeError: unsupported operand type(s) for //: ' int ' and
' complex '
PYTHON LAB MANUAL
Experiment-3
Aim:☛ Write a program to calculate compound interest when
principal, rate and number of periods are given.
Solution :
The formula to calculate compound interest annually is given by:
A = P ( 1 + R / 100 )t
Compound Interest = A - P
Where,
A is amount,
P is the principal amount
R is the rate of interest,
T is the time span
PROGRAM: ([Link])
# Python program to compute compound interest
p = int(input(" Enter the principal amount : "))
t = float(input(" Enter the time in years : " ))
r = float(input(" Enter the rate of interest : "))
# compute compound interest
amount = p * (pow((1 + r / 100), t))
ci = amount - p
# print
print(" Compound Interest : " , ci )
OUTPUT:
Enter the principal amount : 50000
Enter the time in years : 3.8
Enter the rate of interest : 2
Compound Interest : 3907.6819031304913
Experiment-4
PYTHON LAB MANUAL
Aim:
☛ Read the name, address, email and phone number of a
person through the keyboard and print the details.
Solution :
PROGRAM: ([Link])
Name = input("Enter Person Name : ")
Address = input("Enter Person Address : ")
Email = input("Enter Person Email : ")
Phone_no = input("Enter Phone number : ")
print(" Please Confirm your provided Information\n " )
print(" Person Name : " ,Name )
print(" Person Address : " , Address )
print(" Person Email : " , Email )
print(" Person Phone_no : " , Phone_no )
OUTPUT:
Enter Person Name: Madhu
Enter Person Address : HYDERABAD
Enter Person Email : madhu@[Link]
Enter Person Phone_no : 9656123456
Please Confirm your provided Information
Person Name : Madhu
Person Address : HYDERABAD
Person Email : madhu@[Link]
Person Phone_no : 9656123456
Experiment-5
Aim:
PYTHON LAB MANUAL
☛ Print the below triangle using for loop.
5
44
333
2222
11111
Solution :
PROGRAM: ([Link])
Num = int( input ( "Enter no of rows = " ) )
# reverse for loop from 5 to 1
for i in range ( 1 , Num + 1 ) :
for j in range ( i ) :
print ( Num , end = ' ' )
Num -= 1
print ( ' ' )
OUTPUT:
Enter no of rows = 5
5
4 4
3 3 3
2 2 2 2
1 1 1 1 1
Experiment-6
Aim:
PYTHON LAB MANUAL
☛ Write a program to check whether the given input is digit or
lowercase character or uppercase character or a special
character(use 'if-else-if' ladder)
Solution :
PROGRAM: (check_input_char.py)
ch = input ( " Enter a character : " )
if ( ord ( ch ) >= 65 and ord ( ch ) <= 90 ) :
print ( ch , " is Upper Case Character " )
elif ( ord ( ch ) >= 97 and ord ( ch ) <= 122 ) :
print ( ch , " is Lower Case Character " )
elif ( ord ( ch ) >= 48 and ord ( ch ) <= 57 ) :
print ( ch , " is Digit " )
else:
print ( ch , " is Symbol " )
OUTPUT:
Sample Run 1:
Enter a character : 5
5 is Digit
Sample Run 2:
Enter a character : H
H is Upper Case Character
Sample Run 3:
Enter a character : b
b is Lower Case Character
Sample Run 4:
Enter a character : @
@ is Symbol
PYTHON LAB MANUAL
Experiment-7
Aim:
☛ Python program to print all prime numbers in a given interval
(use break)
Solution :
PROGRAM: (prime_numbers.py)
lower_value = int ( input ( "Enter the Lowest Range Value: " ) )
upper_value = int ( input ( "Enter the Upper Range Value: " ) )
print ( "Prime numbers between " , lower_value , " and " ,
upper_value , " are : " )
for num in range ( lower_value , upper_value + 1 ) :
# all prime numbers are greater than 1
if num > 1 :
for i in range ( 2 , num) :
if ( num % i ) == 0 :
break
else :
print ( num , end = " \t " )
OUTPUT:
Enter the Lowest Range Value: 5
Enter the Upper Range Value: 36
PYTHON LAB MANUAL
Prime numbers between 5 and 36 are :
5 7 11 13 17 19 23 29
31
Experiment-8
Aim:
☛ Write a program to convert a list and tuple into arrays
Solution :
PROGRAM: ([Link])
import numpy as np
list = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]
print ( " List to array: " )
array_1 = np . asarray ( list )
print ( type ( array_1 ) )
print ( array_1 )
print ( )
print ( )
tuple = ( [ 8, 4 , 6 ] , [ 1 , 2 , 3 ] )
print ( " Tuple to array: " )
array_2 = np . asarray ( tuple )
print ( type ( array_2 ) )
print ( array_2 )
OUTPUT:
List to array:
< class ' numpy . ndarray ' >
[ 1 2 3 4 5 6 7 8 ]
PYTHON LAB MANUAL
Tuple to array:
< class ' numpy . ndarray ' >
[ [ 8 4 6 ]
[ 1 2 3 ] ]
Experiment-9
Aim:
☛ Write a program to find common values between two arrays
Solution :
PROGRAM: (common_values.py)
import numpy as np
array1 = np . array ( [ 5, 10 , 20 , 40 , 60 ] )
print ( " Array1 : " , array1 )
array2 = np . array ( [10 , 30 , 40 ] )
print ( " Array2 : " , array2 )
print ( " Common values between two arrays are : " )
print ( np . intersect1d ( array1 , array2 ) )
OUTPUT:
Array1 : [ 5 10 20 40 60 ]
Array2 : [ 10 30 40 ]
Common values between two arrays are :
[ 10 40 ]
Experiment-10
Aim:
PYTHON LAB MANUAL
☛ Write a function called palindrome that takes a string
argument and returnsTrue if it is a palindrome and False
otherwise. Remember that you can use the built-in function len to
check the length of a string.
Solution :
PROGRAM: (palindrome_str.py)
def Palindrome(str):
# Run loop from 0 to len/2
for i in range( 0 , int (len(str) / 2)):
if str[i] != str[len(str) - i - 1 ]:
return False
return True
# main function
s = input(" Enter any string to check palindrome = ")
boolean = Palindrome(s)
if (boolean):
print( s , " is palindrome ")
else :
print( s , " is not palindrome ")
OUTPUT:
Sample Output - 1:
Enter any string to check palindrome = MADAM
MADAM is palindrome
Sample Output - 2:
Enter any string to check palindrome = SIR
SIR is not palindrome
Experiment-11
PYTHON LAB MANUAL
Aim:
☛ Write a function called is_sorted that takes a list as a
parameter and returns True if the list is sorted in ascending order
and False otherwise.
Solution :
PROGRAM: (is_sorted.py)
def is_sorted(user_list):
new_list = sorted(user_list)
print(" Your Sorted list : " , sorted(user_list))
if(new_list == user_list):
return True
else:
return False
user_list = [int(x) for x in input("Please enter a list:
").split()]
print(" Your list : " , user_list)
print(is_sorted(user_list))
OUTPUT:
Please enter a list: 7 8 9 4 5 6 10 12
Your list : [7, 8, 9, 4, 5, 6, 10, 12]
Your Sorted list : [4, 5, 6, 7, 8, 9, 10, 12]
False
Experiment-12
Aim:
PYTHON LAB MANUAL
☛ Write a function called has_duplicates that takes a list and
returns True if there is any element that appears more than once.
It should not modify the original list.
Solution :
PROGRAM: (has_duplicates.py)
def has_duplicate(my_list):
for k in my_list:
if my_list.count(k) > 1 :
return True
break
else :
return False
user_list = input(" Please enter a list : ").split()
print(" Your list : " , user_list)
res = has_duplicate ( user_list )
if res :
print( " The list contain duplicate values ")
else :
print( " The list not contain duplicate values ")
OUTPUT:
Sample Output 1:
Please enter a list : 6 7 2 3 1 3 1 8 6
Your list : [ '6' , '7' , '2' , '3' , '1' , '3' , '1' , '8' ,
'6' ]
The list contain duplicate values
Sample Output 2:
Please enter a list : 6 7 2 3 1 8 9
Your list : [ '6' , '7' , '2' , '3' , '1' , '8' , '9' ]
The list not contain duplicate values
PYTHON LAB MANUAL
Experiment-13
Aim:
☛ Write a function called has_duplicates that takes a list and
returns True if there is any element that appears more than once.
It should not modify the original list.
Solution :
PROGRAM: (has_duplicates.py)
def has_duplicate(my_list):
for k in my_list:
if my_list.count(k) > 1 :
return True
PYTHON LAB MANUAL
break
else :
return False
user_list = input(" Please enter a list : ").split()
print(" Your list : " , user_list)
res = has_duplicate ( user_list )
if res :
print( " The list contain duplicate values ")
else :
print( " The list not contain duplicate values ")
OUTPUT:
Sample Output 1:
Please enter a list : 6 7 2 3 1 3 1 8 6
Your list : [ '6' , '7' , '2' , '3' , '1' , '3' , '1' , '8' ,
'6' ]
The list contain duplicate values
Sample Output 2:
Please enter a list : 6 7 2 3 1 8 9
Your list : [ '6' , '7' , '2' , '3' , '1' , '8' , '9' ]
The list not contain duplicate values
Experiment-14
Aim:
☛ Write a function called remove_duplicates that takes a list
and returns a new list with only the unique elements from the
original. Hint: they don't have to be in the same order.
Solution :
PYTHON LAB MANUAL
PROGRAM: (remove_duplicates.py)
def remove_duplicates(my_list):
for k in my_list :
if my_list . count(k) > 1 :
del my_list[my_list.index(k)]
user_list = input(" Please enter a list : ").split()
print(" Your list : " , user_list)
remove_duplicates(user_list)
print(" Your list without duplicates : \n " , user_list)
OUTPUT:
Please enter a list : 6 7 2 3 1 3 1 8 6
Your list : [ '6' , '7' , '2' , '3' , '1' , '3' , '1' , '8' ,
'6' ]
Your list without duplicates :
[ '7' , '2' , '3' , '1' , '8' , '6' ]
Experiment-15
Aim:
☛ The wordlist I provided, [Link], doesn't contain single
letter words. So you might want to add "I", "a", and the empty
string.
Solution :
Text File:[Link]
PYTHON LAB MANUAL
apple
banana
cat
dog
elephant
PROGRAM: (single_letter.py)
def load_words(filename):
with open(filename, "r") as file:
words = [[Link]() for line in file]
# Add missing single-letter words and empty string
if "" not in words:
[Link]("")
if "I" not in words:
[Link]("I")
if "a" not in words:
[Link]("a")
return words
# Sample usage
words = load_words("[Link]")
print("Total number of words:", len(words))
print("Sample words:", words[:10])
OUTPUT:
Total number of words: 8
Sample words: ['apple', 'banana', 'cat', 'dog', 'elephant', '',
'I', 'a']
Experiment-16
PYTHON LAB MANUAL
Aim:
☛ Write a python code to read dictionary values from the user.
Construct a function to invert its content. i.e., keys should be
values and values should be keys.
Solution :
PROGRAM: (invert_dictionary.py)
import ast
def swap_dict(old_dict):
return { value : key for key , value in old_dict.items ()
}
old_dict = input(" Please enter a dictionary : \n ")
old_dict = ast.literal_eval(old_dict)
# Printing original dictionary
print(" Original dictionary is : \n " , old_dict)
print()
new_dict = swap_dict(old_dict)
# Printing new dictionary
print(" New dictionary is : \n " , new_dict)
OUTPUT:
Please enter a dictionary :
{ 'A' : 65 , 'B' : 66 , 'C' : 67 , 'D' : 68 , 'E' : 69 , 'F' :
70 , 'G' : 71 , 'H' : 72 }
Original dictionary is :
{'A': 65, 'B': 66, 'C': 67, 'D': 68, 'E': 69, 'F': 70, 'G':
71, 'H': 72}
New dictionary is :
PYTHON LAB MANUAL
{65: 'A', 66: 'B', 67: 'C', 68: 'D', 69: 'E', 70: 'F', 71:
'G', 72: 'H'}
Experiment-18
Aim:
☛ Add a comma between the characters. If the given word is
'Apple', it should become 'A,p,p,l,e'.
Solution :
PROGRAM: (add_comma.py)
def add_comma(x):
return ','.join(x)
in_str = "Apple"
print(add_comma(in_str))
OUTPUT:
PYTHON LAB MANUAL
A,p,p,l,e
Experiment-19
Aim:
☛ Remove the given word in all the places in a string?
Solution :
PROGRAM: (remove_words.py)
def remove_all_words(in_str,sub):
my_list = in_str.split( )
for i in my_list :
if i == sub :
del my_list[my_list.index(i)]
final_string = ' '
for i in my_list :
final_string = final_string + i + ' '
return final_string
str_in = input(" Enter any string = ")
sub = input(" Enter sub string ( which word you want to delete )
= ")
result = remove_all_words(str_in , sub)
PYTHON LAB MANUAL
print(" Updated string after removal sub_string : \n " , result)
OUTPUT:
Enter any string = Hai, this is python, welcome to Study Glance
Enter sub string ( which word you want to delete ) = Study
Updated string after removal sub_string :
Hai, this is python, welcome to Glance
Experiment-20
Aim:
☛ Write a function that takes a sentence as an input parameter
and replaces the first letter of every word with the corresponding
upper case letter and the rest of the letters in the word by
corresponding letters in lower case without using a built-in
function?
Solution :
PROGRAM: (replace_first.py)
def replace_first(str_in):
x = list(str_in)
i = 0
while i < len(x):
# consider first character of word
if i == 0 :
if ord(x[i]) >= 97 and ord(x[i]) <= 122 :
PYTHON LAB MANUAL
x[i] = chr(ord(x[i]) - 32)
elif x[i] == ' ':
i = i + 1
if ord(x[i]) >= 97 and ord(x[i]) <= 122 :
y = ord(x[i])
x[i] = chr(y - 32)
else :
if ord(x[i]) >= 65 and ord(x[i]) <= 92 :
x[i] = chr(ord(x[i]) + 32)
i = i + 1
str_out = ' '
for j in x :
str_out = str_out + j
return str_out
str_in = input(" Enter any string = ")
str_op = replace_first(str_in)
print(str_op)
OUTPUT:
Enter any string = welcome TO STUdy glance
Welcome To Study Glance
Experiment-21
Aim:
☛ Write a python program that defines a matrix and prints.
Solution :
PROGRAM: (matrix_print.py)
PYTHON LAB MANUAL
R = int(input(" Enter the number of rows : "))
C = int(input(" Enter the number of columns : "))
# Initialize matrix
matrix = []
print(" Enter the entries row - wise : ")
# For user input
for i in range(R):
a = []
for j in range(C):
[Link](int(input()))
[Link](a)
print()
# For printing the matrix
print(" Matrix Values are: \n ")
for i in range(R) :
for j in range(C):
print(matrix[i][j] , end = " ")
print()
OUTPUT:
Enter the number of rows : 3
Enter the number of columns : 3
Enter the entries row - wise :
1
2
3
4
5
6
7
8
9
Matrix Values are:
1 2 3
4 5 6
PYTHON LAB MANUAL
7 8 9
Experiment-22
Aim:
☛ Write a python program to perform multiplication of two
square matrices.
Solution :
In this program we use "numpy" package to create matrices. So we need to
install "numpy" package using the following commands.
To Install in Windows:
pip install numpy
To Install in Linux:
sudo apt-get install numpy
PROGRAM: (matrix_mul.py)
PYTHON LAB MANUAL
import numpy as np
a = [Link]([[ 1 , 2 ] , [ 4 , 5 ]])
b = [Link]([[ 6 , 5 ] , [ 3 , 2 ]])
c = [Link](a,b)
print(" Multiplication of two Square Matrices is \n " , c)
OUTPUT:
Multiplication of two Square Matrices is
[[12 9]
[39 30]]
Experiment-23
Aim:
☛ How do you make a module? Give an example of
construction of a module using different geometrical shapes and
operations on them as its functions.
Solution :
PROGRAM: ([Link])
from math import pi
def rectangle(l,b):
return l * b
def square(s):
return s * s
def circle(r):
return pi * r * r
def triangle(l,b):
PYTHON LAB MANUAL
return 1 / 2 * l * b
PROGRAM: (geometrical_shapes.py)
import area
l = int(input(" Enter value of height = "))
b = int(input(" Enter value of width = "))
s = int(input(" Enter value of side = "))
r = int(input(" Enter value of radius = "))
print(" Area of rectangle = " , [Link](l,b))
print(" Area of square = " , [Link](s))
print(" Area of circle = " , [Link](r))
print(" Area of triangle = " , [Link](l,b))
OUTPUT:
Enter value of height = 6
Enter value of width = 6
Enter value of side = 6
Enter value of radius = 6
Area of rectangle = 36
Area of square = 36
Area of circle = 113.09733552923255
Area of triangle = 18.0
Experiment-24
Aim:
PYTHON LAB MANUAL
☛ Use the structure of exception handling all general-purpose
exceptions.
Solution :
PROGRAM: (exception_handling.py)
def function(a,b):
c = a / b
print ( c )
try :
a = int(input(" Enter a value : "))
b = int(input(" Enter b value : "))
function(a,b)
except ValueError :
print(" Value Error Occurred \t Pease Enter Correct
integer Values ")
except ZeroDivisionError :
print(" Zero Division Error Occurred \t Can't division by
zero ")
else :
print(" Successfully executed without errors ")
OUTPUT:
Sample Run 1:
Enter a value : 5
Enter b value : 0
Zero Division Error Occurred Can't division by zero
Sample Run 2:
Enter a value : 5
Enter b value : 3
1.6666666666666667
PYTHON LAB MANUAL
Successfully executed without errors
Sample Run 3:
Enter a value : 5
Enter b value : d
Value Error Occurred Pease Enter Correct integer Values