Python from zeroBeginnerLesson 996 min read

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.

Lesson in motion

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.

1/5
In simple words
A computer is a very fast helper that does exactly what you tell it, nothing more and nothing less. A program is a list of instructions for that helper, like a recipe. Python is one language for writing those instructions. It is popular because it reads almost like simple English.
Welcome! You do not need to install anything for this track. Every piece of code here runs inside your browser, on a phone, a laptop or a school computer. When you see a code box with a Run button, press Run and the result appears below it. You can change the code and press Run again as many times as you like. Nothing can break.

Your first line of code

The print() instruction shows something on the screen. Whatever you put inside the round brackets gets printed. Text goes inside quote marks.
Python: say hellopython
print("Namaste, world!")
print("I am learning Python.")
Press Run. You should see two lines appear. That's it: you just wrote a program! 🎉 The computer ran the first line, then the second line, from top to bottom.

Python can do maths

Without quote marks, Python treats things as numbers and can calculate. Use + to add, - to subtract, * to multiply and / to divide.
Python: a calculatorpython
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
Look at the last line. With quote marks, "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

A variable is a name for a value, like a label on a box. You make one with the = sign. The name goes on the left, the value on the right. Then you can use the name instead of the value.
Python: a small shop billpython
samosas = 4
price_each = 15          # rupees, a made-up price
total = samosas * price_each
print("Samosas:", samosas)
print("Total in rupees:", total)
When print() gets several things separated by commas, it prints them all on one line with spaces between them.

Comments: notes for humans

Anything after a # 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. 1

    You type code

    Instructions, one per line.
  2. 2

    You press Run

    Python reads your code from the top.
  3. 3

    Python follows each line in order

    Line 1, then line 2, then line 3…
  4. 4

    You see the output

    Anything you printed appears below the code.

Small rules that matter

  • Spelling matters: print works, Print and prnt do 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.
Note
On a phone, the keyboard may turn straight quotes into curly “smart” quotes, or add a capital letter at the start of a line. Python needs plain straight quotes " and lower-case print. If something looks right but does not work, check these first.
Do this
Getting an error message is normal. Every programmer sees them many times a day. An error does not mean you are bad at this. It is Python telling you which line to look at. Lesson 7 teaches you to read them calmly.
Try this. Change the samosa program to 6 samosas. Press Run. Did the total change by itself? That is the power of variables.

Watch and read more

Lab

Your own greeting, and a tea-stall bill that Python works out for you.

~10 min

The problem

Press Run first to see what the starter prints. Then change it:

  1. Put your own name (or a nickname) inside the quotes for name.
  2. Make greeting include your name, for example "Namaste, " + name. The + sign joins two pieces of text.
  3. Make a variable total that works out the bill: cups times price_each. Let Python do the maths; do not type the answer yourself.
  4. 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.

Output

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.

Please sign in to continue.

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.

Sign in to take the test and save your progress