0% found this document useful (0 votes)
13 views22 pages

Garima Python Docs

The document is a Python lab manual containing various programming exercises. It includes examples of data types, arithmetic operations, string manipulation, date formatting, list and tuple operations, dictionary usage, and more. Each section provides source code, expected output, and is attributed to Garima Chaudhary, a B.Tech(CSE) student.

Uploaded by

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

Garima Python Docs

The document is a Python lab manual containing various programming exercises. It includes examples of data types, arithmetic operations, string manipulation, date formatting, list and tuple operations, dictionary usage, and more. Each section provides source code, expected output, and is attributed to Garima Chaudhary, a B.Tech(CSE) student.

Uploaded by

gs6527173
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 22

PYTHON LAB MANUAL

1. Write a program to demonstrate di erent number


datatypes in python.
Source code:
i=7
c=24+8j
f=701
s='HELLO EVERYONE!!\nThis is john\'s python
programming..'
# NOTE: boolean has truth values that are case sensitive
Ex: True (T is caps!)
b= True
print("the value of c is:",i,'\nits type is:',type(i))

print("the value of c is:",f,'\nits type is:',type(f))

print("the value of c is:",c,'\nits type is:',type(c))

print("the value of c is:",s,'\nits type is:',type(s))

print("the value of c is:",b,'\nits type is:',type(b))

print('NOTE: boolean has truth values that are case


sensitive Ex: True (T is caps!)')

Output:
the value of c is: 7
its type is: <class 'int'>
the value of c is: 701
its type is: <class 'int'>
the value of c is: (24+8j)
its type is: <class 'complex'>
the value of c is: HELLO EVERYONE!!
This is john's python programming..
its type is: <class 'str'>
the value of c is: True
its type is: <class 'bool'>
NOTE: boolean has truth values that are case sensitive Ex: True (T is caps!)

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

2. Write a program to perform di erent arithme c opera


ons on numbers in python.

Source code:

a=20; b=2
print("addition of a:",a,"&b:",b,"is:",a+b)
print("substraction of a:",a,"&b:",b,"is:",a-b)
print("multiplication of a:",a,"&b:",b,"is:",a*b)
print("division of a:",a,"&b:",b,"is:",a/b)
print("floor divison of a:",a,"&b:",b,"is:",a//b)
print("moduli of a:",a,"&b:",b,"is:",a%b)
print("exponent of a:",a,"&b:",b,"is:",a**b)

Output:
addition of a: 20 &b: 2 is: 22
substraction of a: 20 &b: 2 is: 18
multiplication of a: 20 &b: 2 is: 40
division of a: 20 &b: 2 is: 10.0
floor divison of a: 20 &b: 2 is: 10
moduli of a: 20 &b: 2 is: 0
exponent of a: 20 &b: 2 is: 400

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

3. Write a program to create, concatenate and print a


string and accessing substring from a given string.

Source code:

pi=3.14
s= "GARIMA"
v= "CHAUDHARY"
print("the value of s is:",s)
print("the value of v is:",v)
string_add = s+v
print("after concatenating s and v the string is:",s+v)

text = 'The value of pi is ' + str(pi)


print("NOTE: variables after '+' operator must be converted to
string before using them as strings\n otherwise value will be
considered as its class type")

print(text)

Output:

the value of s is: GARIMA


the value of v is: CHAUDHARY
after concatenating s and v the string is: GARIMA CHAUDHARY
NOTE: variables after '+' operator must be converted to string
before using them as strings
otherwise value will be considered as its class type The value of
pi is 3.14

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

4. Write a python script to print the current date in


following format “Sun May 29 02:26:23 IST 2017”.

Source code:

import time
import datetime
x =datetime.datetime.now()
print(x.strftime("%c"))

Output:

WED JULY 5 20:14:25 2023

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

5. Write a python program to create, append and


remove lists in python.
Source code:

# creating list with college names..

colleges = ["AECTC", "MCST", "IIT"]


print(colleges)
# appending new college in collges list
colleges.append("NIT")
#checking if its added or not
print(colleges)
#adding a new college at a positon
colleges.insert(1,"BHARAT ")
print(colleges)
#remove a name from colleges
colleges.remove("BHARAT")
print(colleges)
#remove a name with an index value del
colleges[1]
# NOTE: index starts from 0 so 2nd value in list will be removed

print(colleges)

Output:

['AECTC', 'MCST', 'IIT']


['AECTC', 'MCST', 'IIT', 'NIT']
['AECTC', BHARAT', 'MCST', 'IIT', 'NIT']
['AECTC', 'MCST', 'IIT', 'NIT']
['AECTC', 'IIT', 'NIT']

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

6. Write a program to demonstrate working with


tuples in python.

Source code:

# creating tuples with college names.. colleges =


("AECTC","BHARAT","HCST", "IIT") print("the lists in
colleges tuple is",colleges) print("we can\'t add or remove
new elements in a tuple")

print("length of the tuple colleges


is:",len(colleges))
# checking whether 'SIIET' is present in the tuple or not

if "AECTC" in colleges:
print("Yes, 'AECTC' is in the colleges tuple")

Output:

the lists in colleges tuple is ('AECTC', 'BHARAT', 'HCST', 'IIT')


we can't add or remove new elements in a tuple
length of the tuple colleges is: 4
Yes, 'AECTC' is in the colleges tuple

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

7. Write a program to demonstrate working with dic


onaries in python.

Source code:

# creating a dictionary
college ={
"name": "AECTC",
"code": "001",
"id": "x5"
}
print(college)
#adding items to dictionary
college["location"] = "AGRA"
print(college)
#changing values of a key
college["location"] = "KEETHAM"
print(college)
# to remove items
# use pop()
college.pop("code")
print(college)
#know the length using len()
print("length of college is:",len(college))
#to copy the same dictionary use copy()
mycollege= college.copy()
print(mycollege)

Output:

{'name': 'AECTC', 'code': '001', 'id': 'x5'}


{'name': 'AECTC', 'code': '001', 'id': 'x5', 'location': 'AGRA'}
{'name': 'AECTC', 'code': '001', 'id': 'x5', 'location': 'KEETHAM'}
{'name': 'AECTC', 'id': 'x5', 'location': 'KEETHAM'}
length of college is: 3
{'name': 'AECTC', 'id': 'x5', 'location': 'KEETHAM'}

GARMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

8. Write a python program to find largest of three numbers.

Source code:

# user-defined function to know which number is larger

def bigOf3(a,b,c):
if(a>b):
if(a>c):
print("a is greater than b and c") else:

print("c is greater than a and b") elif(b>c):

print("b is greater than a and c")


else:
print("c is greater than a and b") txt=
input("enter a,b,c values:") a,b,c= txt.split()
bigOf3(int(a),int(b),int(c))

#calling the function

Output:

enter a,b,c values:5 6 7


c is greater than a and b

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

9. Write a python program to convert temperature to


and from Celsius to Fahrenheit.

Source code:

while(1):
print("1.CELSIUS TO FAHRENHEIT\n2.FAHRENHEIT TO
CELSIUS\n3.EXIT\n")
choice=input("ENTER YOUR CHOICE:")
ch=int(choice)
if(ch==1):
c=int(input("ENTER TEMPERATURE IN CELSIUS:"))
f=((9*c)/5)+32
print("converted temperature is:",f)
elif(ch==2):
f=int(input("ENTER TEMPERATURE IN FAHRENHEIT:"))
c=((f-32)/9)*5
print("converted temperature is:",c)
elif(ch==3):
exit()
else:
print("wrong choice")

Output:

1.CELSIUS TO FAHRENHEIT
2.FAHRENHEIT TO CELSIUS
3.EXIT

ENTER YOUR CHOICE:2


ENTER TEMPERATURE IN FAHRENHEIT:98 converted
temperature is: 36.666666666666664

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

10.Write a python program to construct the following patt


ern using nested for loop:

*
**
***
****
*****
******
******
*****
****
***
**
*

Source code:

n=int(input("ENTER A VALUE:"))

for x in range(0,n+1,1):
print(x*'*')
if(x==n):
for x in range(n,0,-1):
print(x*'*')

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

Output:

ENTER A VALUE:6

*
**
***
****
*****
******
******
*****
****
***
**
*

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

11. Write a python program to print prim numbers less than


20:
Source code:

n=int(input("enter range of prime numbers:"))


for num in range(2,n+1): #takes each number
count=0
for i in range(2,num//2+1): #checks the divisibility
of each num
if(num%i==0):
count=count+1 #if its noot prime count
increases.
if(count==0):
print(num)

Output:

enter range of prime numbers:20


2
3
5
7
11
13
17
19

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

12.Write a python program to find factorial of a


number using recursion:

Source code:

def recursion(n):
if(n<1):
print("FACTORIAL NOT POSSIBLE!!")
elif(n>1):
return n*recursion(n-1)
else:
return 1
n=int(input("enter a number:"))
print("factorial of",n,"is:",recursion(n))

Output:

enter a number:10
factorial of 10 is: 3628800

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

13.Write a python program to that accepts length


of three sides of a triangle as inputs. The program
should indicate whether or not the triangle is a
right-angled triangle (use Pythagorean theorem):
Source code:

a=float(input("enter length of hypotenuse side:"))

b=float(input("enter length of base side:"))

c=float(input("enter length of height side:"))


def pythagorean(a,b,c): #defining function
a=a*a; b=b*b; c=c*c
if(a==b+c):
print("yes!! the given inputs are triplets of a right
angled triangle!!")
print("height:",c**0.5,"\nbase:",b**0.5,"\nh
ypotenuse:",a**0.5)
pythagorean(a,b,c) # calling function

Output:

enter length of hypotenuse side:10 enter


length of base side:8 enter length of height
side:6
yes!! the given inputs are triplets of a right angled
triangle!!
height: 6.0
base: 8.0
hypotenuse: 10.0

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

14.Write a python program to define a module


to find Fibonacci Numbers and import the
module to another program.
Source code:

def fib(nterms):
n1,n2=0,1
count=0
if(nterms<=0):
print("Please enter a posi ve integer")
elif(nterms==1):
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while(count<nterms):
print(n1)
n1,n2=n2,n1+n2
count+=1
import fibbomodule
nterms=int(input("Enter any number to print Fibonacci series : "))
fibbomodule.fib(nterms)

Output:
Enter any number to print Fibonacci series : 10
Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

15.Write a script named copyfile.py. This script


should prompt the user for the names of two text
files. The contents of the first the second file.

Source code:

Note: create a text file as “input.txt” and write some date in it. This will
be used in the program.

with open("input.txt") as input:


with open("output.txt","w") as output:
for line in input:
output.write(line)
print("JOB DONE!!")

Output:

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

16. Write a program that inputs a text file. The program should
print all of the unique words in the file in alphabe cal order.

Source Code:

fname=input("enter file name with correct extension:")


file_opened=open(fname)
our_list=list() #crea ng an empty list
for line in file_opened:
word=line.rstrip().split() #rstrip for removing
unwanted spaces for element in word:
if element in our_list:
con nue
else:
our_list.append(element)
our_list.sort()
print(our_list)

Output:

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

17.Write a Python class to convert an integer to


a roman numeral.

Source code:

class py_solution:
def int_to_Roman(self, num):
val = [
1000, 900, 500, 400,
100, 90, 50, 40,
10, 9, 5, 4,
1
]
syb = [
"M", "CM", "D", "CD",
"C", "XC", "L", "XL",
"X", "IX", "V", "IV",
"I"
]
roman_num = ''
i=0
while num > 0:
for _ in range(num // val[i]):
roman_num += syb[i]
num -= val[i]
i += 1
return roman_num

print(py_solution().int_to_Roman(1))
print(py_solution().int_to_Roman(4000))

Output:

I
MMMM

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

18. Write a Python class to implement pow(x, n).

Source code:

class py_power:
def power(x,n):
print("power of given literals:\nx:",x,"\nn\
n:",n,"is:",x**n) x=float(input("ENTER X(BASE)
VALUE:")) n=float(input("ENTER N(POWER)
VALUE:")) py_power.power(x,n)

Output:

ENTER X(BASE) VALUE:26.47


ENTER N(POWER) VALUE:2
power of given literals:
x:26.47
n:2.0
is: 700.6609

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

19. Write a Python class to reverse a string word


by word.

Source code:

fname="HELLO EVERYONE THIS IS PYTHON PROGRAMMING


AND WE'RE PLAYING WITH LIST"
our_list=list()
word=fname.split()
for element in word:
our_list.append(element)
print("tried sentence is:",our_list)
our_list.reverse()
print("list after the reverse():",our_list)

Output:

tried sentence is: ['HELLO', 'EVERYONE', 'THIS', 'IS', 'PYTHON',


'PROGRAMMING', 'AND', "WE'RE", 'PLAYING', 'WITH', 'LIST']

list after the reverse(): ['LIST', 'WITH', 'PLAYING', "WE'RE", 'AND',


'PROGRAMMING', 'PYTHON', 'IS', 'THIS', 'EVERYONE', 'HELLO']

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

INDEX

SR.NO. PROGRAM NAME TEACHER’S


SIGNATURE
1 Write a program to demonstrate different number
datatypes in python.
2 Write a program to perform different arithmetic
operations on numbers in python.
3 Write a program to create, concatenate and print a
string and accessing substring from a given string.
4 Write a python script to print the current date in
following format “Sun May 29 02:26:23 IST 2017”.
5 Write a python program to create, append and
remove lists in python.
6 Write a program to demonstrate working with
tuples in python.
7 Write a program to demonstrate working with
dictionaries in python.
8 Write a python program to find largest of three numbers.

9 Write a python program to construct the


following pattern using nested for loop.
10 Write a python program to construct the
following pattern using nested for loop:
11 Write a python program to print prime numbers less than 20:
12 Write a python program to find factorial of a
number using recursion:
13
Write a python program to that accepts length of
three sides of a triangle as inputs. The program
should indicate whether or not the triangle is a
right-angled triangle (use Pythagorean theorem):
14 Write a python program to define a module to find Fibonacci
Numbers and import the module to another program.
15 Write a script named copyfile.py. This script
should prompt the user for the names of two text
files. The contents of the first the second file.
16 Write a program that inputs a text file. The program should
print all of the unique words in the file in alphabe cal order.
17 Write a Python class to convert an integer to
a roman numeral.
18 Write a Python class to implement pow(x, n).
19 Write a Python class to reverse a string word by word.

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022


PYTHON LAB MANUAL

GARIMA CHAUDHARY B.TECH(CSE) 2100010100022

You might also like