Arithmetic,
OPERATORS IN Relational, and
PYTHON Logical
Operators
A Practical Approach
ARITHMETIC OPERATORS
Used for mathematical operations:
+ Addition: 5 + 3 → 8
- Subtraction: 5 - 3 → 2
* Multiplication: 5 * 3 → 15
/ Division (float): 5 / 2 → 2.5
// Floor Division: 5 // 2 → 2
% Modulus (Remainder): 5 % 2 → 1
** Exponentiation: 5 ** 2 → 25
RELATIONAL (COMPARISON)
OPERATORS
Used to compare values:
== Equal to: 5 == 5 → True
!= Not equal to: 5 != 3 → True
> Greater than: 5 > 3 → True
< Less than: 5 < 3 → False
>= Greater than or equal to: 5 >= 5 → True
<= Less than or equal to: 3 <= 5 → True
LOGICAL OPERATORS
Used to combine conditions:
and Returns True if both conditions are True
or Returns True if at least one condition is True
not Negates the condition
Example:
a = True, b = False
print(a and b) → False
print(a or b) → True
print(not a) → False
DISCUSSION POINTS
- Difference between `/` (float division) and `//`
(floor division)
- Operator precedence (`**` > `* / // %` > `+ -
`)
- Chained comparisons (`3 < x < 10` is valid in
Python)
- Short-circuit evaluation in logical operators
- Truthy and Falsy values in Python (0, None, "",
[], {} → False)
HANDS-ON EXERCISES
1. Compute the remainder when a number is divided
by another.
2. Check if a number is even or odd using `%` and
`==`.
3. Given `a = 5, b = 10, c = 15`, evaluate
expressions:
- `(a < b) and (b < c)`
- `not (a > c)`
4. Write a program to check if a number is in the
range [10, 50].
CONDITIONAL STATEMENTS
FOR LOOP
Used for iterating over sequences (lists, strings,
etc.)
Syntax:
for variable in sequence:
# Code executed in each iteration
Example:
for i in range(5): # range(5) generates [0,1,2,3,4]
print("Iteration:", i)
- `range(start, stop, step)` allows custom ranges
- Can iterate over lists, strings, and tuples
WHILE LOOP
Executes a block of code as long as the condition remains True.
Syntax:
while condition:
# Code executed repeatedly
Example:
count = 0
while count < 5:
print("Count:", count)
count += 1
- Beware of infinite loops (`while True`)
- Use `break` to exit early, `continue` to skip iterations
CONTROL STATEMENTS
Used to alter loop execution:
- `break` → Exits the loop prematurely
- `continue` → Skips the current iteration
- `pass` → Placeholder for future code
Example:
for i in range(5):
if i == 2:
continue # Skip when i is 2
if i == 4:
break # Stop loop when i is 4
print(i)
HANDS-ON EXERCISES
FUNCTIONS IN PYTHON
USER INPUT IN
PYTHON
TYPES OF FUNCTIONS
MODULES IN PYTHON
Modules are Python files containing functions,
variables, and classes.
Creating a module (`mymodule.py`):
# mymodule.py
def greet(name):
return f"Hello, {name}!"
Using the module:
import mymodule
print(mymodule.greet("Alice")) # Output: Hello,
Alice!
STANDARD PYTHON
MODULES
Python provides built-in modules:
- `math` → Mathematical operations
- `random` → Random number generation
- `datetime` → Working with dates and time
- ‘numpy’ → For numerical computations
- ‘sympy’- → For symbolic (algebraic) computations
-- ‘matplotlib’- → For plotting.
Example (`math` module):
import math
print(math.sqrt(25)) # Output: 5.0
print(math.pi) # Output: 3.141592653589793
Introduction to Key Libraries for
Mathematics
The math
library