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.
- Basics
- Data Types
- Operators
- Strings
- Control Flow
- Lists
- Tuples
- Sets
- Dictionaries
- Comprehensions
- Functions
- Advanced Functions
- Recursion
- Exception Handling
- File Handling
- Modules
- Standard Library
- OOP
- Advanced OOP
- Iterators and Generators
- Decorators
- Context Managers
- Descriptors and Dataclasses
- Python Internals
- Concurrency
- Regular Expressions
- Serialization
- Command Line Python
- Testing and Debugging
- Type Hints
- Performance
- Python Security
- DSA with Python
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 with | python | python program.py |
| You type | One statement at a time | A whole file, prepared in advance |
| Results | Echoed automatically | Only what you explicitly print |
| Survives after exit | No | Yes, it is a file |
| Use it for | Trying an idea, checking a method | Anything 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_ * 2continues from the previous answer. help(str.upper)anddir(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.pyWhat is your name? Meera
Hello, Meera
Your name has 5 characters.Reading that program
| Part | Meaning |
|---|---|
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 programThe 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.pyorjson.pyin 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
inputreturns 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 greetwithout 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
- In the REPL, work out how many seconds there are in a fortnight in a single expression.
- Write a script that asks for two numbers and prints their sum. Explain why it fails until you convert the input.
- Run the same three lines first in the REPL and then in a script. Describe exactly which outputs differ, and why.
- Create a file named
math.pycontainingimport mathand 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.