OkayTeacher
EN English 한국어 日本語 中文 हिन्दी বাংলা اردو Español Português Français Deutsch Русский العربية Bahasa Indonesia Tiếng Việt ไทย Türkçe

How to enable notifications

Notifications are unavailable in this browser or on this address. Try a supported browser on the secure SONUB site.

If notifications are blocked, allow them in your browser settings.

  1. Open the site controls beside the address bar, or your browser’s site settings. Set Notifications for this site to Allow.
  2. In your device settings, allow notifications for your browser and check Do Not Disturb or Focus.
  3. Return to SONUB, reload the page, and use the bell to enable notifications.

On iPhone or iPad, add SONUB to your Home Screen, open it there, then allow notifications.

Learn the Python language

9 steps of Python you code right in your browser

Learn Python, the top language for building AI, by running code in your browser — nothing to install. Run an example, change one value, and compare the results.

A smiling laptop with an empty speech bubble coming out of its screen, a hand typing on its keyboard, and a blank paper strip held between two big quote-mark posts.
Completed steps
0/9
Progress
0%
Runs
0

Real runs inside your (). It downloads the just once the first time, and after that it runs fast. If you want to learn more deeply, continue on to the Build Your Own AI lab.

Step 1 / 9

Passed! You completed step 1. Shall we move on to the next step?

print() — Talk to the computer

Shall we make the computer talk? () is a magic command that makes the computer "say" words on the screen. Write what you want to say inside the quotes " ", and the computer shows it just as it is. The double quotes are a fence that says "the words go from here to here."

A smiling laptop with an empty speech bubble coming out of its screen, a hand typing on its keyboard, and a blank paper strip held between two big quote-mark posts.
print makes the computer say what is between the quotes, exactly as you wrote it.

Goal · Use print() to show anything you want to say. If words appear on the screen, you did it!

You succeed if it looks like this!

Hello, Python!

I am learning Python

(The words inside the quotes show up just as you wrote them)

You've opened all hints

  1. Put the words, wrapped in quotes, inside the brackets of print( ).

  2. Example: print("I am learning Python")

  3. If you forget the quotes, the computer won’t understand. Always wrap your words "like this".

Step 2 / 9

Passed! You completed step 2. Shall we move on to the next step?

Put a name tag on a value

Just like putting a name tag on a toy box, you can give a value a name. Write name = value, and after that you only need to call the name to get the value back. A value with a name tag like this is called a "". Here = does not mean "equals". It means "put this value into this name".

Boxes on a shelf, each with a blank name tag, holding an apple, a toy car and a ribbon; a star drops into the empty fourth box while a hand holds its tag.
A variable is a box with a name tag. Put a value in, then call the name to get it back.

Goal · Put your own name into the variable called name. Then the computer will say hello with your name!

You succeed if it looks like this!

My name is (the name you typed)

— The name on your name tag shows up in the greeting

You've opened all hints

  1. Write your name between the quotes. Example: name = "Alex"

  2. The left side of = is the name tag. The right side is the value that goes inside it.

  3. Just write a name inside the quotes, like name = "Sam".

Step 3 / 9

Passed! You completed step 3. Shall we move on to the next step?

Calculate with numbers

We wrapped words in quotes, but numbers are written just as they are, with no quotes. Then the computer knows they are real numbers and can even do math with them! You can add like 3 + 5, and keep the answer in a name tag (a ).

Goal · Put apples plus bananas into total, so there are 8 fruits in all.

You succeed if it looks like this!

Fruits in total: 8

— 3 apples + 5 bananas = 8!

You've opened all hints

  1. To add, use the + sign.

  2. You can add name tags together too, like total = apples + bananas.

  3. Write total = apples + bananas and total becomes 8.

Step 4 / 9

Passed! You completed step 4. Shall we move on to the next step?

Calculate like a calculator

Python is a clever calculator. Use + to add and - to subtract. To multiply, use * instead of ×, and to divide, use / instead of ÷. The area of a rectangular room is "width × height", and you tell the computer width * height.

Goal · Multiply width by height so the area of the room becomes 48.

You succeed if it looks like this!

The area of the room is 48

— width 8 × height 6 = 48!

You've opened all hints

  1. To multiply, use the * sign, not ×.

  2. Multiply with a star, like area = width * height.

  3. Write area = width * height and you get 48.

Step 5 / 9

Passed! You completed step 5. Shall we move on to the next step?

if — What if? Make a fork in the road

"If it rains, take an umbrella. If not, just go out." The computer can make forks in the road like this too. if : means "if the condition is true", and : means "otherwise". Lines inside a fork must start with 4 spaces (), so the computer knows "this line is inside the fork".

A child with a backpack at a fork in the road: one path leads into rain with an umbrella, the other into sunshine.
if takes one path when the condition is true, and else takes the other.

Goal · If score is 60 or more, put "pass" into grade. Otherwise, put "fail". (The score right now is 85!)

You succeed if it looks like this!

Result: pass

— 85 is more than 60, so it is "pass"!

You've opened all hints

  1. Write if score >= 60: and on the next line, after 4 spaces, write grade = "pass".

  2. On the line under else:, write grade = "fail" (also after 4 spaces).

  3. if score >= 60:
        grade = "pass"
    else:
        grade = "fail"
Step 6 / 9

Passed! You completed step 6. Shall we move on to the next step?

for — Repeat many times

Counting and adding from 1 to 10 one by one would make your hands tired, right? Computers are great at repeating. for number in (1, 11): means "put 1, 2, 3 … 10 into number one after another, and repeat". (range(1, 11) means 1 to 10.) Inside the , you can keep adding values little by little.

