Python operators

by Alex
Python operators

This tutorial will introduce you to the operators of the Python programming language. You’ll learn about arithmetic, logical, and bitwise operators, as well as comparison, assignment, membership, and identity operators and their syntax. All of this will be illustrated with examples. An operator in Python is a character that performs an operation on one or more operands. The operand is the variable or value on which the operation is performed.

Introduction to Python Operators

Python operators come in 7 types:

  • Arithmetic operators
  • Comparison Operators
  • Assignment operators
  • Boolean operators
  • Identity operators
  • Identity operators
  • Bit operators

Python Arithmetic Operators

This type includes operators for performing basic arithmetic operations.

Addition (+)

Adds a value on either side of the operator. Example:

>>> 3+4
7

Subtraction (-)

Subtracts the value in the right side from the value in the left side. Example:

>>> 3-4
-1

Multiplication (*)

Multiplies values on both sides of the operator. Example:

>>> 3*4
12

Division (/)

Divides the left side value by the right side value. The data type of the division result is a floating point number. Example:

>>> 3/4
0.75

Exponentiation (**)

Expands the first number to the power of the second number. Example:

>>> 3**4
81

Division without a remainder (//)

Performs division and returns the integer value of the quotient, stripping the digits after the decimal point. Example:

>>> 4//3
1
>>> 10//3
3

Modulo division (remainder of division) (%)

Performs division and returns the remainder. Example:

>>> 3%4
3
>>> 4%3
1
>>> 10%3
1
>>> 10.5%3
1.5

Comparison Operators

Comparison operators in Python compare operands. They tell you whether one is greater than the other, less than, equal to, or both.

Less (<)

This operator tests whether the value on the left is less than the value on the right. Example:

>>> 4<3
False

More (>)

Checks if the value on the left is greater than the value on the right. Example:

>>> 4>3
True

Less than or equal to (<=)

Checks whether the left-hand side is less than or equal to the right-hand side. Example:

>>> 7<=7
True

More or equal (>=)

Checks whether the left-hand side is greater than or equal to the right-hand side. Example:

>>> 0>=0
True

Equals (==)

This operator checks to see if the value on the left is equal to the value on the right. 1 equals Boolean True and 2 (double) does not. 0 equals False. Example:

>>> 3==3.0
True
>>> 1==True
True
>>> 7==True
False
>>> 0==False
True
>>> 0.5==True
False

Not equal (!=)

Checks to see if the value on the left equals the value on the right. The operator performs the same task, but was removed in Python 3. When the condition is met, it returns True. Otherwise, it returns False. This return value can be used in subsequent instructions and expressions. Example:

>>> 1!=1.0
False
>>> 1==True # This causes SyntaxError

Assignment Operators

The assignment operator assigns a value to a variable. It can manipulate the value before the assignment. There are 8 assignment operators: 1 simple and 7 using arithmetic operators.

Assignment (=)

Assigns a value to the right of the left hand side. It is worth noting that == is used for comparison and = is used for assignment. Example:

>>> a = 7
>>> print(a)
7

Addition and assignment (+=)

Sums the value of both sides and assigns it to the expression on the left. a += 10 is the same as a = a + 10. The same goes for all other assignment operators. Example:

>>> a += 2
>>> print(a)
9

Subtraction and assignment (-=)

Subtracts the value on the right from the left and assigns it to the expression on the left. Example:

>>> a -= 2
>>> print(a)
7

Division and assignment (/=)

Divides the value on the left by the value on the right. Then assigns it to the expression on the left. Example:

>>> a /= 7
>>> print(a)
1.0

Multiplication and Assignment (*=)

Multiplies the values of both sides. Then assigns the right to the left. Example:

>>> a *= 8
>>> print(a)
8.0

Modulo division and assignment (%=)

Performs division modulo for both parts. Assigns the result to the left part. Example:

>>> a %= 3
>>> print(a)
2.0

Expanding and assigning (**=)

Multiplies the left part by the value of the right part. Then assigns the value of the left part. Example:

>>> a **= 5
>>> print(a)
32.0

Division with a remainder and assignment (//=)

Performs division with a remainder and assigns the result to the left side. Example:

>>> a //= 3
>>> print(a)
10.0

This is one of the important operators of Python

Python logical operators

These are conjunctions that allow you to combine multiple conditions. There are only three operators in Python: and, or, and not.

And

If the conditions on both sides of the and operator are true, then the entire expression is considered true. Example:

>>> a = 7 > 7 and 2 > -1
>>> print(a)
False

Or

An expression is false if both operands on both sides are false. If at least one of them is true, then the whole expression is true. Example:

>>> a = 7 > 7 or 2 > -1
>>> print(a)
True

Not (not)

This operator inverts the boolean values of an expression. True turns into False and vice versa. In the example below the boolean value 0 is False. Therefore it is converted to True. Example:

>>> a = not(0)
>>> print(a)
True

Accessory operators

These operators check whether a value is part of a sequence. A sequence can be a list, a string, or a tuple. There are only two such operators: in and not in.

In (in)

Checks if the value is a member of a sequence. In this example, you can see that the string fox is not in the list of pets. But cat is, so it returns True. Also the string me is a substring of the sequence. Therefore it will return True. Example:

>>> pets=['dog','cat', 'ferret']
>>> 'fox' in pets
False
>>> 'cat' in pets
True
>>> 'me' in 'disappointment'
True

No in (not in)

This operator checks if a value is NOT a member of a sequence. Example:

>>> 'pot' not in 'disappointment'
True

Identity operators

These operators check if the operands are the same (if they occupy the same position in memory).

This (is)

If the operands are identical, it returns True. Otherwise, it returns False. Here 2 is not 20, so it will return False. But '2' is the same as '2‘. The different quotes do not change the objects themselves, so True will be returned. Example:

>>> 2 is 20
False
>>> '2' is "2"
True

It is not (is not)

2 is a number, and '2' is a string. Therefore, it will return True. Example:

>>> 2 is not '2'
True

Python bit operators

These operators work on operands bit by bit.

Binary AND (&)

Performs a bitwise and operation on two values. Here, binary 2 is 10 and 3 is 11. The result of a bitwise and is 10 – binary 2. A bitwise and over 011(3) and 100(4) produces the result 000(0). Example:

>>> 2&3
2
>>> 3&4
0

Binary OR (|)

Performs a bitwise or operation on two values. Here or for 10(2) and 11(3) returns 11(3). Example:

>>> 2|3
3

Binary OR NO (^)

Performs a bitwise xor (exclusive or) operation on two values. Here the result of OR NOT for 10(2) and 11(3) is 01(1). Example:

>>> 2^3
1

Inverting operator (~)

This operator returns inverted binary numbers. In other words, it flips bits. Bit 2 is 00000010. Its inverted version is 11111101. It is a binary -3. Therefore, the result is -3. In a similar way, ~1 equals -2. Example:

>>> ~-3
2

Once again, an inverted -3 is a 2.

Binary shift to the left (<<)

It shifts the value of the left operand to the position that is specified on the right. Thus, a binary 2 is 10. 2 << 2 will move the value two positions to the left and come out 1000 is binary 8. Example:

>>> 2<<2
8

Binary shift to the right (>>)

Shifts the value of the left operator to the position specified on the right. For example, a binary 3 is 11. 3 >> 2 will shift the value two positions to the right. The result is 00, which is 0. 3 >> 1 will shift one position to the right, and the result is 01 – binary 1. Example:

>>> 3>>2
>>> 3>>1
1

Conclusions

This lesson has covered all 7 types of Python operators. An example in the IDE has been given for each. You need to continue working with them, using them in conditional constructions and combining them to understand how they work.

Related Posts

LEAVE A COMMENT