Arithmetic and Logical Operators

Operators are specific symbols used to perform operations on variables and values. In Python, operators are categorized based on the type of operation they perform: arithmetic, comparison, logical, and assignment.


1. Arithmetic Operators

Arithmetic operators are used with numeric values to perform common mathematical operations.

Operator Name Example
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y (Always returns a float)
// Floor Division x // y (Returns the largest integer)
% Modulus x % y (Remainder of division)
** Exponentiation x ** y (Power)

2. Comparison Operators

Comparison operators compare two values and return a Boolean (True or False). These are fundamental for decision-making in code.


3. Logical Operators

Logical operators are used to combine conditional statements.


4. Identity and Membership Operators

These are unique to Python and provide a more readable way to check for equality and existence.

list1 = [1, 2, 3]
list2 = [1, 2, 3]

print(list1 == list2) # True (Values are the same)
print(list1 is list2) # False (They are different objects in memory)

Interview Pro-Tips: The "Short-Circuit" Logic

In Python, logical operators and and or use Short-Circuit Evaluation.