Basic syntax
Basic syntax
Indentations
Indentation or TAB, 2/4 spaces - whitespaces used to delimit program blocks - instead of punctuation or keywords, it uses indentation to indicate the run of a block.
In C we need to use brackets({
and }
) to delimit blocks of code:
In Python the same role goes to indentations which make code much easier to read:
Easter egg describing the "love to braces"
Indentation must maintain the constant level for same level of nesting.
This is correct:
This is incorrect:
🪄 Code:
📟 Output:
If code block is not big it is allowed to use inline form:
And it is possible to specify few statements on the same line using ;
:
These two examples are not readable so used very rarely.
Identificators and reserved words
Naming rules:
can have only alphabetical characters, numbers and
_
can't start with number
case sensitive
can't be one of reserved keywords
Reserved words (which can't be used as variable names):
Comments and documentation strings
#
- starting with this symbol everything till the end of line is ignored
Doc string - first string of module, function or class. These string are used by IDEs, help auto-generating scripts etc. It is possible to specify multiline string by using starting and ending with triple quotes("""
or '''
).
🪄 Code:
📟 Output:
Or:
🪄 Code:
📟 Output:
### Naming rules
Variables can only contain letters, numbers, and underscores. Variable names can start with a letter or an underscore, but can not start with a number.
Spaces are not allowed in variable names, so we use underscores instead of spaces. For example, use student_name instead of "student name".
You cannot use Python keywords as variable names.
Be careful about using the lowercase letter l and the uppercase letter O in places where they could be confused with the numbers 1 and 0.
Last updated