PYTHON LAB Notes
PYTHON LAB Notes
SIGNATURE
2 Fibonacci series
AIM:
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.
radius.
STEP 4 : Calculate the area of the circle using the formula: pi * radius * radius.
1
PROGRAME:
pi = 3.14
perimeter = 2 * pi * radius
2
OUTPUT :
RESULT :
3
EX NO: 2 Develop a Python program to Generate
Fibonacci series.
DATE:
AIM:
ALGORITHM:
STEP 1 : Start the program by taking input from the user for the number
STEP 2 : Initialize two variables ‘first term’ and ’second term’ as the
of terms generated.
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
STEP 6 : Repeat the process until the number of terms generated is equal
4
PROGRAME:
first_term = 0
second_term = 1
count = 0
if n <= 0:
else:
print(first_term)
first_term = second_term
second_term = next_term
count += 1
5
OUTPUT :
13
21
34
RESULT :
6
EX NO: 3 Develop a Python program to compute the GCD
of two numbers.
DATE:
AIM:
ALGORITHM:
STEP 1 : Start the program by taking input from the user for 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
7
PROGRAME:
gcd = 1
# Loop through the numbers from 1 to the smaller of num1 and num2
i=1
gcd = i
i += 1
8
OUTPUT :
RESULT :
9
EX NO: 4 Develop a Python program to Generate first n
prime numbers.
DATE:
AIM:
ALGORITHM:
STEP 1 : Start the program by taking input from the user for the number of
numbers generated.
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
STEP 6 : If the number is divisible by any number in the for loop, set a variable
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
STEP 9 : Increment the value of ‘num’ and repeat the process until the counter
10
PROGRAME:
counter = 0
num = 2
prime = True
if num % i == 0:
prime = False
break
if prime:
print(num)
counter += 1
num += 1
11
OUTPUT :
11
13
17
19
23
29
RESULT :
12
EX NO: 5 Develop a Python program to find the sum of
squares of n natural numbers.
DATE:
AIM:
ALGORITHM:
STEP 1 : Start the program by taking input from the user for the number of
natural numbers.
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.
13
PROGRAME:
sum_of_squares = 0
sum_of_squares += i * i
14
OUTPUT :
RESULT :
15
EX NO: 6 Develop a Python program to find sum of
elements in an array.
DATE:
AIM:
ALGORITHM:
In an array.
STEP 2 : Within the function, initialize a variable ‘sum’ to keep track of the sum
of elements.
STEP 4 : Within the loop, add the current element to the ‘sum’.
STEP 6 : Take input from the user for the elements of the array.
16
PROGRAME:
def sum_of_elements(arr):
sum = 0
sum += element
return sum
result = sum_of_elements(array)
17
OUTPUT :
RESULT :
18
EX NO: 7 Develop a Python program to find largest
element in the array.
DATE:
AIM:
ALGORITHM:
STEP 2 : Inside the function, initialize a variable ‘largest’ with the first element
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 8 : Outside the function, take input from the user for the elements of the
array.
19
PROGRAME:
Def largest_element(arr):
largest = arr[0]
# If the current element is larger than the largest, set it as the new largest
largest = element
return largest
result = largest_element(array)
20
OUTPUT :
RESULT :
21
EX NO: 8 Develop a Python program to check if the given
string is a palindrome or not.
DATE:
AIM:
ALGORITHM:
a string as input.
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 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):
reversed_string = string[::-1]
if reversed_string == string:
return True
else:
return False
result = is_palindrome(word)
if result:
else:
23
OUTPUT :
RESULT :
24
EX NO: 9 Develop a Python program to store string in a list
and print them.
DATE:
AIM:
ALGORITHM :
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 6 : After the loop, use a ‘for’ loop to iterate over the ‘string_list’ and
25
PROGRAME:
string_list = []
while True:
if string == 'q':
break
string_list.append(string)
for s in string_list:
print(s)
26
OUTPUT :
Hello
World
RESULT :
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
STEP 3 : Use a ‘for’ loop to repeatedly prompt the user for input and append
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
STEP 7 : Copy the list using the ‘copy()’ method and store it in a new list
STEP 8 : Clear the original list using the ‘clear()’ method and print it.
28
PROGRAME:
myList=[]
for I in range(n):
myList.append(myVal)
for I in range(n):
print(myList[i],end=” “)
myList1=myList[::-1]
29
OUTPUT :
64931
RESULT :
30
31