burme-coder-max / data /knowledge /skills /python_skills.md
amkyawdev's picture
Upload folder using huggingface_hub
a7d7463 verified
|
Raw
History Blame Contribute Delete
1.66 kB

Python Skills

Python Basics

Variables

name = "Python"
age = 25
is_active = True

Data Types

  • int - Integer: 42
  • float - Decimal: 3.14
  • str - String: "Hello"
  • bool - Boolean: True/False
  • list - List: [1, 2, 3]
  • dict - Dictionary: {"key": "value"}

Functions

def greet(name: str) -> str:
    """Return a greeting message."""
    return f"Hello, {name}!"

# Lambda function
square = lambda x: x ** 2

Control Flow

if condition:
    do_something()
elif other_condition:
    do_other()
else:
    do_default()

Loops

# For loop
for item in items:
    print(item)

# While loop
while condition:
    do_something()

Advanced Topics

Decorators

def my_decorator(func):
    def wrapper():
        print("Before")
        func()
        print("After")
    return wrapper

@my_decorator
def say_hello():
    print("Hello!")

Classes

class Person:
    def __init__(self, name: str, age: int):
        self.name = name
        self.age = age
    
    def __str__(self) -> str:
        return f"{self.name}, {self.age} years old"

Exception Handling

try:
    result = risky_operation()
except ValueError as e:
    print(f"Error: {e}")
finally:
    cleanup()

List Comprehensions

squares = [x**2 for x in range(10)]
evens = [x for x in range(20) if x % 2 == 0]

Context Managers

with open('file.txt', 'r') as f:
    content = f.read()

Async/Await (asyncio)

import asyncio

async def fetch_data():
    await asyncio.sleep(1)
    return "data"