Calculator

This is part of an ongoing series on learning Python. If you haven't read them yet, I advise you to catch up on parts one, two and three before reading this article.

As we've been learning Python, I've referenced operators, and even shown a few in examples. Before we move on, I thought it best to list all of Python's operators, not just make reference to some of the mathematical operators.

Mathematical Operators

The following operators are all used to calclate numeric values in some or other form.

Operator Name Description Example
+ Plus Adds two numbers together. 1 + 2 gives 3

Operator

Name

Description

Example

+

Plus

Adds two numbers together.

1 + 2 gives 3

-

Minus

Subtracts one number from another.

3 - 1 gives 2

*

Multiply

Multiplies one number by another.

2 * 3 gives 6

/

Divide

Divides one number by another. Integers are rounded down to the nearest integer. Add a float to force a floating result.

6 / 3 gives 2
4 / 3 gives 1
4.0 / 3 gives 1.333

//

Floor Divide

Divide a number by another and return a quotient rounded down to the nearest integer.

4.0 // 3.0 gives 1.0

%

Modulo

Return the remainder of a division.

5 % 2 gives 1

**

Power

Raise a number to the power of another number.

2 ** 4 gives 16
(2 * 2 * 2 * 2)

Boolean Operators

Boolean operators are used to compare two values and result in either a "True" or a "False".

Operator

Name

Description

Example

>

Greater Than

Check if the first value is greater than the second.

10 > 5 gives True
4 > 7 gives False

<

Less Than

Check if the first value is less than the second.

8 < 6 gives True
9 < 3 gives False

>=

Greater Than Or Equal To

Check if the first value is equal to or greater than the second.

7 >= 7 gives True
8 >= 7 gives True
6 >= 7 gives False

<=

Less Than Or Equal To

Check if the first value is equal to or less than the second.

5 <= 5 gives True
5 <= 6 gives True
5 <= 4 gives False

==

Equal To

Check that the two values are equal.

'Car' == 'Car' gives True
'Car' == 'Bus' gives False

!=

Not Equal To

Check that the two values are different.

'Car' != 'Bus' gives True
'Car' != 'Car' gives False

not

Boolean NOT

Return the opposite of an expression.

not True gives False
not False gives True

and

Boolean AND

The result of two expressions needs to be True for everything to be True.

'Car' == 'Car' and 5 == 5 gives True
'Car' == 'Bus' and 5 == 5 gives False

or

Boolean OR

The result of either of the two expressions needs to be True for everything to be True.

'Car' == 'Car' or 5 == 6 gives True
'Car' == 'Bus' or 5 == 5 gives True
'Car' == 'Bus' or 5 == 6 gives False

Bitwise Operators

Bitwise operators are used to manipulate the bits in a byte. If you don't understand this just yet, don't worry about it.

Operator Name Description Example
<< Left Shift Shift bits left by a certain number of positions. 0b0010 << 2 gives 0b1000
>> Right Shift Shift bits right by a certain number of positions. 0b1000 >> 2 gives 0b0010
& Bitwise AND Perform a bitwise AND operation on two bytes. 0b1001 & 0b0011 gives 0b0001
| Bitwise OR Perform a bitwise OR operation on two bytes. 0b1001 | 0b0011 gives 0b1011
^ Bitwise XOR Perform a bitwise XOR operation on two bytes. 0b1001 ^ 0b0011 gives 0b1010
~ Bitwise invert Perform a bitwise inversion (twos complement) on a byte. ~0b1001 gives -0b0110

Operator Precedence

When confronted with an expression such as 1 + 2 * 3 - 4 it is difficult to know how to evaluate it. Just as in normal mathematics, when dealing with operators in Python, or any other programming language for that matter, there is an order of precedence when dealing with operators.

In the table below the operators are listed in order of precendence. The operators at the top of the table will be executed before the ones further down.

Operator

Description

**

Exponentiation

~

Bitwise NOT

+x, -x

Positive, Negative

*, /, %

Multiplication, Division, Modulo

+, -

Addition, Subtraction

<<, >>

Shifts

&

Bitwise AND

^

Bitwise XOR

|

Bitwise OR

<, <=, >, >=, !=, ==

Comparisons

is, is not

Identity tests (checking the type of object)

is, is not

Membership tests (checking items in lists, tuples and dictionaries)

not

Boolean NOT

and

Boolean AND

or

Boolean OR

If you want to force the order of evaluation (when expressions are evaluated), use brackets to group them, like (4 + 1) * 2.

Update: Part 5


Comments

comments powered by Disqus