Introduction
- What are operators?
- Special symbols or keywords that perform specific operations on values.
- Essential for manipulating data and making decisions.
- Types: arithmetic, comparison, logical, assignment, bitwise, identity, membership, and ternary.
Arithmetic Operators
- Used for performing mathematical calculations.
+
: Addition (e.g.,x + y
)-
: Subtraction (e.g.,x - y
)*
: Multiplication (e.g.,x * y
)/
: Division (e.g.,x / y
)//
: Floor division (returns integer quotient) (e.g.,x // y
)%
: Modulus (returns remainder) (e.g.,x % y
)**
: Exponentiation (e.g.,x ** y
)
Logical Operators
- Used to combine conditional statements.
and
: Returns True if both operands are True (e.g.,x > 5 and y < 10
)or
: Returns True if at least one operand is True (e.g.,x > 5 or y < 10
)not
: Reverses the logical state of its operand (e.g.,not(x > 5)
)
Detailed Explanation: Logical Operators
and
operator:- Evaluates both operands.
- Returns True only if both operands are True.
- Example:Python
x = 10 y = 5 if x > 5 and y < 10: print("Both conditions are True")
or
operator:- Evaluates both operands.
- Returns True if at least one operand is True.
- Example:Python
x = 10 y = 2 if x > 5 or y > 5: print("At least one condition is True")
not
operator:- Inverts the logical state of its operand.
- Returns True if the operand is False, and vice versa.
- Example:Python
x = 5 if not(x > 10): print("x is not greater than 10")