Skip to main content

Module 1 - Introduction to Python & Setup

This module lays the foundation of Python programming. Before writing any code, you must understand what Python is, why it's popular, where it's used, and how to set up your development environment.


1. What is Python?

Python is a high-level, interpreted, general-purpose programming language created by Guido van Rossum in 1991.

Python Philosophy

Python emphasizes code readability and simplicity. The language follows the Zen of Python principle: "There should be one—and preferably only one—obvious way to do it."

Key Characteristics of Python

  • Simple and easy-to-learn syntax
  • Interpreted (no compilation required)
  • Dynamically typed
  • Object-oriented and functional programming support
  • Extensive standard library
  • Large ecosystem of third-party packages
  • Cross-platform compatibility

Python Use Cases

  • Web Development – Django, Flask, FastAPI
  • Data Science & AI – Pandas, NumPy, TensorFlow, PyTorch
  • Automation & Scripting – Task automation, DevOps
  • Backend Development – REST APIs, microservices
  • Scientific Computing – Research, simulations
  • Testing & QA – pytest, Selenium
Why Learn Python?

Python consistently ranks as one of the most in-demand programming languages globally. It's the language of choice for AI/ML, data science, and rapid application development.


2. Python 2 vs Python 3

Python 2 reached end-of-life on January 1, 2020. All modern development uses Python 3.

Key Differences

FeaturePython 2Python 3
Printprint "Hello"print("Hello")
Division5/2 = 25/2 = 2.5
UnicodeASCII defaultUnicode default
StatusDeprecatedActive
Important

Only learn Python 3. Python 2 is no longer maintained and should not be used for new projects.


3. Installing Python

3.1 Official Installation

Windows:

  1. Download from python.org
  2. Run installer
  3. ✅ Check "Add Python to PATH"
  4. Verify: python --version

macOS:

# Using Homebrew
brew install python3

# Verify
python3 --version

Linux (Ubuntu/Debian):

sudo apt update
sudo apt install python3 python3-pip
python3 --version

Anaconda includes Python + scientific libraries pre-installed.

# Download from anaconda.com
# Verify
conda --version
python --version

4. Python Development Environment

4.1 Interactive Python (REPL)

python3
>>> print("Hello, Python!")
Hello, Python!
>>> 2 + 2
4
>>> exit()
Learning Tool

REPL (Read-Eval-Print Loop) is excellent for quick testing and learning.

4.2 Code Editors & IDEs

  • Free and lightweight
  • Python extension available
  • Excellent debugging support
  • Integrated terminal

PyCharm (Professional IDE)

  • Best-in-class Python IDE
  • Advanced refactoring
  • Database tools
  • Community Edition is free

Jupyter Notebook (Data Science)

  • Interactive notebooks
  • Perfect for data analysis
  • Inline visualizations
  • Browser-based

Other Options

  • Sublime Text
  • Atom
  • Vim/Neovim
  • Google Colab (cloud-based)

5. Setting Up VS Code for Python

Installation Steps

  1. Install VS Code from code.visualstudio.com

  2. Install Python Extension

    • Open Extensions (Ctrl+Shift+X)
    • Search "Python"
    • Install official Microsoft Python extension
  3. Configure Python Interpreter

    • Press Ctrl+Shift+P
    • Type "Python: Select Interpreter"
    • Choose your Python installation
  4. Create Your First File

    # hello.py
    print("Hello, Python!")
  5. Run Your Code

    • Press F5 or
    • Right-click → Run Python File

6. Package Management with pip

pip is Python's package installer.

Basic pip Commands

# Install a package
pip install requests

# Install specific version
pip install requests==2.28.0

# Upgrade a package
pip install --upgrade requests

# Uninstall
pip uninstall requests

# List installed packages
pip list

# Show package details
pip show requests

# Install from requirements file
pip install -r requirements.txt

Creating requirements.txt

# Export current packages
pip freeze > requirements.txt

# Content example:
# requests==2.28.0
# flask==2.3.0
# numpy==1.24.0
Best Practice

Always use virtual environments to isolate project dependencies (covered in Module 3).


7. Your First Python Program

hello.py

"""
My First Python Program
Author: Your Name
Date: 2026-01-18
"""

# This is a single-line comment

def main():
"""Main function"""
name = input("Enter your name: ")
print(f"Hello, {name}! Welcome to Python.")

# Basic calculation
age = int(input("Enter your age: "))
print(f"In 10 years, you will be {age + 10} years old.")

if __name__ == "__main__":
main()

Key Concepts Introduced

  • Comments: # for single-line
  • Docstrings: """...""" for documentation
  • Functions: def function_name():
  • Input: input() function
  • Output: print() function
  • F-strings: f"text {variable}"
  • Type conversion: int(), str(), float()

8. Python Coding Style (PEP 8)

PEP 8 is Python's official style guide.

Essential Rules

# Good: 4 spaces indentation
def calculate_sum(a, b):
result = a + b
return result

# Bad: Inconsistent spacing
def calculate_sum(a,b):
result=a+b
return result

# Good: Descriptive names
user_name = "John"
total_price = 100

# Bad: Cryptic names
un = "John"
tp = 100

# Good: 2 blank lines before top-level functions
def function_one():
pass


def function_two():
pass

Naming Conventions

TypeConventionExample
Variablessnake_caseuser_name
Functionssnake_casecalculate_total()
ClassesPascalCaseUserAccount
ConstantsUPPER_CASEMAX_SIZE
Moduleslowercaseutilities.py
Auto-Formatting

Use tools like black or autopep8 to automatically format your code.

pip install black
black your_file.py

9. Understanding Python Execution

Interpreted Language

Source Code (.py) → Python Interpreter → Bytecode (.pyc) → Python Virtual Machine → Output

Advantages

  • No compilation step
  • Platform-independent
  • Rapid development
  • Easy debugging

Disadvantages

  • Slower than compiled languages (C, C++, Rust)
  • Not suitable for system programming
  • Runtime errors instead of compile-time
Performance Tip

For performance-critical code, use libraries written in C (NumPy, Pandas) or tools like PyPy, Cython, or Numba.


10. Python Standard Library

Python's strength lies in its "batteries included" philosophy.

Essential Built-in Modules

import os          # Operating system interface
import sys # System-specific parameters
import datetime # Date and time handling
import json # JSON encoding/decoding
import math # Mathematical functions
import random # Random number generation
import re # Regular expressions
import pathlib # Object-oriented filesystem paths

Quick Examples

import datetime
import random
import json

# Current date and time
now = datetime.datetime.now()
print(f"Current time: {now}")

# Random number
number = random.randint(1, 100)
print(f"Random number: {number}")

# JSON handling
data = {"name": "John", "age": 30}
json_string = json.dumps(data)
print(json_string)

Summary

✅ Python is a versatile, beginner-friendly language
✅ Always use Python 3 (not Python 2)
✅ VS Code + Python extension is excellent for beginners
✅ pip manages packages and dependencies
✅ Follow PEP 8 style guidelines
✅ Python's standard library is extensive


Next Steps

In Module 2, you'll learn:

  • Python syntax fundamentals
  • Variables and data types
  • Basic operators
  • Input/output operations
  • Type conversions

Practice Exercises

  1. Install Python 3 and verify with python --version
  2. Set up VS Code with the Python extension
  3. Write a program that asks for your name and age, then prints a personalized message
  4. Install three packages using pip: requests, pandas, flask
  5. Create a requirements.txt file for your project
Challenge

Write a program that:

  • Asks the user for two numbers
  • Performs addition, subtraction, multiplication, and division
  • Displays all results in a formatted way