Introduction to Python
1.0 Intro · 1.1 Programming Concepts · 1.2 Basic Syntax
← → or Space to navigate · F for fullscreen
A high-level, interpreted programming language designed for readability
# 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}")
Programming languages, abstraction, constructs, number systems, encoding
Natural languages evolved naturally (English, Spanish). Formal languages are designed for a specific purpose.
Programming languages are formal languages — every detail matters.
Python is interpreted — fast to try ideas, no compile step.
Every program is built from three control structures:
x = 1
y = 2
if x > 0:
for i in range(10):
Plus supporting elements: variables, operators, functions, data types, collections.
0b1010
0o12
10
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.
Indentation, print, input, comments, objects, keywords, modules
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.
{}
IndentationError
print()
# 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}")
# 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, …
if
for
while
def
class
import
return
True
False
None
Modules — import to extend Python:
import math from collections import Counter import random
print(value, sep=' ', end='\n')
f"text {expression}"
name = input("prompt: ")
# single line
"""docstring"""
type(x)
isinstance(x, int)
id(x)
import math
from math import pi
0xff
bin()
hex()
Next: Chapter 2 — Variables & Types
variables · operators · types · built-ins · collections