Python Lab Manual
Python Lab Manual
LAB OBSERVATION
Name: -----------------------------------------------------------------
Reg-No: -----------------------------------------------------------------
1
INDEX
S.No List of Experiments Page Marks Signature
No Awarded
PART - A
1 (i) Write a Python program to compute GCD of two numbers
2
PART-A
Date:
Ex.No :1(i)
Procedure :
2. a mod b = R
5. GCD = a
6. Finish
PROGRAM :
OUTPUT
GCD of 98 and 56 is 14
RESULT
Thus the above program was executed successfully and output was verified
3
Date:
Ex.No :1(ii)
AIM To Write a Python Program to print prime numbers in the given range.
Procedure :
1. Take the range of numbers between which you have to find the prime numbers as input.
2. Check for prime numbers only on the odd numbers between the range.
3. Also check if the odd numbers are divisible by any of the natural numbers starting from 2.
5. Finish.
PROGRAM :
4
OUTPUT
RESULT
Thus the above program was executed successfully and output was verified
5
Date:
Ex.No :2(i)
AIM To Write a Python Program to check the given year is leap year or not.
Procedure :
3. Check whether a year is divisible by four but not with hundred and print leap year.
PROGRAM :
OUTPUT
RESULT
Thus the above program was executed successfully and output was verified
6
7
Date:
Ex.No :2(ii)
AIM To Write a Python Program to print Armstrong numbers between given range.
Procedure :
PROGRAM :
# order of number
order = len(str(num))
# initialize sum
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
if num == sum:
print(num)
8
OUTPUT
RESULT
Thus the above program was executed successfully and output was verified
9
Date:
Ex.No :3(i)
AIM To Write a Python Program to do basic trim and slice operations on String.
Procedure :
The slice() constructor creates a slice object representing the set of indices specified by
range(start, stop, step).
Syntax:
slice(stop)
slice(start, stop, step)
Parameters: start: Starting index where the slicing of object starts. stop: Ending index where the
slicing of object stops. step: It is an optional argument that determines the increment between each
index for slicing. Return Type: Returns a sliced object containing elements in the given range only.
In Python, indexing syntax can be used as a substitute for the slice object. This is an easy and
convenient way to slice a string using list slicing and Array slicing both syntax-wise and execution-
wise. A start, end, and step have the same mechanism as the slice() constructor.
Below we will see string slicing in Python with example.
Syntax
arr[start:stop] # items start through stop-1
arr[start:] # items start through the rest of the
array
arr[:stop] # items from the beginning through stop-
1
arr[:] # a copy of the whole array
arr[start:stop:step] # start through not past stop, by step
Python provides three methods that can be used to trim whitespaces from the string object.
strip(): returns a new string after removing any leading and trailing whitespaces including tabs (\t).
rstrip(): returns a new string with trailing whitespace removed. It’s easier to remember as removing white
spaces from “right” side of the string.
10
lstrip(): returns a new string with leading whitespace removed, or removing whitespaces from the “left”
side of the string.
PROGRAM :
String = 'ASTRING'
print(& quot
String slicing & quot
)
print(String[s1])
print(String[s2])
print(String[s3])
s="Welcome to Python"
OUTPUT
String slicing
AST
SR
GITA
The sliced string from index 2 to 8 is: lcome t
The sliced string from index 0 to 5 is: Welcom
The sliced string from index 5 to last is: me to Python
The string after removing leading and trailing white spaces is: Good Morning
The string after removing leading white spaces is: Good Morning
The string after removing trailing white spaces is: Good Morning
RESULT
11
Thus the above program was executed successfully and output was verified
12
Date:
Ex.No :3(ii)
AIM To Write a Python Program to accept line of text and find the number ofcharacters,
vowels and blank spaces on it
Procedure :
1. Then make three variables, vowel, line , spaces and character to count the number of vowels,
lines, and characters respectively.
2. Make a list of vowels so that we can check whether the character is a vowel or not.
3. When the count hits the ‘\n’ character we have to increase our line variable means a new line in
the file.
4. After that iterate over the characters of the file and count the vowels, lines, and characters.
PROGRAM :
s=input("Enter a string:")
vowels = 0
characters = 0
spaces = 0
s = s.lower()
vow="aeiou"
for i in s:
if i in vow:
vowels = vowels + 1
if (i> 'a'and i<='z'):
characters = characters+ 1
else:
spaces = spaces + 1
OUTPUT
RESULT
Thus the above program was executed successfully and output was verified
13
Date:
Ex.No :4(i)
AIM To Write a Python Program using function to display all such numbers which is
divisible by 3 but are not multiple of 5 in a given range.
Procedure :
PROGRAM :
OUTPUT
Thus the above program was executed successfully and output was verified
14
Date:
Ex.No :4(ii)
AIM To Write a Python Program using recursion to print ‘n’ terms in Fibonacci series.
Procedure :
A Fibonacci series is a series in which next number is a sum of previous two numbers.
For example : 0, 1, 1, 2, 3, 5, 8 ……
In Fibonacci Series, first number starts with 0 and second is with 1 and then its grow like,
0+1=1
1+1=2
1+2=3
2 + 3 = 5 and
so on…
1. Here first of all we have declared one function named fib which will take integer as a input and
will return a next integer element.
4. And each time when loop condition will satisfy the value of i will pass as a parameter to fib ()
method as a input.
15
PROGRAM :
def fib(n):
if n <= 1:
return n
else:
return(fib(n-1) + fib(n-2))
OUTPUT
RESULT
Thus the above program was executed successfully and output was verified
16
Date:
Ex.No : 5
AIM To Write a Python Program to add ‘ing’ at the end of a given string if the string has 3
or more characters . If the given string is already ends with ‘ing’ then add ‘ly’ instead.
If the string has` less than 3 characters, leave it unchanged.
Procedure :
PROGRAM :
l=len(s)
if l>=3:
if s[-3:]=='ing':
s=s+'ly'
else:
s=s+'ing'
print("The given string is: ",sl)
print("The modified string is: ",s)
OUTPUT
RESULT
Thus the above program was executed successfully and output was verified
17
Date:
Ex.No : 6
AIM To Write a Python program to find minimum and maximum of a list of numbers
Procedure :
1. Get total number
2. Input all numbers one by one
3. Find minimum number
4. Find maximum number
5. Print minimum number in list
6. Print maximumnumber in list
PROGRAM :
L=[]
N=int(input("Enter the total number:"))
print("Enter the numbers one by one")
for i in range(N):
x=int(input())
L.append(x)
minl=min(L)
maxl=max(L)
posmin = L.index(minl)
posmax = L.index(maxl)
print("The minimum number among the given numbers is:",minl)
print("The position of the minimum number is:",posmin)
print("The maximum number among the given numbers is:",maxl)
print("The position of the maximum number is: ", posmax)
OUTPUT
Enter the total number:6
Enter the numbers one by one
6
87
3
987
1
8
The minimum number among the given numbers is: 1
The position of the minimum number is: 4
The maximum number among the given numbers is: 987
The position of the maximum number is: 3
RESULT
Thus the above program was executed successfully and output was verified
18
19
Date:
Ex.No : 7
Procedure :
1. Start
2. Get total number of elements
3. Input all numbers one by one upto N elements
4. Display the list by reverse using reverse function
5. Finish
PROGRAM :
L=[]
N=int(input("Enter the total number of list elements: "))
print("Enter the elements one by one:")
for i in range(N):
x=int(input())
L.append(x)
print("The original list is: ",L)
print("The reversed list is: ",end="")
L.reverse()
print(L)
OUTPUT
RESULT
Thus the above program was executed successfully and output was verified
20
Date:
Ex.No : 8
AIM To Write a Python Program to print the first half values of tuple in one line and last
half values in next line.
Procedure :
1. Start
2. Declare tp and assign a value to it
3. Print first half tule value with range 1 to half of tuble
4. Print second half tule value with range half of tuple to last element
5. End
PROGRAM :
OUTPUT
The given tuple is: (3, 4, 5, 6, 7, 19, 70, 'good', 'bad', 89.76)
The Second half tuple values are: (19, 70, 'good', 'bad', 89.76)
RESULT
Thus the above program was executed successfully and output was verified
21
Date:
Ex.No : 9
AIM To Write a Python Program to take a list of words and return the length of the longest
one using string.
Procedure :
1. Start
2. Get no of string N stored in list
3. Get string one by one upto N
4. The length of the list is assigned to a variable.
5. The list is iterated over, and every element’s length is checked to see if it is greater than
length of the first element of the list.
6. If so, this is assigned as the maximum length.
7. Print the longest word inlist
8. End
PROGRAM :
l=[]
OUTPUT
RESULT
Thus the above program was executed successfully and output was verified
22
Date:
Ex.No : 10
AIM To Write a Python Program to find an element in a given set of elements using Linear
Search
Procedure :
PROGRAM :
23
OUTPUT
RESULT
Thus the above program was executed successfully and output was verified
24
Date:
Ex.No : 11
AIM To Write a Python Program to sort a set of elements using Selection sort.
Procedure :
PROGRAM :
25
OUTPUT
RESULT
Thus the above program was executed successfully and output was verified
26
Date:
Ex.No : 12
Procedure :
Step 1: Start
Step 2: Declare matrix A[m][n]
and matrix B[p][q]
and matrix C[m][q]
Step 3: Read m, n, p, q.
Step 4: Now check if the matrix can be multiplied or not, if n is not equal to q matrix can't be
multiplied and an error message is generated.
Step 5: Read A[][] and B[][]
Step 4: Declare variable i=0, k=0 , j=0 and sum=0
Step 5: Repeat Step until i < m
5.1: Repeat Step until j < q
5.1.1: Repeat Step until k < p
Set sum= sum + A[i][k] * B[k][j]
Set multiply[i][j] = sum;
Set sum = 0 and k=k+1
5.1.2: Set j=j+1
5.2: Set i=i+1
Step 6: C is the required matrix.
27
PROGRAM :
OUTPUT
RESULT
Thus the above program was executed successfully and output was verified
28
Date:
Ex.No : 13
Procedure :
1. Start
2. Assign Values for tuple1
3. Assign Values for tuple2
4. For concatenation operation jpon two tuples by t3=t1+t2
5. For Repetition operation jpon two tuples by t4=t3*2
6. For Length of tuple l=len(t3)
7. Find min and maximum of tuple
8. Print Concatenation Tuple
9. Print Repetition Tuple
10. Print Length of Tuple
11. End
PROGRAM :
t2=(89.7,45,76)
t3=t1+t2 # Cocatenation
t4=t3*2 # Repetition
29
OUTPUT
The concatenated tuple is: (17, 'bat', 97.5, 'cat', 'dog', 89.7, 45, 76)
The repeated tuple is: (17, 'bat', 97.5, 'cat', 'dog', 89.7, 45, 76, 17, 'bat', 97.5, 'cat', 'dog', 89.7, 45, 76)
The length of the tuple t3 is: 8
The minimum and maximum elements in tuple t2 are: 45 89.7
RESULT
Thus the above program was executed successfully and output was verified
30
Date:
Ex.No : 14
AIM To Write a Python Program to demonstrate to use Dictionary and related functions.
Procedure :
1. Start
2. Assign Values for Dictionary
3. To find the length of Dictionary
4. To copy
5. To remove an element from the given key
6. To get an element from the given key
7. To view key-value pair in list
8. To get value as a list
9. To remove the last item
10. To remove all elements
11. End
PROGRAM :
31
OUTPUT
RESULT
Thus the above program was executed successfully and output was verified
32
Date:
Ex.No : 15
To Write a Python Program to copy file contents from one file to another and display number of
AIM
words copied.
Procedure :
1. Start
2. Open file1 in read mode
3. Open file2 in write mode
4. Read file1 content count no.of words and write to file2
5. Print no.of words copied to file2
6. End
PROGRAM :
with open('file1.txt','w') as f:
n=int(input("Enter the number of lines of data:"))
for i in range(n):
data=input("Enter the next line of data:")
f.write(data +'\n')
with open('file1.txt', 'r') as f: #Opening filel in read mode
with open('file2.txt', 'w') as s: # Opening file2 in write mode to copy
filel
for i in f:
s.write(i)
with open('file2.txt', 'r') as s:
print("The content of file2 is: \n",s.read())
with open('file1.txt', 'r') as f:
d=f.read()
l=d.split()
count=0
for i in l:
count=count+1
print("Number of words copied from filel to file2 is:",count)
33
OUTPUT
file1.txt
File2.txt
RESULT
Thus the above program was executed successfully and output was verified
34