Installing Python and Running Your First Program

Install Python, check the version, meet the REPL, write a script and understand the difference between interactive mode and script mode.

Getting Python onto the machine

Python is a single install with no separate runtime and development kit to choose between. Download the installer for your operating system from the official site, or use the package manager your system already provides.

One installer option matters more than the rest. On Windows the installer offers a checkbox that adds Python to the PATH. Tick it. Without it the command python is not recognised in a terminal and every later step becomes harder than it needs to be.

Checking the installation

python --version
python -c "print(1 + 1)"

The first command should print a version number beginning with 3. The second proves the interpreter can actually run code, not merely report that it exists.

python or python3

On Windows the command is normally python. On macOS and most Linux distributions the command is python3, because the bare name python was historically reserved for the old Python 2. If python --version reports a version starting with 2, or reports nothing at all, use python3 instead. Everywhere in these notes the command is written as python; substitute whichever name works on your machine.

The two ways to run Python

Interactive mode (REPL)Script mode
Start it withpythonpython program.py
You typeOne statement at a timeA whole file, prepared in advance
ResultsEchoed automaticallyOnly what you explicitly print
Survives after exitNoYes, it is a file
Use it forTrying an idea, checking a methodAnything you intend to run twice

The REPL

REPL stands for read, evaluate, print, loop. Typing python with no file name starts it. The >>> prompt means Python is waiting for a statement.

>>> 7 * 6
42
>>> name = "Meera"
>>> len(name)
5
>>> exit()

Notice that 7 * 6 printed 42 without any print call. The REPL displays the value of every expression you type. A script does not. This is the single most common surprise for a beginner moving from the REPL to a file: the same lines suddenly produce no output.

Two REPL conveniences worth knowing early:

  • The underscore _ holds the value of the last expression, so _ * 2 continues from the previous answer.
  • help(str.upper) and dir(str) read the documentation and list the available methods of anything, without leaving the prompt.

Your first script

Create a file named greet.py. The extension .py is what marks it as Python source.

name = input("What is your name? ")
print("Hello,", name)
print("Your name has", len(name), "characters.")
python greet.py
What is your name? Meera
Hello, Meera
Your name has 5 characters.

Reading that program

PartMeaning
input(...)Prints the prompt, waits for a line to be typed, and returns it as a string. Always a string, even if digits were typed.
name = ...Binds the name name to the returned string.
print(a, b)Prints its arguments separated by a single space, then a newline.
len(name)A built in function returning how many characters the string holds.

Running code in other ways

python greet.py              # run a script
python -c "print(2 ** 10)"   # run one statement and exit
python -i greet.py           # run the script, then drop into the REPL
python -m json.tool data.json  # run a standard library module as a program

The last form, -m, runs a module as if it were a script. It appears repeatedly later in this path, for example when running the test runner or the profiler.

Common mistakes

  • Naming a file after a standard library module. A file called random.py or json.py in your folder will be imported instead of the real module, producing errors that look impossible. Choose a different name.
  • Typing script lines into the REPL and expecting a file. The REPL forgets everything when it closes.
  • Expecting a value to print in a script. A bare expression on a line in a script computes a value and throws it away. Use print.
  • Assuming input returns a number. It returns text. int(input(...)) is needed for arithmetic, and it raises an error if the text is not a whole number.
  • Running python greet without the extension. The interpreter needs the file name, extension included.

Best practices

  • Keep one project in one folder, and run Python from inside that folder so relative file names behave predictably.
  • Use the REPL to explore and a file to build. Move anything you type twice into a file.
  • Give scripts lowercase names with underscores, such as invoice_report.py, and never spaces.
  • Prefer an editor that shows whitespace, because in Python whitespace is meaningful.

Practice

  1. In the REPL, work out how many seconds there are in a fortnight in a single expression.
  2. Write a script that asks for two numbers and prints their sum. Explain why it fails until you convert the input.
  3. Run the same three lines first in the REPL and then in a script. Describe exactly which outputs differ, and why.
  4. Create a file named math.py containing import math and run it. Explain the error in your own words.

Conclusion

The REPL is for questions and a script is for answers you want to keep. Both start the same interpreter; the only real difference is that the REPL prints every expression and a script prints only what you ask it to.

Written by Lorens Mishra

Default administrator account created by the installer.

Continue reading

All Python notes →
Python

Introduction to Python

Python is a high level, dynamically typed, interpreted language whose whole design goal is that a program should be as easy to read as it was to write...

Read more

Discussion

0 comments
Sign in to join the discussion.

No comments yet. Be the first to say something.