Operators and Expressions
Absolutely, let's dive into the topic of operators and expressions in Python:
1. Arithmetic Operators:
- Addition (+), subtraction (-), multiplication (*), division (/).
- Integer division (//) for floor division.
- Modulus (%) for remainder.
- Exponentiation (**) for raising to a power.
2. Comparison Operators:
- Equal to (==), not equal to (!=).
- Greater than (>), less than (<).
- Greater than or equal to (>=), less than or equal to (<=).
3. Logical Operators:
- Logical AND (and), logical OR (or), logical NOT (not).
- Short-circuiting behavior of and and or operators.
4. Assignment Operators:
- Assignment (=) for assigning values to variables.
- Compound assignment operators (+=, -=, *=, /=, //=, %=).
- Chaining assignments (a = b = c = 10).
5. Bitwise Operators:
- Bitwise AND (&), bitwise OR (|), bitwise XOR (^).
- Bitwise NOT (~), left shift (<<), right shift (>>).
6. Identity Operators:
- is and is not for checking object identity.
- Comparing memory addresses of objects.
7. Membership Operators:
- in and not in to check membership in sequences (lists, tuples, strings).
8. Order of Operations (Precedence):
- Understanding how operators are evaluated based on precedence.
- Using parentheses to control evaluation order.
9. Expressions and Evaluation:
- What is an expression?
- Combining operators and operands to create expressions.
10. Boolean Expressions:
- Combining comparison and logical operators to create Boolean expressions.
- Short-circuit evaluation in Boolean expressions.
11. Ternary Conditional Operator:
- Using the ternary operator (expression if condition else expression).
12. Operator Overloading:
- Some operators can behave differently based on data types (e.g., + for addition and concatenation).
- Polymorphism in operator behavior.
13. Math and Built-in Functions:
- Using the math module for advanced mathematical operations.
- Built-in functions for absolute value (abs()), rounding (round()), etc.
14. String Operations:
- Concatenating strings using the + operator.
- Repetition using the * operator.
- Combining strings with variables using f-strings.
15. Order of Evaluation:
- Understanding how expressions are evaluated from left to right.
- Overriding the default order with parentheses.
16. Operator Precedence:
- Knowing the precedence of various operators to avoid ambiguity.
- Using parentheses to explicitly specify evaluation order.