0% found this document useful (0 votes)
34 views

PYTHON LAB Notes

The document contains 10 programming exercises with sample code and outputs. The exercises include: 1. Finding the area and perimeter of a circle 2. Generating a Fibonacci series 3. Computing the greatest common divisor (GCD) of two numbers 4. Generating the first n prime numbers 5. Calculating the sum of squares of n natural numbers 6. Finding the sum of elements in an array 7. Finding the largest elements in an array 8. Checking if a string is a palindrome 9. Storing strings in a list and printing them 10. Finding the length of a list, reversing it, copying it, and clearing it. For each exercise, the

Uploaded by

ammujvp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

PYTHON LAB Notes

The document contains 10 programming exercises with sample code and outputs. The exercises include: 1. Finding the area and perimeter of a circle 2. Generating a Fibonacci series 3. Computing the greatest common divisor (GCD) of two numbers 4. Generating the first n prime numbers 5. Calculating the sum of squares of n natural numbers 6. Finding the sum of elements in an array 7. Finding the largest elements in an array 8. Checking if a string is a palindrome 9. Storing strings in a list and printing them 10. Finding the length of a list, reversing it, copying it, and clearing it. For each exercise, the

Uploaded by

ammujvp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

S.NO DATE TITLE PAGE NO.

SIGNATURE

1 Area and Perimeter of circle

2 Fibonacci series

3 GCD of two numbers

4 Generate first n prime numbers

5 Sum of squares of n natural numbers

Sum of the elements in an array


6

7 Largest elements in an array

To check is the given string is a


8
palindrome or not

To store strings in a list and print


9
them

10 Find the length of a list, reverse it,


copy it and then clear it.
EX NO: 1 Develop a Python program to find the area and
perimeter of a circle.
DATE:

AIM:

To Develop a Python program to find the area and perimeter of a circle.

ALGORITHM:

STEP 1 : Start the program by importing the value of pi from the math module.

STEP 2 :Take input from the user for the radius of the circle.

STEP 3 : Calculate the perimeter of the circle using the formula: 2 * pi *

radius.

STEP 4 : Calculate the area of the circle using the formula: pi * radius * radius.

STEP 5 :Display the calculated values of area and perimeter.

STEP 6 : End the program.

1
PROGRAME:

pi = 3.14

radius = float(input("Enter the radius of the circle: "))

# formula to calculate area of circle

area = pi * radius * radius

# formula to calculate perimeter of circle

perimeter = 2 * pi * radius

# printing the result

print("The area of the circle is: ", area)

print("The perimeter of the circle is: ", perimeter)

2
OUTPUT :

Enter the radius of the circle: 5

The area of the circle is: 78.5

The perimeter of the circle is: 31.4

RESULT :

The above program was executed successfully.

3
EX NO: 2 Develop a Python program to Generate
Fibonacci series.
DATE:

AIM:

Develop a Python program to Generate Fibonacci series

ALGORITHM:

STEP 1 : Start the program by taking input from the user for the number

of terms in the Fibonacci series.

STEP 2 : Initialize two variables ‘first term’ and ’second term’ as the

first two terms in the series.

STEP 3 : Initialize a counter variable ‘count' to keep track of the number

of terms generated.

STEP 4 : Check if the input is a positive integer, if not, display an error

message.

STEP 5 : Use a while loop to generate the series by adding the first two

terms to get the next term, updating the values of the first two

terms, and incrementing the counter.

STEP 6 : Repeat the process until the number of terms generated is equal

to the input value.

STEP 7 : End the program.

4
PROGRAME:

n = int(input("Enter the number of terms: "))

# Initial values of first two terms

first_term = 0

second_term = 1

# Counter variable to keep track of terms

count = 0

# Check if the number of terms is valid

if n <= 0:

print("Enter a positive integer")

else:

# Using while loop to generate fibonacci series

while count < n:

print(first_term)

next_term = first_term + second_term

first_term = second_term

