Python Operator
In Python, operators are symbols or special characters that perform operations on operands (values or variables). Python provides a wide range of operators to perform various tasks, such as mathematical calculations, logical operations, comparisons, and more. Here are some commonly used operators in Python:
In Python, operators are symbols or special characters that perform operations on operands (values or variables). Python provides a wide range of operators to perform various tasks, such as mathematical calculations, logical operations, comparisons, and more. Here are some commonly used operators in Python:
Arithmetic Operators:
- Addition (+): Adds two operands.
- Subtraction (-): Subtracts the second operand from the first.
- Multiplication (*): Multiplies two operands.
- Division (/): Divides the first operand by the second.
- Modulus (%): Returns the remainder after division.
- Exponentiation (**): Raises the first operand to the power of the second.
- Floor Division (//): Returns the integer value of the division result.
Assignment Operators:
- Assignment (=): Assigns a value to a variable.
- Addition Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.
- Subtraction Assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
- Multiplication Assignment (*=): Multiplies the right operand with the left operand and assigns the result to the left operand.
- Division Assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.
- Modulus Assignment (%=): Performs modulus on the left operand with the right operand and assigns the result to the left operand.
- Exponentiation Assignment (**=): Raises the left operand to the power of the right operand and assigns the result to the left operand.
- Floor Division Assignment (//=): Performs floor division on the left operand with the right operand and assigns the result to the left operand.
Comparison Operators:
- Equal to (==): Checks if the operands are equal.
- Not equal to (!=): Checks if the operands are not equal.
- Greater than (>): Checks if the left operand is greater than the right operand.
- Less than (<): Checks if the left operand is less than the right operand.
- Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
- Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.
Logical Operators:
- Logical AND (and): Returns True if both operands are True.
- Logical OR (or): Returns True if at least one of the operands is True.
- Logical NOT (not): Returns the opposite of the operand’s logical value.
Bitwise Operators:
- Bitwise AND (&): Performs bitwise AND operation on the operands.
- Bitwise OR (|): Performs bitwise OR operation on the operands.
- Bitwise XOR (^): Performs bitwise XOR (exclusive OR) operation on the operands.
- Bitwise NOT (~): Performs bitwise NOT (complement) operation on the operand.
- Left Shift (<<): Shifts the bits of the left operand to the left by the number of positions specified by the right operand.
- Right Shift (>>): Shifts the bits of the left operand to the right by the number of positions specified by the right operand.