Algorithms Class Notes
1. Introduction to Python
Python is a high-level, interpreted programming language known for its readability and simplicity.
Key features:
- Dynamically typed
- Interpreted
- Supports multiple paradigms (OOP, functional, procedural)
- Large standard library
2. Basic Syntax and Data Types
Common data types:
- int, float, str, bool, list, tuple, dict, set
Example:
name = 'Alice'
age = 25
print(f'{name} is {age} years old')
3. Control Flow and Loops
Control statements:
- if, elif, else
- for loops (iterate over sequences)
- while loops
Loop control: break, continue, pass
4. Functions and Modules
Algorithms Class Notes
Functions are defined using the `def` keyword.
Example:
def greet(name):
return f'Hello, {name}'
Modules allow code organization. Use `import` to include modules.
5. Object-Oriented Programming
Python supports OOP with classes and objects.
Example:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print(f'{self.name} makes a sound')
6. File I/O and Exception Handling
File operations:
with open('file.txt', 'r') as f:
data = f.read()
Exception handling:
try:
# risky code
Algorithms Class Notes
except Exception as e:
print(e)