Skip to main content
Python Learning

Code Reference

Python Examples Library

Ready-to-run Python examples aligned with all nine chapters. Copy any snippet or open it directly in the interactive Playground.

Hello, World!

# Your first Python programprint("Hello, World!")print("Welcome to Python programming.")

Calls print() twice to display two lines of text in order.

Expected Output

Hello, World!
Welcome to Python programming.

Custom Separators & Line Endings

print("2026", "11", "12", sep="-")print("Step 1", end=" -> ")print("Step 2")

Uses sep='-' to join multiple arguments with hyphens, and end=' -> ' to stay on the same line.

Expected Output

2026-11-12
Step 1 -> Step 2

Adding Two Numbers

first_number = 18second_number = 24total = first_number + second_number print("First:", first_number)print("Second:", second_number)print("Sum:", total)

Stores two integers in variables, adds them with +, and displays the total.

Expected Output

First: 18
Second: 24
Sum: 42

Creating & Updating Variables

student = "Amina"points = 10print(student, "starts with", points) points += 15print(student, "now has", points)

Creates variables and updates points using the compound += operator.

Expected Output

Amina starts with 10
Amina now has 25

Assigning Multiple Variables

width, height = 8, 5area = width * heightprint("Width:", width, "Height:", height)print("Rectangle area:", area)

Assigns two variables on one line and calculates their product.

Expected Output

Width: 8 Height: 5
Rectangle area: 40

Checking Data Types with type()

title = "Python"chapters = 9rating = 4.8is_active = True print(type(title))print(type(chapters))print(type(rating))print(type(is_active))

Demonstrates str, int, float, and bool using Python's built-in type() function.

Expected Output

<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>

Converting Strings & Numbers

price_text = "12.50"quantity_text = "3" total_cost = float(price_text) * int(quantity_text)print("Total cost: $" + str(total_cost))

Converts string digits into float and int to perform math, then back to str for joining.

Expected Output

Total cost: $37.5

String Length, Indexing & Formatting

word = "Python"print("Length:", len(word))print("First letter:", word[0])print("Last letter:", word[-1])print(f"Learning {word} is clear!")

Measures string length with len(), accesses characters by index, and formats text with an f-string.

Expected Output

Length: 6
First letter: P
Last letter: n
Learning Python is clear!

Integers, Floats & Random Dice Roll

import random random.seed(7)roll = random.randint(1, 6)average = (roll + 4) / 2 print("Dice roll:", roll)print("Average:", average, type(average))

Generates a repeatable random integer between 1 and 6 and computes a float average.

Expected Output

Dice roll: 3
Average: 3.5 <class 'float'>

Evaluating Truthy & Falsy Values

Boolean ExpressionsRelated lesson →
print("bool(10):", bool(10))print("bool(0):", bool(0))print("bool('Hi'):", bool("Hi"))print("bool(''):", bool(""))

Shows how non-zero numbers and non-empty strings evaluate to True while 0 and '' evaluate to False.

Expected Output

bool(10): True
bool(0): False
bool('Hi'): True
bool(''): False

Arithmetic Operators (/, //, %, **)

Arithmetic OperatorsRelated lesson →
a = 17b = 5 print("17 + 5 =", a + b)print("17 / 5 =", a / b)print("17 // 5 =", a // b)print("17 % 5 =", a % b)print("2 ** 5 =", 2 ** 5)

Contrasts normal float division (/), floor division (//), modulus remainder (%), and exponentiation (**).

Expected Output

17 + 5 = 22
17 / 5 = 3.4
17 // 5 = 3
17 % 5 = 2
2 ** 5 = 32

Comparison Operators in Action

Comparison OperatorsRelated lesson →
score = 85passing_grade = 60 print("Passed:", score >= passing_grade)print("Perfect 100:", score == 100)print("Needs retake:", score < passing_grade)print("Not zero:", score != 0)

Evaluates >=, ==, <, and != to produce True or False results.

Expected Output

Passed: True
Perfect 100: False
Needs retake: False
Not zero: True

Combining Conditions with and, or, not

Logical OperatorsRelated lesson →
has_homework = Trueattended_class = Trueis_late = False can_submit = has_homework and attended_class and not is_lateprint("Can submit:", can_submit)print("Needs reminder:", is_late or not has_homework)

Uses logical and, or, and not to combine boolean conditions.

Expected Output

Can submit: True
Needs reminder: False

Identity (== vs is) and Membership (in)

Comparison OperatorsRelated lesson →
list_a = [10, 20]list_b = [10, 20]print("Same values (==):", list_a == list_b)print("Same object (is):", list_a is list_b) phrase = "Learn Python"print("'Python' in phrase:", "Python" in phrase)

Clarifies that == checks equal values while is checks whether two variables point to the exact same object.

Expected Output

Same values (==): True
Same object (is): False
'Python' in phrase: True