Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Python Programing

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

PYTHON PROGRAMMING LAB MANNUAL

PYTHON PROGRAMMING LAB


B.Tech. IV Year I Sem. LTPC
Course Code: CS751PC 0032
Prerequisites: Students should install Python on Linux platform.
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
List of Programs:
1. Write a program to demonstrate different number data types in Python.
2. Write a program to perform different Arithmetic Operations on numbers in Python.
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”
5. Write a program to create, append, and remove lists in python.
6. Write a program to demonstrate working with tuples in python.
7. Write a program to demonstrate working with dictionaries in python.
8. Write a python program to find largest of three numbers.
9. Write a Python program to convert temperatures to and from Celsius, Fahrenheit.
[ Formula: c/5 = f-32/9]
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.

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

1. Write a program to demonstrate different number data types in Python.


AIM: to Write a program to demonstrate different number data types in Python.
PROGRAM:
#create a variable with integer value.
a=100
print(a,type(a))
#create a variable with float value.
b=10.2345
print(b,type(b))
#create a variable with complex value.
c=100+3j
print(c, type(c))

OUTPUT:
100 <class 'int'>
10.2345 <class 'float'>
(100+3j) <class 'complex'>

3|Page
PYTHON PROGRAMMING LAB MANNUAL

2. Write a program to perform different Arithmetic Operations on numbers in Python.


AIM: . TO Write a program to perform different Arithmetic Operations on numbers in Python.
PROGRAM:
num1=int(input("ENTER THE FIRST NUMBER :-"))
num2=int(input("ENTER THE SECOND NUMBER :-"))
add=num1+num2
sub=num1-num2
mul=num1*num2
div=num1/num2
Modulus=num1%num2
Exponent=num1**num2
flrdiv=num1//num2
print("Addition of",num1,",",num2,"is:",add)
print("Subtraction of",num1,",",num2,"is:",sub)
print("Multiplication of",num1,",",num2,"is:",mul)
print("Modulus of",num1,",",num2,"is:",Modulus)
print("Division of",num1,",",num2,"is:",div)
print("Floordivision of",num1,",",num2,"is:",flrdiv)
OUTPUT:
ENTER THE FIRST NUMBER :-5
ENTER THE SECOND NUMBER :-6
Addition of 5 , 6 is: 11
Subtraction of 5 , 6 is: -1
Multiplication of 5 , 6 is: 30
Modulus of 5 , 6 is: 5
Division of 5 , 6 is: 0.8333333333333334
Floordivision of 5 , 6 is: 0

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

5. Write a program to create, append, and remove lists in python.


AIM: To Write a program to create, append, and remove lists in python.
PROGRAM:
create:
dict = {'Student Name': 'Berry', 'Roll No.': 12, 'Subject': 'English'}
print("dict['Student Name']: ", dict['Student Name'])
print("dict['Roll No.']: ", dict['Roll No.'])
print("dict['Subject']: ", dict['Subject'])
append:
dict_append = {"1" : "Python", "2" : "Java"}
print(dict_append)
{'1': 'Python', '2': 'Java'}
dict_append["3"] = "CSharp"
print(dict_append)
{'1': 'Python', '3': 'CSharp', '2': 'Java'}
remove:
# Create a Python dictionary
sixMonths = {1:31, 2:28, 3:31, 4:30, 5:31, 6:30}
# Delete a specific element
print(sixMonths.pop(6))
print(sixMonths)
# Delete an random element
print(sixMonths.popitem())
print(sixMonths)
# Remove a specific element
del sixMonths[5]
print(sixMonths)
# Delete all elements from the dictionary
sixMonths.clear()
print(sixMonths)
# Finally, eliminate the dictionary object
del sixMonths
print(sixMonths)
OUTPUT:
dict['Student Name']: Berry
dict['Roll No.']: 12
dict['Subject']: English
{'1': 'Python', '2': 'Java'}
{'1': 'Python', '2': 'Java', '3': 'CSharp'}
30
{1: 31, 2: 28, 3: 31, 4: 30, 5: 31}
(5, 31)
{1: 31, 2: 28, 3: 31, 4: 30}

7|Page
PYTHON PROGRAMMING LAB MANNUAL

6. Write a program to demonstrate working with tuples in python.


AIM: To Write a program to demonstrate working with tuples in python.
PROGRAM:
# Empty tuple
my_tuple = ()
print(my_tuple)
# Output: ()
# Tuple having integers
my_tuple = (1, 2, 3)
print(my_tuple)
# Output: (1, 2, 3)
# tuple with mixed datatypes
my_tuple = (1, "Hello", 3.4)
print(my_tuple)
# Output: (1, "Hello", 3.4)
# nested tuple
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3)
# Output: ("mouse", [8, 4, 6], (1, 2, 3)) print(my_tuple)
PROGRAM:
()
(1, 2, 3)
(1, 'Hello', 3.4)
('mouse', [8, 4, 6], (1, 2, 3))

8|Page
PYTHON PROGRAMMING LAB MANNUAL

7. Write a program to demonstrate working with dictionaries in python.


AIM: to Write a program to demonstrate working with dictionaries in python.
PROGRAM:
my_dict = {'name':'Jack', 'age': 26}
# Output: Jack
print(my_dict['name'])
# Output: 26
print(my_dict.get('age'))
# Trying to access keys which doesn't exist throws error
# my_dict.get('address')
# my_dict['address']
OUTPUT:
Jack
26

9|Page
PYTHON PROGRAMMING LAB MANNUAL

8. Write a python program to find largest of three numbers.


AIM: to Write a python program to find largest of three numbers.
PROGRAM:
# Python program to find the largest number among the three input numbers
# take three numbers from user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if (num1 > num2) and (num1 > num3):
largest = num1
elif (num2 > num1) and (num2 > num3):
largest = num2
else:
largest = num3
print("The largest number is",largest)
OUTPUT:
Enter first number: 5
Enter second number: 6
Enter third number: 79
The largest number is 79.0

10 | P a g e
PYTHON PROGRAMMING LAB MANNUAL

9. Write a Python program to convert temperatures to and from Celsius, Fahrenheit.


[ Formula: c/5 = f-32/9]
AIM: to . Write a Python program to convert temperatures to and from Celsius, Fahrenheit.
[ Formula: c/5 = f-32/9]
PROGRAM:
# Python Program to convert temperature in Celsius to Fahrenheit
# change this value for a different result
celsius =float(input("enter CELSIUS :-"))
# calculate fahrenheit
fahrenheit = (celsius * 1.8) + 32
print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit' %(celsius,fahrenheit))
OUTPUT:
enter CELSIUS :-37
37.0 degree Celsius is equal to 98.6 degree Fahrenheit

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

12. Write a python program to find factorial of a number using Recursion.


AIM: Write a python program to find factorial of a number using Recursion.
PROGRAM:
# Python program to find the factorial of a number using recursion
def recur_factorial(n):
"""Function to return the factorial
of a number using recursion"""
if n == 1:
return n
else:
return n*recur_factorial(n-1)
# Change this value for a different result
num = 7
# uncomment to take input from the user
#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_factorial(num))
OUTPUT:
Enter a number: 5
The factorial of 5 is 120

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

You might also like