Programming Principles Guide
Programming Principles Guide
Programming
Principles
A comprehensive beginner's revision guide — Python edition
01 Variables
02 Data Types
03 Operators
04 Conditionals
05 Loops
06 Functions
Elementary Programming Principles Page 2
Contents
Variables
01
What they are, naming rules, assignment
Data Types
02
int, float, str, bool — and type conversion
Operators
03
Arithmetic, comparison, and logical operators
Conditionals
04
if, elif, else — making decisions in code
Loops
05
for and while — automating repetition
Functions
06
Defining, calling, parameters, return values, scope
Common Mistakes
07
Pitfalls every beginner hits — and how to avoid them
Quick Reference
08
Cheat sheet — all syntax at a glance
Variables
1 Named containers that hold your program's data
What is a Variable?
A variable is a named storage location in your computer's memory. You give it a name, assign it a
value, and then use that name anywhere in your program to read or update the value. Think of it as a
labelled box — the label is the variable name, and whatever you put inside is the value.
ANALOGY
Imagine a row of jars in a kitchen. Each jar has a label — 'sugar', 'salt', 'coffee'. You can look
inside any jar by reading its label, and you can replace the contents whenever you like. Variables
work exactly like this.
"Alice" 17 98.5
# Creating variables
name = "Alice" # str — text
age = 17 # int — whole number
score = 98.5 # float — decimal
passed= True # bool — True/False
# Reading a variable
print(name) # Alice
# Updating a variable
age = age + 1 # now age is 18
Naming Rules
✓ Start with a letter or underscore
In Python, the = sign means ASSIGNMENT (store this value). It is NOT the mathematical equals
sign. Writing x = x + 1 is perfectly valid — it means: take the current value of x, add 1, then store
the result back in x. To CHECK equality, use == (double equals).
Data Types
2 The kind of value a variable holds
x = 42 y = -7 z = 0
str — String
Any sequence of characters enclosed in single or double quotes. Can hold letters, numbers, symbols,
even emojis. The number 42 as a string is '42' — you cannot do maths with it until you convert it.
bool — Boolean
The simplest type — only two possible values: True or False. The result of every comparison or
logical expression is a bool. Used to control if-statements and while-loops.
# Check a type
type(42) # <class 'int'>
isinstance("hi", str) # True
WATCH OUT
Trying to cast a non-numeric string to int crashes the program: int('hello') raises a ValueError.
Always be sure the string actually contains a number before casting.
Operators
3 Performing actions on values
Operators are special symbols that perform operations on one or more values (called operands).
Python has three main families of operators, each producing different kinds of results.
Arithmetic Operators
Operator Name Example Result
+ Addition 7+3 10
- Subtraction 7-3 4
* Multiplication 7*3 21
** Exponent 2 ** 8 256
Comparison Operators
Comparison operators compare two values and always return a bool (True or False). They are the
backbone of if-statements and while-loops.
== Equal to 5 == 5 True
Logical Operators
Logical operators combine or modify boolean values. They let you check multiple conditions at once.
age = 17 score = 85
Conditionals
4 Making decisions — running different code based on conditions
ANALOGY
Think of a traffic light. The light checks a condition (how long has it been red?) and decides what
to do (stay red, or switch to green). Your program does the same — it checks a condition and
takes the appropriate action.
score = ?
Yes
score >= 80? "Distinction"
No
Yes
score >= 50? "Pass"
No
The if / elif / else decision tree — only one branch executes per run.
score = 72
Nested Conditionals
You can put an if-statement inside another if-statement. This is called nesting. Each level of nesting
requires another 4-space indent.
Python uses indentation (4 spaces per level) to define which code belongs inside a block. An
incorrect indent changes the logic of your program — or raises an IndentationError that stops it
running entirely. Always use 4 spaces (not a tab character).
Loops
5 Repeating code automatically — without copy-paste
# 0 1 2 3 4 count += 1
0 1 2 3 4 ?
Runs exactly 5 times Repeats until condition is False
for vs while — choose based on whether you know the iteration count in advance.
count = 1
while count <= 5:
print("Count:", count)
count += 1 # MUST change count or loop runs forever!
A while loop without something that eventually makes its condition False will run forever —
crashing or freezing your program. Always double-check that something inside the loop brings
you closer to the condition being False. If in doubt, add a safety counter: safety += 1 and break if
safety > 1000.
Functions
6 Reusable named blocks — write once, call anywhere
What is a Function?
A function is a named, reusable block of code that performs a specific task. Instead of repeating the
same ten lines of code in five places, you write them once inside a function and call the function's
name wherever you need it. Functions also let you pass in different inputs (arguments) so the same
logic can work on different data.
ANALOGY
A function is like a recipe in a cookbook. You write the recipe once. Whenever you want to make
that dish, you open the book to that page (call the function). You can even make variations by
changing the ingredients (passing different arguments).
def greet(name):
message = "Hello, " + name
return message
result = greet("Alice")
print(result) # Hello, Alice!
Parameters vs Arguments
These two words are often confused. A parameter is the placeholder variable in the function
definition. An argument is the actual value you pass when calling the function.
Return Values
The return keyword sends a value back to the caller. A function without a return statement returns
the special value None. You can return any type — number, string, bool, or even a list.
def is_even(n):
return n % 2 == 0 # returns True or False
print(is_even(4)) # True
print(is_even(7)) # False
Variable Scope
Scope determines where a variable is visible. Variables created inside a function are local — they
only exist while the function runs. Variables outside all functions are global — visible everywhere.
Global Scope
x = 10
Local Scope (inside function)
def my_func():
y = 20 # local only
print(x) # can see x!
print(y) # works here
print(y) # ERROR — y not here!
Global vs local scope — inner functions can read global variables, but not vice versa.
Common Mistakes
7 Pitfalls every beginner hits — and how to avoid them
Wrong Correct
if x = 5: if x == 5: # == checks equality
Wrong Correct
Wrong Correct
Wrong Correct
name = "Alice"
age = 17
print("Name: " + name + age) # ERROR print("Name: " + name + str(age)) # OK
Indentation errors
Everything inside a block must be indented by exactly 4 spaces.
Wrong Correct
if x > 0: if x > 0:
print('positive') # IndentationError print('positive') # 4 spaces
Wrong Correct
Quick Reference
8 All essential syntax at a glance
Integer x = 42
Float pi = 3.14
Operators
Add / subtract a + b a - b
Multiply / divide a * b a / b
Power a ** b
Conditionals
if statement if condition:
Loops
Increment shorthand x += 1
Functions
KEEP PRACTISING!
The best way to learn programming is to write code every day — even just 15 minutes. Try
modifying the examples in this guide. Break them on purpose, then fix them. Every error message
teaches you something. You've got this!