Arithmetic and comparative operators in Python

Computations with numerical data types Arithmetic and comparative operators in Python

Arithmetic and comparative operators allow calculations and comparisons with numerical data types. In the Following, we look at the operators and their functions.

Company about the topic

Application of the is operator in Python.Application of the is operator in Python.

(Image: Joos / Microsoft)

Arithmetic and comparative operators work together with numerical data types such as int (integers), float (floating point numbers), bool (boolean values) or complex (complex numbers).

Conversion between numeric data types can also take place. For example, if we want to generate an integer from the value 3.14159, then this works with int(3.14159). This creates 3 as a new value. However, we usually do not work with a fixed value, but also with variables and value assignments, for example:

x = 3.14159

Conversely, we can return the result to a variable, for example with:

x = 3.14159
y = int(x)

Values with these data types can be edited with arithmetic and comparative operators. However, numeric data types are immutable. When a change is made with an operator, Python creates a new instance of the data type.

Perform arithmetic calculations with numerical data types

Arithmetic operators are used to perform calculations with numerical data types. Examples are x + y or x-y. Products can be combined with x * y calculate the quotient with x / y. To calculate x high y x**y used.

Operators also serve to completely reset values. If, for example, the value x is to be reset by adding x + y, this is done with the following statement: x = x + y. Since such calculations and value changes are often necessary in Python, there are extended possibilities and abbreviations.

The operator for the above example is x + = y. If x is to arise from the subtraction of x with y, i.e. x = x – y, the operator can also x – = y can be used. With x * = y can be abbreviated x = x * y and x / = y can be abbreviated x = x / y. In the calculations, y can also be replaced by arbitrary expressions.

When using the arithmetic operators, Python follows the mathematical rules. Brackets can be used in the same way as in other places. Point is also important in Python. For example, if you want to draw a root from a value, you can use 4 ** 0.5.

Example of an invoice with operators and variables.Example of an invoice with operators and variables.

(Image: Joos / Microsoft)

In order to work with variables in the calculation to make special calculations, several variables can also be used, for example for the calculation of VAT. The following example is not ideal for programming, but shows the possibilities offered by arithmetic operators:

mwst = 0.19
netto = 100.00
brutto = netto * (1 + mwst)

Using comparative operators

Using comparative operators, we compare instances from numerical data types to trigger loops and actions, for example. The best known comparative operators are = = (equal), != (not equal), < (less), > greater, <= (less equal), & gt;= (greater equal).

Using these operators, the program code calculates whether a comparison is true or not and performs further actions based on this calculation. On this basis, if statements can be used, for example:

if x = 0
print ("Wert x ist nicht richtig gesetzt, bitte korrigieren")

Of course, comparisons, and calculations can also be nested to create more complex structures.

Logical Operators

Basically, there are the two logical operators “and” and “or”. At the same time, the result can be negated with “not”. When using boolean values, i.e. the data type “bool”, the result can only be” true “(1) or” false ” (0).

Here, logical operators can be used, for example, not x (not x logical negation), x and y (logical x AND y, the true total value only if x AND y are true), x or y (logical x OR y). With the logical negation the value true can be changed to false and false to true.

The results of a logical comparison can be seen in the following table using the example of “and” :

Result at ” and“ True False
True True False
False False False

With the logical operator ” or ” the result looks like this:

Result at ” or“ True False
True True True
False True False

For example, if the following comparison is used, the result of the logical comparison is true:

(1 == 1) and (2 == 2)

In principle, the brackets can also be omitted here. However, these increase the overview. In the case of arithmetic operators, it must be ensured that the calculations are not falsified by wrong brackets. The following comparison results in false:

(1 == 1) and (2 == 3)

If, however, the logical operator “or” is used here, then the result of the above comparison is true. When using “not”, the result of the following comparison is false:

not (1 == 1)

The next comparison returns true as result:

(1 == 1) and not (2 == 3)


Working with the operatorWorking with the operator “is”.

(Image: Joos / Microsoft)

In parallel, there is also the operator “is”. This checks whether two variables are identical. In the following example, the result is “false“:

x = 1
y = 2
x is y

Only when the variables are actually equated, the operator returns true, for example with:

y = 1
x = y
x is y

The reference of this operator in memory can be checked with id(x); and id(y);. If the memory reference is the same, then x is y also returns true. The operator ” is “does not correspond to the operator”==”. “is” means the identical, physical value. “==”is the same value.


Check subsets with operators.Check subsets with operators.

(Image: Joos / Microsoft)

The operator ” in ” can be used to check whether an object is a subset of another object. For example, to check that the value 10 is part of the list 10,20,30 and 40, the following can be used:

10 in [10,20,30]

Testing can be done by storing the result in a variable and then output it:

y = 10 in [10,20,30


Ready to see us in action:

More To Explore
Enable registration in settings - general
Have any project in mind?

Contact us: