100% found this document useful (1 vote)
6K views22 pages

Python Programming Lab Manual: 1. Write A Program To Demonstrate Different Number Datatypes in Python

This document contains a Python programming lab manual with 20 programming problems and their solutions. The problems cover basic Python concepts like data types, arithmetic operations, strings, lists, tuples, dictionaries, functions, classes, file handling, and more. For each problem, the source code and output are provided. The last problem involves writing a Python class to reverse a given string word by word. This lab manual seems to be intended for teaching basic to intermediate Python programming concepts through hands-on practice exercises.

Uploaded by

Naresh Babu
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
100% found this document useful (1 vote)
6K views22 pages

Python Programming Lab Manual: 1. Write A Program To Demonstrate Different Number Datatypes in Python

This document contains a Python programming lab manual with 20 programming problems and their solutions. The problems cover basic Python concepts like data types, arithmetic operations, strings, lists, tuples, dictionaries, functions, classes, file handling, and more. For each problem, the source code and output are provided. The last problem involves writing a Python class to reverse a given string word by word. This lab manual seems to be intended for teaching basic to intermediate Python programming concepts through hands-on practice exercises.

Uploaded by

Naresh Babu
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
You are on page 1/ 22

SRI INDU INSTITUTE OF ENGINEERING AND TECHNOLOGY

Department of Computer Science and Engineering

PYTHON PROGRAMMING LAB MANUAL

1. Write a program to demonstrate different 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))
rint("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:
2. Write a program to perform different arithematic operations on numbers in python.
Source code:

a=10; b=3

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:
3. Write a program to create, concatenate and print a string and accessing sub-
string from a given string.
Source code:

pi=3.14

s= "Venkata"

v= "Subhramanyam"

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:
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:
5. Write a python program to create, append and remove lists in python.
Source code:

# creating list with college names..

colleges = ["SIIET", "GNIT", "AVN"]

print(colleges)

# appending new college in collges list

colleges.append("MVSR")

#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:
6. Write a program to demonstrate working with tuples in python
Source code:

# creating tuples with college names..

colleges = ("SIIET","BHARAT","GNIT", "AVN")

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 "SIIET" in colleges:

print("Yes, 'SIIET' is in the colleges tuple")

Output:
7. Write a program to demonstrate working with dictionaries in python
Source code:
# creating a dictionary
for SIIET college = {
"name": "siiet",
"code": "INDI",
"id": "x3"
}
print(college)
#adding items to dictionary
college["location"] = "IBP"
print(college)
#changing values of a key
college["location"] = "sheriguda"
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:
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:
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:
10. Write a python program to construct the following pattern 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*'*')

Output:
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:
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:
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,"\nhypotenuse:",a**0.5)

pythagorean(a,b,c) # calling function

Output:
14. Write a python program to define a module to find Fibonacci Numbers and
import the module to another program.
Source code:

fibonacci.py

def fibonacci(n):

n1=0; n2=1;

print(n1)

print(n2)

for x in range(0,n):

n3=n1+n2

if(n3>=n):

break;

print(n3,end = ' ')

n1=n2

n2=n3

using_fibonacci.py

Note: we will be using previous program as a library or package It is mandatory to write both

the programs are separately

import fibonacci

n=int(input("enter range:"))

if(n<0):

print("enter correct range!!")

else:

print("-------------------------------FIBONACCI SERIES ------------------------------- \n")

fibonacci.fibonacci (n)
Output:
15. Write a python program to define a module and import a specific function
in that module to another program.
Source code:

fibonacci.py

def fibonacci(n):

n1=0;

n2=1;

print(n1)

print(n2)

for x in range(0,n):

n3=n1+n2

if(n3>=n):

break;

print(n3,end = ' ')

n1=n2

n2=n3

using_fibonacci.py

Note: we will be using previous program as a library or package It is mandatory to write both
the programs are separately
from fibonacci import fibonacci
n=int(input("enter range:"))
if(n<0):
print("enter correct range!!")
else:
print("-------------------------------FIBONACCI SERIES ------------------------------- \n")
fibonacci (n)

Output:
16. Write a script named copyfile.py. This script should prompt the user for the
names of two text files. The contents of the first file should be input and written to
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:
17. Write a program that inputs a text file. The program should print all of the
unique words in the file in alphabetical order.
Source code:

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

file_opened=open(fname)

our_list=list() #creating 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:

continue

else:

our_list.append(element)

our_list.sort()

print(our_list)

Output:
18. Write a Python class to convert an integer to a roman numeral.
Source code:

class roman_solution:

def int_to_Roman(num):

val = [1000, 900, 500, 400, 100, 90, 50, 40, , 9, 5, 4, 1]

syb = ["M", "CM", "D", "CD", "C", "XC", "L", "XL","X", "IX", "V", "IV", "I” ]

roman_num = “”

i=0

if(n<1 or n>3999):

print("ENTER A GOOD VALUE!")

else:

while num > 0:

if(num-val[i]>=0):

roman_num+=syb[i]

num-=val[i]

else:

i+=1

return roman_num n=int(input("ENTER A NUMBER:"))

print("roman numeral of given number is:",roman_solution.int_to_Roman(n))

Output:
19. 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:
20. 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 LISTS"

our_list=list() #creating an empty list

word=fname.split() #spliting up the list

for element in word:

our_list.append(element)

print("tried sentence is:",our_list)

our_list.reverse() #method to reverse the elements in the list

print("list after the reverse()",our_list)

Output:

You might also like