ProgrammingVery hardAccenture · Cognizant · Python
What does the following Python 3 code print? lst = [x * x for x in range(5)] gen = (x * x for x in range(5)) print(type(lst), type(gen))
- A. <class 'list'> <class 'generator'>
- B. <class 'list'> <class 'list'>
- C. <class 'list'> <class 'tuple'>
- D. <class 'generator'> <class 'list'>
Correct answer: A. <class 'list'> <class 'generator'>
Square brackets build a list comprehension; parentheses build a generator expression, a distinct lazy type.
More Python Programming questions
- What does the following Python 3 code print? a = [1, 2, 3] b = a b.append(4) print(a)
- What does the following Python 3 code print? a = [1, 2, 3] b = a[:] b.append(4) print(a, b)
- What does the following Python 3 code print? def f(a, b, args): return a, b, args print(f(1, 2, 3, 4, 5))
- What does the following Python 3 code print? def f(kwargs): return sorted(kwargs.items()) print(f(b=2, a=1, c=3))
- What does the following Python 3 code print? def f(a, , b): return a + b try: print(f(1, 2)) except TypeError: print("err")
- What does the following Python 3 code print? l = [1, 2, 3] print(l, sep="-")