Quick Links
You’re taking a new programming class this fall, and you don’t know anything about code. Python is taught widely in schools, and it’s one of the most loved programming languages because it’s simple. Spending 20 minutes learning Python has never hurt an aspiring programmer, so do yourself a favor.
Python is a scripting language with the goal of simplicity. A Python script is simply a list of English words and special characters that describe a sequence of steps to solve a problem.
Installing Python
To begin, you need to install the Python interpreter (program). It’s the Python interpreter that does the work by reading the script and executing its commands.
Windows and Mac
To install Python on Windows and Mac, head over to thePython downloads pageand download the installer. Upon installation, it will ask you if you want to install pip and IDLE; say yes to both. You won’t use pip here, but you will need it eventually.
Windows users must select the “add python.exe to PATH” option.
If you need further assistance, here is a useful installation tutorial for Windows:
For Linux, Python is almost certainly pre-installed on your distribution.
Setting Up Your Python Environment
We shall use IDLE for our code editor, because it’s easy to set up and use.
Install
The Python installer for Windows and Mac includes IDLE by default, so if you selected it during installation, no further action is necessary. However, on Linux, you’ll need to run one of the following commands to install it.
For Fedora and other Red Hat-like distros that use dnf:
Start IDLE
Now start IDLE. If you see the following screen:
This is called the REPL (Read-Eval-Print Loop); some call it the shell. The REPL is where we can enter one-off commands to test things. Nothing entered here is saved.
To create a new source code file, click “file” and select “new file”—see the following image:
You will then see a new blank window. This is where we write our code.
Writing and Running Your First Python Program: Hello, World!
Now that we have a new file opened, we can start writing some code. Each command that you write is called a statement. The most basic statement is print, which lets us print a message to the screen.
Write the following statement into the new file:
Then click “run” in the toolbar, and then click “run module” (alternatively, hit the F5 key):
Congratulations, this is your first program.
Understanding Indentation and Code Blocks
Python (like most programming languages) organizes its code into blocks. Indentation defines the boundaries for these blocks. For example:
For now, just focus on the indentation from the previous code. Think of indentation levels as similar to the hierarchy in a to-do list: you can group related tasks under a common heading and even nest additional groups, providing clear structure and organization.
Python uses indentation to group code together in blocks. For example, when you see an “if” statement—which is used to check a condition—the lines indented directly beneath it form what’s known as a code block (in this scenario, an if-block). The code within thisifblock is only executed if the condition evaluates to true. For example:
Try this out in IDLE and run it. Play around with the conditional statements. Remove some of the indentation and see what happens.
Variables and Data Types
In any programming language, variables operate like they do in mathematics: they store values.
Run this in IDLE. Change the value of “foo”.
Variables don’t just hold numbers; they can also store strings (text):
Floats, which are numbers with a decimal point:
Booleans, which are true or false:
There are other complex data types, like tuples, sets, and lists. I’ve writtena comprehensive tutorial on Python lists, which you may want to read later.
These are all commonly referred to as data types. For now, integers, floats, strings, and booleans are enough for you to learn.
Accepting User Input
Now that you know how to use the print statement, the next step is to accept user input.
The script will pause while it waits for input.
Basic Operators and Expressions
Earlier I mentioned that booleans (true or false) controlled if statements:
A static value like “True” is almost entirely useless here. To make it useful, we use expressions. An expression is any statement that evaluates to a value—for example, a boolean:
The “1 == 1” part of the statement is the expression; the “==” part is called theoperator—specifically, theequalityoperator. There are a few common operators:
Using these operators forms a boolean expression. Other operators form numerical expressions, symbols that you’re already familiar with: +, -, * (multiply), and / (divide).
Putting It Together
Let’s put everything together: print, input, variables, ifstatements, blocks, expressions, operators, numbers, and strings:
Try this out in IDLE. Change the code. Try to come up with your own ideas.
We’ve covered a lot of ground in mere minutes, and you may go far with what you’ve learned. I would suggest that you don’t stop there, and thegetting started page on the Python websiteoffers a set of great resources for beginners.
I suggested earlier that you use IDLE for your code editor, but that isn’t a long-term solution. People often use VS Code because it’s powerful, extensible, and supports many programming languages. We have articles onwhy VS Code is so greatandwhy everyone should use it.