No history yet

Python Basics

Setting Up Your Workspace

Before you can write Python code, you need two things: the Python interpreter itself and a place to write your code. The interpreter is a program that understands and runs your Python instructions. A text editor or an Integrated Development Environment (IDE) is where you'll type those instructions.

First, let's get Python. You can download the latest version from the official source.

Download Python from python.org

Lesson image

During installation on Windows, make sure to check the box that says "Add Python to PATH." This small step makes it much easier to run your programs from the command line.

Once Python is installed, you need a code editor. While you could use a simple text editor like Notepad, a dedicated code editor or an IDE offers helpful features like syntax highlighting (coloring your code to make it readable) and debugging tools. Great options for beginners include Visual Studio Code (with the Python extension), Thonny, or PyCharm Community Edition.

Your First Program

It's a long-standing tradition for a programmer's first program to simply display "Hello, World!" on the screen. It’s a simple way to confirm that your setup is working correctly. In Python, this is incredibly straightforward.

# This is a comment. Python ignores lines that start with a #
print("Hello, World!")

Let's break that down. print() is a function, which is a reusable block of code that performs a specific action. In this case, it prints whatever you put inside the parentheses to the screen. The text inside the quotes, "Hello, World!", is called a string. It's just a sequence of characters.

Interacting with Users

Programs are much more interesting when they're interactive. You can get input from a user with the input() function. This function pauses the program and waits for the user to type something and press Enter.

# The text inside the parentheses is a prompt shown to the user
input("What is your name? ")

On its own, that isn't very useful. The program gets the user's name but immediately forgets it. To store and reuse this information, we need to use a variable. A variable is like a labeled box where you can store a piece of data. You create one by giving it a name, followed by an equals sign, and then the data you want to store.

# Get the user's name and store it in a variable called 'name'
name = input("What is your name? ")

# Now we can use the 'name' variable to print a greeting
print("Hello, " + name)

In this example, whatever the user types is stored in the name variable. Then, the print() function combines the string "Hello, " with the value stored in name to create a personalized greeting.

Saving and Running Scripts

Typing code directly into a Python interpreter is great for quick tests, but for anything more complex, you'll want to save your code in a file. A file containing Python code is called a script. By convention, these files end with a .py extension, like my_first_script.py.

Once you've saved your code in a .py file, you can run it from your computer's terminal or command prompt. Navigate to the directory where you saved the file and type:

python your_script_name.py

If you're using an IDE, it will usually have a "Run" button (often a green triangle) that handles this for you.

Lesson image

You've just taken your first steps into programming with Python. You know how to set up your environment, display text, get input from a user, and run a script. These are the fundamental building blocks for everything that comes next.