Python Lab Manual 10.06.2022
Python Lab Manual 10.06.2022
Output:
# week-2
'''Aim:Write a program to perform different Arithmetic Operations on numbers in Python.'''
Output:
1
PYTHON LAB MANUAL 2022
# week-3
''' Aim:Write a program to create, concatenate and print a string and accessing sub-string
from a given string.'''
Output:
# week-4
'''Aim: . Write a python script to print the current date in the following format Sun May 29
02:26:23 IST 2017 '''
import time;
ltime=time.localtime();
print(time.strftime("%a %b %d %H:%M:%S %Z %Y",ltime)); #returns the formatted time
2
PYTHON LAB MANUAL 2022
# week-5
'''Aim: Write a program to create, append, and remove lists in python. '''
# week-6
'''Write a program to demonstrate working with tuples in python'''
T = ("apple", "banana", "cherry","mango","grape","orange")
print("\n Created tuple is :",T)
print("\n Second fruit is :",T[1])
print("\n From 3-6 fruits are :",T[3:6])
print("\n List of all items in Tuple :")
for x in T:
print(x)
if "apple" in T:
print("\n Yes, 'apple' is in the fruits tuple")
print("\n Length of Tuple is :",len(T))
Output:
3
PYTHON LAB MANUAL 2022
# week-7
4
PYTHON LAB MANUAL 2022
Output:
# week-8
Output:
5
PYTHON LAB MANUAL 2022
# week-9
''' Write a Python program to convert temperatures to and from Celsius, Fahrenheit.
[ Formula: c/5 = f-32/9] '''
Output:
6
PYTHON LAB MANUAL 2022
# week-10
'''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('')
for i in range(n,0,-1):
for j in range(i):
print('')
Output:
7
PYTHON LAB MANUAL 2022
# week-11
'''Write a Python script that prints prime numbers less than 20'''
print("Prime numbers between 1 and 20 are:")
ulmt=20;
for num in range(ulmt):
# prime numbers are greater than 1
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
Output:
# week-12
''' Write a python program to find factorial of a number using Recursion.'''
def recur_fact(n):
"""Function to return the factorial
of a number using recursion"""
if n == 1:
return n
else:
return n*recur_fact(n-1)
num = int(input("Enter a number: "))
# check is the number is negative
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",recur_fact(num))
8
PYTHON LAB MANUAL 2022
Output:
# week-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).'''
base=float(input("Enter length of Base : "))
perp=float(input("Enter length of Perpendicular : "))
hypo=float(input("Enter length of Hypotenuse : "))
if hypo**2==((base**2)+(perp**2)):
print("It's a right triangle")
else:
print("It's not a right triangle")
Output:
9
PYTHON LAB MANUAL 2022
fibonacci.py
# Fibonacci numbers module
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print(b, end =" ")
a, b = b, a+b
# week-14
''' Write a python program to define a module to find Fibonacci Numbers and import the
module to another program'''
# import fibonacci module
import fibonacci
num=int(input("Enter any number to print Fibonacci series "))
fibonacci.fib(num)
Output:
# arth.py
''' Arithmetic Operations Module with Multiple functions'''
def Add(a,b):
c=a+b
return c
def Sub(a,b):
c=a-b
return c
def Mul(a,b):
c=a*b
return c
# week-15
'''Write a python program to define a module and import a specific function in that
module to another program.'''
from arth import Add
num1=float(input("Enter first Number : "))
10
PYTHON LAB MANUAL 2022
# file1.txt
This is python program
welcome to python
# week-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'''
fn2.close()
Output:
# file1.txt
This is python program
welcome to python
# week-17
'''Write a program that inputs a text file. The program should print all of the unique
words in the file in alphabetical order'''
Output:
12
PYTHON LAB MANUAL 2022
# week-18
class irconvert:
num_map = [(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'),(50, 'L'),
(40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I')]
def num2roman(self,num):
roman = ''
while num > 0:
for i, r in self.num_map:
while num >= i:
roman += r
num -= i
return roman
OUTPUT:
D:\PythonProject>python week18.py
Enter any Number :5
Roman Number is : V
13
PYTHON LAB MANUAL 2022
# week-19
'''Write a Python class to implement pow(x, n)'''
class py_pow:
def powr(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.powr(x,-n)
val = self.powr(x,n//2)
if n%2 ==0:
return val*val
return val*val*x
OUTPUT:
D:\PythonProject>python week19.py
Enter x value :2
Enter n value :3
pow(x,n) value is : 8
14
PYTHON LAB MANUAL 2022
# week-20
OUTPUT:
D:\PythonProject>python week20.py
Enter a string with 2 or more words : IT STUDENTS
Reverse of string word by word:
STUDENTS IT
15