Skip to content
phpguru.store
Lesson 1 of 12 3 min read Free preview

Your first Python program

What Python is, installing it on the family computer with a grown-up, talking to Python in the shell, and writing, saving and running your first program file.

What Python is

A program is a list of instructions a computer follows exactly. Python is a language for writing those instructions, and it's one of the most popular in the world: scientists use it, games use it, websites use it, and it's often the first "real" language people learn — because it reads almost like English.

In this course you'll write programs that do maths, ask questions, make decisions, play games and draw pictures. Every lesson is short: read a bit, type a program, then change it and see what happens.

Getting Python (with a grown-up)

This part needs a grown-up for about ten minutes.

  1. Go to python.org, open Downloads, and download the latest Python 3 for your computer (Windows or macOS). It's free.
  2. Run the installer. On Windows, tick the box "Add python.exe to PATH" at the bottom of the first screen, then click *Install Now*.
  3. When it finishes, find IDLE in your Start menu (Windows) or Applications → Python folder (Mac). IDLE is the program you'll use to write Python. It came with Python.

You don't need to sign up to anything, and nothing you write goes online.

Talking to Python

When IDLE opens, you'll see a window with >>>. This is the shell: type a line, press Enter, and Python answers straight away. Try these, one at a time:

print("Hello!")
2 + 3
10 * 7
print("I am learning Python")

print(...) shows something on the screen. The words in quotes are called a string — text. Numbers don't need quotes, and Python can do sums with them.

Now try a mistake on purpose:

>>> print("Hello)

Python shows an error, because the closing quote is missing. Errors are normal — every programmer sees them all day. Lesson 11 is all about reading them.

Your first program file

The shell forgets everything when you close it. To keep a program, write it in a file:

  1. In IDLE, choose File → New File. A blank window opens.
  2. Type this program:
print("Hi! I'm a Python program.")
print("I can do sums:")
print(12 * 12)
print("And I can repeat things:")
print("Hip hip hooray! " * 3)
  1. Choose File → Save, make a folder called python in your Documents, and save the file as hello.py.
  2. Press F5 (or Run → Run Module). The output appears in the shell window.

Look at the last line: a string times a number repeats the string. Python is full of little tricks like that.

The habit: change, save, run

Every lesson from now on works like this:

  1. Change something in your program.
  2. Save it (Ctrl+S, or Cmd+S on a Mac).
  3. Run it (F5).

The more you change and run, the faster you learn. You can't break the computer by writing Python — the worst that happens is an error message.

Try it

  • Change the messages to say something about you (just your first name — no surnames or addresses in your programs).
  • Print the answer to 365 * 24 — the number of hours in a year.
  • Make "Python! " repeat 10 times.
  • Remove one bracket on purpose, run it, read the error, and fix it.

That was lesson 1. The other 11 are one payment away.

Next up: Variables and asking questions. Buy once, read every lesson online with your progress saved, and keep the whole course as a Word file.

Get the course · ₹699$19