Python code examples for Beginners
Python code examples for Beginners - Important Points
1. | What is the output of the following code snippet? print("My name is", "John", "and I am", 25, "years old.") |
---|
A. My name is John and I am 25 years old.
B. My name is and I am years old.
C. John 25
D. None of the above
View Answer Discuss Work SpaceAnswer: option a
Explanation:
2. | What is the output of the following code snippet? print("5 + 5 =", 5 + 5) |
---|
A. 10
B. 5 + 5 = 10
C. 5 + 5
D. 5 + 5 =10
View Answer Discuss Work SpaceAnswer: option b
Explanation:
3. | What is the output of the following code snippet? x = 5y = 7print("The sum of", x, "and", y, "is", x + y) |
---|
A. The sum of 5 and 7 is 12
B. The sum of x and y is x + y
C. The sum of and is
D. The sum of x and y "is" x + y
View Answer Discuss Work SpaceAnswer: option a
Explanation:
4. | What is the output of the following code snippet? my_list = [1, 2, 3, 4] print(my_list[2]) |
---|
A. 1
B. 2
C. 3
D. 4
View Answer Discuss Work SpaceAnswer: option c
Explanation:
Lists are indexed starting from 0 in Python. In this case, my_list[2] will access the element at index 2 (the third element), which is the integer 3.
5. | What is the output of the following code snippet? my_tuple = (1, 2, 3, 4) print(my_tuple[1:3]) |
---|
A. (1, 2)
B. (2, 3)
C. (2, 3, 4)
D. (1, 2, 3)
View Answer Discuss Work SpaceAnswer: option b
Explanation:
Tuples are also indexed starting from 0 in Python. In this case, my_tuple[1:3] will access the elements at index 1 and index 2 (the second and third elements), which are the integers 2 and 3.