Coding Basics

A beginner-friendly guide to understanding how programming works

💻 What is Code?

Code is a set of instructions written in a language a computer can understand. Just like a recipe tells a chef exactly what to do step by step, code tells a computer exactly what to do — and in what order.

There are hundreds of programming languages (Python, JavaScript, Java, C++…), but they all share the same fundamental building blocks.

Source Code

The human-readable text you write, e.g. print("Hello")

Compiler / Interpreter

Translates your code into instructions the CPU can execute.

Output

The result: text on screen, a file saved, a website rendered.

Tip: Every program, no matter how complex, is built from simple pieces. Master the basics and everything else follows.

📁 Variables

A variable is a named container that stores a value. Think of it as a labelled box where you can put something, check what's inside, or swap the contents.

# Python example
name = "Alice"        # stores the text "Alice"
age  = 25             # stores the number 25
score = 98.5          # stores a decimal number

print(name)           # Output: Alice

Naming Rules

Variable names must start with a letter or underscore, can contain letters, numbers, and underscores, and cannot be a reserved keyword like if or while.

✅ Valid

score, player1, _total

❌ Invalid

1player, my-score, if

🆕 Data Types

Every value has a type that tells the computer what kind of data it is and what operations make sense on it.

Integer (int)

Whole numbers: 42, -7, 0

Float

Decimal numbers: 3.14, -0.5

String (str)

Text in quotes: "hello", 'world'

Boolean (bool)

True or False only: True, False

List / Array

Ordered collection: [1, 2, 3]

Dictionary / Object

Key-value pairs: {"name": "Alice"}

age     = 30               # int
price   = 9.99             # float
greeting = "Hello!"        # string
active  = True             # boolean
colors  = ["red", "blue"]  # list

⚙️ Operators

Operators let you perform actions on values — math, comparisons, and logic.

Arithmetic

10 + 3   # 13  (addition)
10 - 3   # 7   (subtraction)
10 * 3   # 30  (multiplication)
10 / 3   # 3.33 (division)
10 % 3   # 1   (remainder / modulo)
2 ** 8   # 256 (power / exponent)

Comparison (always returns True or False)

5 == 5   # True  (equal)
5 != 3   # True  (not equal)
5 >  3   # True  (greater than)
5 <= 5  # True  (less than or equal)

Logical

True and False  # False — both must be True
True or  False  # True  — at least one must be True
not True        # False — flips the value

🔄 Conditionals

Conditionals let your program make decisions. The code inside an if block only runs when the condition is True.

temperature = 35

if temperature > 30:
    print("It's hot outside!")
elif temperature > 15:
    print("Nice weather.")
else:
    print("Grab a jacket.")

# Output: It's hot outside!
How it works: Python checks conditions top to bottom. The first one that is True runs its block, then the rest are skipped. else is the catch-all fallback.

🔄 Loops

Loops let you repeat code without writing it out multiple times. There are two main types.

for loop — repeat a known number of times

for i in range(5):
    print("Step", i)

# Output: Step 0, Step 1, Step 2, Step 3, Step 4
fruits = ["apple", "banana", "cherry"]

for fruit in fruits:
    print(fruit)

# Output: apple, banana, cherry

while loop — repeat until a condition becomes False

count = 0

while count < 3:
    print("Count is", count)
    count = count + 1

# Output: Count is 0, Count is 1, Count is 2
Watch out: A while loop that never becomes False runs forever — this is called an infinite loop. Always make sure the condition can eventually be met.

🔧 Functions

A function is a reusable block of code that performs a specific task. Instead of writing the same code repeatedly, you define it once and call it whenever you need it.

def greet(name):
    return "Hello, " + name + "!"

print(greet("Alice"))   # Hello, Alice!
print(greet("Bob"))     # Hello, Bob!

Anatomy of a Function

def

Keyword that starts the function definition.

Parameters

Inputs in parentheses, e.g. name.

Body

Indented code that runs when called.

return

Sends a value back to whoever called the function.

A More Complete Example

def calculate_area(width, height):
    area = width * height
    return area

room = calculate_area(5, 4)
print(room)   # 20
Good practice: Each function should do one thing well. A function named calculate_area should calculate area — not also print it, save it to a file, and send an email.

🏆 Quick Quiz

Test yourself on what you've just learned.

Test Your Knowledge

1. What will print(10 % 3) output?

2. Which keyword starts a function definition in Python?

3. What type is the value True?

4. A loop that never stops is called…