Tech Blog

Python Basics Explained

Welcome to our deep dive into Python fundamentals. Whether you’re brand‑new to programming or transitioning from another language, this guide covers the core concepts you need to start writing clean, effective Python code.

Table of Contents

Variables & Data Types

Python is dynamically typed, meaning you don’t declare a variable’s type explicitly.

# Numbers
age = 30                 # int
price = 19.99            # float

# Strings
name = "Alice"
greeting = f"Hello, {name}!"

# Booleans
is_active = True

# None (null)
result = None

Control Flow

Conditional statements and loops let you direct execution.

# If‑elif‑else
if age >= 18:
    print("Adult")
elif age >= 13:
    print("Teen")
else:
    print("Child")

# For loop
for i in range(5):
    print(i)

# While loop
count = 0
while count < 3:
    print(count)
    count += 1

Functions

Define reusable blocks of code with def. Use default arguments and return values.

def add(a, b=0):
    """Return the sum of a and b."""
    return a + b

result = add(5, 7)      # 12
print(result)

Built‑in Collections

Lists, tuples, sets, and dictionaries are the core data structures.

# List
fruits = ["apple", "banana", "cherry"]
fruits.append("date")

# Tuple (immutable)
point = (10, 20)

# Set (unique items)
colors = {"red", "green", "blue"}
colors.add("yellow")

# Dictionary
person = {"name": "Bob", "age": 28}
person["city"] = "New York"

Modules & Packages

Organize code into modules and import them when needed.

# mymodule.py
def greet(name):
    return f"Hello, {name}!"

# main.py
import mymodule

print(mymodule.greet("Alice"))

Comments