Index
S. No. Title of the Experiment Signature Remark
1. Write a program to demonstrate basic data type in python.
2. Write a program to show how to create identifiers.
3. Write a python Program to show how to create string
literals.
4. Write a Python program to show how numeric literals
work.
5. Write a Python program to show how the Boolean literals
work
6. Write a Python program to show different types of
operators
7. Write a program to create, append, and remove lists in
python
8. Write a program to demonstrate working with dictionaries
in python.
9. Write a Program to check whether a person is eligible to
vote or not.
10. Write a python program to find largest of three numbers.
11 Write a Python program to convert temperatures to and
. from Celsius, Fahrenheit. [ Formula: c/5 = f-32/9 ]
12 Write a Python program to construct the following pattern,
. using a nested for loop
*
**
***
****
*****
****
***
**
*
Page | 1
13. Write a Python script that prints prime numbers less than
20.
14. Write a program to print sum of all even numbers from
10 to 20
15. Write a program to Calculate the square of each number
of list.
16. Write s program to create a list of the odd numbers
between 1 and 20 (use while, break)
17. WAP to print a message through a local variable calling
using function.
18. Python code to show the reciprocal of the given number t
o highlight the difference between def() and lambda().
19. Write function to compute gcd, lcm of two numbers
20 Write a Python class to convert an integer to a roman
numeral.
21 Write a program to implement Selection sort, Insertion
sort. Selection sort.
22. Write a Python program for implementation of Insertion
Sort
Page | 2
Program Number: 01
Objective: Write a python program to demonstrate numeric value
a=5
print("Type of a: ", type(a))
b = 5.0
print("\nType of b: ", type(b))
c = 2 + 4j
print("\nType of c: ", type(c))
Output:
Type of a: <class 'int'>
Type of b: <class 'float'>
Type of c: <class 'complex'>
Page | 3
Program Number: 02
Objective: Write a program to show how to create identifiers.
# Here, String and s are identifiers
String = 'Hello'
s = "Java point"
print(String)
print(s)
# These are also identifiers having digit and underscore
String1 = "World"
String_1 = "!!"
print(String1)
print(String_1)
Output:
Hello
Java point
World
!!
Page | 4
Program Number: 03
Objective: Python program to show how to create string literals
string = 'Hello'
s = "World"
A = "'Python is a
high-level and
general purpose language'"
print(string)
print(s)
print(A)
Output:
Hello
World
Python is a
high-level and
general purpose language
Page | 5
Program Number: 04
Objective: Write a Python program to show how numeric literals work
n1 = 50
n2 = 13.3
n3 = -14
# We will print the literal and its type
print (literal {n1} is of type {type(n1)}")
print (literal {n2} is of type {type(n2)}")
print (literal {n3} is of type {type(n3)}")
Output:
literal 50 is of type <class 'int'>
literal 13.3 is of type <class 'float'>
literal -14 is of type <class 'int'>
Page | 6
Program Number: 05
Objective: Write a Python program to show how the Boolean literals work.
v = True
w = False
y = (x == 3)
z = True + 10
# Printing the above literals i.e., their Boolean value
print(f"The Boolean value of v is {v}")
print(f"The Boolean value of w is {w}")
print(f"The Boolean value of y is {y}")
print(f"The value of z is {z}")
Output:
The Boolean value of v is True
The Boolean value of w is False
The Boolean value of y is True
The value of z is 11
Page | 7
Program Number: 06
Objective: Write a Python program to show different types of operators
x=5
y=2
# equal to operator
print('x == y =', x == y)
# multiplication operator
print('x * y =', x * y)
# Bitwise & operator
print('x & y =', x & y)
# Bitwise OR operator
print('x | y =', x | y)
# add and assign operator
x += y
print('x += y =', x)
# divide and assign operator
x /= y
print('x /= y =', x)
Output:
x == y = False
x * y = 10
x&y=0
x|y=7
x += y = 7
x /= y = 3.5
Page | 8
Program Number: 07
Objective: Write a program to create, append, and remove lists in python
my_ list = ['p','r','o','b','l','e','m'] my_list. remove('p')
# Output: ['r', 'o', 'b', 'l', 'e', 'm'] print(my_list)
# Output: 'o' print(my_list.pop (1))
# Output: ['r', 'b', 'l', 'e', 'm'] print(my_list)
# Output: 'm' print(my_list.pop ())
# Output: ['r', 'b', 'l', 'e'] print(my_list)
my_list. Clear() #
Output: []
print(my_list)
Output with input:
my_list=['p','r','o','b','l','e','m']
>>>my_list[2:3]=[]
>>>my_list
['p','r','b','l','e','m']
>>>my_list[2:5]=[]
>>>my_list
['p','r','m']
Page | 9
Program Number: 08
Objective: Write a program to demonstrate working with dictionaries in python.
my_dict = {'name’: ‘Jack', 'age': 26}
# Output: Jack
print(my_dict['name'])
# Output: 26
print(my_dict.get('ag))
# Trying to access keys which doesn't exist throws
error # my_dict.get('address')
# my_dict['address']
INPUT ANDOUTPUT:
Ja
c
k
2
6
Page | 10
Program Number: 09
Objective: Write a Program to check whether a person is eligible to vote or not.
# Simple Python Program to check whether a person is eligible to vote or not.
age = int (input ("Enter your age: "))
# Here, we are taking an integer num and taking input dynamically
if age>=18:
# here, we are checking the condition. If the condition is true, we will enter the block
print("You are eligible to vote !!");
else:
print("Sorry! you have to wait !!");
Output:
Enter your age: 90
You are eligible to vote !!
Page | 11
Program Number: 10
Objective: Write a python program to find largest of three numbers.
# change the values of num1, num2 and num3
# for a different
result num1 = 10
num2 = 14
num3 = 12
# uncomment following lines to 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 between",num1,",",num2,"and",num3,"is",largest)
INPUT ANDOUTPUT:
The largest number between 10, 14 and 12 is 14.0
Page | 12
Program Number: 11
Objective: Write a Python program to convert temperatures
to and from Celsius, Fahrenheit. [ Formula: c/5 = f-32/9]
# Python Program to convert temperature in Celsius to Fahrenheit
# change this value
for a different result
Celsius = 37.5
# calculate Fahrenheit
Fahrenheit = (Celsius * 1.8) + 32
print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit' %(Celsius,
Fahrenheit))
INPUT ANDOUTPUT:
37.5 degree Celsius is equal to 99.5-degree Fahrenheit
Page | 13
Program Number: 12
Objective: Write a Python program to construct the following pattern, using a nested
for loop
*
**
***
****
*****
****
***
**
*
SOLUTION: -
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('')
INPUT ANDOUTPUT:
*
**
***
****
*****
****
***
**
*
Page | 14
Program Number: 13
Objective: Write a Python script that prints prime numbers less than 20.
r=int(input("Enter upper limit: "))
for a in range(2,r+1): k=0
for i in range(2,a//2+1): if(a
%i==0):
k=k+1
if(k<=0):
print(a)
Output:
Enter upper limit: 15 2
3
5
7
11
13
Page | 15
Program Number: 14
Objective: Write a program to print sum of all even numbers from 10 to 20
Sum = 0
For i in range(2, 22, 2)
Sum = sum + i
print(sum)
Output: - 110
Page | 16
Program Number: 15
Objective: Write a program to Calculate the square of each number of list.
Python list is an ordered sequence of items. Assume you have a list of 10 numbers.
Let’s see how to want to calculate the square of each number using for loop.
numbers = [1,2,3,4,5]
For I in numbers:
square = I ** 2
print(“square of :”, i,”is”, square)
Output: -
Square of: 1 is: 1
Square of: 2 is: 4
Square of: 3 is: 9
Square of: 4 is: 16
Square of: 5 is: 25
Page | 17
Program Number: 16
Objective: Write s program to create a list of the odd numbers between 1 and 20 (use while, break)
num = 1
odd_nums = []
while num:
if num % 2! = 0:
odd_nums. append(num)
if num >=20:
break
num += 1
print("Odd numbers: ", odd_nums)
Output
odd _nums= [1,3,5,7,9,11,13,15,17,19S]
Page | 18
Program Number: 17
Objective: WAP to print a message through a local variable calling using function.
def f():
# local variable
s = "I welcome all of you in IFTM University"
print(s)
f()
Output
I welcome all of you in IFTM University
Page | 19
Program Number: 18
Objective: Python code to show the reciprocal of the given number to highlight the
difference between def() and lambda().
def reciprocal (num):
return 1 / num
lambda_reciprocal = lambda num: 1 / num
# using the function defined by def keyword
print( "Def keyword: ", reciprocal(6) )
# using the function defined by lambda keyword
print( "Lambda keyword: ", lambda_reciprocal(6) )
Output:
Now we compile the above code in python, and after successful compilation, we run it. Then the
output is given below -
Def keyword: 0.16666666666666666
Lambda keyword: 0.16666666666666666
Page | 20
Program Number: 19
Objective: Write function to compute gcd, lcm of two numbers
Python program to find LCM of two numbers #
Python 3 program to find
# LCM of 2 numbers without
# using GCD
import sys
# Function to return
# LCM of two
numbers def
findLCM(a, b):
lar = max(a, b)
small = min(a, b)
i = lar
while(1) :
if (i % small == 0):
return i
i += lar
# Driver Code
a=5
b=7
print("LCM of " , a , " and ",
b , " is " ,
findLCM(a, b), sep = "")
LCM of 15 and 20 is 60
Recursive function to return gcd of a and b def
gcd(a,b):
# Everything divides 0 if
(a == 0):
return
b if (b ==
0):
return a
# base case
if (a == b):
return a
# a is greater
if (a > b):
return gcd(a-b, b)
return gcd(a, b-a)
Output:
GCD of 98 and 56 is 14
Page | 21
Program Number: 20
Objective: Write a Python class to convert an integer to a roman numeral.
n = int (input("Enter the number: "))
num = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)
rom = ('M’, ‘CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I')
result = ''
for i in range(len(num)):
count = int(n / num[i])
result += str(rom[i] * count)
n -= num[i] * count
print(result)
Output: -
Enter the number: 1994
MCMXCIV
=====================================
Enter the number: 1956
MCMLVI
=====================================
Enter the number: 3888
MMMDCCCLXXXVIII
Page | 22
Program Number: 21
Objective: Write a program to implement Selection sort, Insertion sort. Selection Sort
# Python program for implementation of Selection
# Sort
import sys
A = [64, 25, 12, 22, 11]
# Traverse through all array elements
for i in range(len(A)):
# Find the minimum element in remaining
# unsorted array
min_idx = i
for j in range(i+1, len(A)):
if A[min_idx] > A[j]:
min_idx = j
# Swap the found minimum element with
# the first element
A[i], A[min_idx] = A[min_idx], A[i]
# Driver code to test above
print ("Sorted array")
for i in range(len(A)):
print("%d" %A[i]),
Output:
Sorted array
11
12
22
25
64
Page | 23
Program Number: 22
Objective: Python program for implementation of Insertion Sort
# Function to do
insertion sort def
insertion Sort(arr):
# Traverse through
1 to len(arr) for i in
range(1, len(arr)):
key = arr[i]
# Move elements of arr[0..i-
1], that are # greater than
key, to one position ahead #
of their current position
j = i-1
while j >=0 and key < arr[j] :
arr[j+1] = arr[j] j -= 1
arr[j+1] = key
# Driver code to test above
arr = [12, 11, 13, 5, 6]
insertion Sort(arr)
print ("Sorted array is:")
for i in range(len(arr))
print ("%d" %arr[i])
Output: -
sorted array is ;-
5
6
11
12
13
Page | 24