1.3. Operators#
Python operators are used to perform operations on variables and values. They are symbols that represent a form of computation; think of addition or multiplication. The value to which this computation is applied to is called the ’operand’. Most of the common operators you will recognize from mathematics.
Arithmetic Operators
Math sign |
Python sign |
name |
---|---|---|
+ |
+ |
addition |
- |
- |
subtraction |
* |
* |
multiplication |
/ |
/ |
division |
^ |
** |
exponentiation |
mod |
% |
modulus |
// |
floor division |
Most of the mathematical symbols stay the same when transforming a piece of mathematics to Python. Note that the exponentiation sign is a double multiplication sign!
The last two operators, modulus and floor division, can be defined as the following:
modulus: return the remainder of a division
floor division: returns the integer/whole part of the division
Now we will provide some small examples
a. multiply 4 by 3 and then add 2
b. 2 to the power of 4 plus 1
c. take the modulus of 352 over 23
d. the floor division of 352 over 23
a = 2 + 4 * 3
print(a)
14
b = 2**4 + 1
print(b)
17
c = 352 % 23
print(c)
7
Explanation: \(352 = 15 \times 23 + 7\), therefore the modulus operator returns the value \(7\).
d = 352 // 23
print(d)
15
Explanation: \(352 = 15 \times 23 + 7\), therefore the floor division operator returns the value \(15\).
Besides making sure that you use the right operators when writing mathematical functions, it is also important that you pay attention to the order of operators. When not done right, this can cause huge changes in the outcome. Therefore, when writing out large equations it is easier to use parentheses or split it into multiple variables. e.g.:
You could split this equation into four distinct variables:
var_1 \( = x\tan\theta\)
var_2 \(= \frac{1}{2v_0^2}\)
var_3 \(= \frac{g x^2}{\cos^2\theta}\)
var_4 \(= y_0\)
And then re-write it as y = var_1 - (var_2 * var_3) + var_4
parenthesis_before = (2 + 4) * 3
print('With parenthesis before =',parenthesis_before)
parenthesis_after = 2 + (4 * 3)
print('With parenthesis after =',parenthesis_after)
With parenthesis before = 18
With parenthesis after = 14
1.3.1. Comparison Operators#
In Python, you often want to compare a value with another. For that, you use comparison operators.
Math sign |
Python sign |
Meaning |
---|---|---|
\(=\) |
|
Equal to |
\(>\) |
|
Greater than |
\(>\) |
|
Less than |
\(\geqslant\) |
|
Greater than or equal to |
\(\leqslant\) |
|
Less than or equal to |
\(\neq\) |
|
Not equal to |
1.3.1.1. Checking if a value corresponds to the set conditions#
Check if the the variable num
satisfies the set condition.
num = 6
print(num > 2)
True
If the value does not satisfy the condition the system will return False
print(num > 7)
False
1.3.2. Logical & Identity Operators#
sign |
description |
---|---|
and |
returns True if both statements are true |
or |
return True if at least 1 statements is true |
not |
reverse of the results; returns False if the statement is True |
is |
returns True if both variables are the same object |
is not |
returns True if both variables are not the same object |
in |
returns True if a sequence with the specified value is present in the object |
in not |
returns True if a sequence with the specified value is not present in the object |
1.3.2.1. and
statement#
By using the and
statement you can set multiple conditions for the system to return. This can be seen as setting a boundary condition for a mathematical function.
num = 5
print(num > 4 and num < 8)
True
1.3.2.2. checking if a value appears in an object#
Suppose we have a string “sandstone”, we can check if a value is present within the string through the following lines of code.
rock_type = "sandstone"
print("sand" in rock_type)
1.3.3. is
and ==
operators#
The is
operator deserves a little more explanation since it can be easily confused with the ==
operator. The is
statement does not compare the value of a variable but simply checks if two variables are the same object. On the other hand, ==
checks if the values of different variables are the same. In the underneath piece of code this is shown quite clearly. Although the values of the variables are the same, their type
is not. Therefore, when compared using the is
operator, it returns False
.
x = 2.0
y = 2
print(type(x),type(y),x is y)
print(x == y)
<class 'float'> <class 'int'> False
True
Exercise