A toy train goes round a circular track and drops one bead into a jar each lap, while a row of stones beside the track shows how many laps are done.
for repeats the same steps, one value at a time, until the range ends.

Goal · Add up all the numbers from 1 to 10 so that total becomes 55.

You succeed if it looks like this!

1 to 10 added up: 55

— 1 + 2 + 3 + … + 10 = 55!

You've opened all hints

  1. Inside the loop, keep adding number to the old total, like total = total + number.

  2. total = 0 should appear only once, at the top, before the loop starts.

  3. Change the line inside the loop to total = total + number and you get 55.

Step 7 / 9

Passed! You completed step 7. Shall we move on to the next step?

list — Put many things in a box

Just like putting many pencils into one pencil case, you can put many values into one box. This box is called a "", and you make it with square brackets [ ]. To put something new into the box, use box.append("thing"), and to count what is inside, use (box).

An open pencil case with colored pencils in a row and a new pencil sliding into the empty slot at the end.
A list keeps many values in order. append adds a new one at the end.

Goal · Put one more "orange" into the fruits box so there are 4 fruits in all.

You succeed if it looks like this!

['apple', 'pear', 'grape', 'orange']

Count: 4

— The orange went in, so now there are 4!

You've opened all hints

  1. To put something into the box, write fruits.append("orange").

  2. adds the new thing to the very end of the box.

  3. Just add one line: fruits.append("orange").

Step 8 / 9

Passed! You completed step 8. Shall we move on to the next step?

dict — Find values by name tag

When you look up a word in a , you find its meaning, right? A Python dictionary works the same way. Give it the name tag () "reading" and you get the score (value) 90. You make it with curly braces { }, and add a new pair like scores["science"] = 100. Name tags let you find values very quickly.

A wall of small lockers with different label shapes; a hand with a star-shaped key opens the star locker, which holds a trophy card.
A dictionary finds a value straight away by its key, like a locker that opens with its own key.

Goal · Add "science": 100 to scores, then work out the average of the three subjects (reading, math, science) so that it is 90.

You succeed if it looks like this!

{'reading': 90, 'math': 80, 'science': 100}

Average: 90.0

— The average of the three subjects is 90!

You've opened all hints

  1. To add a new pair, write scores["science"] = 100.

  2. The average is (90 + 80 + 100) divided by 3.

  3. Write scores["science"] = 100, then average = (scores["reading"] + scores["math"] + scores["science"]) / 3.

Step 9 / 9

Passed! You completed step 9. Shall we move on to the next step?

def — Make your own magic spell

If you do the same thing often, it helps to make your own magic spell. This spell is called a "". double(x): means "make a spell called double that takes x as its ingredient", and return means "give this value back". Once you make it, you can use it again and again, like double(10) or double(7).

A cute magic machine: one apple goes into a funnel on top and two apples come out of a chute on the side.
A function is a machine you build once. Something goes in, and return gives the result back.

Goal · Finish the spell double: it takes a number and gives back that number made twice as big.

You succeed if it looks like this!

20

14

— double(10) gives back 20, and double(7) gives back 14!

You've opened all hints

  1. Twice as big is x * 2.

  2. Write the value to give back after return.

  3. Change it to return x * 2 and your spell is complete.

Preparing the Python runtime…

Python code

Output


                        

Almost there!

If you see an error

SyntaxError: unterminated string literal means a quote is missing. Every opening " needs a closing ", like print("Hello"). Write print in small letters too: Print gives NameError.

One step further

Print two lines, then try print("Ha" * 3) and guess what you will see before you run it.

If you see an error

NameError: name 'nme' is not defined means Python does not know that name. Usually it is a typo, or the name is used before the line that makes it. A name must match exactly, even capital letters.

One step further

Make one more variable, age = 10, and print both with print(name, age).

If you see an error

"3" + 5 gives TypeError, because text in quotes and a number cannot be added. Take away the quotes, or turn the text into a number with int("3").

One step further

Change apples = 3 to apples = 10 and run again. The total changes by itself, because it is calculated, not typed.

If you see an error

Writing 8 x 6 or 8 × 6 gives SyntaxError, because Python multiplies with *. Also, / always gives a number with a decimal point: 7 / 2 is 3.5, and even 8 / 2 is 4.0. Use // when you want a whole number.

One step further

Try powers with **: 2 ** 10 multiplies 2 by itself ten times.

If you see an error

Two common slips: forgetting the colon at the end of if score >= 60:, and lines inside the fork without 4 spaces, which gives IndentationError. Remember: = puts a value in, and == asks "are these equal?".

One step further

Add elif score >= 90: before else: so a very high score gets its own message.

If you see an error

range(1, 10) stops at 9, because the end number is not included. For 1 to 10, write range(1, 11). And if you set the total to 0 inside the loop, it starts over every time; set it once, before the loop.

One step further

Add only even numbers with range(2, 11, 2), which counts 2, 4, 6, 8, 10.

If you see an error

Counting starts at 0, so the first item is fruits[0]. Asking for fruits[3] in a list of three gives IndexError: list index out of range.

One step further

Take one item out with fruits.remove("pear") and print the list again.

If you see an error

Asking for a key that is not there, like scores["music"], gives KeyError. Check the spelling, or use scores.get("music"), which gives None (meaning "no value") instead of an error.

One step further

Print every pair with for subject, score in scores.items(): and one print line inside.

If you see an error

If you forget return, the function gives back None, so print(double(10)) shows . A function also runs only when you call it with brackets: double(10), not just double.

One step further

Write def greet(name): that returns a greeting with the name in it, and call it with your own name.