Python Introduction

Python is the world's most popular programming language — loved for its readability, versatility, and massive ecosystem. Whether you're building websites, analyzing data, training AI models, or automating tasks, Python has you covered.

Why Python?

"Python is an experiment in how much freedom programmers need. Too much freedom and nobody can read another's code; too little and expressiveness is endangered." — Guido van Rossum (Python's creator)

  • Readable — Code reads almost like English
  • Versatile — Web, data science, AI, automation, games, desktop
  • Ecosystem — 400,000+ packages on PyPI
  • Community — Largest programming community in the world
  • Rapid prototyping — Build ideas fast
  • High demand — Top 3 most-wanted language by employers

Your First Python Program

Python
# Hello, World!
print("Hello, World!")

# Python has no semicolons, no braces — just indentation
name = "Alice"
age = 28
print(f"My name is {name} and I'm {age} years old.")

Variables — No Type Declarations Needed

Python
# Python figures out the type for you (type inference)
name = "Alice"          # str (string)
age = 28                # int (integer)
pi = 3.14159            # float (decimal)
active = True           # bool (boolean)
data = None             # NoneType (Python's null)

# Type hints (optional — helps editors and documentation)
def greet(name: str) -> str:
    return f"Hello, {name}!"

# Check types at runtime
print(type(name))       # <class 'str'>
print(isinstance(age, int))  # True

Strings — Incredibly Powerful

Python
# f-strings (formatted string literals) — the best way
name = "Alice"
print(f"Hello, {name}!")
print(f"2 + 2 = {2 + 2}")
print(f"Pi is approximately {pi:.2f}")  # 3.14

# String methods
text = "  Hello, World!  "
print(text.strip())           # "Hello, World!" (remove whitespace)
print(text.lower())           # "  hello, world!  "
print(text.upper())           # "  HELLO, WORLD!  "
print(text.replace("World", "Python"))  # "  Hello, Python!  "
print(text.split(","))        # ['  Hello', ' World!  ']
print("hello@world.com" in text)  # False

# Multi-line strings
poem = """
Roses are red,
Violets are blue,
Python is awesome,
And so are you.
"""

# String slicing
s = "Python"
print(s[0])      # 'P' (first char)
print(s[-1])     # 'n' (last char)
print(s[0:3])    # 'Pyt' (first 3 chars)
print(s[::-1])   # 'nohtyP' (reversed!)

The Zen of Python

Python
import this
# The Zen of Python, by Tim Peters
#
# Beautiful is better than ugly.
# Explicit is better than implicit.
# Simple is better than complex.
# Complex is better than complicated.
# Flat is better than nested.
# Sparse is better than dense.
# Readability counts.

Python 2 vs Python 3

Always use Python 3. Python 2 reached end-of-life in 2020.

Feature Python 2 Python 3
Print print "hello" print("hello")
Division 5/2 = 2 5/2 = 2.5
Unicode ASCII default Unicode default
f-strings Not available Available (3.6+)
Status Dead (2020) Active development

Installing Python

Bash
# Check if Python is installed
python3 --version

# Install packages with pip
pip install requests flask numpy

# Create a virtual environment (best practice)
python3 -m venv myenv
source myenv/bin/activate  # macOS/Linux
myenv\Scripts\activate     # Windows

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

Knowledge Check

4 questions — test your understanding

1

What is the output of: print(type(True))?

2

Which is the correct way to format a string in modern Python?

3

What does Python's `None` represent?

4

What is the output of: print('Python'[::-1])?