wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Lists in Python

Total questions: 12

Worksheet time: 6mins

Name
Class
Date
1.

A developer wants to join a user's first and last name, stored in variables `first_name = "Alex"` and `last_name = "Chen"`, to create the full name "Alex Chen". Which line of code will achieve this?

a)

`print(first_name.append(last_name))`

b)

`print(first_name + last_name)`

c)

`print(first_name + " " + last_name)`

d)

`print(first_name, " ", last_name)

2.

Given the list `animals = ["cat", "dog", "lion", "tiger"]`, which code snippet will add "bear" to the end of the list?

a)

`len(animals) = "bear"`

b)

`animals[4] = "bear"`

c)

`animals + "bear"`

d)

`animals.append("bear")`

3.

What is the primary reason for using a list to store multiple related items, like a list of favorite fruits, instead of using separate variables for each one (`fruit1`, `fruit2`, etc.)?

a)

Using separate variables is faster for the computer to process.

b)

Lists can hold more types of data than individual variables can.

c)

Lists are more manageable and organized, especially when dealing with a large number of items.

d)

Separate variables require more memory than a list.

4.

Consider the string `word = "Computing"`. What will be the output of `print(word[-1])`?

a)

An error message.

b)

`g`

c)

`C`

d)

`Computin`

5.

In Python, the position of an item in a string or list is called its index. At what number does indexing always begin?

a)

`1`

b)

`0`

c)

It depends on the length of the string or list.

d)

`-1`

6.

A programmer wants to extract a 'slice' of a string. If they run the code `print("PythonRulEs"[2:6])`, what will the output be?

a)

`ThOn`

b)

`Py`

c)

`yt`

d)

`ThOnR`

7.

Given the list `subjects = ["Art", "Math", "Science", "History"]`, what is the result of running `print(subjects[1])`?

a)

`Math`

b)

An IndexError.

c)

`Science`

d)

`Art`

8.

What is the fundamental difference between string indexing (e.g., `word[2]`) and string slicing (e.g., `word[2:5]`)?

a)

Indexing gets a single character, while slicing gets a sequence of characters (a substring)

b)

Indexing is for strings, while slicing is for lists.

c)

Indexing can use negative numbers, but slicing cannot.

d)

Indexing uses square brackets `[ ]`, while slicing uses parentheses `( )`.

9.

If you have the code `message = "learning python"` and you want to display it as "LEARNING PYTHON", which method should you use?

A.

B.

C.

D.

a)

`.lower()`

b)

`.upper()`

c)

`.append()`

d)

`len()`

10.

What happens if you try to access an index that does not exist in a list, such as `print(my_list[5])` when `my_list` only has four items?

a)

Python will raise an IndexError.

b)

The program will print `None` or an empty string.

c)

The program will print the last item of the list.

d)

Python will automatically add a new empty item at that index.

11.

Which of the following lines of code correctly creates a Python list?

a)

`fruit_list = ["banana", "guava", "orange"]`

b)

`fruit_list = ("banana", "guava", "orange")`

c)

`fruit_list = "banana, guava, orange"`

d)

`fruit_list = <"banana", "guava", "orange">`

12.

Examine this code: `code = "PyThOn"; print(len(code.lower()))`. What will be the output?

a)

An error message.

b)

`python`

c)

`2`

d)

`6`