π Truthy vs Falsy in Python (Hidden Behavior)
In Python, not everything is just True or False π€―
Some values behave like Trueβ¦ even when theyβre not.
1οΈβ£ Falsy Values β
These are treated as False:
Output: False False False
π Empty or zero-like values β False
2οΈβ£ Truthy Values β
Everything else is considered True:
Output: True True True
π Non-empty values β True
3οΈβ£ Why This Matters β‘οΈ
Output: Empty
π No need to write
4οΈβ£ Common Mistake π«
π Better way:
π‘ Key Idea
Python doesnβt just check True/False
It checks if something is empty or not π§
In Python, not everything is just True or False π€―
Some values behave like Trueβ¦ even when theyβre not.
1οΈβ£ Falsy Values β
These are treated as False:
print(bool(0))
print(bool(""))
print(bool([]))
Output: False False False
π Empty or zero-like values β False
2οΈβ£ Truthy Values β
Everything else is considered True:
print(bool(1))
print(bool("Hello"))
print(bool([1, 2]))
Output: True True True
π Non-empty values β True
3οΈβ£ Why This Matters β‘οΈ
name = ""
if name:
print("Has value")
else:
print("Empty")
Output: Empty
π No need to write
if name != "" 4οΈβ£ Common Mistake π«
if list == []: # β
π Better way:
if not list: # β
π‘ Key Idea
Python doesnβt just check True/False
It checks if something is empty or not π§
β€2π2
π Python Decorators π
Have you ever wanted to add extra functionality to a function like logging, timing, or permission checks without actually changing the code inside that function? That is exactly what Decorators do. They allow you to "wrap" another function to extend its behavior.
π Decorators are a key part of writing clean, reusable, and "DRY" (Don't Repeat Yourself) Python code.
1. Analogy: The Phone Case
Think of your function as a Smartphone. It has core features like calling and texting. A Decorator is like a Phone Case.
The case doesn't change how the phone's internal circuits work, but it adds new "superpowers" like a kickstand, extra battery life, or water protection.
You can put the same case on different phones!
2. How it Works: Functions are Objects
In Python, functions are "first-class objects." This means you can:
- Assign a function to a variable.
- Pass a function as an argument to another function.
- Return a function from another function.
A decorator is simply a function that takes another function, adds some logic, and returns a new, "wrapped" version of it.
3. The
Instead of writing
π Practical Code Example: A Simple Timer
Let's create a decorator that measures how long a function takes to run.
4. Why Use Decorators?
- Code Reusability: Write the logic once (like logging) and apply it to 50 different functions.
- Separation of Concerns: Keep your main logic clean. The "extra" stuff (security, timing) stays in the decorator.
- DRY Principle: Prevents you from copy-pasting the same setup/teardown code into every function.
π― Today's Goal (What you should do)
βοΈ Understand that decorators "wrap" functions to add functionality
βοΈ Master the
βοΈ Learn how to pass arguments to wrapped functions using
βοΈ Identify common use cases: Logging, Timing, and Authentication
Have you ever wanted to add extra functionality to a function like logging, timing, or permission checks without actually changing the code inside that function? That is exactly what Decorators do. They allow you to "wrap" another function to extend its behavior.
π Decorators are a key part of writing clean, reusable, and "DRY" (Don't Repeat Yourself) Python code.
1. Analogy: The Phone Case
Think of your function as a Smartphone. It has core features like calling and texting. A Decorator is like a Phone Case.
The case doesn't change how the phone's internal circuits work, but it adds new "superpowers" like a kickstand, extra battery life, or water protection.
You can put the same case on different phones!
2. How it Works: Functions are Objects
In Python, functions are "first-class objects." This means you can:
- Assign a function to a variable.
- Pass a function as an argument to another function.
- Return a function from another function.
A decorator is simply a function that takes another function, adds some logic, and returns a new, "wrapped" version of it.
3. The
@ SyntaxInstead of writing
say_hello = my_decorator(say_hello), Python gives us a beautiful shortcut: the @ symbol. Placing @decorator_name above a function automatically wraps it.π Practical Code Example: A Simple Timer
Let's create a decorator that measures how long a function takes to run.
import time
# 1. Define the decorator
def timer_decorator(func):
def wrapper(*args, **kwargs): # The 'wrapper' adds the new behavior
start_time = time.time()
result = func(*args, **kwargs) # Execute the original function
end_time = time.time()
print(f"β±οΈ {func.__name__} took {end_time - start_time:.4f} seconds.")
return result
return wrapper
# 2. Use the decorator
@timer_decorator
def heavy_computation():
print("Computing...")
time.sleep(1.5) # Simulate a long task
print("Done!")
# 3. Call the function
heavy_computation()
4. Why Use Decorators?
- Code Reusability: Write the logic once (like logging) and apply it to 50 different functions.
- Separation of Concerns: Keep your main logic clean. The "extra" stuff (security, timing) stays in the decorator.
- DRY Principle: Prevents you from copy-pasting the same setup/teardown code into every function.
π― Today's Goal (What you should do)
βοΈ Understand that decorators "wrap" functions to add functionality
βοΈ Master the
@ syntax for applying decoratorsβοΈ Learn how to pass arguments to wrapped functions using
*args and *kwargsβοΈ Identify common use cases: Logging, Timing, and Authentication
β€6
βCommon Python Terms
1. Variable: A symbolic name that is a reference or pointer to an object. When you create a variable, you allocate some memory for its value.
2. Data Type: A classification that specifies which type of value a variable can hold and what operations can be performed on it (e.g.,
3. Function: A block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.
4. Method: A function that is associated with an object or a class. It operates on the data (attributes) of that object.
5. Class: A blueprint for creating objects. It defines a set of attributes (data) and methods (functions) that characterize any object created from it.
6. Object: An instance of a class. It is a collection of data (attributes) and functions (methods) that operate on that data.
7. Module: A file containing Python definitions and statements. The file name is the module name with the suffix
8. Package: A Python module can be grouped into a package. A package is a directory of Python modules containing an
9. Interpreter: The program that reads and executes Python code. Python code is not compiled to machine code directly; it is compiled to bytecode, which is then interpreted.
10. Syntax: The set of rules that defines the combinations of symbols that are considered to be correctly structured programs in a particular language.
11. Indentation: Python uses whitespace (spaces or tabs) to define code blocks (e.g., inside loops, functions, or conditional statements) rather than braces like in many other languages.
12. List: An ordered, mutable (changeable) collection of items. Items can be of different data types.
13. Tuple: An ordered, immutable (unchangeable) collection of items. Like lists, tuples can contain items of different data types.
14. Dictionary: An unordered collection of key-value pairs. Keys must be unique and immutable (e.g., strings, numbers, tuples), while values can be of any data type.
15. String: An immutable sequence of characters, used to represent text data.
16. Loop: A control flow statement that allows code to be executed repeatedly. Common loops are
17. Conditional Statement: Statements that perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. Common are
18. Exception Handling: A mechanism to handle runtime errors or unexpected events gracefully, preventing the program from crashing. Uses
19. Library/Framework: A collection of pre-written code (functions, classes) that developers can use to perform common tasks without writing the code from scratch. Examples: NumPy, Pandas, Django, Flask.
20. IDE (Integrated Development Environment): A software application that provides comprehensive facilities to computer programmers for software development. It typically includes a code editor, debugger, and build automation tools. Examples: VS Code, PyCharm.
1. Variable: A symbolic name that is a reference or pointer to an object. When you create a variable, you allocate some memory for its value.
2. Data Type: A classification that specifies which type of value a variable can hold and what operations can be performed on it (e.g.,
int, float, str, bool, list, dict).3. Function: A block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.
4. Method: A function that is associated with an object or a class. It operates on the data (attributes) of that object.
5. Class: A blueprint for creating objects. It defines a set of attributes (data) and methods (functions) that characterize any object created from it.
6. Object: An instance of a class. It is a collection of data (attributes) and functions (methods) that operate on that data.
7. Module: A file containing Python definitions and statements. The file name is the module name with the suffix
.py appended. Modules allow you to logically organize your Python code.8. Package: A Python module can be grouped into a package. A package is a directory of Python modules containing an
__init__.py file (which can be empty). Packages allow for a hierarchical structuring of the module namespace.9. Interpreter: The program that reads and executes Python code. Python code is not compiled to machine code directly; it is compiled to bytecode, which is then interpreted.
10. Syntax: The set of rules that defines the combinations of symbols that are considered to be correctly structured programs in a particular language.
11. Indentation: Python uses whitespace (spaces or tabs) to define code blocks (e.g., inside loops, functions, or conditional statements) rather than braces like in many other languages.
12. List: An ordered, mutable (changeable) collection of items. Items can be of different data types.
13. Tuple: An ordered, immutable (unchangeable) collection of items. Like lists, tuples can contain items of different data types.
14. Dictionary: An unordered collection of key-value pairs. Keys must be unique and immutable (e.g., strings, numbers, tuples), while values can be of any data type.
15. String: An immutable sequence of characters, used to represent text data.
16. Loop: A control flow statement that allows code to be executed repeatedly. Common loops are
for and while.17. Conditional Statement: Statements that perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. Common are
if, elif, else.18. Exception Handling: A mechanism to handle runtime errors or unexpected events gracefully, preventing the program from crashing. Uses
try, except, finally blocks.19. Library/Framework: A collection of pre-written code (functions, classes) that developers can use to perform common tasks without writing the code from scratch. Examples: NumPy, Pandas, Django, Flask.
20. IDE (Integrated Development Environment): A software application that provides comprehensive facilities to computer programmers for software development. It typically includes a code editor, debugger, and build automation tools. Examples: VS Code, PyCharm.
β€5
π 5 Python Mistakes Beginners Make (And Donβt Realize)
If youβre learning Pythonβ¦
youβve probably done these already π
1οΈβ£ Using = Instead of == β
π
π
2οΈβ£ Not Understanding Mutable Objects π
Output: [1, 2, 3]
π Both changed π³
π Because they point to SAME object
3οΈβ£ Forgetting Indentation β οΈ
π Python depends on indentation
π One mistake β code breaks
4οΈβ£ Using is Instead of == π€―
π Might return False
π Because
5οΈβ£ Modifying List While Looping π₯
π Leads to unexpected bugs
π Loop skips elements
π‘ Key Idea
Most bugs in Python
come from misunderstanding how it works internally π
If youβre learning Pythonβ¦
youβve probably done these already π
1οΈβ£ Using = Instead of == β
if x = 5: # β Error
π
= is assignment π
== is comparison 2οΈβ£ Not Understanding Mutable Objects π
a = [1, 2]
b = a
b.append(3)
print(a)
Output: [1, 2, 3]
π Both changed π³
π Because they point to SAME object
3οΈβ£ Forgetting Indentation β οΈ
if True:
print("Hello") # β
π Python depends on indentation
π One mistake β code breaks
4οΈβ£ Using is Instead of == π€―
a = 1000
b = 1000
print(a is b)
π Might return False
π Because
is checks memory, not value 5οΈβ£ Modifying List While Looping π₯
nums = [1, 2, 3]
for x in nums:
nums.remove(x)
π Leads to unexpected bugs
π Loop skips elements
π‘ Key Idea
Most bugs in Python
come from misunderstanding how it works internally π
β€3
βTop 5 Resources for Every Aspiring Python Developer
β1. Automate the Boring Stuff with Python
β’ Type: Book/Online Course
β’ Author: Al Sweigart
β’ This resource is perfect for beginners who want to learn Python through practical projects. The book covers automation tasks like web scraping, working with spreadsheets, and more. The online course is available for free on platforms like Udemy.
β’ Link: Automate the Boring Stuff
β2. Python for Everybody
β’ Type: Online Course
β’ Instructor: Dr. Charles Severance
β’ This course is designed for beginners and covers the basics of programming using Python. It's structured to help learners understand data handling, web scraping, and database management.
β’ Link: Python for Everybody
β3. Real Python
β’ Type: Online Tutorials/Articles
β’ Real Python offers a wealth of tutorials, articles, and video courses on various Python topics. It caters to all skill levels and includes practical examples and projects to reinforce learning.
β’ Link: Real Python
β4. Python Crash Course
β’ Type: Book
β’ Author: Eric Matthes
β’ This hands-on guide is ideal for beginners and intermediate learners. It covers the fundamentals of Python programming and includes projects such as building web applications and data visualizations.
β’ Link: Python Crash Course
β5. The Hitchhiker's Guide to Python
β’ Type: Book/Online Resource
β’ Authors: Kenneth Reitz and Tanya Schlusser
β’ This comprehensive guide offers best practices for writing Python code. It covers everything from installation to advanced topics, making it a valuable resource for both new and experienced developers.
β’ Link: The Hitchhiker's Guide to Python
β1. Automate the Boring Stuff with Python
β’ Type: Book/Online Course
β’ Author: Al Sweigart
β’ This resource is perfect for beginners who want to learn Python through practical projects. The book covers automation tasks like web scraping, working with spreadsheets, and more. The online course is available for free on platforms like Udemy.
β’ Link: Automate the Boring Stuff
β2. Python for Everybody
β’ Type: Online Course
β’ Instructor: Dr. Charles Severance
β’ This course is designed for beginners and covers the basics of programming using Python. It's structured to help learners understand data handling, web scraping, and database management.
β’ Link: Python for Everybody
β3. Real Python
β’ Type: Online Tutorials/Articles
β’ Real Python offers a wealth of tutorials, articles, and video courses on various Python topics. It caters to all skill levels and includes practical examples and projects to reinforce learning.
β’ Link: Real Python
β4. Python Crash Course
β’ Type: Book
β’ Author: Eric Matthes
β’ This hands-on guide is ideal for beginners and intermediate learners. It covers the fundamentals of Python programming and includes projects such as building web applications and data visualizations.
β’ Link: Python Crash Course
β5. The Hitchhiker's Guide to Python
β’ Type: Book/Online Resource
β’ Authors: Kenneth Reitz and Tanya Schlusser
β’ This comprehensive guide offers best practices for writing Python code. It covers everything from installation to advanced topics, making it a valuable resource for both new and experienced developers.
β’ Link: The Hitchhiker's Guide to Python
β€3
π Python Performance: Vectorization vs. Loops in Pandas πΌ
In standard Python, we are taught to use
π To be a pro Data Analyst, you must stop "looping" and start "vectorizing."
π’ The Slow Way: Iterating with Loops
Python is an interpreted language, meaning every time a loop runs a calculation on a row, there is massive "overhead." The computer has to check the data type, find the memory address, and perform the math over and over again.
π The Fast Way: Vectorization
Pandas (and NumPy) use Vectorization, which performs operations on entire arrays (columns) at once. This pushes the heavy lifting down to highly optimized C and Fortran code under the hood.
π» The "Speed Race" Code
Let's say we have 1 million rows of prices and we want to apply a 10% tax.
The result? The loop might take ~0.1 seconds, while the vectorized version takes ~0.001 seconds. On massive datasets, this is the difference between a task taking 10 minutes or 2 seconds.
π When can't you vectorize?
If you have extremely complex logic (like an
π Write your code for the column, not for the row!
In standard Python, we are taught to use
for loops to process data. However, in Data Science, loops are the enemy of speed. If you use a loop to process a million rows in a Pandas DataFrame, your code will be 100x to 1000x slower than it needs to be. π To be a pro Data Analyst, you must stop "looping" and start "vectorizing."
π’ The Slow Way: Iterating with Loops
Python is an interpreted language, meaning every time a loop runs a calculation on a row, there is massive "overhead." The computer has to check the data type, find the memory address, and perform the math over and over again.
π The Fast Way: Vectorization
Pandas (and NumPy) use Vectorization, which performs operations on entire arrays (columns) at once. This pushes the heavy lifting down to highly optimized C and Fortran code under the hood.
π» The "Speed Race" Code
Let's say we have 1 million rows of prices and we want to apply a 10% tax.
import pandas as pd
import numpy as np
import time
# Create a DataFrame with 1 million rows
df = pd.DataFrame({'price': np.random.randint(1, 100, size=1_000_000)})
# β THE SLOW WAY: Manual Loop (Don't do this!)
start = time.time()
taxes = []
for p in df['price']:
taxes.append(p * 0.1)
df['tax_loop'] = taxes
print(f"Loop time: {time.time() - start:.4f} seconds")
# β THE FAST WAY: Vectorization (The Pandas Way)
start = time.time()
df['tax_vec'] = df['price'] * 0.1
print(f"Vectorized time: {time.time() - start:.4f} seconds")
The result? The loop might take ~0.1 seconds, while the vectorized version takes ~0.001 seconds. On massive datasets, this is the difference between a task taking 10 minutes or 2 seconds.
π When can't you vectorize?
If you have extremely complex logic (like an
if/else that depends on three different external APIs), you might use .apply(). While .apply() is slightly better than a manual for loop, it is still significantly slower than true vectorization. Always try math-based column operations first.π Write your code for the column, not for the row!
β€2