Python Basics
Python Basics - Important Points
| 16. | What is the output of the following code? x = 5 print("x =", x) |
|---|
A. x = 5
B. x
C. 5
D. x, 5
View Answer Discuss Work SpaceAnswer: option a
Explanation:
The print() function outputs the value of the variable x along with the text "x =".
| 17. | What is the output of the following code? x = "Hello World" print(x[4]) |
|---|
A. H
B. e
C. l
D. o
View Answer Discuss Work SpaceAnswer: option d
Explanation:
Python uses zero-based indexing for strings, so x[4] is the 5th character of the string, which is "o".
| 18. | Which of the following is the correct syntax for a for loop in Python? |
|---|
A. for i in range(10)
B. for i = 1 to 10
C. for i in (1, 10)
D. for i in [1, 2, 3, 4, 5]
View Answer Discuss Work SpaceAnswer: option a
Explanation:
| 19. | What is the result of the following code? x = ["apple", "banana", "cherry"] print(len(x)) |
|---|
A. 3
B. 5
C. 6
D. p
View Answer Discuss Work SpaceAnswer: option a
Explanation:
The len() function returns the number of items in a list, which is 3 in this case.
| 20. | What is the output of the following code? x = 5 y = "10" print(x + y) |
|---|
A. 15
B. 510
C. TypeError
D. None of the above
View Answer Discuss Work SpaceAnswer: option c
Explanation:
Python does not allow adding a number and a string together using the + operator, so a TypeError will be raised.