WEEK- III PP
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.
CODE:
Method I: (Simple tuple
concatenation using +)
# Program to create tuples and concatenate them
# Creating tuples for two members
member1 =
("John", 21, "Hyderabad", "OU")
member2 =
("Rohit", 22, "Delhi", "DU")
# Concatenating the tuples
concatenated =
member1 + member2
# Printing the concatenated tuple
print("Concatenated
Tuple:", concatenated)
OUTPUT: ----------------------------- Try on your own👏-----------------------------
Method II: (Using tuple() and
list conversion)
member1 =
("Richard", 21, "Hyderabad", "OU")
member2 =
("Rohit", 22, "Delhi", "DU")
concatenated = tuple(list(member1)
+ list(member2))
print("Concatenated
Tuple:", concatenated)
OUTPUT: ----------------------------- Try on your own👏-----------------------------
2.
Write a program to count the number of vowels in a string (No control flow allowed).
CODE:
Method I :( Using count ())
# Program to
count vowels in a string (no control flow)
s = "Education’s purpose is to replace an empty mind with an
open one"
vowels = "aeiouAEIOU"
# Count vowels
by summing occurrences of each vowel
count = sum(s.count(v) for v in vowels)
print("Number of vowels:", count)
OUTPUT: ----------------------------- Try on your own👏-----------------------------
Method II: (Using filter ()
and len())
# Program to
count vowels in a string (no control flow)
s = "Education’s purpose is to replace an empty mind with an
open one"
vowels = "aeiouAEIOU"
# Filter vowels
and count them
count = len(list(filter(lambda ch: ch in vowels, s)))
print("Number of vowels:", count)
OUTPUT: ----------------------------- Try on your own👏-----------------------------
3. Write a program to check if a given key exists in a
dictionary or not.
CODE:
Method I (Using in operator)
# Program to
check if a given key exists in a dictionary
my_dict = {"name": "Richard", "age":
21, "college": "OU"}
key = "age"
if key in my_dict:
print(f"Key '{key}' exists in the
dictionary")
else:
print(f"Key '{key}' does not exist in the
dictionary")
Method II-(Using get ()
method)
# Program to
check if a given key exists in a dictionary
my_dict = {"name": "Alxa", "age": 21,
"college": "OU"}
key = "address"
if my_dict.get(key) is not None:
print(f"Key '{key}' exists in the
dictionary")
else:
print(f"Key '{key}' does not exist in the dictionary")
OUTPUT: ----------------------------- Try on your own👏-----------------------------
4. Write a program to add a new key-value pair to an
existing dictionary.
CODE:
Method I: (Direct Assignment)
# Program to add
a new key-value pair to a dictionary
my_dict = {"name": "kevin", "age": 21}
# Adding a new
key-value pair
my_dict["college"] = "OU"
print("Updated Dictionary:", my_dict)
OUTPUT: ----------------------------- Try on your own👏-----------------------------
Method II: (Using update()
Method)
# Program to add
a new key-value pair to a dictionary
my_dict = {"name": "Richard", "age":
22}
# Adding a new
key-value pair
my_dict.update({"college": "DU"})
print("Updated Dictionary:", my_dict)
OUTPUT: ----------------------------- Try on your own👏-----------------------------
5. Write a program to sum all the items in a given
dictionary.
CODE:
Method I: (Using sum() and
values())
# Program to sum
all values in a dictionary
my_dict = {"a": 100, "b": 200, "c":
300}
total = sum(my_dict.values())
print("Sum of dictionary values:", total)
OUTPUT: ----------------------------- Try on your own👏-----------------------------
Method II: (Using Loop)
# Program to sum
all values in a dictionary
my_dict = {"x": 100, "y": 500, "z": 700}
total = 0
for value in my_dict.values():
total += value
print("Sum of dictionary values:", total)
OUTPUT: ----------------------------- Try on your own👏-----------------------------
***************************************
No comments:
Post a Comment