Your first Python program
Write one line, press Run, and watch the computer do exactly what you said. You will also learn to leave notes for humans with comments.
In 60 seconds
Your first Python program
Write one line, press Run, and watch the computer do exactly what you said. You will also learn to leave notes for humans with comments.
Your first line of code
print() instruction shows something on the screen. Whatever you put inside the round brackets gets printed. Text goes inside quote marks.print("Namaste, world!")
print("I am learning Python.")Python can do maths
+ to add, - to subtract, * to multiply and / to divide.print(12 + 30)
print(100 - 45)
print(4 * 15)
print(90 / 4)
print("4 * 15") # with quotes, this is just text, so no maths happens"4 * 15" is text, so Python prints it exactly as written. Without quote marks, 4 * 15 is a sum, so Python prints the answer, 60.Giving things names: variables
= sign. The name goes on the left, the value on the right. Then you can use the name instead of the value.samosas = 4
price_each = 15 # rupees, a made-up price
total = samosas * price_each
print("Samosas:", samosas)
print("Total in rupees:", total)print() gets several things separated by commas, it prints them all on one line with spaces between them.Comments: notes for humans
# sign on a line is a comment. Python skips it. Comments are for people reading your code, including you next week. Use them to explain why you did something.How running code works
- 1
You type code
Instructions, one per line. - 2
You press Run
Python reads your code from the top. - 3
Python follows each line in order
Line 1, then line 2, then line 3… - 4
You see the output
Anything you printed appears below the code.
Small rules that matter
- Spelling matters:
printworks,Printandprntdo not. - Quote marks come in pairs:
"Hello", not"Hello. - Brackets come in pairs too: every
(needs a). - Variable names cannot have spaces. Use an underscore instead:
price_each.
" and lower-case print. If something looks right but does not work, check these first.Watch and read more
Lab
Your own greeting, and a tea-stall bill that Python works out for you.
The problem
Press Run first to see what the starter prints. Then change it:
- Put your own name (or a nickname) inside the quotes for
name. - Make
greetinginclude your name, for example"Namaste, " + name. The+sign joins two pieces of text. - Make a variable
totalthat works out the bill:cupstimesprice_each. Let Python do the maths; do not type the answer yourself. - Print the greeting and the total. Press Run and check the numbers make sense.
Press Check when you are done. 🎉
Practice lab
Edit the code and run it. Python runs in your browser — nothing is sent to our server.
No output. Add print(...) to see values.
You are done when
Hard questions
Try to answer before you reveal. If you can answer these, you understood the lesson.
Q1Why is it better to write <code>total = cups * price_each</code> than <code>total = 36</code>?Reveal
Both give 36 today. But if the number of cups or the price changes, total = 36 is now wrong and you must fix it by hand. cups * price_each is always right, because Python works it out again each time the program runs.
Q2What would <code>print("Total: " + 36)</code> do, and how could you fix it?Reveal
It gives an error, because + cannot join text and a number. Fix it with a comma, print("Total:", 36), or by turning the number into text: "Total: " + str(36). You will learn a neater way, f-strings, in the next lesson.
Questions people ask
Do I need to install Python?
Not for this track. The code runs inside your browser. Later, if you want to write bigger programs on your own computer, you can install Python for free from the official site, python.org.
What is the difference between "5" and 5?
5 is a number, so you can do maths with it. "5" in quote marks is text, just a picture of the digit. Python treats them differently. You will see more of this in the next lesson.
Why does 90 / 4 print 22.5 and not 22?
In Python, / always gives an exact answer with a decimal point. If you want only the whole part, use //: 90 // 4 gives 22.
Can I break my computer with Python here?
No. The code here runs in a safe space inside the browser. The worst that can happen is an error message or a program that prints the wrong thing. Just fix it and press Run again.
Lesson test
5 questions. Get 3 right (60%) to pass and complete this lesson.