So far your programs have run from top to bottom, one line after another. Real programs need to do more: make decisions ("if this is true, do that") and repeat actions ("do this ten times"). In Python, those two ideas are conditions and loops, and together they unlock most of what you will build.
This guide assumes you know variables and basic types. If not, read my guide on Python variables, strings and numbers first. Type each example yourself and compare the output with what you see here. Every example below was run and checked.
Part 1: Conditions (making decisions)
if, elif and else
score = 72
if score >= 90:
print("Grade A")
elif score >= 70:
print("Grade B")
elif score >= 50:
print("Grade C")
else:
print("Try again")
Output:
Grade B
Python checks the conditions from top to bottom and runs the first block that is true. elif means "otherwise, if", and else catches everything left over. Only one block runs. Remember the colon at the end of each line, and the indentation under it.
Comparison operators
==equal to (note the double equals sign)!=not equal to>and<greater than, less than>=and<=greater or equal, less or equal
A single = stores a value in a variable. A double == compares two values. Mixing them up is one of the most common beginner errors.
Combining conditions: and, or, not
age = 20
has_ticket = True
if age >= 18 and has_ticket:
print("Entry allowed")
if not has_ticket:
print("Buy a ticket")
Output:
Entry allowed
andis true only when both sides are true.oris true when at least one side is true.notflips true to false and false to true.
Part 2: Loops (repeating things)
The for loop with range
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
range(1, 6) produces the numbers from 1 up to, but not including, 6. The variable i takes each value in turn, and the indented block runs once per value.
Adding things up
total = 0
for n in range(1, 6):
total = total + n
print("Total:", total)
Output:
Total: 15
This is a very common pattern: start a variable at zero, then update it inside the loop. Notice that the final print is not indented, so it runs once, after the loop finishes.
Looping over a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print("I like", fruit)
Output:
I like apple
I like banana
I like cherry
You can loop directly over the items of a list, which reads almost like English.
A loop with a condition inside
for n in range(1, 11):
if n % 2 == 0:
print(n, "is even")
Output:
2 is even
4 is even
6 is even
8 is even
10 is even
The % operator gives the remainder after division. A number is even when its remainder after dividing by 2 is zero.
The while loop
count = 3
while count > 0:
print("Countdown:", count)
count = count - 1
print("Liftoff!")
Output:
Countdown: 3
Countdown: 2
Countdown: 1
Liftoff!
A while loop keeps running as long as its condition is true. Make sure something inside the loop eventually makes the condition false. Here, count goes down by one each time. If you forget that line, the loop never ends, which is called an infinite loop. If that happens, press Ctrl+C in the terminal to stop it.
break and continue
for n in range(1, 10):
if n == 3:
continue
if n == 6:
break
print("n =", n)
Output:
n = 1
n = 2
n = 4
n = 5
continueskips the rest of the current round and moves on to the next one (that is why 3 is missing).breakstops the loop completely (that is why nothing after 5 is printed).
for or while: which one?
- Use for when you know what you are looping over: a range, a list, the letters of a word.
- Use while when you repeat until something changes, such as waiting for correct input.
Common beginner mistakes
- Using
=instead of==in a condition. - Forgetting the colon after
if,elif,else,fororwhile. - Wrong indentation. Code inside the block must be indented by the same amount.
- Off-by-one with range.
range(1, 6)stops at 5, not 6. - Infinite while loops. Always change the variable that the condition checks.
Practice tasks
- Ask the user for a number with
input()(remember to convert it) and print whether it is positive, negative or zero. - Print the multiplication table of 7, from 7 x 1 to 7 x 10, using a loop.
- Add up all the numbers from 1 to 100 and print the total.
- Loop through a list of five names and print only the ones that start with the letter "A".
- Challenge: write a program that keeps asking for a password until the user types the correct one, using
while.
Try each task before you look for help. If you get stuck, ask an AI assistant to explain your error rather than write the solution, so that you keep learning.
Final thoughts
Conditions and loops are the two tools that turn a list of instructions into a real program. Practice with small tasks until writing an if or a for feels natural. After that, lists and functions will make your programs shorter and more powerful.
Which practice task was the hardest for you? Tell me in the comments, and follow the blog for more guides on programming, AI, crypto and Kali Linux.

No comments:
Post a Comment