Program 1
Hello, World!
The classic first program — print text to the console.
print("Hello, World!")Output
Hello, World!Topic 01 of 17
Get started with Python syntax, variables, input/output, how programs are executed, the main method, and your first programs.
Program 1
The classic first program — print text to the console.
print("Hello, World!")Output
Hello, World!Program 2
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.
# 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 itOutput
Hello from Python!Program 3
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.
# 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 = 6Program 4
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.
# 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 moduleProgram 5
Declare variables and inspect their dynamic types.
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
Read input from the user and greet them.
name = input("Enter your name: ")
print(f"Welcome, {name}!")Program 1
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`.
#!/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 values without using a temporary variable.
a, b = 5, 10
a, b = b, a
print("a =", a, "b =", b)Output
a = 10 b = 5Program 1
Assign and use a value in a single expression.
numbers = [1, 2, 3, 4, 5]
if (n := len(numbers)) > 3:
print(f"List has {n} items")