No history yet

Python Basics

Getting Started with Python

Before you can write any Python code, you need to have the Python interpreter installed on your computer. The interpreter is a program that reads your Python code and carries out its instructions. Think of it as a translator that converts your commands into actions the computer understands.

You can download the official installer from the Python website, python.org. The site automatically detects your operating system and suggests the best version to download. The installation process is straightforward. On Windows, it's a standard installer; just make sure to check the box that says "Add Python to PATH." This small step makes it much easier to run Python from your command line.

Lesson image

On macOS and most Linux distributions, Python often comes pre-installed. You can check by opening your terminal and typing python3 --version. If you get a version number back, you're all set. If not, the installer from the website will get you up and running.

The Interactive Shell

Once Python is installed, you can start using its interactive shell, also known as the REPL (Read-Eval-Print Loop). It's a great place to experiment with code one line at a time. To open it, just go to your terminal or command prompt and type python or python3 and press Enter.

You'll see a prompt, usually >>>, which means Python is ready for your command. You can use it like a calculator.

>>> 2 + 2
4
>>> 10 * 5
50

You type an expression, press Enter, and the shell immediately evaluates it and shows you the result. This instant feedback is perfect for testing small ideas without the overhead of creating a file.

Writing and Running Scripts

The interactive shell is great for quick tests, but for any program you want to save and run again, you'll need to write a script. A Python script is just a plain text file containing Python code, saved with a .py extension.

Let's create a simple script. Open any basic text editor (like Notepad on Windows or TextEdit on Mac) and type the following line:

# hello.py

print("Hello from a file!")

Save this file as hello.py. Make sure you save it as a plain text file, not a rich text file. Now, navigate to the folder where you saved the file in your terminal or command prompt. To run the script, you type python3 followed by the filename:

python3 hello.py

You should see the output Hello from a file! printed to your screen. The interpreter read the file, executed the code inside, and then exited. This is the fundamental workflow for running any Python program.

Basic Input and Output

So far, our programs have only sent information out. The print() function is Python's primary tool for outputting text, numbers, or other information to the console.

But what about getting information from the user? For that, we use the input() function. It prompts the user to type something and then returns their input as a string of text.

# user_greeting.py

# Ask the user for their name
name = input("What is your name? ")

# Print a personalized greeting
print("Hello, " + name + "!")

Save this code as user_greeting.py and run it. The program will pause after asking "What is your name? " and wait for you to type a response. After you press Enter, it will use your input to print a custom greeting.

The input() function always gives you back text (a string). If you need to treat the input as a number, you'll have to convert it first, but we'll get to that later.

Combining print() and input() allows you to create simple, interactive programs. You can now display information and ask the user for information in return. These two functions are the building blocks for communicating between your program and its user.

Quiz Questions 1/5

What is the primary role of the Python interpreter?

Quiz Questions 2/5

Which command would you use in the terminal to run a Python script named app.py?