def kitchen():
kitchen_items = [
"Rice", "Chickpeas", "Pulses", "bread", "meat",
"Milk", "Bacon", "Eggs", "Rice Cooker", "Sauce",
"Chicken Pie", "Apple Pie", "Pudding"
]
I tried reading PEP8, but the only thing I got from there was -
The closing brace/bracket/parenthesis on multi-line constructs may either line up under the first non-whitespace character of the last line of list
I couldn't really understand what that means. My apologies for not reading it properly.
Create Python Lists In Python, a list is created by placing elements inside square brackets [] , separated by commas. A list can have any number of items and they may be of different types (integer, float, string, etc.). A list can also have another list as an item. This is called a nested list.
According to the source code, the maximum size of a list is PY_SSIZE_T_MAX/sizeof(PyObject*) . On a regular 32bit system, this is (4294967295 / 2) / 4 or 536870912. Therefore the maximum size of a python list on a 32 bit system is 536,870,912 elements.
The most common way to declare a list in Python is to use square brackets. A list is a data structure in Python that contains an ordered sequence of zero or more elements. Lists in Python are mutable, which means they can be changed. They can contain any type of data, like a string or a dictionary.
You need to indent the list contents like this
kitchen_items = [
"Rice", "Chickpeas", "Pulses", "bread", "meat",
"Milk", "Bacon", "Eggs", "Rice Cooker", "Sauce",
"Chicken Pie", "Apple Pie", "Pudding"
]
Or
kitchen_items = [
"Rice", "Chickpeas", "Pulses", "bread", "meat",
"Milk", "Bacon", "Eggs", "Rice Cooker", "Sauce",
"Chicken Pie", "Apple Pie", "Pudding"
]
The section you quoted:
The closing brace/bracket/parenthesis on multi-line constructs may either line up under the first non-whitespace character of the last line of list
Honestly, it means exactly what it says:
my_list = [
'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', <-- "the last line of the list"
^
"the first non-whitespace character"
Thus:
my_list = [
'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h',
]
There's also the second option that PEP-8 refers to,
or it may be lined up under the first character of the line that starts the multi-line construct, as in:
"the first character"
v
my_list = [ <-- "line that starts the multi-line construct"
'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h',
Thus:
my_list = [
'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h',
]
Personally, I prefer this second style, because it give a nice way to scan for the end of the list: the ]
justs back out into the left-hand side:
my_list = [
| 'items', 'items',
| 'items', 'items',
| < a nice line for your eye to track
|
|
] < this stands out more
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With