AI Hints
Home›Python Tutorial›Welcome to Python
Python · Lesson 2 of 49

Setting Up Python

Install, run, and write code like a pro.

⏱ 3 min read▶ Video at 3:30🎓 Free
Watch this lesson in the Python courseWatch on YouTube ↗Full Python course

Ava is excited, but her laptop does not know Python yet. So first, a little preparation. She needs to install Python. Then she needs a comfortable place to write code. And finally, she needs to know how to run a file. Ten minutes from now, her workshop will be ready.

Setting Up Python
From the video: Setting Up Python
  • Step one: install Python
  • Step two: choose a code editor
  • Step three: run your first file

Install Python

To install Python, visit python dot org, open the Downloads page, and choose the latest version of Python three. On Windows, tick the box that says Add Python to PATH, then click install. On a Mac, run the installer. On Linux, Python is usually already there. Never choose Python two. It is old, and no longer supported.

Windowstick 'Add Python to PATH'
macOSdownload and run the installer
Linuxusually installed already
Python 3 onlyPython 2 is retired

The REPL: a calculator for code

To check your installation, open a terminal, and ask for the Python version. Then start Python, and you enter the REPL, short for Read, Evaluate, Print, Loop. You type a line, and Python answers immediately. It is a calculator for code. It is perfect for quick experiments, like adding numbers, repeating text, or asking which version you have.

import sys
2 + 3
5
print("Hello from the REPL")
Hello from the REPL
"leaf " * 3
'leaf leaf leaf '
sys.version_info.major
3

REPL or script?

When should you use the REPL, and when a script? The REPL is for quick experiments, one line at a time, and what you type is forgotten when you close it. A script is a file, with as many lines as you like, that you can save, share, and run again. Real programs live in scripts.

The REPL

  • instant answers
  • one line at a time
  • forgotten when closed
  • great for experiments

A script (.py file)

  • saved on disk
  • as many lines as you need
  • run again any time
  • where real programs live

Choose a code editor

You write scripts in a code editor. Many beginners love Visual Studio Code. It is free, and has excellent Python support. PyCharm Community Edition is a powerful alternative. IDLE comes bundled with Python, and is perfect for the very first day. Any of them works. The best editor is the one you open every day.

Visual Studio Codefree, popular, great extensions
PyCharm Communitya full Python environment
IDLEcomes with Python
Any editor you likeconsistency beats features

Run your first script

Save this code in a file named welcome dot py. The line starting with a hash sign is a comment, a note for humans. Python ignores it. Then, in the terminal, type python three, followed by the file name. And the script runs, line by line.

welcome.pypython
# Leafy Corner: welcome script
print("Welcome to Leafy Corner!")
print("Open every day, 9 to 6")
Output
Welcome to Leafy Corner!
Open every day, 9 to 6

Comments and tidy habits

Comments are your friends. Use them to explain why you did something, not what the code says. You can also comment out a line, to stop it running, without deleting it. Python also cares about spaces at the start of a line, and we will meet that soon. For now, one instruction per line, and keep it tidy.

tidy.pypython
# Greeting for customers
print("Hello!")
# print("This line is switched off")
print("Goodbye!")
Output
Hello!
Goodbye!

Your own script file

Your turn. Create a file named leafy dot py. On the first line, write a comment. Then add two print lines, that welcome a customer, and name one plant that is on sale. Save it, and run it from the terminal. Ready? Here is one solution. A comment, then two prints. Easy to read, and easy to change.

✍️ Exercise: Your own script file

Write leafy.py: one comment, then two print lines (a welcome and a plant on sale).

Show hint

Comment first, then two print calls.

Show solution
leafy.pypython
# Leafy Corner sale banner
print("Welcome, plant lovers!")
print("On sale today: snake plants")
Output
Welcome, plant lovers!
On sale today: snake plants

Summary

Well done. Python is installed, and Ava has a place to write code. You met the REPL for quick experiments, and scripts for real programs. You know how to run a file from the terminal, and how to leave notes with comments. Next, Ava teaches the computer to remember things, with variables.

Key takeaways
  • Install Python three, never Python two
  • The REPL is for experiments, scripts are for programs
  • Comments start with a hash sign
Next up: Variables and Types — teach the computer to remember things