Taking a Programming Class This Fall? Get a Head Start by Learning These Python Basics
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 the Python downloads page and 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 assistance on Mac, you can follow this tutorial:
Linux
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 Debian-derived distros:
sudo apt-get install idle
For Fedora and other Red Hat-like distros that use dnf:
sudo dnf install idle
For other distro, refer to your distro manual.
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:
print("Hello, world!")
Then click “run” in the toolbar, and then click “run module” (alternatively, hit the F5 key):
You should see the results appear in the REPL:
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:
foo = "This is called the top-level."if True:
bar1 = "This is the second level."
bar2 = "This is on the same level as the previous statement."
if True:
baz = "This is the third level."
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 this if block is only executed if the condition evaluates to true. For example:
if 1 == 1: print("This executes because 1 is equal to 1.")
print("This will also execute because the entire if-block executes.")
else:
print("This will never execute because 1 always equals 1, and this is the 'falsy' block.")
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.
foo = 1print(foo)
Run this in IDLE. Change the value of “foo”.
Variables don’t just hold numbers; they can also store strings (text):
foo_string = "A string is a sequence of characters."
Floats, which are numbers with a decimal point:
foo_float = 3.14159
Booleans, which are true or false:
foo_bool = True
There are other complex data types, like tuples, sets, and lists. I’ve written a 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.
name = input("What is your name? ")print("Hello, " + name + "!")
The script will pause while it waits for input.
Basic Operators and Expressions
Earlier I mentioned that booleans (true or false) controlled if statements:
if True: pass
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:
if 1 == 1: print("One is equal to one.")
The “1 == 1” part of the statement is the expression; the “==” part is called the operator—specifically, the equality operator. There are a few common operators:
<: The less than operator.>: The greater than operator.
<=: The less than or equal to operator.
>=: The greater than or equal to operator.
!=: The not equal to operator.
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, if statements, blocks, expressions, operators, numbers, and strings:
name = input("What is your name? ")age = int(input("How old are you? "))
if age > 21:
print(name + ", you are older than 21.")
elif age < 21:
print(name + ", you are younger than 21.")
else:
print(name + ", you are 21!")
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 can go far with what you’ve learned. I would suggest that you don’t stop there, and the getting started page on the Python website offers 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 on why VS Code is so great and why everyone should use it.
link
