Python operators are symbols in a program that execute certain operations on values and variables. They are able of doing mathematical operations, compare values, combine conditions, and perform bitwise operations.
Some common types of Python operators are:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
Arithmetic operators
Python has a number of arithmetic operators that you can use to perform mathematical operations on numbers. These include:
-
+
: Addition -
-
: Subtraction -
*
: Multiplication -
/
: Division -
%
: Modulus (remainder after division) -
**
: Exponentiation (raise to the power of) -
//
: Integer division (division that results in an integer)
Here are a few examples of how you can use these operators:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | # Addition x = 5 + 3 print(x) # Output: 8 # Subtraction x = 5 - 3 print(x) # Output: 2 # Multiplication x = 5 * 3 print(x) # Output: 15 # Division x = 5 / 3 print(x) # Output: 1.6666666666666667 # Modulus x = 5 % 3 print(x) # Output: 2 # Exponentiation x = 5 ** 3 print(x) # Output: 125 # Integer division x = 5 // 3 print(x) # Output: 1 |
Assignment Operators
In Python, we use assignment operators to assign values to variables.
1 | x = 10 |
In addition to the basic assignment operator, there are also compound assignment operators, which perform an operation and then assign the result to a variable in a single step.
The compound assignment operators in Python are:
-
+=
(add and assign) -
-=
(subtract and assign) -
*=
(multiply and assign) -
/=
(divide and assign) -
%=
(modulus and assign) -
//=
(floor divide and assign) -
**=
(exponentiate and assign)
For example:
1 2 3 | x = 10 x += 5 print(x) # Output: 15 |
Comparison Operators:
In Python, comparison operators are used to comparing values and return a Boolean value (True or False).
The comparison operators are:
-
==
(equal to) -
!=
(not equal to) -
>
(greater than) -
<
(less than) -
>=
(greater than or equal to) -
<=
(less than or equal to)
For example:
1 2 3 4 5 6 7 | x = 10 y = 20 print(x == y) # Output: False print(x != y) # Output: True print(x > y) # Output: False print(x < y) # Output: True |
Comparison operators can be used to compare values of different data types, like integers, floats, and strings. However, the result of comparison between different data types may not always be what you expect. It is recommended to have a clear understanding of data types and type conversions before using comparison operators.
Logical operators:
In Python, logical operators are used to combining multiple conditions in an expression and return a Boolean value (True or False).
The logical operators are:
-
and
(returns True if both conditions are True) -
or
(returns True if one of the conditions is True) -
not
(inverts the truthiness of a value)
For example:
1 2 3 4 5 6 | x = 10 y = 20 print(x > 5 and y > 15) # Output: True print(x > 5 or y < 15) # Output: True print(not(x > 5)) # Output: False |
It is important to understand the truthiness of values in Python, as values other than
True
and
False
are evaluated as truthy or falsy based on certain conditions. For example, any non-zero number is considered truthy, and zero is false.
Identity operators
In Python, identity operators are used to comparing the memory locations of objects to determine if they are the same object or not.
The identity operators are:
-
is
(returns True if both operands refer to the same object) -
is not
(returns True if both operands do not refer to the same object)
For example:
1 2 3 4 5 6 7 | x = [1, 2, 3] y = [1, 2, 3] z = x print(x is y) # Output: False print(x is z) # Output: True print(x is not y) # Output: True |
In the above example,
x
and
y
are two different lists that have the same elements, so
x is y
returns
False
. However,
x
and
z
refer to the same list, so
x is z
returns
True
.
Membership operators
In Python, membership operators are used to testing if a value is found in a sequence (such as a string, list, tuple, etc.).
The membership operators are:
-
in
(returns True if a value is found in a sequence) -
not in
(returns True if a value is not found in a sequence)
For example:
1 2 3 4 5 | x = [1, 2, 3] y = 10 print(y in x) # Output: False print(y not in x) # Output: True |
Membership operators are often used to check if a value is found in a sequence, before performing further operations. For example, you can use a membership operator to check if an item exists in a list before appending it to the list.
Bitwise operators:
In Python, we use bitwise operators to perform bit-level operations on integers.
The bitwise operators are:
-
&
(bitwise AND) -
|
(bitwise OR) -
^
(bitwise XOR) -
~
(bitwise NOT) -
<<
(left shift) -
>>
(right shift)
For example:
1 2 3 4 5 6 7 8 9 | x = 10 # binary representation: 00001010 y = 4 # binary representation: 00000100 print(x & y) # Output: 0 print(x | y) # Output: 14 print(x ^ y) # Output: 14 print(~x) # Output: -11 print(x << 2) # Output: 40 print(x >> 2) # Output: 2 |
Bitwise operators are used in low-level programming and system programming but are less commonly used in higher-level programming tasks. It is important to have a basic understanding of binary numbers and bit manipulation before using bitwise operators.