Python Record Final-4-24
Python Record Final-4-24
DATE:
ALGORITHM:
1
Government Arts and Science College, Tirupattur – 635 901
PG Department of Computer Science
PROGRAM:
r=int(input("Enter the Radius of a Circle : "))
area = 3.14 * r ** 2
perimeter = 2 * 3.14 * r
print(f"The Area of the Circle is : {area}")
print(f"The Perimeter of the Circle is : {perimeter}")
OUTPUT:
Enter the Radius of a Circle : 3
The Area of the Circle is : 28.26
The Perimeter of the Circle is : 18.84
Result: Hence the Python program to find the area and perimeter of a
circle is successfully executed.
2
Government Arts and Science College, Tirupattur – 635 901
PG Department of Computer Science
DATE:
2. Fibonacci Series
AIM: To write a Python program to generate Fibonacci series.
ALGORITHM:
3
Government Arts and Science College, Tirupattur – 635 901
PG Department of Computer Science
PROGRAM:
n=int(input("Enter the No. of Terms :"))
f1 = 0
f2 = 1
print(f"The Fibbonacci Series for {n} are: {f1}")
print(f"The Fibbonacci Series for {n} are: {f2}")
for i in range(0,n-2):
f3 = f1 + f2
f1 = f2
f2 = f3
print(f"The Fibbonacci Series for {n} are: {f3}")
OUTPUT:
Enter the No. of Terms :10
The Fibbonacci Series for 10 are: 0
The Fibbonacci Series for 10 are: 1
The Fibbonacci Series for 10 are: 1
The Fibbonacci Series for 10 are: 2
The Fibbonacci Series for 10 are: 3
The Fibbonacci Series for 10 are: 5
The Fibbonacci Series for 10 are: 8
The Fibbonacci Series for 10 are: 13
The Fibbonacci Series for 10 are: 21
The Fibbonacci Series for 10 are: 34
4
Government Arts and Science College, Tirupattur – 635 901
PG Department of Computer Science
DATE:
ALGORITHM:
5
Government Arts and Science College, Tirupattur – 635 901
PG Department of Computer Science
PROGRAM:
m = int(input("Enter first positive number"))
n = int(input("Enter second positive number"))
if m == 0 and n == 0:
print("Invalid Input")
if m == 0:
print(f"GCD is {n}")
if n == 0:
print(f"GCD is {m}")
while m != n:
if m>n:
m = m-n
if n>m:
n=n-m
print(f"GCD of two numbers is {m}")
OUTPUT 1:
Enter first positive number12
Enter second positive number24
GCD of two numbers is 12
OUTPUT 2:
Enter first positive number7
Enter second positive number24
GCD of two numbers is 1
DATE:
ALGORITHM:
7
Government Arts and Science College, Tirupattur – 635 901
PG Department of Computer Science
PROGRAM:
lower_value = int(input ("Please, Enter the Lowest Range Value: "))
upper_value = int(input ("Please, Enter the Upper Range Value: "))
print ("The Prime Numbers in the range are: ")
for number in range (lower_value, upper_value + 1):
if number > 1:
for i in range (2, number):
if (number % i) == 0:
break
else:
print (number)
OUTPUT:
Please, Enter the Lowest Range Value: 5
Please, Enter the Upper Range Value: 20
The Prime Numbers in the range are:
5
7
11
13
17
19
8
Government Arts and Science College, Tirupattur – 635 901
PG Department of Computer Science
DATE:
ALGORITHM:
Step 1: Start the Program.
Step 2: Input the value for N.
Step 3: Assign sum = 0
Step 4: Continue the following steps for i < n+1
Calculate sum = sum+ (i*i)
Step 5: Print the sum as the Sum of Squares.
Step 6: Stop the Execution.
9
Government Arts and Science College, Tirupattur – 635 901
PG Department of Computer Science
PROGRAM:
n = int(input("Enter value of N: "))
sum = 0
for i in range(1, n+1):
sum = sum+ (i*i)
print(f"Sum of squares of {i} = {sum}")
OUTPUT:
Enter value of N: 5
Sum of squares of 1 = 1
Sum of squares of 2 = 5
Sum of squares of 3 = 14
Sum of squares of 4 = 30
Sum of squares of 5 = 55
10
Government Arts and Science College, Tirupattur – 635 901
PG Department of Computer Science
DATE:
ALGORITHM:
Step 1: Start the Program.
Step 2: Input the value for the size of the array as n.
Step 3: Initialize arr as null
Step 4: Continue the following steps to input the elements for the array
until the value of i > n.
Step 5: Add the element in an array using arr.append(element).
Step 6: Arrange the elements in the array in ascending order using
arr.sort().
Step 7: Print the Sorted array elements in arr.
Step 8: Print the Sum of the Elements using sum(arr).
Step 9: Stop the Execution.
11
Government Arts and Science College, Tirupattur – 635 901
PG Department of Computer Science
PROGRAM:
n = int(input("Enter the size of the array : "))
arr = []
print("Enter the Element : ")
for i in range(n):
element = int(input())
arr.append(element)
arr.sort()
print(f"The Sorted array are : {arr} \n")
print(f"The Sum of the Elements are : {sum(arr)}")
OUTPUT:
Enter the size of the array : 5
Enter the Element :
6
4
9
2
5
The Sorted array are : [2, 4, 5, 6, 9]
The Sum of the Elements are : 26
Result: Hence the Python program to find the sum of the elements in
an array is successfully executed.
12
Government Arts and Science College, Tirupattur – 635 901
PG Department of Computer Science
DATE:
ALGORITHM:
Step 1: Start the Program.
Step 2: Input the value for the size of the array as n.
Step 3: Initialize arr as null
Step 4: Continue the following steps to input the elements for the array
until the value of i > n.
Step 5: Add the element in an array using arr.append(element).
Step 6: Assign max as arr[0]
Step 7: Continue the following steps for i < len(arr).
Check whether arr[i] > max, then max = arr[i]
Print the Largest element present in given array as max.
Step 8: Stop the Execution.
13
Government Arts and Science College, Tirupattur – 635 901
PG Department of Computer Science
PROGRAM:
n = int(input("Enter the size of the array : "))
arr = []
print("Enter the Element : ")
for i in range(n):
element = int(input())
arr.append(element)
max = arr[0]
for i in range(0, len(arr)):
if(arr[i] > max):
max = arr[i]
print(f"Largest element present in given array: {max}")
OUTPUT:
Enter the size of the array : 5
Enter the Element :
23
98
56
34
7
Largest element present in given array: 98
Result: Hence the Python program to find the largest element in the
array is successfully executed.
14
Government Arts and Science College, Tirupattur – 635 901
PG Department of Computer Science
DATE:
ALGORITHM:
Step 1: Start the Program.
Step 2: Input the string as str.
Step 3: Assign rev = reversed(str)
Step 4: Check whether list(str) = list(rev), then
Print the given String is a PALINDROME.
Otherwise Print the given String is a NOT
PALINDROME.
Step 5: Stop the Execution.
15
Government Arts and Science College, Tirupattur – 635 901
PG Department of Computer Science
PROGRAM:
str = input("Enter the String : ")
rev = reversed(str)
if list(str) == list(rev):
print(f"The Given String {str} is a PALINDROME")
else:
print(f"The Given String {str} is a NOT PALINDROME")
OUTPUT 1:
Enter the String : MADAM
The Given String MADAM is a PALINDROME
OUTPUT 2:
Enter the String : PYTHON
The Given String PYTHON is a NOT PALINDROME
16
Government Arts and Science College, Tirupattur – 635 901
PG Department of Computer Science
DATE:
ALGORITHM:
Step 1: Start the Program.
Step 2: Input the First String as str1.
Step 3: Input the Second String as str2.
Step 4: Input the Third String as str3.
Step 5: Input the Fourth String as str4.
Step 6: Print the string to split with “*” using str1.split("*").
Step 7: Print the string to split with “@” using str2.split("@").
Step 8: Print the string to split using str3.split().
Step 9: Assign list_str4 = list(map(list,str4))
Step 10: Print the string using (list_str4)
Step 11: Stop the Execution.
17
Government Arts and Science College, Tirupattur – 635 901
PG Department of Computer Science
PROGRAM:
str1 = input(" Enter the First String with *: ")
str2 = input(" Enter the Second String with @ : ")
str3 = input(" Enter the Third String as Normal: ")
str4 = input(" Enter the Fourth String to ptint as Character: ")
print(str1.split("*"))
print(str2.split("@"))
print(str3.split())
list_str4 = list(map(list,str4))
print(list_str4)
OUTPUT:
Enter the First String with *: This*is*sample
Enter the Second String with @ : Python@is@Easy
Enter the Third String as Normal: Welcome to Python Programming
Enter the Fourth String to ptint as Character: I LIKE PYTHON
['This', 'is', 'sample']
['Python', 'is', 'Easy']
['Welcome', 'to', 'Python', 'Programming']
[['I'], [' '], ['L'], ['I'], ['K'], ['E'], [' '], ['P'], ['Y'], ['T'], ['H'], ['O'], ['N']]
Result: Hence the Python program to store strings in a list and print
them is successfully executed.
18
Government Arts and Science College, Tirupattur – 635 901
PG Department of Computer Science
DATE:
ALGORITHM:
Step 1: Start the Program.
Step 2: Assign lst, arr as null.
Step 3: Input the number of elements in n.
Step 4: Continue the following steps to input the elements for the
array until the value of i > n.
Add the element in an array using arr.append(element).
Step 5: Print the list in the array.
Step 6: Assign counter = 0
Step 7: Continue the following steps for i < lst.
Increment the value for counter = counter + 1
Step 8: Print the Length of list in counter.
Step 9: Find the list in reverse using lst.reverse().
Step 10: Print the Reverse of the List Using reverse() in lst
Step 11: Assign copylst = lst.copy().
Step 12: Print("The Copied List Using copy() : ", copylst)
Step 13: Copy the list using copylst.clear()
Step 14: Print the elements after Clearing the List Using clear() in
copylst)
Step 15: Stop the Execution.
19
Government Arts and Science College, Tirupattur – 635 901
PG Department of Computer Science
PROGRAM:
lst = []
arr = []
#INPUT THE LIST
n = int(input("Enter number of elements : "))
for i in range(0, n):
element = int(input())
lst.append(element)
print(lst)
#LENGTH OF THE LIST
counter = 0
for i in lst:
counter = counter + 1
print(f"Length of list : {counter}")
# REVERSE THE LIST
lst.reverse()
print("Reverse of the List Using reverse() : ", lst)
#COPY THE LIST
copylst = lst.copy()
print("The Copied List Using copy() : ", copylst)
#CLEAR THE LIST
copylst.clear()
print("Print after Clearing the List Using clear() : ",copylst)
20
Government Arts and Science College, Tirupattur – 635 901
PG Department of Computer Science
OUTPUT:
Enter number of elements : 5
12
89
56
43
72
[12, 89, 56, 43, 72]
Length of list : 5
Reverse of the List Using reverse() : [72, 43, 56, 89, 12]
The Copied List Using copy() : [72, 43, 56, 89, 12]
Print after Clearing the List Using clear() : []
Result: Hence the Python program to find the length of a list, reverse
it, copy it and then clear it is successfully executed.
21