Python Lab Practicles-2
Python Lab Practicles-2
# Addition
num1 = float(input("Enter the first number for addition: "))
num2 = float(input("Enter the second number for addition: "))
sum_result = num1 + num2
print(f"sum: {num1} + {num2} = {sum_result}")
Output:
Enter the first number for addition: 5
Enter the second number for addition: 6
sum: 5.0 + 6.0 = 11.0
Program 2
Write a Python program to find the area of a triangle.
# Division
num3 = float(input("Enter the dividend for division: "))
num4 = float(input("Enter the divisor for division: "))
if num4 == 0:
print("Error: Division by zero is not allowed.")
else:
div_result = num3 / num4
print(f"Division: {num3} / {num4} = {div_result}")
import random
print(f"Random number: {random.randint(1, 100)}")
Random number: 89
Program 6
Write a Python program to convert kilometers to meters.
import calendar
year = int(input("Enter year: "))
month = int(input("Enter month: "))
cal = calendar.month(year, month)
print(cal)
After swapping:
a = 10
b=5
Program 9
Write a Python Program to Check if a Number is Positive, Negative or Zero.
Enter a number: 7
This is a odd number
Program 11
Check if a number is positive, negative, or zero
if num > 0:
print(f"{num} is a positive number.")
elif num < 0:
print(f"{num} is a negative number.")
else:
print("The number is zero.")
Program 12
Sum of elements in a list
numbers = [1, 2, 3, 4, 5]
total = 0
i=1
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print(f"Sum of list: {total}")
def sum_of_list(lst):
total = sum(lst)
return total
numbers = [1, 2, 3, 4, 5]
print(f"Sum of list: {sum_of_list(numbers)}")
Program 2
2. Program to Find the Maximum and Minimum in a List
def find_max_min(lst):
return max(lst), min(lst)
def count_occurrences(lst):
return {item: lst.count(item) for item in set(lst)}
list1 = [1, 2, 3]
list2 = [4, 5, 6]
merged_list = list1 + list2
print(f"Merged list: {merged_list}")
list1 = [1, 2, 3]
list2 = [4, 5, 6]
merged_list = merge_lists(list1, list2)
print(f"Merged list: {merged_list}")
Program 5
5. Program to Remove Duplicates from a List
items = [1, 2, 2, 3, 4, 4, 5]
unique_items = list(set(items))
print(f"List without duplicates: {unique_items}")
def remove_duplicates(lst):
return list(set(lst))
items = [1, 2, 2, 3, 4, 4, 5]
unique_items = remove_duplicates(items)
print(f"List without duplicates: {unique_items}")
Program 6
6. Program to Generate a List of Squares
n=5
squares = [i**2 for i in range(n)] Output: List of squares: [0, 1, 4, 9, 16]
print(f"List of squares: {squares}")
def generate_squares(n):
return [i**2 for i in range(n)]
squares = generate_squares(5)
print(f"List of squares: {squares}")
Program 7
7. Accessing elements, slicing, and iterating through a tuple
# Defining a tuple
my_tuple = (10, 20, 30, 40, 50)
# Repeating a tuple
repeated_tuple = tuple1 * 3
print(f"Repeated Tuple: {repeated_tuple}") # Output: (1, 2, 3, 1, 2, 3,
1, 2, 3)
# Nested tuple
nested_tuple = (1, (2, 3), (4, 5, 6))
print(f"Nested Tuple: {nested_tuple}") # Output: (1, (2, 3), (4, 5, 6))
# Defining a dictionary
my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}