Operators

Python provides a set of built-in operators that are essential for effectively manipulating data and performing various calculations. These operators facilitate interactions with numbers and other variables that are fundamental to computation and data analysis.

Arithmetic operators

Python has the following standard math operators.

Operation

Description

x + y

Addition – adds the values on either side of the operator.

x - y

Subtraction – subtracts the right operand from the left operand.

x * y

Multiplication – multiplies values on both sides of the operator.

x / y

Division – divides the left operand by the right operand.

x // y

Floor Division – the division of operands where the result is the quotient, and if the result is a decimal, it is rounded down to the nearest integer.

x % y

Modulo – divides the left operand by the right operand and returns the remainder.

x ** y

Exponentiation – performs exponential (power) calculations on operators (x to the power of y).

-x

Unary Minus – operator changes the sign of the operand.

+x

Unary Plus – operator doesn’t change the value of the operand.

In Python, the division operator (‘/‘) always returns a floating-point result, even when it divides two integers. The floor division operator (‘//‘), on the other hand, always returns a result as an integer, truncating any fractional parts. It works with both integers and floats.

These operators can be combined into complex expressions according to the standard rules of precedence in mathematics: parentheses, exponentiation, multiplication and division (from left to right), and then addition and subtraction (from left to right).

In addition, Python provides several built-in math functions that make it easy to perform various mathematical operations. Here are some of them:

Function

Description

abs(x)

Returns the absolute value of ‘x‘.

divmod(x, y)

Returns the quotient and the remainder when ‘x‘ is divided by ‘y‘.

(x // y, x % y)

pow(x, y [, z])

Returns ‘x‘ raised to the power of ‘y‘,

if ‘z‘ is present, ‘x‘ raised to the power of ‘y‘ modulo ‘z‘.

round(x [, n])

Round a number ‘x‘ to a given precision ‘n‘.

sum(iterable, start)

Returns the sum of the elements in an iterable object – if ‘start‘ is given, it will be added to the sum.

These built-in functions are easily accessible, efficient, and do not require importing additional modules. For more advanced mathematical operations, Python also provides the ‘math’ module, which includes functions such as sine, cosine, tangent, primes, logarithms, and many others.

Bitwise operators

Bitwise operators work at the level of individual bits of a number, operate only on integers, and are used primarily in low-level programming, cryptography, and graphics. Python provides a set of standard bitwise operators.

Operation

Description

x & y

Bitwise AND – Compares every bit of two operands. If both bits are 1, the resulting bit is 1; otherwise, the resulting bit is 0.

x | y

Bitwise OR – Compares every bit of two operands. If at least one of the bits is set to 1, the resulting bit is set to 1.

x ^ y

Bitwise XOR – Compares every bit of two operands. If exactly one of the bits is set to 1, the resulting bit is set to 1.

~x

Bitwise NOT – Inverts the bits of the number.

x << y

Left Shift – Shifts the bits of the number to the left by a specified number of positions. Positions that appear on the right after the shift are filled with zeros.

x >> y

Right Shift – Shifts the bits of the number to the right by a specified number of position

These operations are fast and efficient, but using them requires an understanding of how numbers are represented at the bit level. They are particularly useful in cases where direct operations on bits are required.

Compound assignment operators

Compound assignment operators allow you to perform a mathematical operation and assign the result to a variable in one step.

For example, it is common to write an expression that updates the value:

x = x + 1

Instead, one can write the following abbreviated version:

x += 1

Such an abbreviated form can be used with any operators such as:

+, , *, **, /, //, %, &, |, ^, <<, >>

Compound assignment operators make the code more concise and are often considered more readable, especially when variable values are frequently updated.

Comparison operators

In Python, there are eight comparison operators that can be used to compare values.

Operation

Description

x == y

Equality – checks if two values are equal.

x != y

Inequality – checks if two values are different.

x < y

Less than – checks if the left value is less than the right value.

x > y

Greater than – checks if the left value is greater than the right value.

x <= y

Less than or equal to – checks if the left value is less than or equal to the right value.

x >= y

Greater than or equal – checks if the left value is greater than or equal to the right value.

is

Identity – checks whether two variables point to the same object (not just value equality).

is not

Non-identity – checks that two variables do not point to the same object.

Comparison operators play a key role in controlling the flow of a program by returning Boolean values – ‘True’ if the comparison condition is met, or ‘False’ otherwise. Based on these returned Boolean values, decisions are made in conditional statements and loops, allowing you to control the flow of the program based on the result of comparisons.

Logical operators

In Python, there are three main logical operators that are used to create more complex conditions by combining simpler logical expressions.

Operation

Description

x and y

Returns ‘True‘ if both expressions are true. If the first expression is false, the second expression is not evaluated (known as short-circuiting).

x or y

Returns ‘True‘ if at least one of the expressions is true. If the first expression is true, the second expression is not evaluated (known as short-circuiting).

not x

Returns ‘False‘ if the expression is true and ‘True‘ if it is false. This is the negation operator.

Logical operators are often used in conditional statements and loops to combine different conditions and create more complex criteria that control the flow of the program.

Zbigniew Marcinkowski - IamZIBI.com

Zbigniew Marcinkowski

My name is Zbigniew, but you can call me ZiBi. I have been fascinated by technology since my childhood. At that time I tended to break things rather than fix them. Everything changed when I got my first computer. I found my passion, which has stayed with me through good times and bad. You can read more about this on the "About me" page.

Leave a Comment