Conditions
if operator
Python has one omnipotent logical condition check operator:
if
.
🪄 Code:
📟 Output:
bool type
Boolean type is subclass of int. So for Python
True
is1
,False
is0
.
Its only instances are False and True
The built-in function bool() can be used to convert any value to a Boolean, if the value can be interpreted as a truth value
🪄 Code:
📟 Output:
🪄 Code:
📟 Output:
🪄 Code:
📟 Output:
Truth Value Testing
Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered False
:
None
False
zero of any numeric type, for example,
0
,0.0
,0j
.any empty iterable, for example,
''
,()
,[]
,set()
,frozenset()
.any empty mapping, for example,
{}
.instances of user-defined classes, if the class defines a
__bool__()
or__len__()
method, when that method returns the integer zero or bool value False.
All other values are considered true — so objects of many types are always true.
Boolean operations
or
and
not
^
(XOR
- exclusive OR - only if its arguments differ)
Operation | Result |
---|---|
| if |
| if |
| if |
Sometimes it can surprise you. The following example will always print the first quote:
🪄 Code:
📟 Output:
Because the expression is equivalent to:
Solution:
Precedence of operations in Python:
The following table summarizes the operator precedence in Python, from lowest precedence (least binding) to highest precedence (most binding). Operators in the same box have the same precedence. Unless the syntax is explicitly given, operators are binary. Operators in the same box group left to right (except for exponentiation, which groups from right to left).
Operator | Description |
---|---|
| Lambda expression |
| Conditional expression |
| Boolean OR, AND, NOT |
| Comparisons, including membership tests and identity tests |
| Bitwise OR, XOR, AND |
| Shifts |
| Addition and subtraction |
| Multiplication, matrix multiplication, division, floor division, remainder |
| Positive, negative, bitwise NOT |
| Exponentiation |
| Await expression |
| Subscription, slicing, call, attribute reference |
| Binding or tuple display, list display, dictionary display, set display |
Structural Pattern Matching
Sometimes it is pain to manage the tree-like structure of if-elif-elif-elif-else
statements. It is becoming a burden to maintain:
In Python 3.10 we got a completely new syntax for defining of a match statement and case statements of patterns with associated actions allowing substitute a mess of multiple elif
conditions with a much clear structure.
The generic syntax of pattern matching is:
Pattern matching will try each pattern until a match to run an action from it or the lastly defined default
match action. The match is comparing the structure/value of the pattern and a given item. It is possible to substitute exact values in the pattern with variables and use them in a action.
An example:
🪄 Code:
📟 Output:
We can even assign variables using patterns:
🪄 Code:
📟 Output:
If we don't specify a second tuple item or pass None
or any other object instead of a 1,2-item tuple:
🪄 Code:
📟 Output:
Last updated