0% found this document useful (0 votes)
14 views4 pages

Python Unit 3

The document contains several Python programming tasks, including creating and concatenating tuples, counting vowels in a string without control flow, checking for key existence in a dictionary, adding a key-value pair to a dictionary, and summing all items in a dictionary. Each task is accompanied by source code and explanations of how the code works. Sample outputs for each program are also provided.

Uploaded by

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

Python Unit 3

The document contains several Python programming tasks, including creating and concatenating tuples, counting vowels in a string without control flow, checking for key existence in a dictionary, adding a key-value pair to a dictionary, and summing all items in a dictionary. Each task is accompanied by source code and explanations of how the code works. Sample outputs for each program are also provided.

Uploaded by

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

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.

# Define the first person's tuple


person1 = ("Arun", 25, "123 Main St", "IIT Madras")

# Define the second person's tuple


person2 = ("Bhavya", 22, "456 Oak Ave", "Anna University")

# Concatenate the two tuples


concatenated_tuple = person1 + person2

# Print the concatenated tuple


print(concatenated_tuple)

Explanation:
1. person1 = ("Arun", 25, "123 Main St", "IIT Madras"): This line creates the first tuple, storing the name, age,
address, and college for the first person.
2. person2 = ("Bhavya", 22, "456 Oak Ave", "Anna University"): This line creates the second tuple for the second
person.
3. concatenated_tuple = person1 + person2: The + operator joins the person1 and person2 tuples, creating a new
tuple concatenated_tuple that contains all elements from both original tuples in their original order.
4. print(concatenated_tuple): This line displays the resulting concatenated tuple.

O/p:

('Arun', 25, '123 Main St', 'IIT Madras', 'Bhavya', 22, '456 Oak Ave', 'Anna University')

2. Write a program to count the number of vowels in a string (No control flow allowed).

import operator as op

# Sentence

string = "Hulkmaniacs are running wild in the whole world"

# Sentence splitted up into words

wordList = [Link]()

# Defining vowels for uppercase and lowercase

vowels = 'aeiouAEIOU'

# Traversing every word in wordList

for word in wordList:


vowelCount = 0

# Traversing every character of the word

for i in range(0, len(word)):

# If the word contains a vowel then vowelCount adds by 1

if [Link](vowels, word[i]) > 0:

vowelCount += 1

print("The word is", word, "and it contains", vowelCount, "vowels in it")

Output
The word is Hulkmaniacs and it contains 4 vowels in it
The word is are and it contains 2 vowels in it
The word is running and it contains 2 vowels in it
The word is wild and it contains 1 vowels in it
The word is in and it contains 1 vowels in it
The word is the and it contains 1 vowels in it
The word is whole and it contains 2 vowels in it
The word is world and it contains 1 vowels in it

3. Write a program to check if a given key exists in a dictionary or not

1. Declare and initialize a dictionary to have some key-value pairs.


2. Take a key from the user and store it in a variable.
3. Using an if statement and the in operator, check if the key is present in the dictionary using the [Link]()
method.
4. If it is present, print the value of the key.
5. If it isn’t present, display that the key isn’t present in the dictionary.
6. Exit.

Program/Source Code
Here is source code of the Python Program to check if a given key exists in a dictionary or not. The program output
is also shown below.

d={'A':1,'B':2,'C':3}
key=raw_input("Enter key to check:")
if key in [Link]():
print("Key is present and value of the key is:")
print(d[key])
else:
print("Key isn't present!")
Program Explanation
1. User must enter the key to be checked and store it in a variable.
2. An if statement and the in operator is used check if the key is present in the list containing the keys of the
dictionary.
3. If it is present, the value of the key is printed.
4. If it isn’t present, “Key isn’t present!” is printed.
5. Exit.

O/P:

Case 1:
Enter key to check:A
Key is present and value of the key is:
1

Case 2:
Enter key to check:F
Key isn't present!

4. Write a program to add a new key-value pair to an existing dictionary?

[Link] a key-value pair from the user and store it in separate variables.
2. Declare a dictionary and initialize it to an empty dictionary.
3. Use the update() function to add the key-value pair to the dictionary.
4. Print the final dictionary.
5. Exit.

Program/Source Code
Here is source code of the Python Program to add a key-value pair to a dictionary. The program output is also shown
below.
key=int(input("Enter the key (int) to be added:"))
value=int(input("Enter the value for the key to be added:"))
d={}
[Link]({key:value})
print("Updated dictionary is:")
print(d)
Program Explanation
1. User must enter a key-value pair and store it in separate variables.
2. A dictionary is declared and initialized to an empty dictionary.
3. The update() function is used to add the key-value pair to the dictionary.
4. The final dictionary is printed.

O/P
Case 1:
Enter the key (int) to be added:12
Enter the value for the key to be added:34
Updated dictionary is:
{12: 34}

Case 2:
Enter the key (int) to be added:34
Enter the value for the key to be added:29
Updated dictionary is:
{34: 29}
5. Write a program to sum all the items in a given dictionary.

1. Declare and initialize a dictionary to have some key-value pairs.


2. Find the sum of all the values in the dictionary.
3. Print the total sum.
4. Exit.

Program/Source Code
Here is source code of the Python Program to find the sum all the items in a dictionary. The program output is also
shown below.

d={'A':100,'B':540,'C':239}
print("Total sum of values in the dictionary:")
print(sum([Link]()))
Program Explanation
1. The sum() function is used to find the sum of all the values in the dictionary.
2. The total sum of all the values in the dictionary is printed.
3. Exit.

O/P:

Case 1:
Total sum of values in the dictionary:
879

You might also like