3 Arithmetic and Logical Operations and Expressions#
Different data types can be operated on in many different ways. For this purpose, Python offers operators – symbols or keywords that tell Python what kind of operation to perform. For instance, most standard mathematical operation symbols can be used in Python for numerical data types. A code snipped that produces a value, from for example using operators, are referred to as Expressions.
3.1. Arithmetic Operators#
Here is an overview of most of the basic arithmetic operations and their corresponding symbol. They are applied to numerical data and outputs a numerical value.
Operator |
Description |
Example |
Result |
---|---|---|---|
+ |
Addition |
3 + 2 |
5 |
- |
Subtraction |
3 - 2 |
1 |
* |
Multiplication |
3 * 2 |
6 |
/ |
Division (float) |
3 / 2 |
1.5 |
// |
Floor Division |
3 // 2 |
1 |
% |
Modulus (remainder) |
3 % 2 |
1 |
** |
Exponentiation |
3 ** 2 |
9 |
a = 10
b = 3
sum_result = a + b # Output: 13
division_result = a / b # Output: 3.3333...
mod_result = a % b # Output: 1
square_result = a ** 2 # Output: 100
print("Sum of a and b:", sum_result)
print("Division of a and b: ", division_result)
print("Remainder of a and b:", mod_result)
print("Square of a:", square_result)
Sum of a and b: 13
Division of a and b: 3.3333333333333335
Remainder of a and b: 1
Square of a: 100
Important Note About Division
In Python, division using /
always produces a float, even if the result looks like a whole number and integers were used for the division.
For example:
print(4 / 2) # Output: 2.0
You can combine multiple operators in a single expression. Python follows the standard order of operations rules (also known as BIDMAS or PEMDAS):
Parentheses.
Exponents.
Multiplication / Division.
Addition / Subtraction.
result = 3 + 5 * 2 # Output: 13
# Multiplication done first => 3 + 10 = 13
print(result)
parenthesised_result = (3 + 5) * 2 # Output: 16
# Parentheses first => 8 * 2 = 16
print(parenthesised_result)
13
16
3.2. Comparison Operators#
These operators are used to compare two values (mainly numerical values - but could be other data types as well); the result is always a Boolean value (True
or False
).
Operator |
Description |
Example |
Result |
---|---|---|---|
== |
Equal to |
3 == 2 |
False |
!= |
Not equal to |
3 != 2 |
True |
> |
Greater than |
3 > 2 |
True |
< |
Less than |
3 < 2 |
False |
>= |
Greater or equal |
3 >= 3 |
True |
<= |
Less or equal |
3 <= 2 |
False |
3.3. Logical Operators#
Logical operators are used to operate on Boolean values and also return a Boolean.
Operator |
Description |
Example |
Result |
---|---|---|---|
and |
True if both conditions are True |
(3 > 2) and (2 > 1) |
True |
or |
True if at least one condition is True |
(3 > 2) or (2 == 1) |
True |
not |
Inverts the Boolean value (True becomes False) |
not (3 > 2) |
False |
print(2 != 3) # Output: True
print((3 > 2) and (4 > 5)) # Output: False
print((2 < 5) or (10 < 3)) # Output: True
print(not (1 == 1)) # Output: False
True
False
True
False
3.4. Quick Practice#
Try to guess the results of the following expressions. Feel free to write some code to double check your assumptions and play around with it:
5 + 2 * 3
(5 + 2) * 3
10 // 3
10 % 3
(5 > 3) and (2 < 1)
4 == 4
not (4 == 4)
# Put your code here
💡 Solutions
print(5 + 2 * 3) # Output: 11
print((5 + 2) * 3) # Output: 21
print(10 // 3) # Output: 3
print(10 % 3) # Output: 1
print((5 > 3) and (2 < 1)) # Output: False
print(4 == 4) # Output: True
print(not (4 == 4)) # Output: False