127k views
4 votes
For Questions 3-5, consider the following code:

stuff = []





stuff.append(1.0)


stuff.append(2.0)


stuff.append(3.0)


stuff.append(4.0)


stuff.append(5.0)





print(stuff)



3. What data type are the elements in stuff?


4. What is the output for print(len(stuff))?


5. What is the output for print(stuff[0])?

User Meng Lu
by
7.1k points

1 Answer

2 votes

Answer:
The .append() function adds the inserted value to a list. You are passing in floats.

In this case, you appended 5 things to the list, so the list looks like:
stuff = [1.0, 2.0, 3.0, 4.0, 5.0]

And, when you run print(stuff) you will end up with that list. However, when you run print(len(stuff)) you are counting the length of the list. You have 5 things in the list, so the output will be 5.

When you wish to specify a position in a list to print, that's when stuff[] comes in. Essentially, by running print(stuff[0]) you are specifying that you only want the first item in the list. This would output 1.0

You can always try this by putting this into an IDE, and running it.

User Cirrus
by
6.3k points