Python Operators
Python Operators
Python Operators
Topics to be covered:
Python Operator
Operators Type
Operators Precedenc
Operators associativity
Python Operators:
Python operators are symbols or special keywords that are used to perform operations on values or
variables
In Python programming, Operators allow you to manage data, do computations, and make decisions.
Operators Types:
There are several types of python operators as follows:
2.1. Arithmetic Operators: Arithmetic operators are used for mathematical operations.
2.2. Comparison Operators: Comparison operators are used to compare two values and return a
Boolean result (True or False).
Greater than or equal to (>=): Checks if one value is greater than or equal to another.
Less than or equal to (<=): Checks if one value is less than or equal to another.
2.3. Logical Operators: Logical operators are used to combine and manipulate Boolean values.
Logical NOT (not): Negates a condition (True becomes False, and False becomes True).
2.4. Bitwise Operators: In Python, bitwise operators are used to perform operations at the bit-level,
manipulating individual bits within integers.
Bitwise AND (&): Returns 1 for each bit position where both operands have a 1.
Example:
Bitwise OR (|): Returns 1 for each bit position where at least one of the operands has a 1.
OR Operation Table
Bitwise NOT (~): Inverts the bits; 1s become 0s and vice versa.
Example:
Bitwise XOR (^): Returns 1 for each bit position where exactly one of the operands has a 1.
Left Shift (<<): Shifts the bits to the left by a specified number of positions, filling with 0s on the right.
Right Shift (>>): Shifts the bits to the right by a specified number of positions, filling with 0s on the left (for non-
negative numbers) or sign bits (for negative numbers).
2.5. Assignment Operators: Assignment operators are used to assign values to variables.
Addition Assignment (+=): Adds a value to the variable and updates it.
Multiplication Assignment (*=): Multiplies the variable by a value and updates it.
Division Assignment (/=): Divides the variable by a value and updates it.
2.6. Membership Operators: Membership operators are used to test if a value is present in a sequence.
2.7. Identity Operators: Identity operators are used to compare the memory location of two objects.
Operator’s Precedence:
Python operators have different levels of precedence, which determine the order in which they are
evaluated in an expression. Higher precedence operators are evaluated first
Here's a simplified Python operator precedence table, arranged from highest precedence to lowest
precedence:
Example:
Example:
Example:
5. Addition +, Subtraction :
Example:
Example:
Example:
Operators Associativity:
Operator associativity in Python is an important concept that determines how operators are grouped and
evaluated in expressions. Here's a concise explanation in four bullet points:
1. Order of Evaluation: Operator associativity defines the order in which operators of the same precedence are
evaluated within an expression. It specifies whether operators are evaluated from left to right or right to left.
2. Left-to-Right Default: By default, most operators in Python have left-to-right associativity, which means they
are evaluated from left to right when they appear in sequence in an expression.
3. Right-to-Left Associativity: Some operators, like the exponentiation operator `**`, have right-to-left
associativity, meaning they are evaluated from right to left. This can affect the order of operations in
expressions with multiple operators of the same precedence.
4. Control with Parentheses: If you need to override the default associativity and explicitly control the order of
evaluation, you can use parentheses `()` to group operations. This allows you to ensure that specific operations
are performed before others.