Chapter 1

Introduction to Python

1.0 Intro · 1.1 Programming Concepts · 1.2 Basic Syntax

← → or Space to navigate · F for fullscreen

What is Python?

A high-level, interpreted programming language designed for readability

Why Python?

  • Readable — code reads almost like English
  • Versatile — web, data science, automation, AI
  • Large ecosystem — 400,000+ packages on PyPI
  • Interactive — run code line-by-line in Jupyter
# Your first Python program
print("Hello, world!")

# Simple data science
scores = [72, 88, 95, 61, 79]
average = sum(scores) / len(scores)
print(f"Average: {average:.1f}")

1.1 Programming Concepts

Programming languages, abstraction, constructs, number systems, encoding

Natural vs. Formal Languages

Natural languages evolved naturally (English, Spanish).
Formal languages are designed for a specific purpose.

Property Natural Formal
Ambiguity High None
Redundancy High Low
Literalness Low Exact

Programming languages are formal languages — every detail matters.

Interpreted vs. Compiled

Style Examples How it runs
Interpreted Python, JS Line by line at runtime
Compiled C, Rust Translated to machine code first
Hybrid Java Compiled to bytecode, then run

Python is interpreted — fast to try ideas, no compile step.

Programming Constructs

Every program is built from three control structures:

Construct Meaning Python example
Sequence Execute one after another x = 1 then y = 2
Selection Choose a path if x > 0:
Iteration Repeat for i in range(10):

Plus supporting elements: variables, operators, functions, data types, collections.

Number Systems & Character Encoding

System Base Python prefix
Binary 2 0b1010
Octal 8 0o12
Decimal 10 10
Hex 16 0xa
print(bin(100))   # '0b1100100'
print(hex(255))   # '0xff'

ASCII — 128 characters, 1 byte each.
Unicode / UTF-8 — 1.1M+ code points; backward-compatible with ASCII.

print(ord('A'))      # 65
print(chr(65))       # 'A'
print(ord('😊'))     # 128522

All text in Python 3 is Unicode by default.

1.2 Basic Syntax

Indentation, print, input, comments, objects, keywords, modules

Indentation & Block Structure

for i in range(5):
    print(i)          # indented → part of the loop

if x > 0:
    print("positive") # indented → part of the if
    y = x * 2         # still in the if block
print("done")         # not indented → after the if

Python uses indentation (4 spaces) to define code blocks — not {}. Inconsistent indentation raises IndentationError.

# Basic print
print("Hello")
print("a", "b", sep="-")  # a-b
print("x", end=" ")       # no newline
# F-strings (Python 3.6+)
name = "Alice"
age = 25
print(f"Hello, {name}! You are {age}.")

x, y = 3, 4
print(f"Distance: {(x**2 + y**2)**0.5:.2f}")

Comments, Objects, Keywords & Modules

# Single-line comment

def area(r):
    """Docstring: return area of circle."""
    import math
    return math.pi * r ** 2

# Everything in Python is an object
x = 42
print(type(x))   # <class 'int'>
print(id(x))     # unique identity

Keywords — reserved words Python uses for structure:
if, for, while, def, class, import, return, True, False, None, …

Modules — import to extend Python:

import math
from collections import Counter
import random

Chapter 1 — Quick Reference

Concept Key syntax / notes
Print print(value, sep=' ', end='\n')
F-string f"text {expression}"
Input name = input("prompt: ")
Comment # single line · """docstring"""
Type check type(x) · isinstance(x, int)
Object identity id(x)
Import import math · from math import pi
Binary / Hex 0b1010, 0xff · bin(), hex()

End of Chapter 1

Next: Chapter 2 — Variables & Types

variables · operators · types · built-ins · collections