Python Beginner
Simple, readable, and powerful — the perfect first language.
Table of Contents
Hello World & Comments
Every Python program starts with a simple print statement. No semicolons, no curly braces — just clean, indented code.
# This is a single-line comment print("Hello, World!") # inline comment """ This is a multi-line comment (also called a docstring). """ print("Python is awesome!")
python hello.py. You can also open an interactive shell by typing python alone.
Variables & Data Types
Python is dynamically typed: you don't declare a type — Python figures it out from the value. You can check the type of any variable with type().
name = "Alice" # str age = 25 # int height = 1.68 # float active = True # bool # f-strings: embed variables inside strings print(f"Name: {name}, Age: {age}") # Check type print(type(height)) # <class 'float'> # Multiple assignment x, y, z = 1, 2, 3
Operators
Python supports all standard arithmetic, comparison, and logical operators.
# Arithmetic print(10 + 3) # 13 — addition print(10 - 3) # 7 — subtraction print(10 * 3) # 30 — multiplication print(10 / 3) # 3.33.. — division print(10 // 3) # 3 — integer division print(10 % 3) # 1 — modulo (remainder) print(2 ** 8) # 256 — exponentiation # Comparison → returns bool print(5 == 5) # True print(5 != 3) # True print(5 > 3) # True # Logical print(True and False) # False print(True or False) # True print(not True) # False
String Methods
Strings in Python are objects with many built-in methods. They are immutable — methods always return a new string.
s = " Hello, Python! " print(s.strip()) # "Hello, Python!" print(s.lower()) # " hello, python! " print(s.upper()) # " HELLO, PYTHON! " print(s.replace("Python", "World")) # Slicing word = "Python" print(word[0]) # P print(word[1:4]) # yth print(word[::-1]) # nohtyP (reversed) # f-string formatting pi = 3.14159 print(f"Pi is approximately {pi:.2f}") # 3.14
Conditionals
Python uses if, elif, and else for decision-making. Indentation (4 spaces) is mandatory — it replaces the curly braces of other languages.
score = 72 if score >= 90: print("Grade: A") elif score >= 75: print("Grade: B") elif score >= 60: print("Grade: C") # ← this runs else: print("Grade: F") # Ternary (one-line) conditional status = "Pass" if score >= 60 else "Fail" print(status) # Pass
switch statement, but from Python 3.10+ you can use match / case for the same purpose.
Loops
Python has two loop types: for (iterate over a sequence) and while (repeat while a condition is true).
# for loop with range() for i in range(5): # 0, 1, 2, 3, 4 print(i) # for loop over a list fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(f"I like {fruit}") # while loop count = 0 while count < 3: print(f"count = {count}") count += 1 # break and continue for n in range(10): if n == 3: continue # skip 3 if n == 6: break # stop at 6 print(n) # 0 1 2 4 5
Lists & Tuples
A list is mutable (changeable), while a tuple is immutable. Both are ordered sequences.
# List — mutable nums = [10, 20, 30, 40] nums.append(50) # add to end nums.remove(20) # remove by value print(nums[0]) # 10 (indexing) print(len(nums)) # 3 # List comprehension squares = [x**2 for x in range(1, 6)] print(squares) # [1, 4, 9, 16, 25] # Tuple — immutable coords = (48.8566, 2.3522) # lat/lon Paris lat, lon = coords # unpacking print(lat, lon)
Functions
Functions let you package reusable logic. Define them with def, call them by name, and optionally return a value with return.
# Basic function with default parameter def greet(name="stranger"): return f"Hello, {name}!" print(greet()) # Hello, stranger! print(greet("Alice")) # Hello, Alice! # Multiple return values def min_max(numbers): return min(numbers), max(numbers) lo, hi = min_max([3, 1, 9, 4]) print(lo, hi) # 1 9 # Lambda — anonymous one-liner function square = lambda x: x ** 2 print(square(7)) # 49
os, json, math).