Operators
Operators are used to perform operations on values.
The most basic are arithmetic operators, that are similar to regular arithmetic maths.
Operator | Operation | Example |
+ | Addition | 4+1=5 |
- | Subtraction | 5-3=2 |
* | Multiplication | 4×2=8 |
/ | Division | 4/2=2 |
An example where basic arithmetic operators are used is:
a = 3
b = 2
c = a + b # c holds the value of 5Modulo Operator
A modulo operator (%) tells you what’s left after dividing one number by another.
dividend: The number being divided.
divisor: The number that divides the dividend.
result: The remainder of the division.
Often used to check if a number is odd or even.
If a number is even, dividing it by 2 will leave a result of 0.
If a number is odd, dividing it by 2 will leave a result of 1.
result = dividend % divisorresult = 10 % 310 is divided by 3. 3 goes into 10 three times, with the remainder left that cannot be divided being 1. So the result will be 1.
Arithmetic Shortcuts
Python has shortcuts for self arithmetic operations.
a = 5
a = a + 3 # a holds value 8Can be simplified to:
a = 5
a += 3 # a holds value 8The operator += adds the value 3 to a.
This can be applied to:
Operator | Shortcut |
+ | += |
- | -= |
* | *= |
/ | /= |
% | %= |
Comparison Operators
Comparison operators are operators used to compare two operands.
Used to check if operand is greater, less than or equal to other operand.
Operator | Meaning | Example |
== | Equal | 1 == 2 false |
!= | Not equal | 1 != 2 true |
> | Greater than | 1 > 2 false |
< | Less than | 1 < 2 true |
>= | Greater than or equal | 1 >= 2 false |
<= | Less than or equal | 1 <= 2 true |
Logical operators
Logical operators are used to combine conditional statements
Python has three logical statements:
and, here’s an example:
# Create two boolean variables x = True y = True # Check if both x and y are True result = x and y # Result will be True result = Trueor
not
Example on how to use logical operators:
# Code determines elegibility to drive based on min. age of 18 and license status
age = 20 # Person is 20
has_license = True # Person has license
result = age >= 18 and has_license
result = True # Eligible to drive“Truth table”
A truth table shows the combination of logical operators returns.
Different table for each logical operator.
and
The only way to get true, is if a and b or both true.
a | b | a and b |
False | False | False |
False | True | False |
True | False | False |
True | True | True |
or
To get true, a or b should be true.
a | b | a or b |
False | False | False |
False | True | True |
True | False | True |
True | True | True |
not
If a is false, then not a is true.
a | not a |
False | True |
True | False |
Multiple conditions
Logical operators can have multiple conditions.
To check if a number is positive and even, the following code can be written:
# Here are two conditions
is_postive = number > 0 # Number is above 0
is_even = number % 2 == 0 # Number can be divided by 2
# Conditions combined
result = is_postive and is_even
# When result is true, both conditions are met.
# This can be done with less variables.
result = number > 0 and number / 2 == 0Simplification and rearranging
Logical expressions can be simplified and rearranged.
Ex. not in front of two conditions joined by and, can be split into two. Therefore and becomes or and each part gets its own not.
not (A and B) is the same as (not A) or (not B)
Here simplification is used to see if a number upholds both variables as True:
# Check if a number is not between 1 and 10.
number = 15
# These expressions are equivalent, result 2 is simplified:
result1 = not (number >= 1 and number <= 10)
result2 = (not number >= 1) or (not number <=10)
print(result1) # True
print(result2) # True