Python Generators & next() Quiz
1. What will be the output? g = (x for x in range(8) if x % 2 != 0) print(next(g) + next(g))
A. 4
B. 8
C. 3
D. 6
2. What will be the output? g = (x for x in range(6) if x > 2) print(next(g) * next(g))
A. 12
B. 20
C. 9
D. 15
3. What will be the output? g = (x for x in range(10) if x % 3 == 0) print(next(g) + next(g))
A. 0
B. 9
C. 6
D. 3
4. What will be the output? g = (x for x in range(5)) a = next(g) b = next(g) print(a * b + next(g))
A. 3
B. 2
C. 0
D. 1
5. What will be the output? g = (x for x in range(1, 8) if x % 2 == 0) print(next(g) + next(g) + next(g))
A. 14
B. 10
C. 12
D. 16
1 comment:
What will be the output?
g = (x for x in range(8) if x % 2 != 0)
print(next(g) + next(g))
The first two generated values are 1 and 3, and their sum is 4.
What will be the output?
g = (x for x in range(6) if x > 2)
print(next(g) * next(g))
The first two generated values are 3 and 4, and 3 × 4 equals 12.
What will be the output?
g = (x for x in range(10) if x % 3 == 0)
print(next(g) + next(g))
The first two generated values are 0 and 3, and their sum is 3.
What will be the output?
g = (x for x in range(5))
a = next(g)
b = next(g)
print(a * b + next(g))
The first three generated values are 0, 1, and 2; therefore 0 × 1 + 2 equals 2.
What will be the output?
g = (x for x in range(1, 8) if x % 2 == 0)
print(next(g) + next(g) + next(g))
The generator produces 2, 4, and 6 first, and 2 + 4 + 6 equals 12.
Post a Comment