A Tutorial Introduction to Python 3
The only way to learn to program a computer is by writing programs. Rather than beginning with the theory behind everything, we’ll jump right in and write our first program.
Along the way, we’ll pause to understand the tools we’re using. But for now, buckle up, because we’ll soon write our first program!
Getting Python 3
You can grab a free copy of Python from Python.org. Make sure you download version 3, as we’ll use the Python 3 interpreter throughout this book.
The older version 2.7 of Python is still available, but most of the code we write won’t work with that version, so, again, make sure you’ve downloaded and installed the right version.
After you’ve downloaded the Python interpreter, you’ll need to open and run the program itself.
On Windows platforms, this is easy enough. The Python installer adds a Python interpreter shortcut to your Start menu. We’ll want to launch the one labeled “IDLE.”
If you’re not running Windows, you won’t see an icon to run after Python has been installed. This is because the Python interpreter is usually meant to be run from a command line. Windows doesn’t really have a full command line just yet, which is why it uses a built-in launcher for Python. For platforms other than Windows, we’ll need to use the terminal.
The Terminal: A Program to Control Programs
Have you ever wondered how server rooms work? There are hundreds of computers in a server room, all put together in racks, with network cables running out of them. Not one of these servers has a monitor or a keyboard attached to it. So how is it that they’re able to serve up things like Facebook, YouTube, and Snapchat?
These computers are controlled via terminal programs.
The terminal provides a way of running, controlling, and manipulating programs through text only. Instead of using a mouse to control the programs being run, the terminal allows us to send keyboard commands to tell the computer what to run. The programs run in terminals themselves then spit things back out in text.
The Python interpreter is usually run from a terminal. When we’re using a terminal locally (as opposed to remotely, on a server connected to the Internet), we call it a command line.
Although entering all your computer’s commands through the command line may seem intimidating at first, learning to use the command line well will turn you into a master of computation.
Let’s get started with the command line by opening a terminal.
On MacOS, press Command+Space at the same time, and then type in Terminal. This will launch a terminal for you. On Linux, you should have a terminal icon by default.
Once the terminal pops up, you’ll be given a prompt. Here you can just type python3 and be greeted by the Python shell. Now we can begin. If you’re running Windows, we’ve just gotten to the point where we can see the shell that you got by opening the IDLE Python program from its icon in the Start menu.
Writing Our First Program
With our Python shell running, we can now begin by typing our first Python program directly into the shell. Each command we type runs immediately. Start by writing the following lines, each followed by a return:
If you’ve done the above correctly, a window pops up. In it you’ll see a tiny arrow at the center, and in the upper left corner of the window you’ll see “Python Turtle Graphics.”
Next, we can move our arrow (or turtle) forward, and draw a square. Enter the following commands sequentially, into the Python interpreter:
Once entered, we should now see our square in our Python Turtle Graphics window. Hurray!
Now that you’ve written something that works, look through the preceding code. Can you make any judgements about it? How would you draw a triangle? Do you remember the rules for a triangle? Three sides, and the sum of the angles must equal 360 to make a full loop. Try changing the preceding values to create three 120-degree turns instead of four 90-degree turns, and see if you can draw an equilateral triangle.
How the Development Cycle Works
Now that we’ve written a program and played around a bit, what is happening?
When we started by running the Python interpreter, we gave ourselves an environment in which to start typing out Python code and to see it execute right away. When you’re trying to solve a problem in Python, it can be helpful to try it out in a shell. Here, we’ve used the shell to draw using a library called “turtle.”
Libraries give Python its power. There are Python libraries for doing nearly everything, from developing artificial intelligence, creating images, or analyzing DNA, to controlling robots.
Normally, our Python programs are written into text files, from an authoring program called an editor. Editors help us write code by letting us know whether we’ve made a mistake when typing them out and managing our files if we have many of them in our program. The Python interpreter normally loads a set of instructions just like what we’ve written above from a file. In fact, we can type the above code into a file, and then send it to Python to be run.
The way we tell Python to run the file is by passing its name. It looks something like this:
Let’s download an editor and run a Python program like that now.
The Editor: A Place for Writing Programs
Of all things programmers take personally, their choice of an editor in which to write their programs is probably the most important. Each person will have their own reasons for using their editor, and depending on their personality, they may feel very strongly about their choice.
For this book, we’ll use a newer editor called Visual Studio Code. It’s a free editor developed by Microsoft that comes with some nice Python editing features. Visual Studio Code is available for Windows, macOS, and Linux, so users of each platform will have mostly the same experience writing inside it.
You can download a copy from https://code.visualstudio.com/. Download and install it, and then the first thing we’ll do is add the great Python extensions (libraries) we talked about earlier.
Once you’ve downloaded the Visual Studio Code editor, press Ctrl+e to open the program, and type out ext install python. This will let us write Python code within the editor and see color highlighting and utilize autocomplete for all the libraries we import.
Next, let’s create our first Python file. Create a new file from the menu, and then click Save. You’ll need to name your file with a “.py” extension. So, your filename could be “first.py” or “firstProgram.py”. The “.py” lets the editor know you’re writing a Python program and that it should be checking the code you’re writing.
Once this is done, Visual Studio Code might bring up a dialog about Pylint not being installed. Click to install it, because this is the Python library used to check our Python code as it’s being written.
Finally, we have a place to type out our program. Type out the code from earlier, either your square or your triangle. Then, press Ctrl+` (that’s the backtick command), or click View > Integrated Terminal from the Visual Studio Code menu to open the terminal.
Your editor should now look like this:
From here, we can run our first saved Python program from the built-in terminal in Visual Studio Code. Type out
python yourfilename.py, and you should see your program run.
But wait: Is it just drawing and then immediately disappearing?
The Python interpreter doesn’t wait for new commands when it’s sent a Python program file. Instead, it reads until the end and then exits. To see what we’ve drawn, we’ll need to make sure the Python interpreter waits.
Luckily, the turtle library has anticipated this need and has added a helper for us. We just need to add a single line at the end of our code,
turtle.done(), and we’ll have a program we can view and close by clicking the Close button on our window.
Congratulations! If you’ve made it this far, you’ve written your first program, which technically makes you a programmer. Now that you can draw a square, and hopefully a triangle too, you’re ready to take the next step and learn about the tools for building more complicated programs.
We’ll stick with the
turtle library for now, and use it to see concepts visually.