Python Operators
Operators in Python are used to perform various operations on variables and values. Python provides a wide range of operators, categorized into different groups. Here, we will explore the main groups of Python operators.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations on numeric values:
+
Addition
x + y
Adds two values
-
Subtraction
x - y
Subtracts the second value from the first
*
Multiplication
x * y
Multiplies two values
/
Division
x / y
Divides the first value by the second
%
Modulus
x % y
Returns the remainder of the division of the first value by the second
**
Exponentiation
x ** y
Raises the first value to the power of the second
//
Floor Division
x // y
Returns the floor of the division of the first value by the second
Assignment Operators
Assignment operators are used to assign values to variables:
=
x = 5
x = 5
+=
x += 3
x = x + 3
-=
x -= 3
x = x - 3
*=
x *= 3
x = x * 3
/=
x /= 3
x = x / 3
%=
x %= 3
x = x % 3
//=
x //= 3
x = x // 3
**=
x **= 3
x = x ** 3
&=
x &= 3
x = x & 3
|=
x |= 3
x = x
^=
x ^= 3
x = x ^ 3
>>=
x >>= 3
x = x >> 3
<<=
x <<= 3
x = x << 3
Comparison Operators
Comparison operators are used to compare two values:
==
Equal
x == y
Returns True
if both values are equal
!=
Not Equal
x != y
Returns True
if the values are not equal
>
Greater Than
x > y
Returns True
if the first value is greater than the second
<
Less Than
x < y
Returns True
if the first value is less than the second
>=
Greater or Equal
x >= y
Returns True
if the first value is greater than or equal to the second
<=
Less or Equal
x <= y
Returns True
if the first value is less than or equal to the second
Logical Operators
Logical operators are used to combine or modify conditional statements:
and
Returns True
if both statements are True
x < 5 and x < 10
or
Returns True
if at least one of the statements is True
x < 5 or x < 4
not
Reverses the result, returns False
if the result is True
not(x < 5 and x < 10)
Identity Operators
Identity operators are used to compare objects' memory locations:
is
Returns True
if both variables are the same object
x is y
is not
Returns True
if both variables are different objects
x is not y
Membership Operators
Membership operators are used to test if a sequence is present in an object:
in
Returns True
if a sequence with the specified value is present in the object
x in y
not in
Returns True
if a sequence with the specified value is not present in the object
x not in y
Bitwise Operators
Bitwise operators are used to compare binary numbers:
&
AND
Sets each bit to 1 if both bits are 1
|
OR
Sets each bit to 1 if one of two bits is 1
^
XOR
Sets each bit to 1 if only one of two bits is 1
~
NOT
Inverts all the bits
<<
| >> | Right Shift | Shifts right by pushing copies of the leftmost bit in from the left |
Operator Precedence
Operator precedence describes the order in which operations are performed. Operators with higher precedence are evaluated first. In case of operators with the same precedence, the expression is evaluated from left to right. Parentheses have the highest precedence.
Some of the key operator precedences are:
Parentheses
()
Exponentiation
**
Unary plus
+
, unary minus-
, bitwise NOT~
Multiplication
*
, division/
, floor division//
, modulus%
Addition
+
, subtraction-
Bitwise left shift
<<
, bitwise right shift>>
Bitwise AND
&
Bitwise XOR
^
Bitwise OR
|
Comparisons, identity, and membership operators
Logical NOT
not
Logical AND
and
Logical OR
or
Understanding operator precedence is crucial for writing correct and efficient Python code.
Remember that you can always use parentheses to explicitly control the order of evaluation in complex expressions.
Practice
Now, try the following exercise:
Exercise: Multiply 10 by 5 and print the result.
Operators play a fundamental role in Python, allowing you to perform various operations on values, make decisions, and control the flow of your programs. Understanding how to use operators and their precedence is essential for effective programming in Python.
Last updated