1.3 Python 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)
b = 2**4 + 1
print(b)
c = 352 % 23
print(c)

Explanation: \(352 = 15 \times 23 + 7\), therefore the modulus operator returns the value \(7\).

d = 352 // 23
print(d)

Explanation: \(352 = 15 \times 23 + 7\), therefore the floor division operator returns the value \(15\).

Note

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.:

\[ y = x\tan\theta - \frac{1}{2v_0^2}\frac{g x^2}{\cos^2\theta} + y_0 \]

You could split this equation into four distinct variables:

  1. var_1 \( = x\tan\theta\)

  2. var_2 \(= \frac{1}{2v_0^2}\)

  3. var_3 \(= \frac{g x^2}{\cos^2\theta}\)

  4. var_4 \(= y_0\)

And then re-write it as y = var_1 - (var_2 * var_3) + var_4


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

>=

Greater than or equal to

<=

Less than or equal to

!=

Not equal to

Checking if a value corresponds to the set conditions#

Check if the the variable num satisfies the set condition.

num = 6
print(num > 2)

If the value does not satisfy the condition the system will return False

print(num > 7)

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

in

returns True if a sequence with the specified value is present in the object

not in

returns True if a sequence with the specified value is not present in the object

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)

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)

Additional study material

After studying Sections 1.1 to 1.3 you should be able to:#

  • create and re-assign new variables

  • determine the type of a variable using type()

  • slice strings

  • perform simple math using arithmetic operators

  • compare two or more variables