1.
Write a Python program to perform basic arithmetic operations (addition, subtraction,
multiplication, and division).
print("Select operation:")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
choice = input("Enter choice (1/2/3/4): ")
# Taking input for the two numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
result = num1 + num2
print("The result of addition: " + str(result))
elif choice == '2':
result = num1 - num2
print("The result of subtraction: " + str(result))
elif choice == '3':
result = num1 * num2
print("The result of multiplication: " + str(result))
elif choice == '4':
if num2 != 0:
result = num1 / num2
print("The result of division: " + str(result))
else:
print("Error! Division by zero.")
else:
print("Invalid input")
2. Python program to find the largest of three numbers using if-elif-else.
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
# Using if-elif-else to find the largest number
if num1 >= num2 and num1 >= num3:
largest = num1
elif num2 >= num1 and num2 >= num3:
largest = num2
else:
largest = num3
# Displaying the largest number
print("The largest number is:", largest)
3. ''' Write a Python program to convert temperatures to and from Celsius, Fahrenheit.
[ Formula: c/5 = f-32/9] '''
print("Options are \n")
print("1.Convert temperatures from Celsius to Fahrenheit \n")
print("2.Convert temperatures from Fahrenheit to Celsius \n")
opt=int(input("Choose any Option(1 or 2) : "))
if opt == 1:
print("Convert temperatures from Celsius to Fahrenheit \n")
cel = float(input("Enter Temperature in Celsius: "))
fahr = (cel*9/5)+32
print("Temperature in Fahrenheit =",fahr)
elif opt == 2:
print("Convert temperatures from Fahrenheit to Celsius \n")
fahr = float(input("Enter Temperature in Fahrenheit: "))
cel=(fahr-32)*5/9;
print("Temperature in Celsius =",cel)
else:
print("Invalid Option")
4 .Illustrate various operations that can be performed with lists in Python.
numbers = [10, 20, 30, 40, 50]
length = len(numbers)
print("Length of the list:", length)
# Program to append an element to a list
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print("Updated list:", fruits)
# Program to insert an element at a specific position
numbers = [1, 2, 3, 5, 6]
numbers.insert(3, 4) # Insert 4 at index 3
print("Updated list:", numbers)
# Program to remove an element from a list
colors = ["red", "green", "blue", "yellow"]
colors.remove("green") # Remove 'green'
print("Updated list:", colors)
# Program to sort a list
numbers = [5, 2, 9, 1, 5, 6]
numbers.sort() # Sort in ascending order
print("Sorted list:", numbers)
# Program to reverse a list
numbers = [1, 2, 3, 4, 5]
numbers.reverse()
print("Reversed list:", numbers)
# Program to count occurrences of an element in a list
numbers = [1, 2, 3, 2, 4, 2, 5]
count = numbers.count(2) # Count how many times 2 appears
print("Occurrences of 2:", count)
# Program to find the maximum and minimum elements in a list
numbers = [10, 20, 5, 40, 15]
max_num = max(numbers)
min_num = min(numbers)
print("Maximum number:", max_num)
print("Minimum number:", min_num)
# Program to concatenate two lists
list1 = [1, 2, 3]
list2 = [4, 5, 6]
concatenated_list = list1 + list2
print("Concatenated list:", concatenated_list)
5 '''Write a program to demonstrate working with dictionaries in python.'''
dict1 = {'StdNo':'532','StuName': 'Naveen', 'StuAge': 21, 'StuCity': 'Hyderabad'}
print("\n Dictionary is :",dict1)
#Accessing specific values
print("\n Student Name is :",dict1['StuName'])
print("\n Student City is :",dict1['StuCity'])
#Display all Keys
print("\n All Keys in Dictionary ")
for x in dict1:
print(x)
#Display all values
print("\n All Values in Dictionary ")
for x in dict1:
print(dict1[x])
#Adding items
dict1["Phno"]=85457854
#Updated dictionary
print("\n Uadated Dictionary is :",dict1)
#Change values
dict1["StuName"]="Madhu"
#Updated dictoinary
print("\n Updated Dictionary is :",dict1)
#Removing Items
dict1.pop("StuAge");
#Updated dictoinary
print("\n Updated Dictionary is :",dict1)
#Length of Dictionary
print("Length of Dictionary is :",len(dict1))
#Copy a Dictionary
dict2=dict1.copy()
#New dictoinary
print("\n New Dictionary is :",dict2)
#empties the dictionary
dict1.clear()
print("\n Uadated Dictionary is :",dict1)
6. Illustrate various operations that can be performed with tuples in Python, including creation,
access, slicing, concatenation, repetition, and more.
# Creating a tuple
my_tuple = (1, 2, 3, "apple", "banana")
print("Tuple:", my_tuple)
# Accessing elements in a tuple
my_tuple = (10, 20, 30, 40, 50)
print("First element:", my_tuple[0])
print("Last element:", my_tuple[-1])
# Slicing a tuple
my_tuple = (1, 2, 3, 4, 5, 6)
sliced_tuple = my_tuple[2:5] # Get elements from index 2 to 4
print("Sliced Tuple:", sliced_tuple)
# Concatenating tuples
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
concatenated_tuple = tuple1 + tuple2
print("Concatenated Tuple:", concatenated_tuple)
# Repeating a tuple
my_tuple = ("Hello",)
repeated_tuple = my_tuple * 3
print("Repeated Tuple:", repeated_tuple)
# Finding length of a tuple
my_tuple = (1, 2, 3, 4, 5)
length = len(my_tuple)
print("Length of the tuple:", length)
# Checking for an element in a tuple
my_tuple = (1, 2, 3, 4, 5)
exists = 3 in my_tuple
print("Is 3 in the tuple?", exists)
# Iterating through a tuple
my_tuple = ('apple', 'banana', 'cherry')
print("Fruits in the tuple:")
for fruit in my_tuple:
print(fruit)
# Nested tuples
nested_tuple = (('a', 'b'), (1, 2, 3), ('apple', 'banana'))
print("Nested Tuple:", nested_tuple)
7. illustrate various operations that can be performed with sets in Python, including creation, adding
and removing elements, set operations (union, intersection, difference, symmetric difference)
# Creating a set
my_set = {1, 2, 3, 4, 5}
print("Set:", my_set)
# Adding elements to a set
my_set = {1, 2, 3}
my_set.add(4) # Add 4 to the set
print("Set after adding an element:", my_set)
# Removing elements from a set
my_set = {1, 2, 3, 4, 5}
my_set.remove(3) # Remove 3 from the set
print("Set after removing an element:", my_set)
# Union of two sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2) # Or use set1 | set2
print("Union of sets:", union_set)
# Intersection of two sets
set1 = {1, 2, 3}
set2 = {2, 3, 4}
intersection_set = set1.intersection(set2) # Or use set1 & set2
print("Intersection of sets:", intersection_set)
# Difference of two sets
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5}
difference_set = set1.difference(set2) # Or use set1 - set2
print("Difference of sets:", difference_set)
# Symmetric difference of two sets
set1 = {1, 2, 3}
set2 = {2, 3, 4}
symmetric_difference_set = set1.symmetric_difference(set2) # Or use set1 ^ set2
print("Symmetric difference of sets:", symmetric_difference_set)
# Checking for membership in a set
my_set = {1, 2, 3, 4, 5}
is_present = 3 in my_set
print("Is 3 in the set?", is_present)
# Clearing a set
my_set = {1, 2, 3, 4, 5}
my_set.clear()
print("Set after clearing:", my_set)