Prem’s Python Lab Programs
G. Pullaiah College of Engineering and Technology (Autonomous)
Department of Computer Science and Engineering
Python Programming (Skill Enhancement Course R23)
Unit – 3
# 13.Write a program to create tuples (name, age, address, college)
# for at least two members and concatenate the tuples and print
# the concatenated tuples.
# Define tuples for two members
member1 = ("Prem", 21, "123 Bhagyanagar, Kurnool", "GPCET")
member2 = ("Kumar", 22, "456 Venkayapalli, Kurnool", "GPCET")
# Concatenate the tuples
concatenated_tuple = member1 + member2
# Print the concatenated tuple
print("Concatenated Tuple:")
print(concatenated_tuple)
'''OUTPUT:
Concatenated Tuple:
('Prem', 21, '123 Bhagyanagar, Kurnool', 'GPCET', 'Kumar', 22, '456 SBI circle, Kurnool',
'GPCET')
'''
#14. Write a program to count the number of vowels in a string (No control
flow allowed).
# Input: A string from the user
input_string = input("Enter a string: ")
# Define vowels
vowels = "aeiouAEIOU"
# Count the number of vowels using a list comprehension and sum
vowel_count = sum([1 for char in input_string if char in vowels])
# Display the number of vowels
print(f"The number of vowels in the string is: {vowel_count}")
Prem’s Python Lab Programs
'''OUTPUT:
Enter a string: Prem Kumar
The number of vowels in the string is: 3
'''
# 15. Write a program to check if a given key exists in a dictionary or not.
# Initialize a dictionary
my_dict = {
'name': 'Prem',
'age': 23,
'address': '123, Bhagyanagar',
'college': 'G.Pullaiah College of Engineering'
}
# Input: Key to check
key_to_check = input("Enter the key to check: ")
# Check if the key exists in the dictionary
if key_to_check in my_dict:
print(f"The key '{key_to_check}' exists in the dictionary.")
else:
print(f"The key '{key_to_check}' does not exist in the dictionary.")
'''OUTPUT:
Enter the key to check: name
The key 'name' exists in the dictionary.
'''
# 16.Write a program to add a new key-value pair to an existing dictionary.
# Initialize an existing dictionary
my_dict = {
'name': 'Prem',
'age': 21,
'address': '123 Bhagyanagar, Kurnool'
}
# Input: New key and value to add
new_key = input("Enter the new key: ")
new_value = input("Enter the new value: ")
Prem’s Python Lab Programs
# Add the new key-value pair to the dictionary
my_dict[new_key] = new_value
# Display the updated dictionary
print("Updated dictionary:")
print(my_dict)
'''OUTPUT:
Enter the new key: email
Enter the new value: [email protected]
Updated dictionary:
{'name': 'Prem', 'age': 21, 'address': '123 Bhagyanagar, Kurnool', 'email': '[email protected]'}
'''
# 17.Write a program to sum all the items in a given dictionary
# Initialize a dictionary with numeric values
my_dict = {
'a': 10,
'b': 20,
'c': 30,
'd': 40
}
# Calculate the sum of all values in the dictionary
total_sum = sum(my_dict.values())
# Display the total sum
print(f"The sum of all items in the dictionary is: {total_sum}")
'''OUTPUT:
The sum of all items in the dictionary is: 100
'''