PYTHON PROGRAMMING LAB (15MC65P)
Script No 1.) Develop a Python Script for temperature conversions using F=C*9/5+32
and C=(f-32)*5/9. Test your program with several values for Celsius and
Fahrenheit.
PROGRAM:
c=input("Enter the Temperature in Celsius:")
f=input("Enter the Temperature in Fahrenheit:")
F=int(c)*9/5+32
print ("Temperature Conversion from Celsius to Fahrenheit is:",F)
C=(int(f)-32)*5/9
print ("Temperature Conversion from Fahrenheit to Celsius is:",C)
OUTPUT WINDOW:
B.E.T’s M.L.B.P., Department of Mechatronics Engg., Belgaum Page 1
PYTHON PROGRAMMING LAB (15MC65P)
Script No 2.) Develop a Python Script that displays various date-related values such as
a. Current date and time
b. The day of the week, month and year
c. Time in seconds since Jan 1, 2012(epoch)
PROGRAM:
import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S"))
currentDay = datetime.datetime.now().day
currentMonth = datetime.datetime.now().month
currentYear = datetime.datetime.now().year
currentWeekDay=datetime.datetime.today().weekday()
print("Current Date:",currentDay)
print("Current Month:",currentMonth)
print("Current Year:",currentYear)
print("Current Week Day:",currentWeekDay)
OUTPUT WINDOW:
B.E.T’s M.L.B.P., Department of Mechatronics Engg., Belgaum Page 2
PYTHON PROGRAMMING LAB (15MC65P)
Script No 3.) Develop a Python Script to print consecutive integers in the form as shown.
1
12
123
1234
12345
PROGRAM:
n = int(input("Enter the Number of Rows of Pattern to be Printed: "))
for i in range(1,n+1):
for j in range(1,i+1):
print(j,end=" ")
print()
OUTPUT WINDOW:
B.E.T’s M.L.B.P., Department of Mechatronics Engg., Belgaum Page 3
PYTHON PROGRAMMING LAB (15MC65P)
Script No 4.) Develop a Python Script using split() to compare word abc in a string x and
find the match
PROGRAM:
s1 = 'abc def'
x = 'abc def'
set1 = set(s1.split(' '))
set2 = set(x.split(' '))
print (set1 == set2)
OUTPUT WINDOW:
B.E.T’s M.L.B.P., Department of Mechatronics Engg., Belgaum Page 4
PYTHON PROGRAMMING LAB (15MC65P)
Script No 5.) Develop a Python Script using split() to print justified text
PROGRAM:
string="Hello Welcome to Python World! , This is beginning of new Programming Life"
lines=string.split(',')
print (lines)
OUTPUT WINDOW:
B.E.T’s M.L.B.P., Department of Mechatronics Engg., Belgaum Page 5
PYTHON PROGRAMMING LAB (15MC65P)
Script No 6.) Develop a Python Script to explain Break, Continue and Pass statements
using a While Loop find the divisor of a number
PROGRAM:
a = 16
b=2
i=1
for i in range(1,2):
continue
print("Using Continue, The Quotient of the Division is",a/b,"The remainder of the division is:",a
%b)
for i in range(1,2):
break
print("Using Break, The Quotient of the Division is",a/b,"The remainder of the division
is:",a%b)
for i in range(1,2):
pass
print("Using pass, The Quotient of the Division is",a/b,"The remainder of the division is:",a%b)
OUTPUT WINDOW:
B.E.T’s M.L.B.P., Department of Mechatronics Engg., Belgaum Page 6
PYTHON PROGRAMMING LAB (15MC65P)
Script No 7.) Develop a Python Script to find Prime Numbers using while loop
PROGRAM:
def is_prime(n):
i=2
while i < n:
if n%i == 0:
return False
i += 1
return True
n = int(input("Enter number: "))
p=2
while p <= n:
if is_prime(p):
print (p)
p+=1
print ("Done")
OUTPUT WINDOW:
B.E.T’s M.L.B.P., Department of Mechatronics Engg., Belgaum Page 7
PYTHON PROGRAMMING LAB (15MC65P)
Script No 8.) Develop a Python Script to find factorial of a number using Recursion
PROGRAM:
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(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",recur_factorial(num))
OUTPUT WINDOW:
B.E.T’s M.L.B.P., Department of Mechatronics Engg., Belgaum Page 8
PYTHON PROGRAMMING LAB (15MC65P)
Script No 9.) Develop a Python Script to find GCD of two positive integers using Euclid’s
algorithm & Recursion
PROGRAM:
def gcd(a,b):
if b > a:
return gcd(b, a)
elif a % b == 0:
return b
else:
return gcd(b, a % b)
a=10
b=25
print("The GCD of 10 and 15 is", gcd(a,b))
OUTPUT WINDOW:
B.E.T’s M.L.B.P., Department of Mechatronics Engg., Belgaum Page 9
PYTHON PROGRAMMING LAB (15MC65P)
Script No 10.) Develop a Python Script to Calculate LCM of two positive integers
PROGRAM:
def lcm(x, y):
if x > y:
greater = x
else:
greater = y
while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1
return lcm
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("The L.C.M. of", num1,"and", num2,"is", lcm(num1, num2))
OUTPUT WINDOW
B.E.T’s M.L.B.P., Department of Mechatronics Engg., Belgaum Page 10
PYTHON PROGRAMMING LAB (15MC65P)
Script No 11.) Develop a Python Script to count the occurrences of digits and letters in a
string
PROGRAM:
x="Bharatesh Polytechnic 591124 &*"
letters=0
digit=0
space=0
other=0
for i in x:
if i.isalpha():
letters+=1
elif i.isnumeric():
digit+=1
elif i.isspace():
space+=1
else:
other+=1
print ("Total Letters in String:",letters)
print ("Total Digits in String:",digit)
print ("Total Space in String:",space)
print ("Total Other String:",other)
OUTPUT WINDOW
B.E.T’s M.L.B.P., Department of Mechatronics Engg., Belgaum Page 11
PYTHON PROGRAMMING LAB (15MC65P)
Script No 12.) Develop a Python Script to find the various attributes of a file-modes like
r,w,rw
PROGRAM:
import stat
import os
print (os.access("testfile2.txt",os.R_OK))
print (os.access("testfile2.txt",os.X_OK))
print (os.access("testfile2.txt",os.R_OK or os.X_OK))
OUTPUT WINDOW
Note: The file whose
attributes have to be checked,
has to in the same location as
that of the Python program
B.E.T’s M.L.B.P., Department of Mechatronics Engg., Belgaum Page 12
PYTHON PROGRAMMING LAB (15MC65P)
Script No 13.) Develop a Python Script to read and write data into a text file
PROGRAM:
file = open("testfile2.txt","w")
file.write("Hello World")
file.write("This is our new text file")
file.write("and this is another line.")
file.write("Why? Because we can.")
file.close()
file = open("testfile2.txt", "r")
print (file.readlines())
OUTPUT WINDOW
Using Python Program we
have written text into the
Note: The file whose attributes have Text Notepad file named as
to be checked, has to in the same testfile2
location as that of the Python
program
B.E.T’s M.L.B.P., Department of Mechatronics Engg., Belgaum Page 13
PYTHON PROGRAMMING LAB (15MC65P)
Script No 14.) Develop a Python Script to find whether a specified path references a file and if
yes find the file size and timestamp
PROGRAM:
import os.path, time
print("Last modified: %s" % time.ctime(os.path.getmtime("testfile2.txt")))
print("Created: %s" % time.ctime(os.path.getctime("testfile2.txt")))
print ((os.path.getsize("testfile2.txt") ),"kb")
OUTPUT WINDOW
B.E.T’s M.L.B.P., Department of Mechatronics Engg., Belgaum Page 14