UNIT-III
1. Write a program to create tuples (name, age, address, college) for at least two
members and concatenate the tuples and print the concatenated tuples.
# Creating tuples
member1 = ('Saketh', 25, 'Agiripalli', 'NRI College')
member2 = ('Anirudh', 22, 'vijayawada', 'XYZ College')
# Concatenating tuples
concatenated_tuple = member1 + member2
print(concatenated_tuple)
OUTPUT:
2. Write a program to count the number of vowels in a string (No control flow allowed).
def count_vowels_no_control_flow(input_string):
"""
Counts the number of vowels in a string without explicit control flow.
Args:
input_string: The string to analyze.
Returns:
The number of vowels in the string.
"""
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
# The generator expression (1 for char in input_string if char in vowels)
# yields 1 for each character that is a vowel.
# sum() then adds up these 1s to get the total count.
return sum(1 for char in input_string if char in vowels)
# Example usage:
my_string = "Hello World"
vowel_count = count_vowels_no_control_flow(my_string)
print(f"The number of vowels in '{my_string}' is: {vowel_count}")
my_string_2 = "Python Programming"
vowel_count_2 = count_vowels_no_control_flow(my_string_2)
print(f"The number of vowels in '{my_string_2}' is: {vowel_count_2}")
OUTPUT:
3. Write a program to check if a given key exists in a dictionary or not.
# Creating a sample dictionary
sample_dict = {'a': 1, 'b': 2, 'c': 3}
# Checking if a key exists
key_to_check = 'b'
key_exists = key_to_check in sample_dict
print(key_exists)
OUTPUT:
[Link] a program to add a new key-value pair to an existing dictionary.
# Existing dictionary
existing_dict = {'a': 1, 'b': 2}
# Adding a new key-value pair
existing_dict['c'] = 3
print(existing_dict)
OUTPUT:
[Link] a program to sum all the items in a given dictionary.
# Sum of values of a dictionary
# function
def Sum(dic):
#sum variable
sum=0
#iterate through values
for i in [Link]():
sum=sum+i
return sum
#initialisation
dic={ 'x':30, 'y':145, 'z':55 }
print("Dictionary: ", dic)
#print sum
print("sum: ",Sum(dic))
OUTPUT: