DEPARTMENT
OF
COMPUTER SCIENCE & ENGINEERING
INFORMATION TECHNOLOGY
LAB MANUAL
PYTHON PROGRAMMING LAB
LIST OF PROGRAMS
1. Compute the GCD of two numbers.
2. Exponentiation. (power of a number)
3. Find the maximum of a list of numbers.
4. Linear search.
5. How to create, slice, change, delete and index elements using Tuple.
6. Find First n prime numbers.
7. How to create, slice, change, add, delete and index elements using list.
8. Length of string.
9. Write a program to reverse the string.
10. To check whether a number is Armstrong or not.
PROGRAM: 1
COMPUTE THE GCD OF TWO NUMBERS
AIM:
To write a python program to compute the GCD of two numbers.
ALGORITHM :
Step 1: Start
Step 2: read two numbers to find the GCD n1,n2.
Step 3: rem=d1%d2
Step 4: while rem!=0
d1=d2
d2=rem
Rem=d1%d2
Step 5: print GCD is d2.
Step 6: Stop
PROGRAM/SOURCE CODE :
d1=int(input("Enter a number:"))
d2=int(input("Enter another number"))
rem=d1%d2
while rem!=0 :
d1=d2
d2=rem
rem=d1%d2
print ("gcd of given numbers is ",d2)
OUTPUT :
Enter a number:54
Enter another number:24
GCD of given number is: 6
RESULT:
Thus the program to find the GCD of two numbers is executed and the output is obtained.
PROGRAM: 2
EXPONENTIATION (POWER OF A NUMBER)
AIM:
To write a python program to find the exponentiation of a number.
ALGORITHM :
Step 1: Start.
Step 2: read base value
Step 3: Read exponent value.
Step 4: if base value is equal to one return base
Step 5: if base value is not equal to one return .
return(base*power(base,exp-1))
Step 6: print the result of program.
Step 7: Stop.
PROGRAM/SOURCE CODE:
def power(base, exp):
if (exp==1):
return (base)
if (exp!=1):
return (base*power(base,exp-1))
base= int (input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base, exp))
OUTPUT :
Enter the base:3
Enter exponential value:2
Result: 9
RESULT:
Thus the program to find the exponentiation of a number is executed and the output is obtained.
PROGRAM: 3
FIND THE MAXIMUM OF A LIST OF NUMBERS
AIM:
To write a python program to find the maximum of a list of numbers.
ALGORITHM :
Step 1: Start.
Step 2: Read the number of element in the list.
Step 3: Read the number until loop n-1.
Step 4: Then Append the all element in list
Step 5: Go to STEP-3 upto n-1.
Step 6: Sort the listed values.
Step 7: Print the a[n-1] value.
PROGRAM/SOURCE CODE:
a=[ ]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
b=int(input("Enter element:"))
[Link](b)
[Link]()
print("Largest element is:",a[n-1])
OUTPUT :
Enter number of elements:5
Enter element: 3
Enter element:2
Enter element:1
Enter element:5
Enter element:4
Largest element is: 5
RESULT:
Thus the program to find the Maximum of a List of numbers is executed and the output is
obtained.
PROGRAM: 4
LINEAR SEARCH
AIM:
To write a python program to perform the linear search.
ALGORITHM:
Step 1: Start
Step 2: Read the element in list.
Step 3: Read the searching element from the user
Step 4: Assign to FALSE flag value
Step 5: Search the element with using for loop until length of list
Step 6: If value is found assign the flag value is true
Step7: Then print the output of founded value and position.
Step8: If value is not found then go to next step
Step9: Print the not found statement
PROGRAM/SOURCE CODE :
list_of_elements = [14, 20, 58, 90, 3, 17]
x = int(input("Enter number to search: "))
found = False
for i in range(len(list_of_elements)):
if(list_of_elements[i] == x):
found = True
print("%d found at %dth position"%(x,i))
break
else:
print("%d is not in list"%x)
OUTPUT:
Enter number to search: 90
90 found at 4th position
RESULT:
Thus the program to perform linear Search is executed and the output is obtained.
PROGRAM: 5
Tuple
AIM:
To write a python program to create, slice, change, delete and index elements using Tuple.
ALGORITHM :
Step 1: Create the tuple.
Step 2: Indexing the tuple using the index operator [ ].
Step 3: Silicing the tuple by using the slicing operator - colon ":"
Step 4: Changing the tuple.
Step 5: Deleting the tuple
PROGRAM/SOURCE CODE :
print("Tuple is created in the name: my_tuple")
my_tuple = ('p','e','r','m','i','t')
print("Tuple indexing",my_tuple[0])
print("Tuple negative indexing",my_tuple[-1])
print("Tuple slicing",my_tuple[1:4])
my_tuple = (4, 3, 2,5, [6, 5])
my_tuple[4][1] = 9
print("Tuple changing",my_tuple)
del my_tuple
print("Tuple Delete",my_tuple)
OUTPUT :
Tuple is created in the name: my_tuple
Tuple indexing p
Tuple negative indexing t
Tuple slicing ('e', 'r', 'm')
Tuple changing (4, 3, 2, 5, [6, 9])
RESULT:
Thus the program to create, slice, change, delete and index elements using Tuple and the output is
obtained.
PROGRAM:6
FIRST N PRIME NUMBERS
AIM:
To write a python program to find first n prime numbers.
ALGORITHM :
Step1: Take in the upper limit for the range and store it in a variable.
Step 2:Let the first for loop range from 2 to the upper limit.
Step3: Initialize the count variable to 0.
Step4:Let the second for loop range from 2 to half of the number (excluding 1 and the number itself).
Step 5:Then find the number of divisors using the if statement and increment the count variable each
time.
Step 6:If the number of divisors is lesser than or equal to 0, the number is prime.
Step 7:Print the final result.
PROGRAM/SOURCE CODE :
i=1
x = int(input("Enter the number:"))
for k in range (1, (x+1), 1):
c=0;
for j in range (1, (i+1), 1):
a = i%j
if (a==0):
c = c+1
if (c==2):
print (i)
else:
k = k-1
i=i+1
OUTPUT :
Enter the number : 15
2
3
4
5
7
11
13
RESULT:
Thus the program to find first n prime numbers is executed and the output is obtained.
PROGRAM:7
List
AIM:
To write a python program to create, slice, change, delete and index elements using Tuple
ALGORITHM :
Step 1: Create the List.
Step 2: Indexing the List using the index operator [ ].
Step 3: Silicing an element from the List by using the slicing operator - colon ":"
Step 4: Changing an element from the List.
Step 5: Appending the List.
Step 6: Removing an element from the List.
Step 7: Deleting an element from the List
PROGRAM/SOURCE CODE :
print("List is created in the name: list")
list = ['p','e','r','m','i','t']
print("List Created",list)
print("List indexing",list[0])
print("List negative indexing",list[-1])
print("List slicing",list[1:4])
list = ['p','e','r','m','i','t']
print("Given list",list)
list[0]=2
print("List Changing",list)
list[1:4]=[1,2,3]
print("List Changing",list)
list = ['p','e','r','m','i','t']
print("Given list",list)
[Link](['add','sub'])
print("List appending",list)
list = ['p','e','r','m','i','t']
print("Given list",list)
[Link]('p')
print("List Removing",list)
list = ['p','e','r','m','i','t']
print("Given list",list)
list[2:5] = []
print("List Delete",list)
OUTPUT :
List is created in the name: list
List Created ['p', 'e', 'r', 'm', 'i', 't']
List indexing p
List negative indexing t
List slicing ['e', 'r', 'm']
Given list ['p', 'e', 'r', 'm', 'i', 't']
List Changing [2, 'e', 'r', 'm', 'i', 't']
List Changing [2, 1, 2, 3, 'i', 't']
Given list ['p', 'e', 'r', 'm', 'i', 't']
List appending ['p', 'e', 'r', 'm', 'i', 't', ['add', 'sub']]
Given list ['p', 'e', 'r', 'm', 'i', 't']
List Removing ['e', 'r', 'm', 'i', 't']
Given list ['p', 'e', 'r', 'm', 'i', 't']
List Delete ['p', 'e', 't']
RESULT:
Thus program to create, slice, change, delete and index elements using List is executed ant the
output is obtained.
PROGRAM:8
PROGRAM TO CALCULATE LENGTH OF THE STRING
AIM:
To write a python program to calculate length of the string.
ALGORITHM :
Step 1: Start
Step 2: Read input string.
Step 3: Count number of characters in string including spaces.
Step 4: Display the answer.
Step 5: Stop
PROGRAM/SOURCE CODE :
def LenOfStr(s):
count=0
for i in s:
count=count+1
return count
s = "Geeksforgeeks"
print ("The number of character in original string is : ",LenOfStr(s))
OUTPUT :
The number of character in original string is : 13
RESULT:
Thus the program to count the words is executed and the output is obtained.
PROGRAM: 9
REVERSE STRING
AIM:
To write a python program to reverse a string.
ALGORITHM :
Step 1: Define a function reverse(s)
Step 2: Read a string
Step 3: Print the original string
Step 4: Print the reversed string
PROGRAM/SOURCE CODE :
def reverse(s):
str = ""
for i in s:
str = i + str
return str
s = "Geeksforgeeks"
print ("The original string is : ",end="")
print (s)
print ("The reversed string(using loops) is : ",end="")
print (reverse(s))
OUTPUT :
The original string is : Geeksforgeeks
The reversed string
(using loops) is : skeegrofskeeG
RESULT:
Thus the program to reverse a string is executed and the output is obtained.
PROGRAM: 10
ARMSTRONG NUMBER
AIM:
To write a Python program to check whether the number is Armstrong or not.
ALGORITHM
1. Start
2. Declare variables
3. Read the Input number.
4. Calculate sum of cubic of individual digits of the input.
5. Match the result with input number.
6. If match, Display the given number is Armstrong otherwise not.
7. Stop
SOURCE CODE
num = 1634
# Changednum variable to string,
# and calculated the length (number of digits)
order = len(str(num))
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
OUTPUT
1634 is an Armstrong number