8.
Declare, Access, and Print a Dictionary
Code:
python
Copy code
# Declare a dictionary
my_dict = {"name": "John", "age": 25, "city": "New York"}
# Access and print the dictionary
print("The dictionary is:", my_dict)
# Accessing specific elements
print("Name:", my_dict["name"])
print("Age:", my_dict["age"])
Output:
csharp
Copy code
The dictionary is: {'name': 'John', 'age': 25, 'city': 'New York'}
Name: John
Age: 25
9. Check if a Key Exists in a Dictionary
Code:
python
Copy code
# Declare a dictionary
my_dict = {"name": "John", "age": 25, "city": "New York"}
# Key to check
key_to_check = "age"
# Check if the key exists
if key_to_check in my_dict:
print(f"The key '{key_to_check}' exists in the dictionary.")
else:
print(f"The key '{key_to_check}' does not exist in the dictionary.")
Output:
vbnet
Copy code
The key 'age' exists in the dictionary.
10. Declare, Access, and Print a List with 10 Elements
Code:
python
Copy code
# Declare a list with 10 elements
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Print the list
print("The list is:", my_list)
# Accessing specific elements
print("First element:", my_list[0])
print("Last element:", my_list[-1])
Output:
yaml
Copy code
The list is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
First element: 1
Last element: 10
11. Declare, Access, and Print a Tuple
Code:
python
Copy code
# Declare a tuple
my_tuple = (10, 20, 30, 40, 50)
# Print the tuple
print("The tuple is:", my_tuple)
# Accessing specific elements
print("First element:", my_tuple[0])
print("Last element:", my_tuple[-1])
Output:
yaml
Copy code
The tuple is: (10, 20, 30, 40, 50)
First element: 10
Last element: 50
12. Declare, Access, and Print a Set of Values
Code:
python
Copy code
# Declare a set
my_set = {1, 2, 3, 4, 5}
# Print the set
print("The set is:", my_set)
# Accessing elements (sets are unordered, so you can't use an index)
print("Checking if 3 is in the set:", 3 in my_set)
print("Checking if 6 is in the set:", 6 in my_set)
Output:
python
Copy code
The set is: {1, 2, 3, 4, 5}
Checking if 3 is in the set: True