✅ Chapter 13 – Advanced Python 2: Concepts, Theory, Examples
📦 1. Virtual Environments (venv/virtualenv)
Topic |
Description |
What is venv? |
A virtual environment is a self-contained Python environment where you can install specific package versions without affecting the system Python. Ideal for projects that need specific dependencies. |
Why use it? |
To avoid version conflicts, isolate dependencies, manage multiple projects easily. |
Creating venv |
python -m venv envname or virtualenv envname |
Activating venv |
Windows: .\\env\\Scripts\\activate.ps1 (or .bat )Mac/Linux: source env/bin/activate |
Freezing packages |
pip freeze shows installed packages |
Saving requirements |
pip freeze > requirements.txt helps recreate envs elsewhere via pip install -r requirements.txt |
🔑 Note: virtualenv
is older but still popular; venv
is built-in from Python 3.3+
⚡ 2. Lambda Functions (Anonymous Functions)
Feature |
Description |
Syntax |
lambda arguments: expression |
Purpose |
To create short functions without def |
Examples |
Given Below |
square = lambda x: x * x
print(square(5)) # Output: 25
sum = lambda a, b, c: a + b + c
print(sum(3, 3, 3)) # Output: 9
🧠 Use when:
- You need a quick function passed to
map
, filter
, reduce
- You don’t want to clutter code with one-time
def
🧩 3. String .join()
Method
Topic |
Description |
Purpose |
Concatenate elements of an iterable into a single string |
Syntax |
'separator'.join(iterable) |
Example |
Given Below |
names = ["Prathamesh", "Harry", "Tarry"]
joined = "-".join(names)
print(joined) # Output: Prathamesh-Harry-Tarry
💡 Tip: join()
is faster than concatenation in loops.
🧩 4. String .format()
Method
Topic |
Description |
Purpose |
Insert variables into strings using placeholders {} |
Syntax |
"{} {}".format(var1, var2) |
Example |
|