0% found this document useful (0 votes)
10 views6 pages

Week-3 Lab Programs

Uploaded by

yakshitkanna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Week-3 Lab Programs

Uploaded by

yakshitkanna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Week-3 (python programming)

13.
Experiment:
Write a program to create tuples (name, age, address, college) for at least two members and concatenate
the tuples and print the concatenated tuples
Aim:
To create tuples (name, age, address, college) for at least two members and concatenate the tuples and
print the concatenated tuples
Description:
Creating Tuple

 Tuples are used to store multiple items in a single variable


 A tuple is a collection which is ordered and unchangeable.
 Tuples are written with round brackets.
 In Python, tuples are created by placing a sequence of values separated by ‘comma’
 For concatenate of tuples we will is plus (+) operator
 Tuples can contain any number of elements and of any data type (like strings, integers, list, etc.).
 Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3
are List, Set, and Dictionary, all with different qualities and usage.
Accessing elements of Tuple
 In order to access the tuple items refer to the index number.
 Use the index operator [ ] to access an item in a tuple. The index must be an integer.
 Nested tuples are accessed using nested indexing.
Program:
# Creating two tuples for two members with (name, age, address, college)

member1 = ("John ", 21, "1234 Bangalore, Springfield", "JNTUA University")


member2 = ("Smith", 22, "5678 hyderbad, amreerpt", "Loyala College")

concatenated_tuple = member1 + member2 # Concatenating the tuples

print("Concatenated Tuple:", concatenated_tuple) # Printing the concatenated tuple

Output:
Concatenated Tuple: ('John ', 21, '1234 Bangalore, Springfield', 'JNTUA University', 'Smith', 22, '5678

hyderbad, amreerpt', 'Loyala College')


14.
Experiment:
Write a program to count the number of vowels in a string (No control flow allowed)
Aim:
To count the number of vowels in a string (No control flow allowed)
Description:
 Define one function and store all vowels in one variable
 The map() function is used to apply a given function to every item of an iterable, such as
a list or tuple, and returns a map object (which is an iterator)
 Use convert in to lower case using lower() and use sum function and return
Program:
def count_vowels(s):
vowels = 'aeiouAEIOU'
return sum(map([Link]().count, vowels))

text = "Functional programming in Python is fun!"


vowel_count = count_vowels(text)

print("Vowels count is:",vowel_count)

Output:
Vowels count is: 11
15
Experiment:
Write a program to check if a given key exists in a dictionary or not
Aim:
To check if a given key exists in a dictionary or not
Description:
 Dictionary in Python is an unordered collection of data values in the form of key , value pair
 Each key-value pair in a Dictionary is separated by a colon :, whereas each key is separated by a
‘comma’.
 In Python, a Dictionary can be created by placing a sequence of elements within curly {} brace.
 Values in a dictionary can be of any datatype and can be duplicated, whereas keys can’t be repeated
and must be immutable.
 Dictionary can also be created by the built-in function dict().
Note – Dictionary keys are case sensitive, same name but different cases of Key will be treated distinctly
Program:

dic = {}

n = int(input("Enter number of key value pairs to be added:"))

for i in range(0,n):
key = input("Enter any key:")
value = int(input("Enter any value:"))
dic[key] = value

key_exists = input("Enter key element to search:")

if key_exists in [Link]():
print("Key existed")
else:
print("Key not Existed")

Output:

Enter number of key value pairs to be added: 5

Enter any key:a


Enter any value:1
Enter any key:b
Enter any value:2
Enter any key:c
Enter any value:3
Enter any key:d
Enter any value:4
Enter any key:e
Enter any value:5

Enter key element to search:c


Key existed
16. Experiment:
Write a program to add a new key-value pair to an existing dictionary
Aim:
To add a new key-value pair to an existing dictionary
Description:
Changing and Adding Dictionary elements
 Dictionaries are mutable. We can add new items or change the value of existing items using an
assignment operator.
 If the key is already present, then the existing value gets updated. In case the key is not present, a new
(key: value) pair is added to the dictionary.
Program:

dic1 = {}
dic2 = {}

n1 = int(input("Enter number of key value pairs to be added in dictionary1:"))

for i in range(0,n1):
key1 = input("Enter any key:")
value1 = int(input("Enter any value:"))
dic1[key1] = value1

print("THE ELEMENTS OF Dictionary 1: ",dic1)

n2 = int(input("Enter number of key value pairs to be added in dictionary1:"))

for i in range(0,n2):
key2 = input("Enter any key:")
value2 = int(input("Enter any value:"))
dic2[key2] = value2

print("THE ELEMENTS OF Dictionary 2: ",dic2)

[Link](dic2)

print("After adding key - value pair to the exisisting dictionary is:")

print(dic1)
Output:
Enter number of key value pairs to be added in dictionary1: 2

Enter any key:CPU


Enter any value:1
Enter any key:MOUSE
Enter any value:2

THE ELEMENTS OF Dictionary 1: {'CPU': 1, 'MOUSE': 2}

Enter number of key value pairs to be added in dictionary1: 2

Enter any key:KEYBOARD


Enter any value:3
Enter any key:RAM
Enter any value:4

THE ELEMENTS OF Dictionary 2: {'KEYBOARD': 3, 'RAM': 4}

After adding key - value pair to the exisisting dictionary is:


{'CPU': 1, 'MOUSE': 2, 'KEYBOARD': 3, 'RAM': 4}

17.
Experiment:
Write a program to sum all the items in a given dictionary
Aim:
To sum all the items in a given dictionary
Description:
Input : {‘a’: 100, ‘b’:200, ‘c’:300}
Output : 600

Input : {‘x’: 25, ‘y’:18, ‘z’:45}


Output : 88
Method : Use the sum function to find the sum of dictionary values.
Program:

def sum_dictionary_items(input_dict):
total_sum = sum(input_dict.values())
return total_sum

# Example dictionary
example_dict = {
'a': 10,
'b': 20,
'c': 30,
'd': 40,
'e': 50
}

# Calculate the sum


result = sum_dictionary_items(example_dict)

# Print the result


print("The sum of all items in the dictionary is:", result)

Output:
The sum of all items in the dictionary is: 150

You might also like