Computer Science
What is the output of the following ?
x = 123
for i in x:
print(i)
- 1 2 3
- 123
- Error
- Infinite loop
Python Control Flow
2 Likes
Answer
Error
Reason — The output of the code will be an error because we cannot directly iterate over an integer object in Python using a for loop. The for loop in Python iterates over iterable objects such as lists, tuples, strings, or dictionaries, but not individual integers
Answered By
1 Like
Related Questions
Which of the following Python functions is used to iterate over a sequence of numbers by specifying a numeric end value within its parameters ?
- range()
- len()
- substring()
- random()
What is the output of the following ?
d = {0: 'a', 1: 'b', 2: 'c'} for i in d: print(i)
1.
0 1 2
2.
a b c
3.
0 a 1 b 2 c
4.
2 a 2 b 2 c
Which arithmetic operator(s) cannot be used with strings ?
- +
- *
- -
- All of these
What will be the output when the following code is executed?
>>> strl = "helloworld" >>> strl[ : : -1]
- dlrowolleh
- hello
- world
- helloworld