- Code blocks are an important part of any
programming language as they allow you to reuse code.
Code blocks are also one area
where the authors of JavaScript and Python
took a different approach.
Python uses a concept called significant white space,
which means it uses tabs or spaces to determine where code
blocks begin and end.
JavaScript, on the other hand
uses curly brackets to determine code blocks.
Look at the following examples,
which take a list of pizza toppings loop through them
and print out each topping.
Notice the use of the colon that indicates to Python
that anything that follows that is indented
should be considered part of the code block,
where the print statement to be moved back to the same
indentation level as the for statement,
the program would have an error.
Code has to be indented in order
for Python to know it's part of the code block.
In JavaScript, the for statement has to be wrapped
in parentheses and the code block
is indicated using curly brackets.
The use of significant white space in Python
is loved by some developers and hated by others.
It forces users to be consistent with their spacing,
which lends itself to greater readability.
In this example,
I have also introduced the list or array data type,
which allows you to group together information.
Go ahead and play around with this example
and see what else
you can do with lists and code blocks.