Python Lab File
Python Lab File
PRACTICAL LAB
OF
PYTHON PROGRAMMING
(BTCS 510-18)
BACHELOR OF TECHNOLOGY
In
(Electronics & Communication Engineering)
Submitted By : Submitted To :
Ankit Kumar Komal Sharma
Roll No. (1816652) (Assistant Professor )
Batch 2018-2022 CSE Department
Table of Contents
No. Of Practicals
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 sub-string
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 convert temperature to and from Celsius to fahrenheit.
10. Write a python program to construct the following pattern using nested for loop.
11. Write a python program to print prim 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 python program to define a module and import a specific function in
that module to another program.
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 innput and written to the
second file.
17. Write a program that inputs a text file. The program should print all of the unique
words in the file in alphabetical order.
l,
18.
i=7
c=24+8j
f=701
# NOTE: boolean has truth values that are case sensitive Ex: True (T is
caps!) b= True
print('NOTE: boolean has truth values that are case sensitive Ex: True (T is caps!)')
l,
Output:
l,
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:
l,
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:
l,
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:
l,
Source code:
print(colleges)
colleges.append("MVSR")
print(colleges)
colleges.insert(1,"BHARAT")
colleges colleges.remove("BHARAT")
print(colleges)
del colleges[1]
Output:
l,
Output:
l,
# creating a dictionary
for SIIET college = {
"name": "siiet",
"code": "INDI",
"id": "x3"
print(college)
college["location"] = "IBP"
print(college)
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:
l,
if(a>b):
if(a>c):
else:
elif(b>c):
else:
a,b,c= txt.split()
Output:
l,
ch=int(choice)
if(ch==1):
f=((9*c)/5)+32
elif(ch==2):
c=((f-32)/9)*5
elif(ch==3):
exit()
else:
print("wrong choice")
Output:
l,
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:
l,
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:
l,
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:
l,
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:
l,
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:
l,
fibonacci.fibonacci (n)
l,
Output:
l,
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
n=int(input("enter range:"))
if(n<0):
else:
fibonacci (n)
l,
Output:
l,
16.Write a script named copyfile.py. This script should prompt the user for the names of
two text files. Thhe contents of the first file should be innput and written to the second
file.
Source code:
Note: create a text file as “inpput.txt” and write some date in it. This will be used in the program.
print("JOB DONE!!")
Output:
l,
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:
l,
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:
l,
class py_power:
def power(x,n):
py_power.power(x,n)
Output:
l,
our_list.append(element)
Output: