Python Lab Manual
Python Lab Manual
2. Write /execute simple ‘Python’ program: Develop minimum 2 programs using different data types (numbers,
string, tuple, list, and dictionary).
a=5
b=3
c=a+b
print("Sum of", a, "and", b, "is", c)
d=a-b
print("Difference of", a, "and", b, "is", d)
e=a*b
print("Product of", a, "and", b, "is", e)
f=a/b
print("Quotient of", a, "and", b, "is", f)p
selected_fruit = input("Enter the name of the fruit you would like to have: ")
if selected_fruit in fruits:
print("You have selected", selected_fruit)
else:
print("Sorry, we don't have", selected_fruit)
3. Write /execute simple ‘Python’ program: Develop minimum 2 programs using Arithmetic Operators,
exhibiting data type conversion.
Program1: Using Arithmetic Operators
a=5
b=3
c=a+b
print("Sum of", a, "and", b, "is", c)
d=a-b
print("Difference of", a, "and", b, "is", d)
e=a*b
print("Product of", a, "and", b, "is", e)
f=a/b
print("Quotient of", a, "and", b, "is", f)
g = int(a / b)
print("Integer Quotient of", a, "and", b, "is", g)
(ii) Write simple programs to convert bits to Megabytes, Gigabytes and Terabytes.
# calculates Bits
# 1 kilobytes(s) = 8192 bits
Bits = kilobytes * 8192
return Bits
# calculates Bytes
# 1 KB = 1024 bytes
Bytes = kilobytes * 1024
return Bytes
# Driver code
if __name__ == "__main__" :
kilobytes = 1
import math
pi = math.pi
# Driver Code
radius = float(5)
height = float(12)
slat_height = float(13)
print( "Volume Of Cone : ", volume(radius, height) )
print( "Surface Area Of Cone : ", surfacearea(radius, slat_height) )
6. Write program to: (i) determine whether a given number is odd or even. (ii) Find the greatest of the three
numbers using conditional operators.
def odd_even(num):
if num % 2 == 0:
return "Even"
else:
return "Odd"
7. Write a program to: i) Find factorial of a given number. ii) Generate multiplication table up to 10 for numbers
1 to 5.
def factorial(num):
if num == 0:
return 1
return num * factorial(num - 1)
def multiplication_table(n):
for i in range(1, 11):
print(n, "x", i, "=", n * i)
8. Write a program to: i) Find factorial of a given number. ii) Generate multiplication table up to 10 for numbers
1 to 5 using functions.
def factorial(num):
if num == 0:
return 1
return num * factorial(num - 1)
def multiplication_table(n):
for i in range(1, 11):
print(n, "x", i, "=", n*i)
print("\nMultiplication table:")
for i in range(1, 6):
print("Table for", i)
multiplication_table(i)
print("\n")
9. Write a program to: i) Find factorial of a given number using recursion. ii) Generate Fibonacci sequence up to
100 using recursion.
def factorial(num):
if num == 0:
return 1
return num * factorial(num - 1)
def fibonacci(num):
if num <= 1:
return num
return fibonacci(num - 1) + fibonacci(num - 2)
print("\nFibonacci sequence:")
for i in range(100):
fib = fibonacci(i)
if fib > 100:
break
print(fib, end=", ")
10. Write a program to: Create a list, add element to list, delete element from the lists.
my_list = []
def add_to_list(element):
my_list.append(element)
print(element, "added to the list")
def delete_from_list(element):
if element in my_list:
my_list.remove(element)
print(element, "deleted from the list")
else:
print(element, "not found in the list")
if choice == 1:
element = int(input("Enter an integer to add: "))
add_to_list(element)
elif choice == 2:
element = int(input("Enter an integer to delete: "))
delete_from_list(element)
elif choice == 3:
print("The list contains:", my_list)
elif choice == 4:
print("Exiting the program...")
break
else:
print("Invalid choice. Try again.")
11. Write a program to: Sort the list, reverse the list and counting elements in a list.
my_list = [3, 4, 1, 7, 5, 9, 2, 8, 6, 0]
def sort_list(lst):
return sorted(lst)
def reverse_list(lst):
return lst[::-1]
def count_elements(lst):
count = {}
for element in lst:
count[element] = count.get(element, 0) + 1
return count
sorted_list = sort_list(my_list)
print("Sorted list:", sorted_list)
reversed_list = reverse_list(my_list)
print("Reversed list:", reversed_list)
count = count_elements(my_list)
print("Count of elements in the list:", count)
12. Write a program to: Create dictionary, add element to dictionary, delete element from the dictionary.
import statistics
numbers = [3, 4, 1, 7, 5, 9, 2, 8, 6, 0]
def average(lst):
return sum(lst) / len(lst)
def mean(lst):
return statistics.mean(lst)
def median(lst):
return statistics.median(lst)
def standard_deviation(lst):
return statistics.stdev(lst)
print("Numbers:", numbers)
avg = average(numbers)
print("Average:", avg)
mn = mean(numbers)
print("Mean:", mn)
md = median(numbers)
print("Median:", md)
stddev = standard_deviation(numbers)
print("Standard Deviation:", stddev)
def factors(n):
for i in range(1, n + 1):
if n % i == 0:
print(i)
# Step 2: Open the file in append mode and append "Hello World" at the end
with open("hello.txt", "a") as file:
file.write("Hello World\n")
print("File 'hello.txt' appended successfully.")
16. Write a program to :i) To open a file in read mode and write its contents to another file but replace every
occurrence of character ‘h’ ii) To open a file in read mode and print the number of occurrences of a
character ‘a’.
# Step 1: Open a file in read mode and write its contents to another file with 'h' replaced
with open("input.txt", "r") as file1, open("output.txt", "w") as file2:
for line in file1:
modified_line = line.replace('h', 'X') # Replace 'h' with 'X'
file2.write(modified_line)
print("Contents of 'input.txt' copied to 'output.txt' with 'h' replaced.")
# Step 2: Open a file in read mode and count the number of occurrences of 'a'
char_to_count = 'a'
count = 0
with open("input.txt", "r") as file:
for line in file:
count += line.count(char_to_count) # Count occurrences of 'a' in each line
print(f"Number of occurrences of '{char_to_count}' in 'input.txt': {count}")
17. Write a Program to: Add two complex number using classes and objects.
class ComplexNumber:
def __init__(self, real, imag):
self.real = real
self.imag = imag
def display(self):
print(f"Sum: {self.real} + {self.imag}i")
18.Write a Program to: Subtract two complex number using classes and objects.
class ComplexNumber:
def __repr__(self):
return f"({self.real}+{self.imaginary}j)"
def main():
c1 = ComplexNumber(1, 2)
c2 = ComplexNumber(3, 4)
c3 = c1 - c2
print(c3)
if __name__ == "__main__":
main()
# Create a package
package = __import__("my_package")
Procedure:
2. Open a new file or an existing one, then write your Python code in the editor.