Python Lab Manual 23000749: BIRVA DHOLAKIYA
PYTHON LAB MANNUAL
Practical 1.1 -
AIM: Write a program to print “Hello World” in Python
INPUT:
print ('Hello World')
OUTPUT:
CONCLUSION: Hence, I’ve learned the ‘print’ function in python language
Practical 1.2 -
AIM: Learn simple operations in python
INPUT:
x=4
y=5
z = (x+y)**3
print (("(x + y) ** 3 =", z))
Page 1 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
OUTPUT:
CONCLUSION: Hence, I have learnt the basic mathematical operations in python language
Practical 2
AIM: Take input variables x and y to calculate z.
INPUT:
x = int(input("Enter a value for x:"))
y = int(input("Enter a value for y:"))
z = (x+y)**3
print (("(x + y) ** 3 =", z))
OUTPUT:
CONCLUSION: Hence, I learnt to take input of variables in python
Page 2 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
Practical 3.1
AIM: Check whether a given integer x is odd or even in python
INPUT:
x = int(input("Enter an integer: "))
if x % 2 == 0:
print(x, "is even.")
else:
print(x, "is odd.")
OUTPUT:
CONCLUSION: Hence, I have learnt to find out if an integer is odd or even.
Practical 3.2
AIM: Check whether a given integer x is odd or even in python by using AND operator
INPUT:
x = int(input("Enter an integer: "))
if x & 1 == 0:
print(x, "is even.")
else:
print(x, "is odd.")
Page 3 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
OUTPUT:
CONCLUSION: Hence, I learnt to find out if an integer is odd or even by using AND operator.
Practical 4
AIM: Check whether the integer x is divisible by y or not.
INPUT:
x = int(input("Enter an integer (dividend): "))
y = int(input("Enter another integer (divisor): "))
if x % y == 0:
print(x, "is divisible by", y)
else:
print(x, "is not divisible by", y)
OUTPUT:
CONCLUSION: Hence, I learnt to check divisibility of numbers
Page 4 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
Practical 5.1
AIM: Find the divisors of an integer x, including 1 and x.
INPUT:
x = int(input("Enter an integer: "))
divisors = []
for i in range(1, x + 1):
if x % i == 0:
divisors.append(i)
print("Divisors of", x, "are:", divisors)
OUTPUT:
CONCLUSION: Hence, I learnt to find the divisors of an integer x, including 1 and x.
Practical 5.2
AIM: Find the divisors of an integer x, excluding 1 and x.
INPUT:
x = int(input("Enter an integer: "))
divisors = []
for i in range(2, x):
if x % i == 0:
divisors.append(i)
print("Divisors of", x, "excluding 1 and", x, "are:", divisors)
OUTPUT:
Page 5 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
CONCLUSION: Hence, I learnt to find out the divisors of integer x, excluding 1 and x.
Practical 6.1
AIM: Find out all the prime numbers between 1 and integer x.
INPUT:
def prime(n):
prime = [True for _ in range(n + 1)]
p=2
while p * p <= n:
if prime[p]:
for i in range(p * p, n + 1, p):
prime[i] = False
p += 1
# Print all prime numbers
primes = []
for p in range(2, n):
if prime[p]:
primes.append(p)
return primes
x = int(input("Enter an integer: "))
prime_numbers = prime(x + 1)
print("Prime numbers between 1 and", x, "are:", prime_numbers)
OUTPUT:
Page 6 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
CONCLUSION: Hence, I learnt to find out prime numbers between 1 and x.
Practical 6.2
AIM: Find GCD of two integers x and y
INPUT:
def gcd(x, y):
while y != 0:
x, y = y, x % y
return x
x = int(input("Enter the first integer: "))
y = int(input("Enter the second integer: "))
result = gcd(x, y)
print("The Greatest Common Divisor (GCD) of", x, "and", y, "is:", result)
OUTPUT:
CONCLUSION: Hence, I learnt to find out GCD of two integers
Practical 6.3
AIM: Find GCD and LCM of two integers x and y.
INPUT:
def gcd(x, y):
while y != 0:
x, y = y, x % y
return x
def lcm(x, y):
Page 7 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
return (x * y) // gcd(x, y)
x = int(input("Enter the first integer: "))
y = int(input("Enter the second integer: "))
gcd_result = gcd(x, y)
lcm_result = lcm(x, y)
print("The Greatest Common Divisor (GCD) of", x, "and", y, "is:", gcd_result)
print("The Least Common Multiple (LCM) of", x, "and", y, "is:", lcm_result)
OUTPUT:
CONCLUSION: Hence, I learnt to find out GCD and LCM of two integers.
Practical 7.1
AIM: Sum of finite series 1+2+3….+n
INPUT:
def sum_of_series(n):
return (n * (n + 1)) // 2
n = int(input("Enter the value of n: "))
result = sum_of_series(n)
print("The sum of the series is:", result)
OUTPUT:
CONCLUSION:
Page 8 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
Practical 7.2
AIM: Find factorial of a number
INPUT:
def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
n = int(input("Enter the value of n: "))
result = factorial(n)
print("The factorial of", n, "is:", result)
OUTPUT:
CONCLUSION:
Practical 7.3
AIM: Find the sum of the series : 2+4+6+….+2n
INPUT:
def sum_of_series(n):
return (n / 2) * (2 + 2 * n)
n = int(input("Enter the value of n: "))
result = sum_of_series(n)
print("The sum of the series is:", result)
Page 9 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
OUTPUT:
CONCLUSION:
Practical 8
AIM: Sum of finite series 1+3+5+…n
INPUT:
def sum_of_series(n):
return (n / 2) * (1 + n)
n = int(input("Enter the value of n: "))
result = sum_of_series(n)
print("The sum of the series is:", result)
OUTPUT:
Practical 9
AIM: Write a function to multiply two non-negative numbers by repeated additions, for example, 7
*5=7+7+7+7+7
INPUT:
def multiply(a, b):
result = 0
for _ in range(b):
result += a
return result
Page 10 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
a=7
b=5
result = multiply(a, b)
print(f"{a} * {b} =", result)
OUTPUT:
Practical 10
AIM: Write a program to swap 2 given numbers using strings.
INPUT:
def swap_numbers(a, b):
a_str = str(a)
b_str = str(b)
a_str, b_str = b_str, a_str
a = int(a_str)
b = int(b_str)
return a, b
num1 = 5
num2 = 10
print("Before swapping: num1 =", num1, ", num2 =", num2)
num1, num2 = swap_numbers(num1, num2)
print("After swapping: num1 =", num1, ", num2 =", num2)
OUTPUT:
Page 11 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
Practical 11
AIM: Write a program to swap 2 given numbers using functions.
INPUT:
def swap_numbers(a, b):
return b, a
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
print("Before swapping: num1 =", num1, ", num2 =", num2)
num1, num2 = swap_numbers(num1, num2)
print("After swapping: num1 =", num1, ", num2 =", num2)
OUTPUT:
Practical 12
AIM: Write a function area of Triangle() that takes the lengths of three sides: side1, side2, and
side3 of the triangle as the input parameters and returns the area of the triangle as the output. Also
assert that the sum of the lengths of any 2 sides is greater than the third side. Write a function main()
that accepts inputs from the user interactively and computes the area of the triangle using the
function area of Triangle().
INPUT:
import math
def areaTriangle(side1, side2, side3):
assert side1 + side2 > side3, "Invalid triangle: sum of two sides is not greater than the third side"
assert side1 + side3 > side2, "Invalid triangle: sum of two sides is not greater than the third side"
assert side2 + side3 > side1, "Invalid triangle: sum of two sides is not greater than the third side"
s = (side1 + side2 + side3) / 2
area = math.sqrt(s * (s - side1) * (s - side2) * (s - side3))
return area
Page 12 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
def main():
side1 = float(input("Enter the length of side 1: "))
side2 = float(input("Enter the length of side 2: "))
side3 = float(input("Enter the length of side 3: "))
try:
triangle_area = areaTriangle(side1, side2, side3)
print("The area of the triangle is:", triangle_area)
except AssertionError as e:
print("Error:", e)
if __name__ == "__main__":
main()
OUTPUT:
Practical 13
AIM: Write a function that returns True or False depending on whether the given number is a
palindrome or not.
INPUT:
def is_palindrome(number):
num_str = str(number)
return num_str == num_str[::-1]
number = int(input("Enter a number: "))
if is_palindrome(number):
print(number, "is a palindrome.")
else:
print(number, "is not a palindrome.")
OUTPUT:
Page 13 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
Practical 14
AIM: Write a function to evaluate each of the following infinite series for a given level of accuracy:
(a) 1 – x2 / 2! + x4 / 4! – x6 / 6! + …
INPUT:
import math
def evaluate_series(x, accuracy):
result = 1
term = 1
factorial = 1
for n in range(1, accuracy):
factorial *= 2 * n * (2 * n - 1)
term *= -(x ** 2) / factorial
result += term
return result
x = float(input("Enter the value of x: "))
accuracy = int(input("Enter the desired level of accuracy: "))
series_sum = evaluate_series(x, accuracy)
print("The value of the series for x =", x, "and accuracy =", accuracy, "is:", series_sum)
OUTPUT:
Page 14 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
Practical 15
AIM: Write a function to evaluate each of the following infinite series for a given level of accuracy:
ex = 1 + x / 1! + x2 / 2! + x3 / 3! + …
INPUT:
'''
import math
def evaluate_series(x, accuracy):
result = 1
term = 1
factorial = 1
for n in range(1, accuracy):
factorial m*= n
term *= x / factorial
result += term
return result
x = float(input("Enter the value of x: "))
accuracy = int(input("Enter the desired level of accuracy: "))
series_sum = evaluate_series(x, accuracy)
print("The value of e^x for x =", x, "and accuracy =", accuracy, "is:", series_sum)
OUTPUT:
Practical 16
Page 15 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
AIM: Write a function that returns the sum of digits of a number, passed to it as an argument.
INPUT:
def sum_of_digits(number):
num_str = str(number)
digit_sum = 0
for digit in num_str:
digit_sum += int(digit)
return digit_sum
number = int(input("Enter a number: "))
result = sum_of_digits(number)
print("The sum of digits of the number is:", result)
OUTPUT:
Practical 17
AIM: Write a function that takes two numbers as input parameters and returns True or False
depending on whether they are co-primes or not.
INPUT:
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
def are_coprimes(a, b):
return gcd(a, b) == 1
# Test the function
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
if are_coprimes(num1, num2):
print(num1, "and", num2, "are coprime.")
else:
print(num1, "and", num2, "are not coprime.")
Page 16 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
OUTPUT:
Practical 18
AIM: Write a function that takes a string as a parameter and returns a string with every successive
repetitive character replaced with a star(*)
INPUT:
'''
def replace_repetitive_characters(string):
result = string[0]
for i in range(1, len(string)):
if string[i] == string[i - 1]:
result += '*'
else:
result += string[i]
return result
input_string = input("Enter a string: ")
result_string = replace_repetitive_characters(input_string)
print("Result string:", result_string)
OUTPUT:
Practical 19
Page 17 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
AIM: Write a function that takes two strings and returns True if they are anagrams and False
otherwise.
INPUT:
def are_anagrams(str1, str2):
# Remove spaces and convert strings to lowercase
str1 = str1.replace(" ", "").lower()
str2 = str2.replace(" ", "").lower()
# Check if the sorted versions of the strings are equal
return sorted(str1) == sorted(str2)
# Test the function
string1 = input("Enter the first string: ")
string2 = input("Enter the second string: ")
if are_anagrams(string1, string2):
print("The strings are anagrams.")
else:
print("The strings are not anagrams.")
OUTPUT:
Practical 20
Page 18 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
AIM: Write a function that takes a list of values as input parameter and returns another list without
duplicates.
INPUT:
def remove_duplicates(input_list):
return list(set(input_list))
input_list = input("Enter a list of values separated by spaces: ").split()
result_list = remove_duplicates(input_list)
print("List without duplicates:", result_list)
OUTPUT:
CONCLUSION: Hence, I learnt programming in python language
Page 19 of 19