|From Get Programming by Ana Bell
It’s likely you’re reading this article because you’ve heard about the Python programming language. This article introduces Python, teaches you how to install it and set up your workspace, and how to write code in the IPython console.
Meet Python: a programming language
The Python programming language is, at the time of this writing, the most popular language for teaching introductory computer science. The language is used by top universities to expose students to programming and many students cite Python as a language they’re familiar with upon entering university. Broadly, Python is used to build applications and websites, and is used by companies like NASA, Google, Facebook, and Pinterest behind the scenes to maintain features and analyze collected data.
Python is a great general-purpose language that can be used to write quick and simple programs. Once you set up a working environment, writing a program in Python doesn’t require much setup.
What is Python?
Python is a programming language that was created by Guido van Rossum at Centrum Wiskunde & Informatica in the Netherlands. But the name Python is also used to refer to the interpreter.
Definition A Python interpreter is a program used to run programs that you write in the Python programming language.
In the Python programming language, every “thing”, called an object, has some characteristics (data) associated with it and some ways to interact with it. For example, any word is a “thing”, or object, in Python. The data associated with the word “summer” is the letter characters in that sequence and one way you can interact with it is to change every letter to upper case. An example of a more complicated object is a bicycle. The data associated with a bicycle could be the number of wheels, its height, its length, and its color. The actions that a bike can do might be to fall over, for a person to ride it, and you can re-paint it.
Downloading Python Version 3.5
You can download Python Version 3.5 several ways; you can get it from the official website of Python, https://www.python.org/, or through any third-party programs that offer the Python language, as well as extra packages pre-installed. In this article, I recommend that you download a specific third-party program called the Anaconda Python Distribution.
Anaconda Python Distribution
You can download the Anaconda Python Distribution from https://www.continuum.io/. It’s a free Python distribution that offers different versions of Python and includes over four hundred of the most popular packages for science, math, engineering, and data analysis. They also offer a lighter version, without any of the extra packages, called Miniconda.
Go to the downloads page, https://www.continuum.io/downloads, and choose the Python 3.5 download link for your appropriate operating system. Follow the installation instructions with the default values to install the distribution on your computer.
Integrated Development Environments
After the installation is complete, open the program part of Anaconda, called Spyder. Spyder is an Integrated Development Environment (IDE) that you’ll use to write your programs. In this article, we’ll use Spyder to write and run our programs.
Definition An Integrated Programming Environment (IDE) is a complete programming environment that helps make your program writing experience a lot nicer.
In Windows only, you can open Spyder from the Anaconda folder in the Start menu, shown in figure 1.
Figure 1 Anaconda folder in the Start menu.
Some of the following important features that the Spyder IDE offers, shown in Figure 2, are:
- an editor to write your Python programs
- a way to see lines of code, before running your program, that may contain potential errors or inefficiencies
- a console to interact with the user of your programs, through input and output
- a way to see values of variables in your program
- a way to step through your code line-by-line
Figure 2 shows the entire Spyder IDE and some code written in the code editor. You don’t need to understand the code.
Figure 2 (1) the Python console showing the version of Python and any output you program which is shown to the user; (2) the program editor window in which you can have multiple files open; (3) a space to indicate any potential lines that may contain errors before running the code; (4) a variable explorer that shows you values of objects in your programs; (5) the Debug menu that contains options to run your program step-by-step.
Setting up your Workspace
When you open Spyder, as shown in Figure 2, you see that the program window is separated into three different window panes.
- The left pane is the editor, originally containing no code, only a few lines of text that tells you when the file was created and your username. You’ll notice that this text is colored in green, meaning that this text is a comment – not code that will be run.
- The top right pane might contain the “object inspector,” “variable explorer,” or “file explorer.” We won’t use this window pane, but the variable explorer shows you what values each of the variables in your program has after the program finishes running.
- The bottom right pane is, by default, the IPython console. In this article, you’ll see some basics regarding the IPython console and the file editor.
The next two sections guide you through simple computations in Spyder. You’ll see how to enter the computations directly into the console and how to write more complicated programs in the code editor. At the end of the next two sections, your Spyder session should look like Figure 3.
Figure 3 A Spyder session after entering expressions in the IPython console and the code editor.
The IPython Console
The IPython console is the primary way to quickly test out commands to see what they do and, more importantly, it’s how users interact with your programs. The “I” in “IPython” stands for interactive and the IPython console is an advanced console that gives users neat features, like autocompletion, a history of previous command typed in, and color highlighting of special words.
writing commands directly into the console
You can write single commands directly into the IPython console to try things out and see what they do. If you’re beginning to program, you should try things out a lot – it’s the best way to get an intuition for what statements do and what expressions evaluate to.
3+2 in the console and hit enter to perform this addition. You’ll see the result
5 preceded by the text Out. Now type
4/5 to perform this division and you’ll see the result
0.8 preceded by the text Out.
You can think of this console as something that lets you “peek” into the values of the expressions that you type in. Why do I say “peek”? Because the results of these expressions aren’t visible to a user. To make them visible to a user, you must explicitly print their values to the console. Type
print(3+2) into the console. The number
5 is printed again, except that there’s no Out before it.
4/5 are called Python expressions. In general, anything in Python that can be evaluated to a value is called an expression. In the next section, you’ll see how to enter commands in the file editor to write more complicated programs.
Quick Check 3-1
Will the following expressions show output to the user or are they only letting you peek into their value? Type the expressions in the console to check yourself!
6 < 7
primary uses of the console
Few programmers can write a perfect program on the first go. Even experienced programmers make mistakes. Your first attempt to write a program will be a little unsteady and you’ll have bugs (errors) that show up when you try to run your program.
Definition A bug is an error in a program.
If a program has bugs, big or small, you’ll need to fix them, and you can learn a lot from the debugging process. As you start to write more complicated programs, you can think of using the console from the point of view of two roles: you as a programmer, and a person interacting with your program (the user). Figure 4 shows the role the console plays in these two roles. A programmer primarily uses the console to test out commands and to debug their programs. A user uses the console to interact with a running program by typing in input and seeing what the program outputs.
Figure 4. A programmer uses the console for their own testing and debugging. They type commands directly into the console and see the output. A user interacts with a program via the console. They type in input to a program and view output from a program in the console.
Most programs don’t have a visual interface. Instead, you’ll write programs that interact with users via text in the console; users are given the opportunity to enter text/numbers/symbols when prompted in the console and your program displays results in the console. Figure 5 shows an example of how the user may interact with the programs you write.
Figure 5. An example of a user interacting with a program. In (1) the program waits for the user to input their name. In (2) the program prints back out to the console a greeting message using the user input.
As a programmer, you’ll use the console to take on the role of a user of your program. This is most useful when debugging programs, i.e. when you’re trying to figure out why your program isn’t working as expected. When you use the file editor to write more complicated programs, it’s often useful to have the console print values of any computations or objects in your programs, not only the final value. Doing this can help you determine intermediary values in your programs and help you debug. If your running program is like trying out a recipe, then printing out intermediary values is like tasting items in your recipe to make sure everything is going well.
The console is useful to try out single expressions and see what their values are. Unfortunately, you must re-type expressions in the console if you want to run them again. A file editor saves your expressions to a file, allowing you to not need to re-type them. This saves a lot of time when you want to write programs that are more than one line long.
The File Editor
When you write more complicated Python programs containing more than a couple of lines, you should use the file editor window pane. Here, you can type the commands (in programming called statements), one on each line, like in Figure 3. After you finish writing a set of commands, you can run the program by clicking the green arrow in the toolbar at the top of Spyder, shown in Figure 6. Editing and running files is the same for all operating systems that Anaconda supports: PC; Mac; and Linux. This article shows screenshots from a Windows operating system.
Figure 6. Click the green arrow button to run the program.
not all lines of code produce output visible to the user
In the empty file, type
3+2 on line 8, as in Figure 2. On the next line, type
4/5. Don’t write anything else yet. Now click the green arrow to run the program. What happened? Your console at the bottom right showed some red text, like the following:
That line means that the program that you wrote ran, but nothing was shown to the user.
Now make the following additions. On line 10, type
print(3+2). On the following line type
print(4/5). Run the program again. Now what happened? You should see the same thing as in Figure 2. The console shows the results of the calculations to the user, each on a different line. How did that work? The Python interpreter executed each line in the file. It first ran the statement
3+2 and internally calculated the result of this expression. Then it internally calculated
4/5. Because these two statements didn’t tell Python to show the output of the calculations, their values don’t show up on the console.
4/5, in that order.
Quick Check 3-2
Which of these expressions will the user see on the console? Type the expressions in the file editor and click Run to check yourself!
You should save every program you write in a separate file to keep you organized. The file you wrote the previous code in is a temporary file, saved in some location in the Anaconda installation folder. Open a new file from the Spyder menu bar, as shown in Figure 7. Type the previous two print statements again in the new file.
TIP I strongly encourage you to type the commands out again instead of copy and pasting. Repetition is a great way to help you get the hang of programming. Forcing yourself, particularly at the beginning of your programming career, to type commands out explicitly helps speed up the learning process and makes writing code second-nature more quickly.
Now save the file in a directory of your choosing. You must save it with a .py extension. If you don’t save it with a .py extension, you won’t be able to run the program (the green run button will be grey). After you save the file, click the green run button. The same output as before should show up in the console.
Figure 7. Multiple files open in Spyder, each one its own tab in the file editor window pane.
If you close the saved file, your program isn’t lost. You can re-open the file from the File menu. All the code is still there and you can run the program as if you wrote it up from scratch.
That’s all for this article.