Hello World!

It's customary to start a programming book with a simple "Hello World!" example, and who am I to break with tradition?

greeting = ['Hello', 'World']

This is a "literal" definition of a list. It consists of a comma-separated series of values enclosed in square brackets.

This particular list contains two string values. Lists can contain any type of value, and different types can be mixed together in the same list.

List values can span multiple lines. Here is the same list formatted a bit differently.

greeting = [
    'Hello',
    'World'
]

Trailing commas

The last item in a list can be followed by an optional "trailing comma". This comma has no effect, but Python allows it so you don't have to treat the last value differently from the others.

greeting = [
    'Hello',
    'World',  # notice the trailing comma
]

No comments:

Post a Comment