Syntax
I would describe syntax as the grammar of the programming language. In normal language grammar mistakes are not a particularly big deal. In programming languages, however, grammar mistakes causes errors. Frequently the program will crash. Some programming languages don't allow you to run your code if there's a grammatical error.
I briefly want to mention that Python code is run line by line from top to bottom... kind of. This is true for many (almost all?) programming languages. You will understand what I mean by "kind of" when you read the page on control flow. A more accurate description would be to say that it runs from top to bottom, with jumps all over the place (depending on the code).
Finding info
After this section we will actually look at Python's syntax.
The best way to learn programming is by programming. It is a practical skill that cannot solely be learned from reading a book. Still, you need to know something before you start programming, otherwise you will be stuck forever. If you know a little bit you will only be stuck for a couple of hours or days.
I linked the official documentation above. When I look for information I usually use Google and click results related to the official documentation. Get used to searching Google for relevant information. Acquiring information from the internet is an essential skill when programming.
If you come across a website called Stack Overflow when you search for information you are on the right path. Stack Overflow is a Q&A site for developers. There are answers for almost any programming related question you can come up with. Unfortunately, some answers are so old they are no longer relevant. That's life.
I found this tutorial (you don't have to read it): Learn Python in 10 minutes. It actually manages to include what I would consider the most important properties and features of Python. It is aimed at programmers so some keywords that are mentioned won't make much sense to non-programmers, but I still think it's good. I will paraphrase a bit from the tutorial.
When going through the basics the Python shell can be pretty practical. I mentioned it previously in the Installing - Python in the Terminal section.
Syntax (for real)
The things I'm about to mention, termination, indentation, assignment and equality, may be confusing, but it makes sense when you start working with it.
As mentioned in the "10 minute"-tutorial, Python has no mandatory statement termination characters. In some
programming languages you have to end each statement with a character, often with a semicolon
;. This is not necessary in Python.
Code blocks are specified by indentation. This can be tricky at first. In many other languages indentation is only
used for making the code look nice. Those languages often create blocks with
{ ... }. In Python you indent a line of code to move it into another block.
To a assign a value to a variable you use the equal sign =. You can assign new values to a variable
even after it has already been assigned a value. Clearly, the equal sign in Python is not the same as the
equal sign in mathematics.
To check if two variables, or two anything, are equal you use two equal signs ==. This is one of several
comparison operators.
Some other comparison operators:
==: Equality!=: Inequality<: Less than<=: Less than or equal to>: Greater than>=: Greater than or equal to
It is not possible to use <= or >= as a substitute for ==.
Comments are text that Python ignores. They are written by adding the hash sign
# in front of the text you want Python to ignore. Everything on the line that comes after the hash sign
is ignored.
Code example
Below we have the first real code example on this website. If you want to run the code you can copy and paste it into a Python file and run the file, like the "Hello, World!" example on the previous page. Or you can copy and paste it into a terminal with the Python shell open.
# This is a comment
foo = 123 # This is a comment at the end of a line
comparison_result = foo < 400
print(comparison_result)
foo = 500
print(comparison_result)
Let's try to understand it!
The first line is a comment. Python ignores that line. On the second line a variable called foo
is declared and assigned the numerical value 123. The line ends with a comment after it, which Python
of course ignores.
On line three something interesting happens. A variable called comparison_result is declared and
immediately assigned the value created by executing the expression foo < 400. What do you think
the value of comparison_result is? What value do you think would make sense? Remember that
this programming language has been designed by people.
The value of comparison_result is True. Makes sense right?
On line four the value is printed. print() is a function, which can be seen by the parenthesis.
Technically the name of the function is print and the parenthesis are used to "invoke" or "call"
or "execute" the function. If you run the code, True will appear in the terminal.
The last two lines are interesting. foo is assigned the value 500 and
comparison_result is printed again. What is the result now? 500 is greater than 400,
so is comparison_result false now? Nope. It's still True.
comparison_result was assigned the value generated by evaluating foo < 400,
which was True. Just because the value of foo has changed it doesn't mean
the value of comparison_result has automatically changed.
There are ways to make sure comparison_result sort of stays up-to-date.
But that's another topic.
Next: Types