Python Lab Manual
Python Lab Manual
AIM:
Apparatus Required:
PYTHON 3.5
SOURCECODE:
a=5
print(a, "is of type", type(a))
a = 2.0
print(a, "is of type", type(a))
a = 1+2j
print(a, "is complex number?", isinstance(1+2j,complex))
INPUT AND OUTPUT:
5 is of type <class 'int'>
2.0 is of type <class 'float'>
(1+2j) is complex number? True
5 is of type <class 'int'>
2.0 is of type <class 'float'>
(1+2j) is complex number? True
CONCLUSIONS:
Department Of Computer Science and Information Technology, Rabindranath Tagore University Bhopal Page 1
EXPERIMENT NO - 2
AIM:
Apparatus Required:
PYTHON 3.5
SOURCECODE:
x = 15
y=4
Output: x + y
= 19 print('x + y
=',x+y)
Output: x - y
= 11 print('x - y
=',x-y)
Output: x * y
= 60 print('x * y
=',x*y)
Output: x / y =
3.75 print('x / y
=',x/y)
Output: x ** y =
print('x ** y =',x**y)
x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625
CONCLUSIONS:
Department Of Computer Science and Information Technology, Rabindranath Tagore University Bhopal Page 2
EXPERIMENT NO -3
AIM:
Write a program to create, concatenate and print a string and accessing sub string from a
given string.
Apparatus Required:
PYTHON 3.5
SOURCECODE:
all of the following are equivalent
my_string = 'Hello' print(my_string)
my_string = "Hello"
print(my_string)
my_string = '''Hello'''
print(my_string)
triple quotes string can extend multiple lines my_string
= """Hello, welcome to
the world of Python"""
print(my_string)
c=" mlritm"
print(my_string+c)
substring function
print(my_string[5:11])
Hello
Hello
Hello
Hello, welcome to
the world of Python
Hello, welcome to
the world of Python mlritm
, welc
CONCLUSION :
A string and accessing sub- string fron a given string has been studied.
Department Of Computer Science and Information Technology, Rabindranath Tagore University Bhopal Page 3
EXPERIMENT NO -4
AIM:
Write a python script to print the current date in the following format “Sun May 29
Apparatus Required:
PYTHON 3.5
SOURCECODE:
today =date.today()
# dd/mm/YY
d1 =today.strftime("%d/%m/%Y")
print("d1 =", d1)
mm/dd/y
d3 =today.strftime("%m/%d/%y")
print("d3 =", d3)
Department Of Computer Science and Information Technology, Rabindranath Tagore University Bhopal Page 4
EXPERIMENT NO -5
AIM:
Apparatus Required:
PYTHON 3.5
SOURCECODE:
my_list = ['p','r','o','b','l','e','m']
my_list.remove('p')
Output: 'o'
print(my_list.pop(1))
Output: 'm'
print(my_list.pop())
my_list.clear()
Output: []
print(my_list)
my_list=['p','r','o','b','l','e','m']
>>>my_list[2:3]=[]
>>>my_list
['p','r','b','l','e','m']
>>>my_list[2:5]=[]
>>>my_list
['p','r','m']
CONCLUSION :
A program has been studied to create , append and remove lists in python .
Department Of Computer Science and Information Technology, Rabindranath Tagore University Bhopal Page 5
EXPERIMENT NO -6
AIM:
Apparatus Required:
PYTHON 3.5
SOURCECODE:
empty tuple
Output: ()
my_tuple =
()
print(my_t
uple)
nested tuple
Output: ("mouse", [8, 4, 6], (1, 2,
3)) my_tuple = ("mouse", [8, 4, 6],
(1, 2, 3)) print(my_tuple)
Department Of Computer Science and Information Technology, Rabindranath Tagore University Bhopal Page 6
3
4.6
dog
a, b, c = my_tuple
print(a)
print(b)
print(c)
()
(1, 2, 3)
(1, 'Hello', 3.4)
('mouse', [8, 4, 6], (1, 2, 3))
(3, 4.6, 'dog')
3
4.6
dog
CONCLUSION :
Department Of Computer Science and Information Technology, Rabindranath Tagore University Bhopal Page 7
EXPERIMENT NO -7
AIM:
Apparatus Required:
PYTHON 3.5
SOURCECODE:
my_dict = {'name':'Jack', 'age': 26}
Output: Jack
print(my_dict['na
me'])
Output: 26
print(my_dict.get('age'))
Trying to access keys which doesn't exist throws error
my_dict.get('address')
my_dict['address']
Jack
26
CONCLUSION :
Working with dictionaries has been studied in python.
Department Of Computer Science and Information Technology, Rabindranath Tagore University Bhopal Page 8
EXPERIMENT NO -8
AIM:
Apparatus Required:
PYTHON 3.5
SOURCECODE:
Python program to find the largest number among the three input numbers
CONCLUSION :
Department Of Computer Science and Information Technology, Rabindranath Tagore University Bhopal Page 9
EXPERIMENT NO -9
AIM:
Apparatus Required:
PYTHON 3.5
SOURCECODE:
Python Program to convert temperature in celsius to fahrenheit
calculate fahrenheit
fahrenheit = (celsius * 1.8)
+ 32
print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit' %(celsius,fahrenheit))
INPUT ANDOUTPUT:
CONCLUSION :
Conversion of temperature has been studied .
Department Of Computer Science and Information Technology, Rabindranath Tagore University Bhopal Page 10
EXPERIMENT NO -10
AIM:
Write a Python program to construct the following pattern, using a nested for loop
*
**
***
****
*****
****
***
**
*
Apparatus Required:
PYTHON 3.5
SOURCECODE:
n=5;
for i in range(n):
for j in range(i):
print ('* ', end="")
print('')
for i in range(n,0,-
1):
for j in range(i):
print('* ', end="")
print('')
INPUT ANDOUTPUT:
*
**
***
****
*****
****
***
**
*
CONCLUSION :
A program has been studied to construct the pattern , using a nested loop in python .
Department Of Computer Science and Information Technology, Rabindranath Tagore University Bhopal Page 11