Python Lab Manual
Python Lab Manual
Practical 1.1 -
INPUT:
OUTPUT:
Practical 1.2 -
INPUT:
x=4
y=5
z = (x+y)**3
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
INPUT:
x = int(input("Enter a value for x:"))
OUTPUT:
Page 2 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
Practical 3.1
INPUT:
if x % 2 == 0:
print(x, "is even.")
else:
print(x, "is odd.")
OUTPUT:
Practical 3.2
AIM: Check whether a given integer x is odd or even in python by using AND operator
INPUT:
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
INPUT:
if x % y == 0:
print(x, "is divisible by", y)
else:
print(x, "is not divisible by", y)
OUTPUT:
Page 4 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
Practical 5.1
INPUT:
OUTPUT:
Practical 5.2
INPUT:
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):
if prime[p]:
prime_numbers = prime(x + 1)
OUTPUT:
Page 6 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
Practical 6.2
INPUT:
OUTPUT:
Practical 6.3
INPUT:
OUTPUT:
CONCLUSION: Hence, I learnt to find out GCD and LCM of two integers.
Practical 7.1
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
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
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
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:
OUTPUT:
Practical 10
INPUT:
OUTPUT:
Page 11 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
Practical 11
INPUT:
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
Page 12 of 19
Python Lab Manual 23000749: BIRVA DHOLAKIYA
def main():
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
factorial *= 2 * n * (2 * n - 1)
result += term
return result
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
return result
x = float(input("Enter the value of x: "))
accuracy = int(input("Enter the desired level of accuracy: "))
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:
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
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:
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:
Page 19 of 19