Python code examples for Beginners
Python code examples for Beginners - Important Points
6. | What is the output of the following code snippet? my_dict = {"apple": 1, "banana": 2, "orange": 3} |
---|
A. apple
B. 1
C. banana
D. 2
View Answer Discuss Work SpaceAnswer: option d
Explanation:
Dictionaries in Python are composed of key-value pairs. In this case, we access the value of the "banana" key in the dictionary my_dict, which is the integer 2.
7. | What is the output of the following code snippet? my_string = "Hello, World!" print(my_string.upper()) |
---|
A. hello, world!
B. Hello World!
C. HELLO, WORLD!
D. HELLO, WORLD!
View Answer Discuss Work SpaceAnswer: option c
Explanation:
The upper() method is used to convert all characters in a string to uppercase. In this case, it will convert the string "Hello, World!" to "HELLO, WORLD!".
8. | What is the output of the following code snippet? my_list = [1, 2, 3] my_list.append(4) print(my_list) |
---|
A. [1, 2, 3, 4]
B. [1, 2, 4, 3]
C. [4, 3, 2, 1]
D. None of the above
View Answer Discuss Work SpaceAnswer: option a
Explanation:
The append() method is used to add an element to the end of a list. In this case, it will add the integer 4 to the end of the list my_list.
9. | What is the output of the following code snippet? my_list = [1, 2, 3] my_list.pop() print(my_list) |
---|
A. [1, 2]
B. [2, 3]
C. [1, 3]
D. None of the above
View Answer Discuss Work SpaceAnswer: option a
Explanation:
The pop() method is used to remove and return the last element of a list. In this case, it will remove the integer 3 from the end of the list my_list and return it. Therefore, the list my_list will now contain [1, 2].
10. | What is the output of the following code snippet? my_dict = {"apple": 1, "banana": 2, "orange": 3} |
---|
A. 1
B. 2
C. 3
D. None of the above
View Answer Discuss Work SpaceAnswer: option c
Explanation: