Exercise: Python

Questions for: Variables

What is the output of the following code:
my_tuple = (1, 2, 3)
my_tuple[0] = 4
print(my_tuple)
A:
(1, 2, 3)
B:
(4, 2, 3)
C:
An error is raised
D:
None of the above
Answer: C

In the given code, the tuple my_tuple is created with the values (1, 2, 3). However, the subsequent attempt to modify the first element of the tuple using the assignment my_tuple[0] = 4 will result in an error.

Tuples in Python are immutable, meaning their elements cannot be modified after the tuple is created. Therefore, the code will raise a TypeError when trying to modify the tuple.

The output of the code will be:

TypeError: 'tuple' object does not support item assignment
What is the output of the following code:
my_dict = {"a": 1, "b": 2}
del my_dict["c"]
print(my_dict)
A:
{"a": 1, "b": 2}
B:
{"a": 1, "b": 2, "c": None}
C:
{"a": 1, "b": 2, "c": undefined}
D:
An error is raised
Answer: D

The given code attempts to delete the key "c" from the dictionary my_dict.

However, since "c" is not a valid key in the dictionary, the del my_dict["c"] operation will raise a KeyError.

Therefore, the output of the code will be a KeyError:

Traceback (most recent call last):
  File "example.py", line 2, in 
    del my_dict["c"]
KeyError: 'c'
What is the output of the following code:
my_list = [1, 2, 3]
my_list.append([4, 5])
print(my_list)
A:
[1, 2, 3]
B:
[1, 2, 3, 4, 5]
C:
[1, 2, 3, [4, 5]]
D:
An error is raised
Answer: C

In this code, a list my_list is defined with the values [1, 2, 3].

The second line appends the list [4, 5] to my_list.

When the modified list is printed to the console, the output is [1, 2, 3, [4, 5]].

Which of the following is a valid way to delete a key-value pair from a dictionary?
A:
my_dict.delete("key")
B:
del my_dict["key"]
C:
my_dict.del("key")
D:
All of the above are valid
Answer: B

The del keyword can be used to remove a key-value pair from a dictionary. To delete the pair with key "key", you can use the syntax del my_dict["key"].

Other methods for removing key-value pairs from a dictionary include using the pop() method or the clear() method.

1. Using the pop() method:

my_dict.pop("key")

This method removes the key and returns its value. If the key is not found, a specified default value is returned, or a KeyError is raised.

2. Using the clear() method:

my_dict = {"a": 1, "b": 2, "c": 3}
my_dict.clear()
print(my_dict)  # Output: {}

It does not delete individual key-value pairs, but rather empties the entire dictionary.

What is the output of the following code:
my_string = "hello world"
print(my_string[1:8:2])
A:
eoo
B:
el o
C:
elwrd
D:
hlowr
Answer: B

In this code, a string my_string is defined with the value "hello world".

The second line uses slice notation to select every second character from the substring starting at index 1 and ending at index 8.

The expression my_string[1:8:2] selects the characters with the following indices: 1, 3, 5, and 7, which correspond to "el o" in the string "hello world".

The resulting substring is "el o", which is then printed to the console.

Ad Slot (Above Pagination)
Quiz