Back to: Python Programming
Introduction
Operators in Python are symbols that are used to perform operations on one or more operands, such as variables or values. In this tutorial, we’ll explore the different types of operators available in Python.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations on numerical values. Here are the arithmetic operators in Python:
Operator | Description |
---|---|
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
// | Floor division |
% | Modulus (remainder) |
** | Exponentiation |
Here’s an example of using arithmetic operators in Python:
a = 5
b = 3
print(a + b) # Output: 8
print(a - b) # Output: 2
print(a * b) # Output: 15
print(a / b) # Output: 1.6666666666666667
print(a // b) # Output: 1
print(a % b) # Output: 2
print(a ** b) # Output: 125
In this example, we define two variables a
and b
with the values 5
and 3
, respectively. We then use the arithmetic operators to perform various mathematical operations on these values and print the results to the console.
Comparison Operators
Comparison operators are used to compare two values and return a Boolean value (True
or False
). Here are the comparison operators in Python:
Operator | Description |
---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Here’s an example of using comparison operators in Python:
a = 5
b = 3
print(a == b) # Output: False
print(a != b) # Output: True
print(a > b) # Output: True
print(a < b) # Output: False
print(a >= b) # Output: True
print(a <= b) # Output: False
In this example, we define two variables a
and b
with the values 5
and 3
, respectively. We then use the comparison operators to compare these values and print the results to the console.
Logical Operators
Logical operators are used to combine two or more Boolean values and return a Boolean value. Here are the logical operators in Python:
Operator | Description |
---|---|
and | Logical AND |
or | Logical OR |
not | Logical NOT |
Here’s an example of using logical operators in Python:
a = True
b = False
print(a and b) # Output: False
print(a or b) # Output: True
print(not a) # Output: False
In this example, we define two Boolean variables a
and b
with the values True
and False
, respectively. We then use the logical operators to combine these values and print the results to the console.
Assignment Operators
Assignment operators in Python are used to assign values to variables. They allow you to perform an operation and assign the result to a variable in a single step. In this tutorial, we’ll explore the different types of assignment operators available in Python.
Basic Assignment Operator
The basic assignment operator in Python is the =
operator. It is used to assign a value to a variable. Here’s an example:
x = 5
In this example, the value 5
is assigned to the variable x
using the =
operator.
Compound Assignment Operators
Compound assignment operators in Python combine an arithmetic operation with the basic assignment operator. They allow you to perform an operation and assign the result to a variable in a single step. Here are the compound assignment operators in Python:
Operator | Example | Equivalent to |
---|---|---|
+= | x += 5 | x = x + 5 |
-= | x -= 5 | x = x – 5 |
*= | x *= 5 | x = x * 5 |
/= | x /= 5 | x = x / 5 |
%= | x %= 5 | x = x % 5 |
//= | x //= 5 | x = x // 5 |
**= | x **= 5 | x = x ** 5 |
Here’s an example of using compound assignment operators in Python:
x = 5
x += 3 # Equivalent to x = x + 3
print(x) # Output: 8
x = 5
x -= 3 # Equivalent to x = x - 3
print(x) # Output: 2
x = 5
x *= 3 # Equivalent to x = x * 3
print(x) # Output: 15
x = 5
x /= 3 # Equivalent to x = x / 3
print(x) # Output: 1.6666666666666667
x = 5
x %= 3 # Equivalent to x = x % 3
print(x) # Output: 2
x = 5
x //= 3 # Equivalent to x = x // 3
print(x) # Output: 1
x = 5
x **= 3 # Equivalent to x = x ** 3
print(x) # Output: 125
In this example, we define a variable x
with the value 5
. We then use compound assignment operators to perform various operations on the value of x
and assign the results back to x
.
Bitwise Assignment Operators
Bitwise assignment operators in Python perform bitwise operations on integers and assign the result to a variable. Here are the bitwise assignment operators in Python:
Operator | Example | Equivalent to |
---|---|---|
&= | 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 |
Here’s an example of using bitwise assignment operators in Python:
# Bitwise AND assignment operator
x = 15
x &= 3 # Equivalent to x = x & 3
print(x) # Output: 3
# Bitwise OR assignment operator
x = 15
x |= 8 # Equivalent to x = x | 8
print(x) # Output: 15
# Bitwise XOR assignment operator
x = 15
x ^= 3 # Equivalent to x = x ^ 3
print(x) # Output: 12
# Bitwise left shift assignment operator
x = 15
x <<= 2 # Equivalent to x = x << 2
print(x) # Output: 60
# Bitwise right shift assignment operator
x = 15
x >>= 1 # Equivalent to x = x >> 1
print(x) # Output: 7
In this example, we first assign the value 15
to the variable x
. Then we use the different bitwise assignment operators to perform various bitwise operations on x
and assign the results back to x
. We print the value of x
after each operation to see the changes.
Note that each of these bitwise assignment operators can also be combined with other arithmetic or assignment operators to perform more complex operations on the value of x
. For example, x += 3
would add 3
to the value of x
using the regular addition assignment operator.
Follow us on social media
Follow Author