Python Tutorial — Learn Python Step by Step
Key takeaway: Master Python programming from scratch — variables, loops, functions, OOP, file handling, and libraries — with clear human-readable explanations and runnable code examples.
Python Package
A package is a directory containing Python modules and a special __init__.py file. Packages let you organize related modules into a hierarchical structure.
Package Structure
# A typical package structure:
# myproject/
# ├── __init__.py # makes this directory a package
# ├── core.py # module with core logic
# ├── utils.py # utility functions
# └── models/ # sub-package
# ├── __init__.py
# ├── user.py
# └── product.py
# __init__.py can be empty or contain package initialization codeCreating a Package
# File: mypackage/__init__.py
"""My reusable package."""
__version__ = "1.0.0"
# Optionally expose key items at package level
from .core import process_data
from .utils import clean_text
# File: mypackage/core.py
def process_data(data):
"""Process raw data and return results."""
return [item.strip().lower() for item in data]
# File: mypackage/utils.py
def clean_text(text):
"""Remove extra whitespace."""
return " ".join(text.split())
def format_name(first, last):
"""Format a full name."""
return f"{first.title()} {last.title()}"Importing from Packages
# Import the package (runs __init__.py)
import mypackage
print(mypackage.__version__) # "1.0.0"
# Import a module from the package
from mypackage import utils
print(utils.clean_text(" hello world "))
# Import a specific function
from mypackage.utils import format_name
print(format_name("john", "doe")) # "John Doe"
# Import from sub-package
from mypackage.models.user import User
# Alias for convenience
from mypackage.core import process_data as processRelative Imports (Within a Package)
# File: mypackage/core.py
# Use relative imports to reference other modules in the same package
from .utils import clean_text # same directory
from .models.user import User # sub-package
from ..shared import constants # parent package (if nested)
# Relative import syntax:
# . = current package
# .. = parent package
# ... = grandparent package
# Note: relative imports only work inside packages
# Running a file directly (python core.py) breaks relative imports
# Always run from outside: python -m mypackage.corePackage with __init__.py Configuration
# File: mypackage/__init__.py
# Control what "from mypackage import *" exports
__all__ = ["process_data", "clean_text", "User"]
# Package-level imports for convenient access
from .core import process_data
from .utils import clean_text
from .models.user import User
# Now users can do:
# from mypackage import process_data, User
# instead of:
# from mypackage.core import process_data
# from mypackage.models.user import UserInstalling Your Own Package
# Create a minimal pyproject.toml (modern standard)
# mypackage/
# ├── pyproject.toml
# ├── src/
# │ └── mypackage/
# │ ├── __init__.py
# │ └── core.py
# pyproject.toml content:
# [project]
# name = "mypackage"
# version = "1.0.0"
# [build-system]
# requires = ["setuptools"]
# build-backend = "setuptools.backends._legacy:_Backend"
# Install in development mode:
# pip install -e .
# Now you can import mypackage from anywhere- A package is a directory with
__init__.py— it groups related modules together. - Use
__init__.pyto expose a clean public API and controlimport *behavior. - Use relative imports (
from .module import func) within packages. - Run package modules with
python -m package.moduleto avoid import issues. - Use
pip install -e .for development to make your package importable globally.
Python Programming Tutorial — Learn Python in a practical, step-by-step way. According to the official Python documentation, Python emphasizes code readability and simplicity, making it ideal for beginners.
Related tools: Python Online Compiler | Python Examples | JavaScript Tutorial | Java Tutorial
Python Career & Learning Guide
Python is the world's most popular programming language for beginners, data science, AI/ML, web development, and automation. This tutorial teaches Python step-by-step with clear explanations and runnable code examples in our free Python Compiler.
Why Learn Python in 2026?
- #1 Most Popular Language: Ranked first on TIOBE, Stack Overflow, and GitHub.
- AI and Data Science: The primary language for machine learning (TensorFlow, PyTorch, scikit-learn) and data analysis (Pandas, NumPy).
- Web Development: Django and Flask power scalable backends at companies like Instagram and Spotify.
- Automation & Scripting: Automate files, emails, web scraping, and data processing in minutes.
Python vs Other Languages
| Feature | Python | Java | JavaScript | C++ |
|---|---|---|---|---|
| Syntax | Very clean, readable | Verbose | Moderate | Complex |
| Typing | Dynamic, strong | Static, strong | Dynamic, weak | Static, strong |
| Best For | AI/ML, Data, Web | Enterprise, Android | Web Frontend/Backend | Systems, Games |
| Learning Time | 2–4 weeks basics | 4–6 weeks basics | 3–4 weeks basics | 8–12 weeks basics |
Frequently Asked Questions
Do I need prior programming experience to learn Python?
No! Python is specifically designed to be beginner-friendly. This tutorial starts from zero and builds up gradually.
Which Python version should I use?
Python 3.10+ is recommended. All examples in this tutorial use Python 3 syntax.
Is this Python tutorial completely free?
Yes, 100% free with no registration or sign-up required. Access all lessons and code examples anytime.