What is the expected output of the following code?

What is the expected output of the following code?

def func(data):

for d in data[::2]:

yield d

for x in func(‘abcdef’):

print(x, end=”)
A . bdf
B. An empty line.
C. abcdef
D. ace

Answer: D

Explanation:

Topics: def yield for print() with end parameter list slicing

Try it yourself:

def func(data):

for d in data[::2]:

yield d

for x in func(‘abcdef’):

print(x, end=”) # ace

The generator function will return every second element of the passed data.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments