# Chapter 10 Tuples and Dictionaries in Python NCERT Solution

Share with others

## Tuples and Dictionaries in Python

```Tuples and Dictionaries in Python

Tuples are immutable sequences, i.e., we cannot change the elements of a tuple once it is created.

Elements of a tuple are put in round brackets separated by commas.

If a sequence has comma separated elements without parentheses, it is also treated as a tuple.

Tuples are ordered sequences as each element has a fixed position.

Indexing is used to access the elements of the tuple; two way indexing holds in dictionaries as
in strings and lists.

Operator ‘+’ adds one sequence (string, list, tuple) to the end of other.

Operator ‘*’ repeats a sequence (string, list, tuple) by specified number of times

Membership operator ‘in’ tells if an element is present in the sequence or not and ‘not in’ does the opposite.

Tuple manipulation functions are: len(), tuple(), count(), index(), sorted(), min(), max(),sum().

Dictionary is a mapping (non-scalar) data type. It is an unordered collection of key-value pair; key value pair are put inside curly braces.

Each key is separated from its value by a colon.

Keys are unique and act as the index.

Keys are of immutable type but values can be mutable

Tuples and Dictionaries in Python```

## vii. print(stateCapital.get(“Assam”))

NOTE : Part viii will return an error as there is a space between Andhra Pradesh in question and there is no such key in given dictionary. I removed the space and written output accordingly

## Q4. With the help of an example show how can you return more than one value from a function.

```Show AnswerAns.

def multivalue(a, b, c):
a = a + 2
b = b + 3
c = c + 4
return (a, b, c)
p = 8
q = 9
r = 10
p, q, r = multivalue(p, q, r)
print(p)
print(q)
print(r)

OUTPUT:

10
12
14```

## tuple1 = (5) #statement 1len(tuple1) #statement 2

PROGRAMMING PROBLEMS

## Q1. Write a program to read email IDs of n number of students and store them in a tuple. Create two new tuples, one to store only the usernames from the email IDs and second to store domain names from the email ids. Print all three tuples at the end of theprogram. [Hint: You may use the function split()]

```Show AnswerAns.

n=int(input("Enter how many email id to enter  : "))
t1=()
un = ()
dom = ()
for i in range(n):
em = input("Enter email id  : ")
t1 = t1 + (em,)
for i in t1:
a = i.split('@')
un = un + (a,)
dom = dom + (a,)
print("Original Email id's are  : " , t1)
print("Username are  : " , un)
print("Doman name are  : " , dom)

OUTPUT :

Enter how many email id to enter  : 2
Enter email id  : csip@gmail.com
Enter email id  : bike@yahoo.com
Original Email id's are  :  ('csip@gmail.com', 'bike@yahoo.com')
Doman name are  :  ('gmail.com', 'yahoo.com')```

## We can accomplish these by:(a) writing a user defined function(b) using the built-in function

```Show AnswerAns. a)

def searchname(t1,nm):
for a in t1:
if a == nm:
print(nm , " Name is present in Tuple")
return
n = int(input("How many names you want to enter  : "))
name = ()
for i in range(n):
nm = input("Enter name  : ")
name = name + (nm,)
nm = input("Enter name to search  : ")
searchname(name, nm)

OUTPUT

How many names you want to enter  : 3
Enter name  : Amit
Enter name  : Anuj
Enter name  : Ashish
Enter name to search  : Anuj
Anuj  Name is present in Tuple

Ans b)

n = int(input("How many names you want to enter  : "))
name = ()
for i in range(n):
nm = input("Enter name  : ")
name = name + (nm,)
nm = input("Enter name to search  : ")
if nm in name:
print(nm," Name is present in Tuple")
else:
print(nm," Name is not present in Tuple")

OUTPUT:

How many names you want to enter  : 3
Enter name  : Amit
Enter name  : Anuj
Enter name  : Ashu
Enter name to search  : Ashu
Ashu  Name is present in Tuple```

## Q3. Write a Python program to find the highest 2 values in a dictionary.

```Show AnswerAns.

D = {'A' : 23, 'B' : 56, 'C' : 29, 'D' : 42, 'E' : 78 }
val = [ ]
v = D.values()
for i in v:
val.append(i)
val.sort()
print("Two maximum values are")
print(val[-1])
print(val[-2])

OUTPUT:

Two maximum values are
78
56
```

## Q4. Write a Python program to create a dictionary from a string.Note: Track the count of the letters from the string.Sample string : ‘w3resource’Expected output : {‘3’: 1, ‘s’: 1, ‘r’: 2, ‘u’: 1, ‘w’: 1, ‘c’: 1,’e’: 2, ‘o’: 1}

```Show AnswerAns.

str = input("Enter any String")
D = { }
for i in str:
if i not in D:
D[ i ] = str.count(i)
print(D)

OUTPUT :

Enter any String  : amitamit
{'a' : 2, 'm' : 2, 'i' : 2, 't' : 2}```

## Tuples and Dictionaries in Python

```Show AnswerAns.

friends = { }
while True:
print("2. Display all ")
print("3. Delete any contact")
print("4. Modify/Change Phone Number")
print("5. Check if a friend is present or not")
print("6. Display in sorted order of names")
print("7. Exit")
ch = int(input("Enter your choice  : "))
if ch == 1 :
nm = input("Enter name  : ")
ph = input("Enter phone number  : ")
friends[ph] = nm
elif ch == 2 :
for i in friends :
print("Name  : " , friends[i])
print("Contact number : ", i)
elif ch == 3 :
c = input("Enter the phone number to delete")
if c in friends:
del friends[c]
else:
print("No Record found")
elif ch == 4:
p = input("Enter the contact number to be modified")
if p in friends:
n = input("Enter the new name")
friends[p] = n
print("Record Modified")
else:
print("No Record Found")
elif ch == 5:
ph = input("Enter the contact number to check")
if ph in friends:
print("Record Present")
else :
elif ch == 6:
for i in sorted(friends.values()):
print(i)
elif ch == 7:
break
else:
print("Enter Value between 0 and 7")```

## Tuples and Dictionaries in Python

Disclaimer : I tried to give you the correct “Tuples and Dictionaries in Python Chapter 10” , but if you feel that there is/are mistakes in the “Tuples and Dictionaries in Python Chapter 10 given above, you can directly contact me at csiplearninghub@gmail.com.

## Tuples and Dictionaries in Python

Class 12 Computer Science Sample Paper 2020-2021.

Class 12 Computer Science Sample Paper Marking Scheme

Class 12 Computer Science Test Series

Share with others