Python operators are symbols or characters that are used to perform operations on one or more values or variables. There are several types of operators in Python, including arithmetic operators, comparison operators, logical operators, assignment operators, bitwise operators, and membership operators.
- Arithmetic Operators Arithmetic operators are used to perform mathematical operations. The common arithmetic operators in Python are:
- Addition (+): adds two values or variables
- Subtraction (-): subtracts one value or variable from another
- Multiplication (*): multiplies two values or variables
- Division (/): divides one value or variable by another
- Modulus (%): returns the remainder after division
- Exponentiation (**): raises a value or variable to a power
- Comparison Operators Comparison operators are used to compare two values or variables. The common comparison operators in Python are:
- Equal to (==): returns True if two values or variables are equal
- Not equal to (!=): returns True if two values or variables are not equal
- Greater than (>): returns True if one value or variable is greater than another
- Less than (<): returns True if one value or variable is less than another
- Greater than or equal to (>=): returns True if one value or variable is greater than or equal to another
- Less than or equal to (<=): returns True if one value or variable is less than or equal to another
- Logical Operators Logical operators are used to combine two or more conditions. The common logical operators in Python are:
- And (and): returns True if both conditions are True
- Or (or): returns True if at least one condition is True
- Not (not): returns True if the condition is False, and False if the condition is True
- Assignment Operators Assignment operators are used to assign values to variables. The common assignment operators in Python are:
- Equals (=): assigns a value to a variable
- Plus equals (+=): adds a value to the variable and assigns the result to the variable
- Minus equals (-=): subtracts a value from the variable and assigns the result to the variable
- Multiplication equals (*=): multiplies the variable by a value and assigns the result to the variable
- Division equals (/=): divides the variable by a value and assigns the result to the variable
- Modulus equals (%=): calculates the modulus of the variable and a value, and assigns the result to the variable
- Bitwise Operators Bitwise operators are used to perform bitwise operations on binary numbers. The common bitwise operators in Python are:
- Bitwise AND (&): returns a 1 in each bit position where both corresponding bits are 1
- Bitwise OR (|): returns a 1 in each bit position where at least one corresponding bit is 1
- Bitwise XOR (^): returns a 1 in each bit position where only one corresponding bit is 1
- Bitwise NOT (~): inverts all the bits
- Left shift (<<): shifts the bits to the left by a specified number of positions
- Right shift (>>): shifts the bits to the right by a specified number of positions
- Membership Operators Membership operators are used to test whether a value or variable is a member of a sequence or a set. The common membership operators in Python are:
- In (in): returns True if a value or variable is found in a sequence or a set
- Not in (not in): returns True if a value or variable is not found in a sequence or a set
These are the main types of operators in Python, and understanding them is essential for writing effective code in the language.