PyForge · Python practice library
All topics

Topic 01 of 17

Python Basics

Get started with Python syntax, variables, input/output, how programs are executed, the main method, and your first programs.

print() & input()
Variables & types
Comments
Indentation
Type casting
How Python executes
__main__ guard
Scripts vs Modules

Basic

6 programs

Program 1

Hello, World!

Basic

The classic first program — print text to the console.

python
print("Hello, World!")

Output

Hello, World!

Program 2

How Python code runs

Basic

Python is an interpreted language. When you run a .py file, the interpreter reads the source, compiles it to bytecode (.pyc), and the Python Virtual Machine (PVM) executes that bytecode line by line. This makes Python portable but slower than compiled languages like C.

python
# Step 1: Write source code in hello.py
print("Hello from Python!")

# Step 2: Run it in terminal
# python hello.py

# Behind the scenes:
# 1. Source code -> Bytecode (cached as __pycache__/hello.cpython-311.pyc)
# 2. Bytecode -> Python Virtual Machine executes it

Output

Hello from Python!

Program 3

The main method / entry point

Basic

In Python there is no required 'main' function, but the idiomatic entry point uses `if __name__ == '__main__':`. This block runs only when the file is executed directly (as a script), not when it is imported as a module. It keeps reusable code separate from script logic.

python
# calculator.py
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

# This block only runs when the file is executed directly
if __name__ == "__main__":
    print("Running as script")
    print("5 + 3 =", add(5, 3))
    print("10 - 4 =", subtract(10, 4))

Output

Running as script
5 + 3 = 8
10 - 4 = 6

Program 4

Script vs Module execution

Basic

When you import a file, Python sets `__name__` to the module name. When you run the file directly, `__name__` is set to `'__main__'`. This distinction lets you write code that behaves differently when imported vs. when run.

python
# my_module.py
print(f"__name__ is: {__name__}")

if __name__ == "__main__":
    print("Executed directly")
else:
    print("Imported as module")

# If you run: python my_module.py
# Output: __name__ is: __main__
#         Executed directly
#
# If you import: import my_module
# Output: __name__ is: my_module
#         Imported as module

Program 5

Variables and Types

Basic

Declare variables and inspect their dynamic types.

python
name = "Ada"
age = 36
height = 5.6
is_dev = True

print(name, type(name))
print(age, type(age))
print(height, type(height))
print(is_dev, type(is_dev))

Program 6

Take user input

Basic

Read input from the user and greet them.

python
name = input("Enter your name: ")
print(f"Welcome, {name}!")

Intermediate

2 programs

Program 1

Shebang line (Unix/Linux/Mac)

Intermediate

On Unix-like systems, adding a shebang (`#!/usr/bin/env python3`) as the very first line lets you run the script directly without typing 'python3' before it. The file must also be made executable with `chmod +x script.py`.

python
#!/usr/bin/env python3
# Save this as greet.py, then run: chmod +x greet.py && ./greet.py

name = input("Your name: ")
print(f"Hello, {name}!")

Program 2

Swap two numbers

Intermediate

Swap values without using a temporary variable.

python
a, b = 5, 10
a, b = b, a
print("a =", a, "b =", b)

Output

a = 10 b = 5

Advanced

1 program

Program 1

Walrus operator (:=)

Advanced

Assign and use a value in a single expression.

python
numbers = [1, 2, 3, 4, 5]
if (n := len(numbers)) > 3:
    print(f"List has {n} items")