Lab Manual of Python
Lab Manual of Python
on
ThirdYear
CS-366
CODE –
#print('Hello')
str = 'Hello'
print(str)
OUTPUT –
PROGRAM 2
CODE –
Radius = float(input('Enter The Radius of Circle - '))
Circumference = 2*3.14*Radius
Area = 3.14*Radius*Radius
print('Circumference of the Circle = ', Circumference)
print('Area of the Circle = ', Area)
OUTPUT –
PROGRAM -3
CODE –
Num1 = int(input('Enter The First Number - '))
Num2 = int(input('Enter The Second Number - '))
#sum = Num1 + Num2
print('Sum of the two numbers = ', Num1+Num2)
OUTPUT –
PROGRAM -4
CODE –
P = float(input('Enter the Principal Amount - '))
R = float(input('Enter the Rate - '))
T = float(input('Enter the Time - '))
print('Simple Interest = ', P*R*T/100)
OUTPUT –
PROGRAM -5
CODE –
Num = int(input('Enter the Number - '))
print('The Square Root - ', Num**0.5)
#import math
#print(math.sqrt(4))
OUTPUT –
PROGRAM -6
CODE –
a = int(input("Enter the First Number - "))
b = int(input("Enter the Second Number - "))
print("NUMBERS ARE ", a, b)
c=a
a=b
b=c
print("SWAPPED NUMBERS ARE ", a, b)
OUTPUT –
PROGRAM -7
CODE –
Num = int(input("Enter the Number - "))
if(Num%2==0):
print(Num, "is an Even Number")
else:
print(Num, " is an Odd Number")
OUTPUT –
PROGRAM -8
CODE –
a = int(input("Enter the First Number - "))
b = int(input("Enter the Second Number - "))
c = int(input("Enter the Third Number - "))
if(a>b and a>c):
print("The greatest number is ", a)
elif(b>a and b>c):
print("The greatest number is ", b)
elif(c>a and c>b):
print("The greatest number is ", c)
else:
print("All the three numbers are equal")
OUTPUT –
PROGRAM -9
CODE –
Num = int(input("Enter the Number - "))
print("Table of ", Num)
for i in range(0,11):
print(Num, 'x', i, '=', Num*i)
OUTPUT –
PROGRAM -10
CODE –
num = int(input("Enter the Number - "))
n = num*(num+1)/2
print("Sum = ", n)
if num < 0:
print("Enter a positive number")
else:
sum = 0
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)
OUTPUT –
PROGRAM -11
CODE –
lower = 900
upper = 1000
print("PRIME NUMBERS BETWEEN", lower, "&", upper)
for i in range(lower, upper+1):
for n in range(2, i):
if(i%n==0):
break
else:
print(i)
OUTPUT –
PROGRAM -12
CODE –
#string = input("Enter the String - ")
#for word in reversed(string):
# print(word)
OUTPUT –
PROGRAM -13
CODE –
for i in range(0,5):
for j in range(0,i+1):
print("#", end="")
print()
OUTPUT –
PROGRAM -14
OBJECT – Write a program to display the Fibonacci sequence upto n-th terms.
CODE –
n_terms = int(input("Enter the Number of Terms Required - "))
n1, n2 = 0, 1
c=0
if n_terms <=0:
print("INVALID!..!Please enter a positive integer!")
elif n_terms == 1:
print("FIBONACCI SERIES UPTO", n_terms, "TERM")
print(n1)
else:
print("FIBONACCI SERIES UPTO", n_terms, "TERMS")
while c < n_terms:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
c += 1
OUTPUT –
PROGRAM -15
CODE –
dec = int(input("Enter the Decimal Value - "))
print("Binary Value = ", bin(dec))
print("Octal Value = ", oct(dec))
print("Hexadecimal Value = ", hex(dec))
OUTPUT –
PROGRAM -16
OBJECT – Write a program to display the Calendar of the given month and year.
CODE –
import calendar
yy = int(input("Enter year - "))
mm = int(input("Enter month - "))
print(calendar.month(yy, mm))
OUTPUT –
PROGRAM -17
CODE –
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
for r in result:
print(r)
OUTPUT –
PROGRAM -18
CODE –
a = 100
b = 2000
print("ARMSTRONG NUMBERS between", a, "and", b, "are")
for num in range (a, b):
order = len(str(num))
sum = 0
temp = num
while (temp > 0):
digit = temp % 10
sum += digit**order
temp//=10
if (num == sum):
print(num)
OUTPUT –
PROGRAM -19
OBJECT – Write a program to find that the number is positive or negative using ladder if-else.
CODE –
num = float(input("Enter the Number - "))
if num > 0:
print(num, "IS A POSITIVE NUMBER")
elif num == 0:
print(num, "IS A ZERO INTEGER")
else:
print(num, "IS A NEGATIVE NUMBER")
OUTPUT –
PROGRAM -20
OBJECT – Write a program to find that the number is positive or negative using nested if-else.
CODE –
num = float(input("Enter the Number - "))
if num >= 0:
if num == 0:
print(num, "IS A ZERO INTEGER")
else:
print(num, "IS A POSITIVE NUMBER")
else:
print(num, "IS A NEGATIVE NUMBER")
OUTPUT –
PROGRAM -21
CODE –
def print_factors(x):
print("The Factors of", x)
for i in range(1, x+1):
if x%i == 0:
print(i)
num = int(input("Enter the Number - "))
print_factors(num)
OUTPUT –
PROGRAM – 22
CODE –
# This function adds the two numbers
def add(x, y):
return x + y
while True:
# take input from the user
choice = input("Enter the choice(1/2/3/4): ")
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
else:
print("Invalid Input")
OUTPUT –
PROGRAM – 23
CODE –
def compute_lcm(x, y):
while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1
return lcm
OUTPUT –
PROGRAM – 24
CODE –
terms = int(input("How many number of terms? - "))
OUTPUT –
PROGRAM – 25
CODE –
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
OUTPUT –
PROGRAM – 26
CODE –
X = [[1,2,3],
[4 ,5,6],
[7 ,8,9]]
print("The Matrix X is ", X)
result = [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
result[j][i] = X[i][j]
print("The Transpose of Matrix X is ")
for r in result:
print(r)
OUTPUT –
PROGRAM – 27
OBJECT – Write a program to sort alphabetically the words from a string provided by the user.
CODE –
#my_str = "Hello this Is an Example With cased letters"
# To take input from the user
my_str = input("Enter a String - ")
OUTPUT –
PROGRAM – 28
OBJECT – Write a program to find the Sum of Natural Numbers using Recursion.
CODE –
def recur_sum(n):
if n <= 1:
return n
else:
return n + recur_sum(n-1)
if num < 0:
print("Enter a positive number")
else:
print("The sum is",recur_sum(num))
OUTPUT –
PROGRAM – 29
CODE –
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
OUTPUT –
PROGRAM – 30
CODE –
def convertToBinary(n):
if n > 1:
convertToBinary(n//2)
print(n % 2,end = '')
OUTPUT –
PROGRAM – 31
CODE –
# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
OUTPUT –
PROGRAM – 32
CODE –
# str1 = "Race"
# str2 = "Care"
# input from the user
str1 = input("Enter the first string: ")
str2 = input("Enter the second string: ")
OUTPUT –
PROGRAM – 33
CODE –
def foo():
x = 20
def bar():
global x
x = 25
foo()
print("Value of x in main - ", x)
OUTPUT –
OBJECT – Write a program to import a module.
CODE –
# import statement example to import standard module math
import math
print("(Importing Module) The value of pi = ", math.pi)
import math as m
print("(Importing & Renaming Module) The value of pi = ", m.pi)
OUTPUT –
PROGRAM – 35
CODE –
# string of vowels
vowels = 'aeiou'
print(count)
OUTPUT –
PROGRAM – 36
CODE –
# Names are in the file names.txt
# Body of the mail is in body.txt
OUTPUT –
PROGRAM – 37
CODE –
def jpeg_res(filename):
""""This function prints the resolution of the jpeg image file passed into it"""
# calculate height
height = (a[0] << 8) + a[1]
# calculate width
width = (a[0] << 8) + a[1]
jpeg_res("Clouds.jpg")
OUTPUT –
PROGRAM – 38
OBJECT – Write a program to catch Multiple Exceptions as a Parenthesized Tuple (in one line).
CODE –
string = input()
try:
num = int(input())
print(string+num)
except (TypeError, ValueError) as e:
print(e)
OUTPUT –
PROGRAM – 39
CODE –
def split(list_a, chunk_size):
# chunk_size = 2
chunk_size = int(input("Enter the Chunk Size - "))
# my_list = [1,2,3,4,5,6,7,8,9]
my_list = []
n = int(input("Enter the number of elements in the List - "))
for i in range(0, n):
element = int(input("Enter the elements - "))
my_list.append(element)
print(list(split(my_list, chunk_size)))
OUTPUT –
PROGRAM – 40
OBJECT – Write a program to find the Hash of a File & display it.
CODE –
# Python program to find the SHA-1 message digest of a file
def hash_file(filename):
""""This function returns the SHA-1 hash
of the file passed into it"""
message = hash_file("joy-of-travel-6671.mp3")
print(message)
OUTPUT –
PROGRAM – 41
CODE –
# Return values using comma
print("Return Values Using Comma")
def name():
return "Ojasvi","Jarvis"
# Using a Dictionary
print("Return Values Using a Dictionary")
def name():
n1 = "Ojasvi"
n2 = "Jarvis"
names = name()
print(names)
OUTPUT –
PROGRAM – 42
CODE –
# import module sys to get the type of exception
import sys
randomList = ['a', 0, 2]
OUTPUT –