Algorithmic Thinking and Its Applications
Session Summary: Python Fundamentals
Session Overview
This session focuses on Python operators and collections, providing a comprehensive guide to these fundamental programming concepts. We’ll explore various types of operators including arithmetic, assignment, comparison, logical, and bitwise operators, as well as Python’s collection data types. The session is designed to strengthen your understanding of these core elements and their practical applications in Python programming.
Key Topics Covered
Arithmetic Operators
These operators perform basic mathematical calculations:
| Operator | Description | Example | Result |
|---|---|---|---|
| + | Addition | 5 + 3 | 8 |
| – | Subtraction | 5 – 3 | 2 |
| * | Multiplication | 5 * 3 | 15 |
| / | Division | 5 / 3 | 1.6667 |
| % | Modulus (Remainder) | 5 % 3 | 2 |
| ** | Exponentiation | 5 ** 3 | 125 |
| // | Floor Division | 5 // 3 | 1 |
These operators are commonly used in mathematical operations, financial calculations, and scientific computations.
Assignment Operators
Used to assign values to variables and simplify code in loops and calculations:
| Operator | Equivalent To | Example | Result |
|---|---|---|---|
| = | Assign | x = 5 | x = 5 |
| += | Add & assign | x += 3 | x = 8 |
| -= | Subtract & assign | x -= 3 | x = 5 |
| *= | Multiply & assign | x *= 3 | x = 15 |
| /= | Divide & assign | x /= 3 | x = 5.0 |
Comparison (Relational) Operators
Used to compare values and return True or False, useful in decision-making and loops:
| Operator | Description | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 3 | False |
| != | Not equal to | 5 != 3 | True |
| < | Less than | 5 < 3 | False |
| > | Greater than | 5 > 3 | True |
| <= | Less than or equal to | 5 <= 5 | True |
| >= | Greater than or equal to | 5 >= 3 | True |
Logical Operators
Used for combining Boolean expressions, widely used in conditions and control flow:
| Operator | Description | Example | Result |
|---|---|---|---|
| and | True if both conditions are True | (5 > 3) and (10 > 5) | True |
| or | True if at least one condition is True | (5 > 3) or (10 < 5) | True |
| not | Negates the result | not (5 > 3) | False |
Bitwise Operators
Bitwise operators perform operations on the binary representation of numbers:
| Operator | Description | Example | Result |
|---|---|---|---|
| & | Bitwise AND | 5 & 15 | 5 |
| | | Bitwise OR | 5 | 15 | 15 |
| ^ | Bitwise XOR | 5 ^ 15 | 10 |
| ~ | Bitwise NOT | ~5 | -6 |
| << | Left Shift | 5 << 1 | 10 |
| >> | Right Shift | 20 >> 1 | 10 |
Bitwise operators are often used in low-level programming, data compression, and encryption.
Membership & Identity Operators
Membership operators check if a value exists in a sequence:
| Operator | Description | Example | Result |
|---|---|---|---|
| in | Returns True if value is found in sequence | ‘a’ in ‘abc’ | True |
| not in | Returns True if value is not found in sequence | ‘d’ not in ‘abc’ | True |
Identity operators check if objects share the same memory location:
| Operator | Description | Example | Result |
|---|---|---|---|
| is | Returns True if variables point to same object | x is y | True/False |
| is not | Returns True if variables don’t point to same object | x is not y | True/False |
Operator Precedence
When an expression contains multiple operators, Python evaluates them in a specific order:
| Precedence | Operator(s) | Description |
|---|---|---|
| 1 (Highest) | () | Parentheses |
| 2 | ** | Exponentiation |
| 3 | +x, -x, ~x | Unary Operators (Positive, Negative, Bitwise NOT) |
| 4 | *, /, //, % | Multiplication, Division, Floor Division, Modulus |
| 5 | +, – | Addition, Subtraction |
| 6 | <<, >> | Bitwise Shift (Left & Right) |
| 7 | & | Bitwise AND |
| 8 | ^, | | Bitwise XOR, Bitwise OR |
| 9 | ==, !=, >, <, >=, <= | Comparison Operators |
| 10 | is, is not, in, not in | Identity and Membership Operators |
| 11 | not | Logical NOT |
| 12 | and | Logical AND |
| 13 (Lowest) | or | Logical OR |
Using parentheses can make your code more readable and prevent precedence mistakes.
Python Collections
Python collections store multiple items in a single variable:
| Collection | Properties | Use Cases |
|---|---|---|
| Lists | Mutable, Ordered | When you need an ordered, changeable collection that allows duplicates |
| Tuples | Immutable, Ordered | When you need an ordered, unchangeable collection that allows duplicates |
| Sets | Mutable, Unordered, No Duplicates | When you need a collection with no duplicate values |
| Dictionaries | Mutable, Unordered, Key-Value Pairs | When you need a collection of key-value pairs for efficient lookup |
Key Takeaways
- Understanding operator precedence is crucial for writing correct mathematical expressions in Python.
- Arithmetic operators form the foundation for mathematical operations in programming.
- Comparison and logical operators enable decision-making through conditional statements.
- Bitwise operators are powerful for low-level operations like data compression and encryption.
- Membership operators provide a clean way to check for element existence in collections.
- Python collections (lists, tuples, sets, dictionaries) offer versatile data storage options for different scenarios.
- Using parentheses can make complex expressions more readable and help avoid precedence errors.