Python is a programming language for giving computers instructions. Start by predicting what a small program will do, then change one line and explain the result. This beginner overview introduces the same printing, variables, decisions, and loops found in AI Ka School’s coding curriculum.
Break a small project into steps and check the result.
[ READ · TRY · CHECK ]
Your lessons are ready.
Everything below is available without an account. Pause at each question and make a prediction before opening the answer.
01
LESSON 01 / 03
Print, variables, and types
Use print() to display a message, name a value, and compare the number 5 with the text "5". Trace each line before looking at an answer.
The idea
A variable is a name for a value, not a box that remembers every earlier value.
Python follows the statements in order. When you assign score = score + 4, it first reads the current score, adds four, then lets the name score refer to the new result. Text inside quotes is a string; the number 7 without quotes is an integer.
Trace a program by writing the value beside each line. Before running it, predict what print() will show. A wrong prediction is a clue about which line you need to understand.
WORKED EXAMPLE
Trace a score
The first assignment gives score the value 3. The second changes it to 7.
score = 3
score = score + 4
print(score)
Result7
YOUR TURN
What prints if the first line is score = 8?
Need a hint?
Keep the + 4 line and change only the starting value.
Check your answer
12. The second line adds 4 to the new starting value of 8.
Try it further: A score starts at 3 and increases by 4. What should print(score) display?
02
LESSON 02 / 03
Decisions with if and else
Write a condition that chooses between two outcomes. Test an ordinary value and the value at the boundary so you know your rule does what you intended.
The idea
A condition chooses one branch based on a true or false expression.
An if statement asks a question. Python runs its indented block only when the condition is true; otherwise it can run an else block. The boundary value matters: >= includes the threshold, while > does not.
Make a small table of inputs before coding. For a badge unlocked at ten points, test 9, 10, and 11. This catches a common mistake where exactly ten is left out.
WORKED EXAMPLE
Check the boundary
The score equals the threshold, so the first branch runs.
The else branch runs. Ten is equal to ten, but it is not greater.
Try it further: A badge unlocks at 10 points. Should a score of exactly 10 unlock it?
03
LESSON 03 / 03
Loops and debugging
Repeat a short action with a for loop, identify the values in range(), and find a small mistake by comparing expected output with actual output.
The idea
A loop repeats a block once for each item in a sequence.
range(3) produces 0, 1, and 2. Its ending value is excluded. The indented line runs three times, once with each value. A while loop repeats while its condition remains true, so it needs a way to stop.
When a loop surprises you, write the loop variable and any changing score in a table for every pass. That is usually faster than guessing from the final line.
WORKED EXAMPLE
Count the turns
The loop prints three numbers; the last value is two.
for turn in range(3):
print(turn)
Result0
1
2
YOUR TURN
How many lines print for range(5)?
Need a hint?
List the values starting at zero and stop before five.
Check your answer
Five lines: 0, 1, 2, 3, and 4.
Try it further: Predict every line printed by a loop over range(3) before running it in a Python environment.
[ PUT IT TO WORK ]
Design a three-question quiz
Plan a quiz with three questions, a score variable, and a final message. First write the steps on paper; then implement them in a Python environment as your skills grow.
Check your work
The score begins at zero.
A correct answer increases the score exactly once.
You test both a correct answer and a wrong answer.
[ GO DEEPER / FOLLOW THE SOURCE ]
Further reading
These sources support further study. The lessons above are original AI Ka School material.