Python Programming Lab Manual: 1. Write A Program To Demonstrate Different Number Datatypes in Python
Python Programming Lab Manual: 1. Write A Program To Demonstrate Different Number Datatypes in Python
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("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"
string_add = s+v
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:
print(colleges)
colleges.append("MVSR")
print(colleges)
colleges.insert(1,"BHARAT") print(colleges)
colleges.remove("BHARAT")
print(colleges)
del colleges[1]
print(colleges)
Output:
6. Write a program to demonstrate working with tuples in python
Source code:
if "SIIET" in colleges:
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):
Output:
11. Write a python program to print prim numbers less than 20:
Source code:
count=0
if(num%i==0):
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):
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:
if(a==b+c):
print("height:",c**0.5,"\nbase:",b**0.5,"\nhypotenuse:",a**0.5)
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;
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
import fibonacci
n=int(input("enter range:"))
if(n<0):
else:
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;
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.
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:
file_opened=open(fname)
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):
syb = ["M", "CM", "D", "CD", "C", "XC", "L", "XL","X", "IX", "V", "IV", "I” ]
roman_num = “”
i=0
if(n<1 or n>3999):
else:
if(num-val[i]>=0):
roman_num+=syb[i]
num-=val[i]
else:
i+=1
Output:
19. Write a Python class to implement pow(x, n)
Source code:
class py_power:
def power(x,n):
py_power.power(x,n)
Output:
20. Write a Python class to reverse a string word by word.
Source code:
our_list.append(element)
Output: