Python basic to advance
43 subscribers
131 photos
98 links
Learn Python daily with bite-sized lessons, code examples, output previews, and fun quizzes. Perfect for beginners and enthusiasts!
Download Telegram
Here's your Telegram post for the Python learning series!

---

🚀 Ready to truly command your Python types? Dive into the "Deep Magic" of how classes are born! ✨ This week, we're unraveling Metaclasses and the powerful `__init_subclass__` hook. Learn to architect runtime behavior, enforce design patterns, and inject custom logic *before* your classes even fully exist. Elevate your object-oriented design from consumer to creator! 🛠️

Let's see how they let us define a custom class creation pipeline:

```python
import datetime

# --- Part 1: Using __init_subclass__ for a simple hook ---
# This method is called on the *base class* when a new *subclass* is defined.
class BaseModel:
_registry = [] # A simple registry for demonstration

def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
# Enforce a docstring for all subclasses
if not cls.__doc__ or not cls.__doc__.strip():
raise TypeError(f"Class '{cls.__name__}' must have a docstring!")

# Add a creation timestamp at class creation time
cls._creation_timestamp = datetime.datetime.now()
BaseModel._registry.append(cls)
print(f"[{cls.__name__}] registered and timestamped at {cls._creation_timestamp.strftime('%H:%M:%S')}")

class MyData(BaseModel):
"""Represents some user data."""
def __init__(self, value):
self.value = value

class AnotherData(BaseModel):
"""More data here."""
pass

# # Uncomment the class below to see the TypeError in action!
# class NoDocstring(BaseModel):
# pass

# --- Part 2: A simple Metaclass example ---
# A metaclass controls how classes (its instances) are created.
class EnforcingMeta(type):
def __new__(mcs, name, bases, namespace):
if 'ID_PREFIX' not in namespace:
raise AttributeError(f"Class '{name}' using EnforcingMeta must define 'ID_PREFIX'.")
# Ensure ID_PREFIX starts with 'APP_'
if not namespace['ID_PREFIX'].startswith('APP_'):
raise ValueError(f"ID_PREFIX for '{name}' must start with 'APP_'.")

# Add a default method if not present
if 'get_full_id' not in namespace:
def default_get_full_id(self):
return f"{self.ID_PREFIX}-{self.item_id}"
namespace['get_full_id'] = default_get_full_id

return super().__new__(mcs, name, bases, namespace)

class MyItem(metaclass=EnforcingMeta):
ID_PREFIX = "APP_ITEM"
def __init__(self, item_id):
self.item_id = item_id

# Metaclasses are inherited, but specifying it explicitly works too.
class MyProduct(MyItem, metaclass=EnforcingMeta):
ID_PREFIX = "APP_PRODUCT"
def __init__(self, item_id, price):
super().__init__(item_id)
self.price = price

print("n--- Testing Instances ---")
data1 = MyData("Hello")
print(f"MyData created at: {MyData._creation_timestamp.strftime('%H:%M:%S')}")

item1 = MyItem(123)
print(f"Item ID: {item1.get_full_id()}")

product1 = MyProduct(456, 99.99)
print(f"Product ID: {product1.get_full_id()} | Price: {product1.price}")

print("n--- Registry Check ---")
for cls in BaseModel._registry:
print(f"- Registered: {cls.__name__}")
```

Output example:
```
[MyData] registered and timestamped at 14:02:45
[AnotherData] registered and timestamped at 14:02:45

--- Testing Instances ---
MyData created at: 14:02:45
Item ID: APP_ITEM-123
Product ID: APP_PRODUCT-456 | Price: 99.99

--- Registry Check ---
- Registered: MyData
- Registered: AnotherData
```

💡 **Challenge:** Modify the `EnforcingMeta` to also automatically add a *class method* `get_class_prefix()` that returns the `ID_PREFIX` defined by the class. How would you ensure this method correctly reflects the `ID_PREFIX` of the *subclass* if it overrides it? Share your solution in the comments! 👇

---
Hey Pythonistas! 👋 It's Day 69, and we're diving deep into the very fabric of Python! 🚀 Ever wondered how frameworks define their own rules or how you could build a mini-language within Python? Today, we unlock the power of Metaclasses! 🏗️

Think of them as factories for classes – they let you define *how* classes are created, enforcing architectural patterns, injecting attributes, or even building Domain-Specific Languages (DSLs) directly. It's advanced, it's mind-bending, and incredibly powerful! ✨ Get ready to architect reality.

```python
class DomainMetaclass(type):
"""
A metaclass to enforce structure and add common behavior to domain objects.
Here, it ensures a 'key' attribute exists and creates a registry for instances.
"""
def __new__(mcs, name, bases, dct):
# 1. Enforce/Default an attribute: Ensure every class has a 'key'
if "key" not in dct:
dct["key"] = name.lower() # Default key if not provided

# 2. Add class-level attributes: A registry for elements of this type
dct["_registered_elements"] = []

# 3. Create the class using the modified dictionary
cls = super().__new__(mcs, name, bases, dct)

# 4. Add methods to the newly created class
def register_instance(instance):
"""Class method to register instances of this class."""
cls._registered_elements.append(instance)
print(f"✨ Registered '{instance}' with key '{instance.key}' in {cls.__name__}'s domain.")

# Attach the class method
cls.register_instance = classmethod(register_instance)

return cls

# --- Define Domain-Specific Classes using our Metaclass ---

class Command(metaclass=DomainMetaclass):
"""Represents a command in our DSL."""
# Metaclass ensures 'key' exists. We can override it:
key = "cmd"

def __init__(self, name, action):
self.name = name
self.action = action
# Use the metaclass-provided method to register this instance
self.register_instance(self)

def __repr__(self):
return f"<{self.__class__.__name__}: {self.name}>"

class Query(metaclass=DomainMetaclass):
"""Represents a query in our DSL."""
# Let the metaclass define the 'key' (will be 'query')

def __init__(self, statement):
self.statement = statement
self.register_instance(self)

def __repr__(self):
return f"<{self.__class__.__name__}: '{self.statement[:20]}...'>"

# --- Demonstrate the DSL-like behavior ---
print("--- Creating Domain Objects ---")
run_cmd = Command("RunScript", "Execute the main script logic")
stop_cmd = Command("StopService", "Gracefully halt a service")
get_users_query = Query("SELECT * FROM users WHERE status='active'")
count_products_query = Query("COUNT(*) FROM products")

print("n--- Inspecting Registered Elements ---")
print(f"Registered Commands (key='{Command.key}'): {Command._registered_elements}")
print(f"Registered Queries (key='{Query.key}'): {Query._registered_elements}")
```

Output example:
```
--- Creating Domain Objects ---
✨ Registered '<Command: RunScript>' with key 'cmd' in Command's domain.
✨ Registered '<Command: StopService>' with key 'cmd' in Command's domain.
✨ Registered '<Query: 'SELECT * FROM users...' with key 'query' in Query's domain.
✨ Registered '<Query: 'COUNT(*) FROM prod...' with key 'query' in Query's domain.

--- Inspecting Registered Elements ---
Registered Commands (key='cmd'): [<Command: RunScript>, <Command: StopService>]
Registered Queries (key='query'): [<Query: 'SELECT * FROM users...'>, <Query: 'COUNT(*) FROM prod...'>]
```

**Small challenge:**
Modify `DomainMetaclass` to automatically add a default `execute` method (printing "Executing...") to `Command` classes and a `fetch` method (printing "Fetching...") to `Query` classes. How would you ensure `Command` classes *must* have a `description` attribute (raising an error if missing) instead of just a default `key`?

---
Hey Pythonistas! 👋 Ready to unlock Python's deepest secrets?

For Day 70 of our learning series, we're diving into the mind-bending world of **Python as a Self-Modifying Organism: Advanced AST Introspection for Runtime Rewrites!** 🐍🔬 Ever wondered if your code could change its own logic while running? Today, we'll explore Abstract Syntax Trees (ASTs) not just for static analysis, but for actively inspecting, transforming, and even generating Python code at runtime. This isn't just theoretical; it's a powerful technique for building custom DSLs, optimizing on-the-fly, or creating advanced metaprogramming tools! Let's manipulate the very structure of our programs! ✨

```python
import ast
import inspect

# 1. Define a simple function whose AST we want to modify
def calculate_expression(a, b):
# This function performs multiplication, addition, and subtraction
return a * b + 5 - 2

# 2. Convert the function's source code into an AST
# We need the source of just the function body for easier transformation.
# For demonstration, we'll use a simplified string to avoid inspect issues with decorators/indentation.
code_to_transform = """
def transformed_expression(a, b):
result = a * b + 5 - 2
return result
"""
tree = ast.parse(code_to_transform)

# 3. Create a NodeTransformer to replace specific operations
class MultiplyToAddTransformer(ast.NodeTransformer):
"""
Transforms multiplication operations (ast.Mult) into addition operations (ast.Add).
Also changes subtraction (ast.Sub) to division (ast.Div).
"""
def visit_BinOp(self, node):
if isinstance(node.op, ast.Mult):
# Replace multiplication with addition
node.op = ast.Add()
elif isinstance(node.op, ast.Sub):
# Replace subtraction with division
node.op = ast.Div()
# Ensure we visit child nodes to apply transformations recursively
self.generic_visit(node)
return node

# Apply the transformer to our AST
transformed_tree = MultiplyToAddTransformer().visit(tree)
ast.fix_missing_locations(transformed_tree) # Best practice for transformed ASTs

# 4. Compile and execute the transformed AST
# 'compile' converts the AST back into a code object.
# 'exec' runs the code object within a specified namespace.
compiled_code = compile(transformed_tree, filename='<transformed_ast>', mode='exec')

# Create a new scope for our transformed function
transformed_scope = {}
exec(compiled_code, transformed_scope)

# Get the newly defined function from our transformed scope
modified_expression_func = transformed_scope['transformed_expression']

# 5. Demonstrate the original vs. transformed behavior
val_a, val_b = 10, 2

print(f"Original logic (conceptually for calculate_expression({val_a}, {val_b})):")
print(f" {val_a} * {val_b} + 5 - 2 = {calculate_expression(val_a, val_b)}")

print("nTransformed logic (after AST rewrite):")
print(f" (Original 'a * b' became 'a + b', '5 - 2' became '5 / 2')")
# Conceptually: 10 + 2 + 5 / 2 -> 12 + 2.5 = 14.5
print(f" transformed_expression({val_a}, {val_b}) -> {modified_expression_func(val_a, val_b)}")
```

**Output example:**
```
Original logic (conceptually for calculate_expression(10, 2)):
10 * 2 + 5 - 2 = 23

Transformed logic (after AST rewrite):
(Original 'a * b' became 'a + b', '5 - 2' became '5 / 2')
transformed_expression(10, 2) -> 14.5
```

