Python Lab Manual
Python Lab Manual
LAB MANUAL
I-B.TECH II-SEM
CSE (AI & ML)
JNTUH-R22
Faculty In-charge:
Mr.A.SATISH,M.Tech(P.hD) Asst.Prof.
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
PYTHON PROGRAMMING LAB MANUAL COMPUTER SCIENCE & ENGINEERING
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 the following format “Sun May 29
02:26:23 IST 2017”
10. Write a Python program to construct the following pattern, using a nested for loop
11. Write a Python script that prints prime numbers less than 20.
13. Write a program that accepts the lengths of three sides of a triangle as inputs. The
program output should indicate whether or not the triangle is a right triangle (Recall
from the Pythagorean Theorem that in a right triangle, the square of one side equals the sum
of the squares of the other two sides).
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 input 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.
Course Objectives:
To be able to introduce core programming basics and program design with functions
using Python programming language.
To understand a range of Object-Oriented Programming, as well as in-depth data and
information processing techniques.
To understand the high-performance programs designed to strengthen the practical
expertise.
Course Outcomes:
Student should be able to understand the basic concepts scripting and the
contributions of scripting language
Ability to explore python especially the object oriented concepts, and the built in
objects of Python.
Ability to create practical and contemporary applications such as TCP/IP network
programming, Web applications, discrete event simulations
3. Work quietly and carefully and equally share the work with your partners.
14.Return all the equipment you have signed out for the purpose of your experiment.
x=10
print(type(x))
x=10.6
print(type(x))
x='abc'
print(type(x))
x=1+2j
print(type(x))
x=[1,'a',10.6,1+2j]
print(type(x))
x=(1,'a',10.6,1+2j)
print(type(x))
x={"name":'abc',"age":29,"salary":30000}
print(type(x))
OUTPUT:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'complex'>
<class 'list'>
<class 'tuple'>
<class 'dict'>
a=int(input("enter a value"))
b=int(input("enter b value"))
x=a+b
y=a-b
z=a*b
p=a/b
print(x,y,z,p)
OUTPUT:
enter a value10
enter b value20
30 -10 200 0.5
3. Write a program to create, concatenate and print a string and accessing sub-string
from a given string.
AIM: Write a program to create, concatenate and print a string and accessing sub-string
from a given string.
a = "Hello"
b = "world"
print(a[:])
print(a[0:])
print(a[:5])
print(a*3) # prints HelloHelloHello
print(a+b)# prints Hello world
print(a[4]) # prints o
print(a[2:4]); # prints ll
print('w' in a) # prints false as w is not present in str
print('wo' not in b) # prints false as wo is present in str1.
OUTPUT:
Hello
Hello
Hello
HelloHelloHello
Helloworld
o
ll
Helloworld
HelloHello
False
False
4. Write a python script to print the current date in the following format “Sun May 29
02:26:23 IST 2017”
AIM: Write a python script to print the current date in the following format “Sun May
29 02:26:23 IST 2017”
import datetime
print(datetime.datetime.now())
import time
print(time.asctime(time.localtime(time.time())))
OUTPUT:
OUTPUT:
['John', 102, 'USA', 10.6, (1+2j)]
John
102
['John', 102, 'USA', 10.6, (1+2j)]
['John', 102, 'USA', 10.6, (1+2j)]
[102, 'USA']
1
2
['John', 102, 'USA', 10.6, (1+2j), [103, 'VIJAY']]
['John', 102, 'USA', 10.6, (1+2j), 20, [103, 'VIJAY']]
[102, 'USA', 10.6, (1+2j), 20, [103, 'VIJAY']]
[102, 'USA', (1+2j), 20, [103, 'VIJAY']]
for x in d:
print(x)
for x in d:
print(d[x])
del d["Name"]
print(d)
for x in d:
print(x)
for x in d:
print(d)
OUTPUT:
<class 'dict'>
{'Name': 'John', 'Age': 29, 'salary': 25000, 'Company': 'GOOGLE'}
John
29
dict_keys(['Name', 'Age', 'salary', 'Company'])
dict_values(['John', 29, 25000, 'GOOGLE'])
Name
Age
salary
Company
John
29
25000
GOOGLE
OUTPUT:
Enter first number: 10
Enter second number: 20
Enter third number: 30
The largest number is 30.0
OUTPUT:
Enter temperature in Celsius: 37
10. Write a Python program to construct the following pattern, using a nested for loop
AIM: Write a Python program to construct the following pattern, using a nested for loop
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('')
OUTPUT:
11. Write a Python script that prints prime numbers less than 20.
AIM: Write a Python script that prints prime numbers less than 20.
OUTPUT:
def recfac(n):
if n == 1:
return n
else:
return n*recfac(n-1)
num = int(input("Enter a number: "))
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of",num,"is",recfac(num))
OUTPUT:
Enter a number: 5
The factorial of 5 is 120
13. Write a program that accepts the lengths of three sides of a triangle as inputs. The
program output should indicate whether or not the triangle is a right triangle (Recall
from the Pythagorean Theorem that in a right triangle, the square of one side equals
the sum of the squares of the other two sides).
AIM: Write a program that accepts the lengths of three sides of a triangle as inputs. The
program output should indicate whether or not the triangle is a right triangle (Recall
from the Pythagorean Theorem that in a right triangle, the square of one side equals
the sum of the squares of the other two sides).
if x == y == z:
print("Equilateral triangle")
elif x==y or y==z or z==x:
print("isosceles triangle")
elif x==90 or y==90 or z==90:
print("right angle triangle")
else:
print("Scalene triangle")
OUTPUT:
14. Write a python program to define a module to find Fibonacci Numbers and import
the module to another program.
AIM: 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.
AIM: Write a python program to define a module and import a specific function in that
module to another program.
def sum(a,b):
return a+b
def mul(a,b):
return a*b
def div(a,b):
return a/b
OUTPUT:
enter the first number 10
enter the second number 20
sum=30
mul=200
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.
AIM: . 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.
OUTPUT:
Enter filename to copy:a.txt
Enter filename to save:b.txt
File copied successfully.
17. Write a program that inputs a text file. The program should print all of the unique
words in the file in alphabetical order.
AIM: Write a program that inputs a text file. The program should print all of the unique
words in the file in alphabetical order.
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:
IX
V
class py_solution:
def pow(self, x, n):
if x==0 or x==1 or n==1:
return x
if x==-1:
if n%2 ==0:
return 1
else:
return -1
if n==0:
return 1
if n<0:
return 1/self.pow(x,-n)
val = self.pow(x,n//2)
if n%2 ==0:
return val*val
return val*val*x
print(py_solution().pow(2, -3));
print(py_solution().pow(3, 5));
print(py_solution().pow(100, 0));
OUTPUT:
0.125
243
1
class py_solution:
def reverse_words(self, s):
return ' '.join(reversed(s.split()))
print(py_solution().reverse_words('murali krishna'))
OUTPUT:
li ra mu