Python Programing
Python Programing
Python Programing
1|Page
PYTHON PROGRAMMING LAB MANNUAL
12. Write a python program to find factorial of a number using Recursion.
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.
18. Write a Python class to convert an integer to a roman numeral.
19. Write a Python class to implement pow(x, n)
20. Write a Python class to reverse a string word by word.
2|Page
PYTHON PROGRAMMING LAB MANNUAL
OUTPUT:
100 <class 'int'>
10.2345 <class 'float'>
(100+3j) <class 'complex'>
3|Page
PYTHON PROGRAMMING LAB MANNUAL
4|Page
PYTHON PROGRAMMING LAB MANNUAL
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.
PROGRAM:
String1='we are cse'
String2='students'
String= String1+' '+String2
print(String)
5|Page
PYTHON PROGRAMMING LAB MANNUAL
4. Write a python script to print the current date in the following format “Sun May 29
02:26:23 IST 2017”
AIM: To Write a python script to print the current date in the following format “Sun May 29
02:26:23 IST 2017”
PROGRAM:
import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S"))
OUTPUT:
Current date and time :
2019-09-09 22:55:01
6|Page
PYTHON PROGRAMMING LAB MANNUAL
7|Page
PYTHON PROGRAMMING LAB MANNUAL
8|Page
PYTHON PROGRAMMING LAB MANNUAL
9|Page
PYTHON PROGRAMMING LAB MANNUAL
10 | P a g e
PYTHON PROGRAMMING LAB MANNUAL
11 | P a g e
PYTHON PROGRAMMING LAB MANNUAL
10. Write a Python program to construct the following pattern, using a nested for loop
*
**
***
****
*****
****
***
**
*
AIM: Write a Python program to construct the above pattern, using a nested for loop.
PROGRAM:
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:
*
**
***
****
*****
****
***
**
*
12 | P a g e
PYTHON PROGRAMMING LAB MANNUAL
11. Write a Python script that prints prime numbers less than 20.
AIM: Write a Python script that prints prime numbers less than 20.
PROGRAM:
# Python program to check if the input number is prime or not
num = 407
# take input from the user
# num = int(input("Enter a number: "))
# prime numbers are greater than 1
if num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")
# if input number is less than
# or equal to 1, it is not prime
else:
print(num,"is not a prime number")
OUTPUT:
13 | P a g e
PYTHON PROGRAMMING LAB MANNUAL
14 | P a g e
PYTHON PROGRAMMING LAB MANNUAL
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: TO 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).
PROGRAM:
print("Input lengths of the triangle sides: ")
x = int(input("x: "))
y = int(input("y: "))
z = int(input("z: "))
if x == y == z:
print("Equilateral triangle")
elif x==y or y==z or z==x:
print("isosceles triangle")
else:
print("Scalene triangle")
OUTPUT:
Input lengths of the triangle sides:
x: 52
y: 98
z: 47
Scalene triangle
15 | P a g e
PYTHON PROGRAMMING LAB MANNUAL
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..
PROGRAM:
# Fibonacci numbers module
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b
def fib2(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while b < n:
result.append(b)
a, b = b, a+b
return result
#Now enter the Python interpreter and import this module with the following command:
import fibo
“””This does not enter the names of the functions defined in fibo directly in the current symbol
table; it only enters the module name fibo there. Using the module name you can access the
functions:”””
fibo.fib(1000)
fibo.fib2(100)
fibo.__name__
'fibo'
“”If you intend to use a function often you can assign it to a local name:””
fib = fibo.fib
fib(500)
16 | P a g e