**Small Challenge:**
Can you modify the `MultiplyToAddTransformer` to instead replace all integer literals (e.g., `5`, `2`) in the `transformed_expression` function with a different fixed value, like `0`? (Hint: You'll need to look for `ast.Constant` nodes and check their `value` type.) Good luck, AST alchemist! 🧙‍♂️

---
Hey everyone! We're starting again from Day 1 so everyone can build a strong foundation by learning everything from the basics this time.
🚀 **Day 1: Your Python Journey Begins!** 🐍

Welcome to the very first step of our Python learning series! Today, we're diving straight into the basics with the legendary "Hello, World!" program. It's the simplest way to confirm your setup works and celebrate your first line of code. Get ready to write, run, and feel the magic! ✨ Let's make some noise!

Here's your first piece of Python code:

```python
print("Hello, World!")
```

Output example:
```
Hello, World!
```

💪 **Your challenge:**
1. Change the text inside the `print()` function to say "Hello, Python Learners!"
2. Try printing your own name!

---
Day 1: Introduction to Python with Hello World \- Code & Output
🚀 Welcome back, aspiring Pythonistas!

After a fantastic Day 1 intro, it's time to dive into the fundamental building blocks of any program: *Variables and Data Types*! 🧱

Think of variables as named containers for storing information. And data types? They tell Python what kind of information that is – numbers, text, true/false values, and more! Understanding these is crucial for writing dynamic and useful code. Let's get hands-on! 💻

---

```python
# Day 2: Variables and Data Types

# 1. Variable Assignment (creating containers for data)
student_name = "Alice Smith" # A string (text)
student_age = 20 # An integer (whole number)
gpa = 3.85 # A float (decimal number)
is_enrolled = True # A boolean (True/False)

# 2. Printing Values and their Data Types
print(f"Name: {student_name}, Type: {type(student_name)}")
print(f"Age: {student_age}, Type: {type(student_age)}")
print(f"GPA: {gpa}, Type: {type(gpa)}")
print(f"Is Enrolled: {is_enrolled}, Type: {type(is_enrolled)}")

# 3. Reassigning a variable (changing the content of a container)
student_age = 21 # Alice had a birthday!
print(f"nAlice's new age: {student_age}")

# You can also use single quotes for strings
course_name = 'Introduction to Python'
print(f"Course: {course_name}")
```

---

Output example:
```
Name: Alice Smith, Type: <class 'str'>
Age: 20, Type: <class 'int'>
GPA: 3.85, Type: <class 'float'>
Is Enrolled: True, Type: <class 'bool'>

Alice's new age: 21
Course: Introduction to Python
```

---

💡 **Your Challenge:**
Create three new variables:
1. `city` (a string for your hometown)
2. `population` (an integer)
3. `average_temp` (a float)
Then, print all three variables along with their respective data types using the `type()` function! Share your code or output in the comments! 👇

---
Which of the following is NOT a valid Python variable name?
Anonymous Quiz
0%
my_variable
0%
_private_data
0%
2nd_attempt
0%
user_count
Here's your Telegram post and quiz question!

---

🚀 Day 4: Time to make your Python programs smart! Today, we're mastering *Conditional Statements*: `if`, `elif`, and `else`. 🤖 These powerful tools let your code make decisions and execute different blocks of instructions based on specific conditions. No more rigid scripts – let's add some flexible logic! Get ready to control the flow of your programs like a pro! 👇

```python
# Day 4: Conditional Statements (if, elif, else)

# Get a score from the user
score = int(input("Enter your test score (0-100): "))

# Use if, elif, else to assign a grade based on the score
if score >= 90:
print("🎉 Excellent! You got an A!")
elif score >= 75:
print("👍 Good job! You got a B.")
elif score >= 50:
print("✅ You passed! Keep practicing.")
else:
print("🙁 Needs improvement. Don't give up!")

print("Program finished.")
```

Output example:
```
Enter your test score (0-100): 78
👍 Good job! You got a B.
Program finished.
```

✨ **Your Challenge:** Modify the code above! Add an `elif` condition to print 'Amazing!' if the score is exactly 100. How would you adjust the existing `if score >= 90` condition to fit this new rule? 🤔

---
Day 4: Conditional Statements \(if, elif, else\) \- Code & Output
What will be the output of the following Python code snippet?
```python
number = 10 if number > 15: message = "High" elif number == 10: message = "Exact" elif number < 12: message = "Low" else: message = "Other" print(message) ```
Anonymous Quiz
0%
High
100%
Exact
0%
Low
0%
Other
Here's your Telegram post for Day 5 of the Python learning series!

---

**Day 5: Loops (for and while) 🔄**

Hello, Python learners! 👋 Today we're diving into the powerful world of loops! Loops are absolutely essential for automating repetitive tasks and making your code super efficient. We'll explore two main types: `for` loops, perfect for iterating over sequences like lists or ranges, and `while` loops, which repeat as long as a certain condition remains true. Get ready to automate! 🚀

```python
# --- Python Loops: for and while ---

print("--- For Loop Example (Iterating over a list) ---")
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(f"I love {fruit}!")

print("n--- While Loop Example (Counting) ---")
count = 0
while count < 3:
print(f"Count is {count}")
count += 1 # IMPORTANT: Increment to avoid an infinite loop!

print("n--- For Loop with range() ---")
for i in range(4): # This will iterate for i = 0, 1, 2, 3
print(f"Number from range: {i}")

print("n--- For Loop with else (optional!) ---")
for char_code in [65, 66, 67]:
print(f"Character: {chr(char_code)}")
else:
print("All characters processed!")
```

Output example:
```
--- For Loop Example (Iterating over a list) ---
I love apple!
I love banana!
I love cherry!

--- While Loop Example (Counting) ---
Count is 0
Count is 1
Count is 2

--- For Loop with range() ---
Number from range: 0
Number from range: 1
Number from range: 2
Number from range: 3

--- For Loop with else (optional!) ---
Character: A
Character: B
Character: C
All characters processed!
```

💡 **Your Challenge:**
1. Use a `while` loop to print numbers from 5 down to 1.
2. Then, use a `for` loop to print each character in the word "Python" on a separate line.

Share your solutions or questions in the comments! 👇 Happy coding! ✨

---
Day 5: Loops \(for and while\) \- Code & Output
What will be the output of the following Python code snippet?

```python result = "" for i in range(4): if i == 2: break result += str(i) print(result) ```
Anonymous Quiz
0%
012
50%
01
50%
0123
0%
0
🚀 **Python Learning Series - Day 6: Functions in Python!** 🚀

Hello, amazing learners! ✨ Today, we're diving into one of the most fundamental and powerful concepts in Python: **Functions**! Functions allow us to write reusable blocks of code, making our programs organized, efficient, and easier to understand. No more repetitive code! Let's unlock the magic of `def`! 🔑

---

```python
# 1. Defining a simple function
def greet_user(name):
"""This function greets the user by name."""
print(f"Hello, {name}! Welcome to Day 6 of our Python journey. 👋")

# 2. Calling the function
greet_user("Pythonista")

# 3. Function with parameters and a return value
def calculate_sum(num1, num2):
"""This function takes two numbers and returns their sum."""
total = num1 + num2
return total

# 4. Using the returned value
result = calculate_sum(15, 25)
print(f"The sum of 15 and 25 is: {result}")

# 5. Another call
greet_user("Beginner")
```

---

Output example:
```
Hello, Pythonista! Welcome to Day 6 of our Python journey. 👋
The sum of 15 and 25 is: 40
Hello, Beginner! Welcome to Day 6 of our Python journey. 👋
```

---

💡 **Your Challenge:**
Write a Python function called `get_fahrenheit` that takes a temperature in Celsius as a parameter and returns the temperature in Fahrenheit. (Formula: `F = C * 9/5 + 32`). Then, call your function with a sample Celsius value and print the result! Share your code snippets below! 👇

---