second_term = next_term

count += 1

5
OUTPUT :

Enter the number of terms: 10

13

21

34

RESULT :

The above program was executed successfully.

6
EX NO: 3 Develop a Python program to compute the GCD
of two numbers.
DATE:

AIM:

To develop a python program to compute the GCD of two numbers.

ALGORITHM:

STEP 1 : Start the program by taking input from the user for the two

numbers.

STEP 2 : Find the smaller of the two numbers.

STEP 3 : Initialize a variable ‘GCD’ to 1.

STEP 4 : Use a while loop to go through the numbers from 1 to the

smaller of the two numbers.

STEP 5 : Within the loop, check if both numbers are divisible by the

current number.

STEP 6 : If both numbers are divisible, update the value of gcd to the

current number.

STEP 7 : Repeat the process until the loop has gone through all the

numbers from 1 to the smaller of the two numbers.

STEP 8 : End the program by printing the final result.

7
PROGRAME:

# GCD of two numbers using while loop

# Take input from the user

num1 = int(input("Enter the first number: "))

num2 = int(input("Enter the second number: "))

# Find the smaller of the two numbers

smaller = min(num1, num2)

# Initialize the gcd to be 1

gcd = 1

# Loop through the numbers from 1 to the smaller of num1 and num2

i=1

while i <= smaller:

if num1 % i == 0 and num2 % i == 0:

gcd = i

i += 1

# Print the final result

print("The GCD of", num1, "and", num2, "is", gcd)

8
OUTPUT :

Enter the first number: 54

Enter the second number: 24

The GCD of 54 and 24 is 6

RESULT :

The above program was executed successfully.

9
EX NO: 4 Develop a Python program to Generate first n
prime numbers.
DATE:

AIM:

To develop a python program to generate first n prime numbers.

ALGORITHM:

STEP 1 : Start the program by taking input from the user for the number of

prime numbers they want to generate.

STEP 2 : Initialize a counter variable to keep track of the number of prime

numbers generated.

STEP 3 : Initialize a variable ‘num’ to 2, which is the first prime number.

STEP 4 : Use a while loop to go through the numbers from 2 to a large number.

STEP 5 : Within the loop, use a for loop to check if the current number is

divisible by any number from 2 to the current number minus 1.

STEP 6 : If the number is divisible by any number in the for loop, set a variable

‘prime’ to False and break the loop.

STEP 7 : If the number is not divisible by any number, set ’prime’ to True.

STEP 8 : After the for loop, check if ‘prime’ is True. If it is, print the number and

increment the counter.

STEP 9 : Increment the value of ‘num’ and repeat the process until the counter

is equal to the input value.

STEP 10 : End the program.

10
PROGRAME:

# Generating first n prime numbers

# Take input from the user

n = int(input("Enter the number of prime numbers you want to generate: "))

# Initialize the counter for prime numbers

counter = 0

# Initialize the number to be checked as 2

num = 2

# Loop through the numbers from 2 to a large number

while counter < n:

prime = True

for i in range(2, num):

if num % i == 0:

prime = False

break

if prime:

print(num)

counter += 1

num += 1

11
OUTPUT :

Enter the number of prime numbers you want to generate: 10

11

13

17

19

23

29

RESULT :

The above program was executed successfully.

12
EX NO: 5 Develop a Python program to find the sum of
squares of n natural numbers.
DATE:

AIM:

To develop a python program to find the sum of squares of n natural


numbers.

ALGORITHM:

STEP 1 : Start the program by taking input from the user for the number of

natural numbers.

STEP 2 : Initialize a variable ‘sum_of_squares’ to keep track of the sum of


squares.

STEP 3 : Use a for loop to go through the numbers from 1 to the input value.

STEP 4 : Within the loop, square the current number and add it to the

‘sum_of_squares’.

STEP 5 : Repeat the process until the loop has gone through all the numbers.

STEP 6 : End the program by printing the final result.

