Example 1 — Create Lists Six Different Ways
A literal, list() on a string, range(), a comprehension, split(), and a list that mixes types.
primes = [2, 3, 5, 7]
letters = list("hey")
evens = list(range(0, 10, 2))
squares = [n * n for n in range(1, 6)]
words = "red green blue".split()
mixed = [1, "two", 3.0, None, [5]]
print(primes, letters)
print(evens, squares)
print(words)
print(mixed, len(mixed)) How It Works
- Literal and constructor. Square brackets list the items directly, while
list()turns a string into one item per character. - Numbers on demand.
range(0, 10, 2)counts from 0 up to, but not including, 10 in steps of 2; the comprehension squares 1 through 5. - From text.
split()with no argument breaks the string at whitespace into a list of words. - Any types together. One list can hold an int, a str, a float,
None, and even another list, which counts as a single item.

