Operators are used to perform operations on variables and values.
Python divides the operators in the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
Python Assignment Operators
Assignment operators are used to assign values to variables.
Python Comparison Operators
Comparison operators are used to compare two values:
Python Logical Operators
Logical operators are used to combine conditional statements:
Python Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:
Python Membership Operators
Membership operators are used to test if a sequence is presented in an object:
Python Bitwise Operators
Bitwise operators are used to compare (binary) numbers:
There can be more than one operator in an expression. To evaluate these types of expressions there is a rule of precedence in Python. It guides the order in which these operations are carried out. For example, multiplication has higher precedence than subtraction.
Python has well-defined rules for specifying the order in which the operators in an expression are evaluated when the expression has several operators. For example, multiplication and division have a higher precedence than addition and subtraction. Precedence rules can be overridden by explicit parentheses.
When two operators share an operand, the operator with the higher precedence goes first. For example, since multiplication has a higher precedence than addition, a + b * c is treated as a + (b * c), and a * b + c is treated as (a * b) + c
Precedence of Python Operators
The combination of values, variables, operators, and function calls is termed as an expression. The Python interpreter can evaluate a valid expression.
For example, multiplication has higher precedence than subtraction.
The operator precedence in Python is listed in the following table. It is in descending order (upper group has higher precedence than the lower ones).
Example for precedence of or & and:
Associativity in Python Operators
We can see in the above table that more than one operator exists in the same group. These operators have the same precedence.
When two operators have the same precedence, associativity helps to determine the order of operations.
Associativity is the order in which an expression is evaluated that has multiple operators of the same precedence. Almost all the operators have left-to-right associativity.
For example, multiplication and floor division have the same precedence. Hence, if both of them are present in an expression, the left one is evaluated first.
Python supports the usual logical conditions from mathematics:
Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
An “if statement” is written by using the if keyword.
Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose.
The elif keyword is pythons way of saying “if the previous conditions were not true, then try this condition”.
The else keyword catches anything which isn’t caught by the preceding conditions.
Short hand if
If you have only one statement to execute, you can put it on the same line as the if statement.
Short Hand If … Else
If you have only one statement to execute, one for if, and one for else, you can put it all on the same line.
You can have if statements inside if statements, this is called nested if statements.
With the while loop we can execute a set of statements as long as a condition is true.
The else Statement
With the else statement we can run a block of code once when the condition no longer is true.
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
Looping Through a String
Even strings are iterable objects, they contain a sequence of characters.
The range() Function
To loop through a set of code a specified number of times, we can use the range() function,
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.
Using the start parameter:
To increment the sequence
Else in For Loop
The else keyword in a for loop specifies a block of code to be executed when the loop is finished.
A nested loop is a loop inside a loop.
The “inner loop” will be executed one time for each iteration of the “outer loop”:
Break and Continue statements
The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop.
If the break statement is inside a nested loop the break statement terminates the innermost loop.
The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop doesn’t terminate but continues on with the next iteration.
A pass statement is a null statement. The difference between a comment and a pass statement in Python is that while the interpreter ignores a comment entirely, pass is not ignored.
Python Range functions ( )
Definition and Usage
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.
To create a sequence of numbers from 0 to 8, print each item in the sequence.
|start||Optional. An integer number specifying at which position to start. Default is 0|
|stop||Required. An integer number specifying at which position to stop (not included).|
|step||Optional. An integer number specifying the incrementation. Default is 1|
To create a sequence of numbers from 4 to 10, and print each item in sequence.
To create a sequence of numbers from 3 to 20 with an increment of 3 instead of 1.