What are operators?
In programming, operators are special symbols or keywords that perform operations on one or more operands. An operand is a value or a variable on which an operation is performed.
Types of Operators
Here are some common types of operators:
Arithmetic Operators:
Perform basic arithmetic operations like addition, subtraction, multiplication, division, etc.
Example:
+, -, *, /, %
(modulo),**
(exponentiation).a = 10 b = 3 # Arithmetic operations sum_result = a + b difference_result = a - b product_result = a * b division_result = a / b remainder_result = a % b exponentiation_result = a ** b
Comparison Operators:
Compare two values and return a Boolean result (
True
orFalse
).Example:
==
(equal),!=
(not equal),<
,>
,<=
,>=
.x = 5 y = 8 # Comparison operations is_equal = x == y not_equal = x != y greater_than = x > y less_than_or_equal = x <= y
Logical Operators:
Combine Boolean values and return a Boolean result.
Example:
and
,or
,not
.p = True q = False # Logical operations logical_and = p and q logical_or = p or q logical_not = not p
Assignment Operators:
Assign values to variables.
Example:
=
,+=
,-=
,*=
,/=
,%=
,**=
.x = 10 # Assignment operations x += 5 # Equivalent to x = x + 5
Bitwise Operators:
Perform operations on binary representations of integers.
Example:
&
(bitwise AND),|
(bitwise OR),^
(bitwise XOR),<<
(left shift),>>
(right shift).a = 5 b = 3 # Bitwise operations bitwise_and = a & b bitwise_or = a | b bitwise_xor = a ^ b left_shift = a << 1 right_shift = a >> 1
Identity Operators: They are used to compare the memory locations of two objects. They check if two variables refer to the same object in memory. There are two identity operators in Python:
is:
The
is
operator returnsTrue
if both operands refer to the same object in memory; otherwise, it returnsFalse
.x = [1, 2, 3] y = [1, 2, 3] z = x print(x is y) # False, different objects print(x is z) # True, same object as z
is not:
The
is not
operator returnsTrue
if both operands do not refer to the same object in memory; otherwise, it returnsFalse
.a = "hello" b = "world" c = a print(a is not b) # True, different objects print(a is not c) # False, same object as c
Membership operators: Membership operators are used to test whether a value or variable is a member of a sequence, such as a string, list, tuple, or set. There are two membership operators in Python:
in:
The
in
operator returnsTrue
if the specified value is found in the sequence; otherwise, it returnsFalse
.numbers = [1, 2, 3, 4, 5] print(3 in numbers) # True print(6 in numbers) # False
not in:
The
not in
operator returnsTrue
if the specified value is not found in the sequence; otherwise, it returnsFalse
.fruits = ["apple", "banana", "orange"] print("banana" not in fruits) # False print("grape" not in fruits) # True
Precedence of operators
Operator precedence refers to the order in which operations are performed in an expression. When an expression contains multiple operators, precedence determines which operations are performed first.
If operators have the same precedence, the associativity comes into play, determining the order of evaluation.
In Python, and many other programming languages, operators have different levels of precedence. Operators with higher precedence are evaluated before those with lower precedence. For example, multiplication has a higher precedence than addition, so in the expression 2 + 3 * 4
, the multiplication (*
) is performed first.
Here are some common operators and their general precedence (from highest to lowest):
Parentheses:
()
Exponentiation:
**
Unary Plus and Minus (Sign):
+x
,-x
Multiplication, Division, and Remainder:
*
,/
,%
Addition and Subtraction:
+
,-
Bitwise Shifts:
<<
,>>
Bitwise AND:
&
Bitwise XOR:
^
Bitwise OR:
|
Comparison Operators:
<
,<=
,>
,>=
,==
,!=
Logical NOT:
not
Logical AND:
and
Logical OR:
or
Conditional Expression (Ternary Operator):
x if condition else y
Operator precedence can be overridden by using parentheses. Expressions inside parentheses are evaluated first. For example, (2 + 3) * 4
ensures that the addition is performed before the multiplication.
Understanding operator precedence is crucial for writing correct and predictable expressions in your code. It helps you avoid unexpected behavior and ensures that expressions are evaluated in the intended order.
What's next?
In the next article in this blog series, we will look into conditional handling in Python.
Stay tuned.