What is the expected output of the following code?

What is the expected output of the following code?

data = [‘Peter’, 404, 3.03, ‘Wellert’, 33.3]

print(data[1:3])
A . None of the above.
B. [‘Peter’, 404, 3.03, ‘Wellert’, 33.3]
C. [‘Peter’, ‘Wellert’]
D. [404, 3.03]

Answer: D

Explanation:

Topic: list indexing

Try it yourself:

data = [‘Peter’, 404, 3.03, ‘Wellert’, 33.3]

print(data[1:3]) # [404, 3.03]

You have a list of five elements of various data types.

[1:3] slices inclusive the first index and exclusive the third index.

Meaning it slices the first and second index.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments