Operators

Python has the following major operators:

<, >, =, ==, >=, <=, !=, *, +, -, **, +=, -=, /=, *=, in, and, not, is.

Operator priority

When expressions containing more than one operators are evaluated, the operator priority is followed, it is just like the BODMAS (PEMDAS for Americans) rule of maths. Usage of () can override priority.

>>> 3 * 2 + 20 - 46
-20
>>> 3 * (2 + 20 - 46)
-72

Assignment

= is the assignment operator.

>>> a = 1 # creates a new integer object of value 1
      # and stores the address in variable a.
>>> a = 2 # creates a new integer object of value 2
      # and stores the address in variable a.
>>> b = a # stores the address of variable a inside
       # variable b, they point to the same object.

Multiply

Exercise:

  1. Take two numbers from the user and print their multiplication.

  2. Take three numbers from the user and print their multiplication.

Add

Exercise:

  1. Take two strings from a variable and print their concatenation.

  2. Take two numbers from the user and add them (you need to use the int() to convert the input to integer)

  3. Take three numbers from the user and print their addition.

Equality

== is the equality operator, it returns true if both operands have the same value.

Note: It is a classic mistake to use == when you really want to use = or vice versa.

Division

27/7 divides 27 by 7 and returns a floating point result

27//7 divides 27 by 7 and returns an integer result.

Power

** is the operator for calculating power.

Shortcut operators

Consider that you have to create a variable a = 3. If you want to add 4 to the variable a.

You can do the following:

  1. a = a + 4. But this tends to be verbose.

  2. a += 4: Another way to do exactly the same calculation.

+= is a shortcut operator. There are other shortcut operators like: +=, -=, /=, *=. No spaces are allowed between -=, +=.

In other languages, you can use ++ or --, but they are not available in Python.

Membership test

The in operator tests if the element on the left hand side is present in the right hand side sequence (list, tuple, set).

Boolean operators

Read the docs

not

Read the docs not converts True to False and vice versa.

False like values

Variables of any data type when they are null or have no value, they are False like values. The negation of a False like value is True

True like

Variables of any data type when have some value, any value, they are True like values. The negation of a True like value is False

or

OR is true when either of the operand is true.

and

AND is true when both the operands are true.

Comparision Operators

Read the docs

There are eight comparison operations in Python. They all have the same priority (which is higher than that of the Boolean operations). Comparisons can be chained arbitrarily; for example, x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases y <= z is not evaluated at all if x < y is found to be false).

This table summarizes the comparison operations:

Operation

Meaning

<

strictly less than

<=

less than or equal

>

strictly greater than

>=

greater than or equal

==

equal

!=

not equal

is

object identity

is not

negated object identity

Links

Last updated

Was this helpful?