13
PROGRAME:

# Sum of squares of n natural numbers

# Take input from the user

n = int(input("Enter the number of natural numbers: "))

# Initialize the sum

sum_of_squares = 0

# Loop through the natural numbers

for i in range(1, n + 1):

sum_of_squares += i * i

# Print the final result

print("The sum of squares of", n, "natural numbers is", sum_of_squares)

14
OUTPUT :

Enter the number of natural numbers: 5

The sum of squares of 5 natural numbers is 55

RESULT :

The above program was executed successfully.

15
EX NO: 6 Develop a Python program to find sum of
elements in an array.
DATE:

AIM:

To develop a python program to find sum of elements in an array.

ALGORITHM:

STEP 1 : Start the program by defining a function ‘sum_of_elements’ that takes

In an array.

STEP 2 : Within the function, initialize a variable ‘sum’ to keep track of the sum

of elements.

STEP 3 : Use a for loop to go through the elements of the array.

STEP 4 : Within the loop, add the current element to the ‘sum’.

STEP 5 : Return the ‘sum’ at the end of the function.

STEP 6 : Take input from the user for the elements of the array.

STEP 7 : Call the function and store the result in a variable.

STEP 8 : End the program by printing the result.

16
PROGRAME:

# Sum of elements in an array

def sum_of_elements(arr):

# Initialize the sum

sum = 0

# Loop through the elements of the array

for element in arr:

sum += element

# Return the final result

return sum

# Take input from the user

