Welcome to Jamming with Python! If this is your first programming language, congratulations, you've made a good choice for a first one to learn. For those who are very new to programming, we use languages like Python to write computer programs, which tell a computer what you want it to do.
Python is a language first released in 1991 by Guido van Rossum. It's unusual for most programming languages, in that how many spaces you use to indent lines of Python code changes the meaning of what you write on those lines. As a result, it's one of the easier programming languages to read. As an interpreted language with multiple options for compilation, it's also very portable, and very powerful.
With that said, let's get started!
First, we need to install Python on your computer. You might already have a version of Python installed, but we need to make sure you have Python 3. If you don't know if you have it, you probably will be better off downloading the newest version, which should be here:
https://www.python.org/downloads/
If you are using Windows, detailed instructions are found here: https://docs.python.org/3/using/windows.html
If you are using macOS, further instructions are found here: https://docs.python.org/3/using/mac.html
If you are on Linux, BSD, or another Unix like, detailed instructions are found here: https://docs.python.org/3/using/unix.html
Those familiar with Python might wonder about our choice of Python 3 over Python 2, while those who aren't familiar with Python might wonder why there are two competing versions like this. The long story short is, Python 3 fixed a lot of weird issues with Python 2's structure, but a lot of older packages hadn't updated to Python 3 in the first few years after release. For now, and the future, focus on Python 3; everything we're going to use in these lessons, from basic packages to game engines, runs on Python 3.
Once you've installed Python, test that you have everything set up by opening a terminal window, command prompt, or equivalent for your platform. When in there, type
which python3
or instead if on Windows:
where python3
if you have installed python correctly, this should give a path as output, telling you where the python3 binary is installed. If it's not installed, this will either give no output at all, or give you an error. For example, mine gives this response on Mac if I try to run without installing python3 first:
Inari-III:~ yaodema$ which python3
Inari-III:~ yaodema$
Since there's no output between these terminal prompts, you can tell python3 is not yet installed. After installation, the result is this:
Inari-III:~ yaodema$ which python3
/usr/local/bin/python3
Inari-III:~ yaodema$
Once you've successfully installed Python 3, it's time to move on to:
The typical way to work with Python is through the command line. Python script files can be run directly, which will use the command line program; we'll get to how those work in a bit. Otherwise, you can do tests of Python by entering the Read, Evaluate, and Print Loop, REPL for short. In a terminal or command prompt window, you can enter python3, by itself, to enter the Python REPL.
Inari-III:~ yaodema$ python3
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
In this view, any lines of Python code you enter at the prompt will be executed. If the line would have a value, you can see the resulting value on the next line.
>>> 2
2
>>> 2 + 3
5
>>> 2 - 4
-2
Above, the number 2, followed by the results of simple addition and subtraction operations, are shown. The resulting value is given immediately. This can be useful for learning the language, or for testing out ideas that only take a small number of lines. You can technically pass entire programs to the REPL, but typing them by hand is not recommended. We have script files for that.
When you're done using the REPL, type exit() (or on most OSes, you can also pass the "end of stream" signal to the program by holding Ctrl and pressing D. (This shortcut, Ctrl-D, and any further shortcuts we'll cover later, will be typed in the modifier-key-dash-other-key shorthand from now on.) If you mistype by typing just exit instead of exit(), the REPL gives you some basic help so you can quit properly.
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> exit()
Another way to access the REPL is in your programs under Windows, or in your Applications folder on Mac. There, you should have a Python directory. In this is IDLE, the window-based Python REPL and editing system. You aren't required to use IDLE to write programs, and can use any text editor you happen to like (I've been fond of TextWrangler on macOS, but I've also used SublimeText in the past. Some swear by it.)
The advantage to IDLE is that you can test run the file right in IDLE, and it has some debugging tools that can be helpful when you make mistakes. Note, that is not an "if"; every programmer makes mistakes, and you will have bugs you need to track down. If you have a bug you can't track down with other tools, try using IDLE to help you out.
To run a file in IDLE, have the file open and saved, then press F5. You can also select Run Module from the Run menu. If not in IDLE, you can double click on the file from Windows. On MacOS you may need to right-click (or ctrl-click) and select the Launcher from the Open With menu; this is after setting up the Launcher correctly, as we'll get to soon.
In other OSes (the Unix-likes), you may need to set the file to be executable in order to simply run it. To do this, you just type
chmod +x filename.py
with filename.py replaced with whatever the name of your script file is. Please note that this is case sensitive, so make sure any capital letters in the file name match!
All script files should start with the following line:
#!/usr/bin/env python3
This line tells the OS to check what "python3" is if a user types it in the terminal, then sends the rest of the file to that program if it exists. The rest of the program that follows after that can be whatever code you want to run.
Finally, on some platforms you may need to open the Python Launcher and set it to python3. MacOS requires this. You want to set the Preferences on the Launcher so that the interpreter it uses is /usr/local/bin/python3 instead of the default, so it doesn't try to use python 2.7. Then you can run your Python programs using the launcher as well, if you want to.
It's traditional, when learning a new language, to write your first program to simply say "Hello World!" on the terminal, and exit. For Python, this is a very simple program! Simply type (or copy) the following into your editor, save the file as Hello.py, and run in IDLE or on the command line.
#!/usr/bin/env python3
print("Hello World!")
If you want to run it on the command line and you are not on Windows, make sure to go to that folder in your terminal window with cd, then type
chmod +x Hello.py
and you should be able to run it with
./Hello.py
When run it should look like this:
Inari-III:Documents yaodema$ ./Hello.py
Hello World!
Inari-III:Documents yaodema$
This program is easy to explain, because it's only one line, but there are a few different parts we need to talk about.
The first line, the "hash-bang" line, starts with #! to tell the OS which program to read the script with. That's all it does.
The second line does all the work. First, there's the "print" function. A function is a piece of code that can be "called" by sending it some input, which are called "arguments." A function call is made by writing the function's name, followed by its arguments inside of round brackets (). Some functions may take one argument, or no arguments. This print function call is being given only one argument, that being our greeting message.
The print function can be used in a more complicated way, but for our purposes, it takes whatever it's given as a first argument, and prints it to the command line. Thus, it is an "output" function, since it prints something out to the user. This print function is taking our greeting message, which is a string.
A string is a way of representing written text in a program. Strings can also contain more than written text, but that's what ours is doing. "Hello World!" is our string, and the writing inside the double quotation marks is the actual contents of the string. You might be wondering how you would write a string that contains a double quote; we'll get to that in the next lesson.
For now, if you've completed installation, written your first program, and tested it, it's time to feel good about it! This is your first step in learning to program!
Once you've read through Lesson 1 above, complete the installation of Python 3 and your Hello.py test program.
After doing that, take your Hello.py file and edit it so, instead of saying "Hello World!" it says "Hello, my name is" followed by your name. When you know this works, this exercise is complete!