141,640 views
29 votes
29 votes
Which of these is a correct example of the creation of a list in Python? A. names = [“James”, “Omar”, “Hazel”, “Lee”, “Mia”] B. names = (“James”, “Omar”, “Hazel”, “Lee”, “Mia”) C. list[names] = [“James”, “Omar”, “Hazel”, “Lee”, “Mia”] D. list(names) = (“James”, “Omar”, “Hazel”, “Lee”, “Mia”)

User Pargat
by
2.6k points

2 Answers

14 votes
14 votes

Final answer:

The correct example of creating a list in Python is option A, which uses square brackets to define a list. The other options are either to create a tuple or use incorrect syntax for list creation.

Step-by-step explanation:

The correct example of the creation of a list in Python is:

A. names = [“James”, “Omar”, “Hazel”, “Lee”, “Mia”]

This syntax uses square brackets to define a list, which is a mutable, ordered sequence of elements in Python.

Option B creates a tuple, which is an immutable sequence. Option C is incorrect syntax, as lists don't need to be declared in this way. Option D attempts to create a list from a tuple using incorrect syntax; the correct way would be names = list((“James”, “Omar”, “Hazel”, “Lee”, “Mia”)), but even so, it's not the syntax used for simply creating a list.

User Moshen
by
2.9k points
18 votes
18 votes

Answer:

A.

The round brackets () are called tuples. Tuples are another way of storing data, and are slightly different from lists.

Lists are changeable.

Tuples are not changeable outside their codeline.

User Carlosvini
by
3.3k points