Untitled 2
Untitled 2
Tuples Methods
→ Consider the following tuple
a = (1, 7, 2)
1. Count():
→ This method will count the number of elements that occur in the given tuple.
a = (1, 7, 2)
print(a.count(1))
2. Index():
→ This method will return the index of the first occurrence of an element in the
given tuple.
a = (1, 7, 2)
print(a.index(1))
Untitled 1
numbers = (10, 20, 30, 40, 50)
print(numbers[0]) # Output: 10
print(numbers[2]) # Output: 30
print(numbers[4]) # Output: 50
2. Given a tuple a = (4, 2, 3, 4, 5) , use the count() method to find how many times
the number 4 appears in the tuple.
a = (4, 2, 3, 4, 5)
print(a.count(4)) # Output: 2
3. Create a tuple of five colors and find the index of "blue" using the index()
method.
4. Write a program that creates a tuple of names ("Alice", "Bob", "Charlie") and
prints the index of "Charlie".
5. Create a tuple with some repeated elements, then use count() to find how
many times a specified element appears in the tuple.
Untitled 2
Mixed Practice Questions (Lists and Tuples)
1. Create a list of five numbers and convert it to a tuple. Then, use count() to
check if any number appears more than once.
numbers_list = [1, 2, 3, 2, 5]
numbers_tuple = tuple(numbers_list)
print(numbers_tuple.count(2)) # Output: 2
2. Create a tuple of colors, convert it to a list, add a new color, then convert
it back to a tuple and print it.
3. Create a list of names ["Alice", "Bob", "Eve"] . Convert it to a tuple and find the
index of "Eve".
DICTIONARIES IN PYTHON
→ Dictionary is a collection of key-value pairs.
→ Dictionaries are ordered collection of data items. They store multiple items in
a single variable. Dictionary items are key-value pairs that are separated by
commas and enclosed within curly brackets {}.
Untitled 3
#SYNTAX:
Example:
#Output
Karan
True
#Output
dict_values(['Karan', 19, True])
Untitled 4
We can print all the keys in the dictionary using keys() method.
#Output
dict_keys(['name', 'age', 'eligible'])
#Output
dict_items([('name', 'Karan'), ('age', 19), ('eligible', True)])
Dictionary Methods
The dictionary uses several built-in methods for manipulation. They are listed
below:
update()
The update() method updates the value of the key provided to it if the item
already exists in the dictionary, else it creates a new key-value pair.
Example:
Output:
Untitled 5
{'name': 'Karan', 'age': 19, 'eligible': True}
{'name': 'Karan', 'age': 20, 'eligible': True, 'DOB': 2001}
clear():
The clear() method removes all the items from the list.
Example:
Output:
{}
pop():
The pop() method removes the key-value pair whose key is passed as a
parameter.
Example:
Output:
Untitled 6
popitem():
The popitem() method removes the last key-value pair from the dictionary.
Example:
Output:
del:
We can also use the del keyword to remove a dictionary item.
Example:
Output:
If the key is not provided, then the del keyword will delete the dictionary
entirely.
Example:
Output:
Untitled 7
NameError: name 'info' is not defined
python
Copy code
book = {"title": "To Kill a Mockingbird", "author": "Harper Lee", "year": 1
960}
print(book["title"]) # Output: To Kill a Mockingbird
2. Given a dictionary person = {'name': 'Alice', 'age': 25, 'city': 'New York'} , use the get()
python
Copy code
person = {'name': 'Alice', 'age': 25, 'city': 'New York'}
print(person.get("city")) # Output: New York
3. Create a dictionary grades = {"Math": 90, "Science": 85, "English": 88} and use the
values() method to print all the grades.
python
Copy code
grades = {"Math": 90, "Science": 85, "English": 88}
print(grades.values()) # Output: dict_values([90, 85, 88])
python
Copy code
Untitled 8
student = {"name": "John", "age": 16, "grade": "10th"}
student.update({"school": "High School"})
print(student) # Output: {'name': 'John', 'age': 16, 'grade': '10th', 'schoo
l': 'High School'}
5. Given a dictionary info = {'name': 'Karan', 'age': 19, 'eligible': True} , remove the key
"eligible" using the pop() method and print the modified dictionary.
python
Copy code
info = {'name': 'Karan', 'age': 19, 'eligible': True}
info.pop("eligible")
print(info) # Output: {'name': 'Karan', 'age': 19}
6. Create a dictionary employee = {'name': 'Alex', 'position': 'Manager', 'salary': 50000} . Use
the items() method to print all key-value pairs in the dictionary.
python
Copy code
employee = {'name': 'Alex', 'position': 'Manager', 'salary': 50000}
print(employee.items()) # Output: dict_items([('name', 'Alex'), ('positio
n', 'Manager'), ('salary', 50000)])
Assignment Questions
1. Create a dictionary car with keys "brand", "model", and "year". Then
update the "year" to the current year (e.g., 2023) and print the updated
dictionary.
2. Given a dictionary product = {'name': 'Laptop', 'price': 800, 'stock': 50} , use the popitem()
method to remove the last key-value pair and print the modified dictionary.
Untitled 9
using the update() method.
5. Create a dictionary scores = {"Math": 95, "Science": 80, "History": 88} . Use the del
keyword to delete the "Science" key, then print the updated dictionary.
6. Given a dictionary inventory = {'apples': 30, 'bananas': 50, 'oranges': 20} , clear all items
using the clear() method and print the result.
python
Copy code
car = {"brand": "Toyota", "model": "Camry", "year": 2015}
car.update({"year": 2023})
print(car) # Output: {'brand': 'Toyota', 'model': 'Camry', 'year': 2023}
2. Given a dictionary product = {'name': 'Laptop', 'price': 800, 'stock': 50} , use the popitem()
method to remove the last key-value pair and print the modified
dictionary.
python
Copy code
product = {'name': 'Laptop', 'price': 800, 'stock': 50}
product.popitem()
print(product) # Output: {'name': 'Laptop', 'price': 800}
python
Copy code
Untitled 10
profile = {'username': 'john_doe', 'email': '[email protected]', 'age': 3
0}
print(profile.keys()) # Output: dict_keys(['username', 'email', 'age'])
'Paris', 'Japan': 'Tokyo'} . Add a new country "India" with its capital "New Delhi"
using the update() method.
python
Copy code
countries = {'USA': 'Washington, D.C.', 'France': 'Paris', 'Japan': 'Toky
o'}
countries.update({"India": "New Delhi"})
print(countries) # Output: {'USA': 'Washington, D.C.', 'France': 'Paris', 'J
apan': 'Tokyo', 'India': 'New Delhi'}
5. Create a dictionary scores = {"Math": 95, "Science": 80, "History": 88} . Use the del
keyword to delete the "Science" key, then print the updated dictionary.
python
Copy code
scores = {"Math": 95, "Science": 80, "History": 88}
del scores["Science"]
print(scores) # Output: {'Math': 95, 'History': 88}
6. Given a dictionary inventory = {'apples': 30, 'bananas': 50, 'oranges': 20} , clear all items
using the clear() method and print the result.
python
Copy code
inventory = {'apples': 30, 'bananas': 50, 'oranges': 20}
inventory.clear()
print(inventory) # Output: {}
Untitled 11