array = [int(x) for x in input("Enter the elements of the array, separated by a


space: ").split()]

# Call the function

result = sum_of_elements(array)

# Print the final result

print("The sum of elements in the array is", result)

17
OUTPUT :

Enter the elements of the array, separated by a space: 1 2 3 4 5

The sum of elements in the array is 15

RESULT :

The above program was executed successfully.

18
EX NO: 7 Develop a Python program to find largest
element in the array.
DATE:

AIM:

To develop a python program to find largest element in the array.

ALGORITHM:

STEP 1 : Start the program by defining a function ‘largest_element’ which

takes an array as input.

STEP 2 : Inside the function, initialize a variable ‘largest’ with the first element

Of the array.

STEP 3 : Use a for loop to go through the elements of the array.

STEP 4 : Within the loop, check if the current element is larger than the

‘largest’ value.

STEP 5 : If the current element is larger, set it as the new ‘largest’ value.

STEP 6 : Repeat the process until the loop has gone through all the elements of

the array.

STEP 7 : End the function by returning the final result.

STEP 8 : Outside the function, take input from the user for the elements of the

array.

STEP 9 : Call the function ‘largest_elements’ with the input array.

STEP 10 : End the program by printing the final result.

19
PROGRAME:

# Largest element in the array

Def largest_element(arr):

# Initialize the largest number

largest = arr[0]

# Loop through the elements of the array

for element in arr:

# If the current element is larger than the largest, set it as the new largest

if element > largest:

largest = element

# Return the final result

return largest

# Take input from the user

array = [int(x) for x in input("Enter the elements of the array, separated by a


space: ").split()]

# Call the function

result = largest_element(array)

# Print the final result

print("The largest element in the array is", result)

20
OUTPUT :

Enter the elements of the array, separated by a space: 1 5 7 9 4

The largest element in the array is 9

RESULT :

The above program was executed successfully.

21
EX NO: 8 Develop a Python program to check if the given
string is a palindrome or not.
DATE:

AIM:

To develop a python program to check if the given string is a palindrome of


not.

ALGORITHM:

STEP 1 : Start the program by defining a function ‘is_palindrome’ which takes

a string as input.

STEP 2 : Inside the function, reverse the string using slicing.

STEP 3 : Check if the reversed string is equal to the original string.

STEP 4 : If the reversed string is equal to the original string, return ‘True’.

STEP 5 : If the reversed string is not equal to the original string, return

‘False’.

STEP 6 : Outside the function, take input from the user for the word to be

checked.

STEP 7 : Call the function ‘is_palindrome’ with the input word.

STEP 8 : End the program by printing the final result, indicating if the word is

a palindrome or not.

22
PROGRAME:

# Palindrome check

def is_palindrome(string):

# Reverse the string

reversed_string = string[::-1]

# Check if the reversed string is equal to the original string

if reversed_string == string:

return True

else:

return False

# Take input from the user

word = input("Enter a word: ")

# Call the function

result = is_palindrome(word)

# Print the final result

if result:

print("The word is a palindrome.")

else:

print("The word is not a palindrome.")

23
OUTPUT :

Enter a word: racecar

The word is a palindrome.

Enter a word: hello

The word is not a palindrome.

RESULT :

The above program was executed successfully.

24
EX NO: 9 Develop a Python program to store string in a list
and print them.
DATE:

AIM:

To develop a python program to store string in a list and print them.

ALGORITHM :

STEP 1 : Create an empty list ‘string_list’ to store the strings.

STEP 2 : Use a ‘while’ loop to repeatedly prompt the user for input until they

enter

STEP 3 : Within the ‘while’ loop, get a string from the user using ‘input()’.

STEP 4 : If the user inputs 'q', break out of the loop.

STEP 5 : Otherwise, append the string to the ‘string_list’.

STEP 6 : After the loop, use a ‘for’ loop to iterate over the ‘string_list’ and

print each string.

STEP 7 : End the program.

25
PROGRAME:

# Create an empty list

string_list = []

# Take input from the user

while True:

string = input("Enter a string (or type 'q' to quit): ")

if string == 'q':

break

# Add the string to the list

string_list.append(string)

# Print the list of strings

print("The list of strings:")

for s in string_list:

print(s)

26
OUTPUT :

Enter a string (or type 'q' to quit): Hello

Enter a string (or type 'q' to quit): World

Enter a string (or type 'q' to quit): q

The list of strings:

Hello

World

RESULT :

The above program was executed successfully.

27
EX NO: 10 Develop a Python program to find the length of a
list, reverse it, copy it and then clear it.
DATE:

AIM:

To develop a python program to find the length of a list, reverse it, copy it,
and then clear it.

ALGORITHM :

STEP 1 : Take input ‘n’ from the user, which represents the number of

elements in the list.

STEP 2 : Initialize an empty list ‘myList’.

STEP 3 : Use a ‘for’ loop to repeatedly prompt the user for input and append

the values to the ‘mylist’.

STEP 4 : After the loop, print the list elements.

STEP 5 : Find the length of the list using the ’len()’ function and print it.

STEP 6 : Reverse the list using list slicing ‘[: : -1] ‘ and store it in a new list

‘mylist’, then print it.

STEP 7 : Copy the list using the ‘copy()’ method and store it in a new list

‘myList2’, then print it.

STEP 8 : Clear the original list using the ‘clear()’ method and print it.

STEP 9 : End the program.

28
PROGRAME:

# program for list operations using built-in functions

n=int(input(“How many elements in a list?”))

myList=[]

for I in range(n):

myVal=int(input(“Enter list elements :”))

myList.append(myVal)

print(“The List elements are :”)

for I in range(n):

print(myList[i],end=” “)

print(“\nThe length of the List is : “,len(myList))

myList1=myList[::-1]

print(“The reverse of the List is : “,myList1)

print(“The copy of the List is :”,myList.copy())

print(“The List after clear is : “,myList.clear())

29
OUTPUT :

How many elements in a list? 5

Enter list elements : 6

Enter list elements : 4

Enter list elements : 9

Enter list elements : 3

Enter list elements : 1

The List elements are :

64931

The length of the List is : 5

The reverse of the List is : [1, 3, 9, 4, 6]

The copy of the List is : [6, 4, 9, 3, 1]

The List after clear is : None

RESULT :

The above program was executed successfully.

30
31

You might also like