Lexical Structure
Python is very particular about program layout, especially with regard to lines and indentation.
Lines and Indentation
- In Python, the end of a physical line marks the end of most statements. Unlike in othe programming languages such as C, C++, Python statements are not normally terminated with a delimiter, such as a semicolon (
;). - When a statement is too long to fit on a single physical line, you can join two adjacent physical lines into a logical line by ensuring that the first physical line has no comment and ends with a backslash (
\) - Python also joins adjacent physical lines into one logical line if an open parenthesis(
(), bracket ([), or brace ({) has not yet been closed. - Python uses indentation to express the block structure of a program. Unlike other languages, Python does not use braces or being/end delimiters around blocks of statements:
- Indentation is the only way to indicate such blocks
- All statements in a block must have the same indentation
- Python style is to use four spaces per indentation level
Identifiers
- An identifier is a name used to identify a variable, function, class, module, or other object.
- Normal Python style is to start class names with an uppercase letter and other identifiers with a lowercase letter.
- Starting an identifier with a single leading underscore indicates by convention that the identifier is meant to be private.
- Starting an identifier with two leading underscores indicates a strongly private identifier; if the identifier also ends with two trailing underscores, the identifier is a language-defined special name.
- The identifier
_(a single underscore) is special in interactive interpreter sessions: the interpreter binds_to the result of the last expression statement evaluated interactively, if any.
Keywords
- Python version
3+has a set of 33 keywords that are reserved words that cannot be used as variable names, function names, or any other identifiers:
| Method | Description |
|---|---|
| and | A logical operator |
| as | To create an alias |
| assert | For debugging |
| break | To break out of a loop |
| class | To define a class |
| continue | To continue to the next iteration of a loop |
| def | To define a function |
| del | To delete an object |
| elif | Used in conditional statements, same as else if |
| else | Used in conditional statements |
| except | Used with exceptions, what to do when an exception occurs |
| False | Boolean value, result of comparison operations |
| finally | Used with exceptions, a block of code that will be executed no matter if there is an exception or not |
| for | To create a for loop |
| from | To import specific parts of a module |
| global | To declare a global variable |
| if | To make a conditional statement |
| import | To import a module |
| in | To check if a value is present in a list, tuple, etc. |
| is | To test if two variables are equal |
| lambda | To create an anonymous function |
| None | Represents a null value |
| nonlocal | To declare a non-local variable |
| not | A logical operator |
| or | A logical operator |
| pass | A null statement, a statement that will do nothing |
| raise | To raise an exception |
| return | To exit a function and return a value |
| True | Boolean value, result of comparison operations |
| try | To make a try…except statement |
| while | To create a while loop |
| with | Used to simplify exception handling |
| yield | To end a function, returns a generator |