When working with Python, operators play a crucial role in manipulating values and variables. In Python, operators can be categorized based on the type of operation they perform. These categories include assignment operators, arithmetic operators, comparison operators, logical operators, bitwise operators, as well as some interesting operators like ‘is’ and ‘in’.
Assignment Operator
The assignment operator is used to assign a value to a variable or to assign the value of one variable to another variable. For example:
age = 8
With Python 3.8, the ‘:=’ (walrus operator) can be used to assign a value to a variable as part of another operation, such as an ‘if’ statement or the conditional part of a loop.
Arithmetic Operators
Python provides several arithmetic operators, including addition (+), subtraction (-), multiplication (*), division (/), remainder (%), exponentiation (**), and floor division (//). These operators allow you to perform basic mathematical operations. For example:
1 + 1 # 2 2 - 1 # 1 2 * 2 # 4 4 / 2 # 2 4 % 3 # 1 4 ** 2 # 16 4 // 2 # 2
It’s worth noting that you don’t need to include a space between the operands, but it’s good practice for readability. Additionally, the minus sign (-) can also be used as a unary operator to indicate a negative value. The plus sign (+) can be used to concatenate string values.
Comparison Operators
Python also provides a range of comparison operators, including equality (==), inequality (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). These operators allow you to compare values and variables, and the result is a boolean value (True or False). For example:
a = 1 b = 2
a == b # False a != b # True a > b # False a <= b # True
Logical Operators
Python offers three logical operators: not, and, and or. These operators are used with boolean values (True or False) and are often used in conditional expressions, such as the ‘if’ statement. For example:
condition1 = True condition2 = False
not condition1 # False condition1 and condition2 # False condition1 or condition2 # True
It’s important to note that the ‘or’ operator returns the value of the first operand that is not a falsy value (e.g., False, 0, ‘’, []). If all the operands are falsy, it returns the last operand. On the other hand, the ‘and’ operator only evaluates the second operand if the first one is true.
Bitwise Operators
Bitwise operators are used to manipulate binary numbers or bits. These operators include binary AND (&), binary OR (|), binary XOR (^), binary NOT (~), left shift («), and right shift (»). These operators are typically used in specific situations and are less commonly used compared to other operators.
‘is’ and ‘in’ Operators
The ‘is’ operator, known as the identity operator, is used to compare whether two objects are the same. It returns True if both objects are the same object.
The ‘in’ operator, known as the membership operator, is used to check if a value is present in a list or another sequence.
Overall, understanding Python operators is essential for efficient programming. These operators allow you to perform various operations and comparisons, making your code more flexible and powerful.