We’ve been using IDLE to edit code, and that’s fine to get started. But it’s a little bit underpowered for getting to the next level when it comes to programming.
In this section, we’ll look at some tools that professional programmers use to get the job done.
Back in the day, people accessed mainframes through dedicated pieces of hardware called terminals. These looked like old TV sets with keyboards attached. Screens were typically monochrome, either white, green, or amber colored, with pixely text.
Barely anyone has a real terminal anymore, but they have programs called terminal emulators that pretend to be terminals. In fact, real terminals are so rare, and terminal emulators are so common, if you hear someone say “terminal”, they’re certainly talking about a terminal emulator.
So a terminal emulator is a program you run that gives you a text window to interact with your computer.
You know how you point, click, and drag with the mouse to command your computer to do things? We’re going to do the same things, except we’re going type commands in to get them done, instead.
Sometimes you’ll hear people talk about the “shell” and “terminal” interchangeably.
Technically, though, the terminal is what you launch, and then the terminal immediately launches another program, the shell, that you interact with.
The terminal is pretty dumb on its own. All it does it handle input from the keyboard and output to its “screen”. It’s the middleperson between you and the shell. The shell is the real brains of the outfit.
The shell is a program that takes your typed-in commands and executes them, and tells you if something went wrong.
The most popular shell program on Unix-likes and Macs is the Bourne Again Shell (Bash)172, although the Z-shell (Zsh) is growing in popularity. Bash is known by a $
prompt (sometimes prefixed with other information about which directory you’re in, or something similar). Zsh uses a %
prompt.
There are multitudes of shells, but we’ll just assuming you’re going to use Bash or Zsh (with a hat-tip to Windows’s built-in shells), and they’re compatible enough for our purposes.
For Windows, there are plenty of options, some of which you have installed already.
Unless you’re going with one of the bash options, you should use PowerShell because it’s newer, better, and maintained.
Almost all of the bash commands we use in this guide also work in PowerShell and CMD.
Hitting the Windows key and running cmd
will bring up the CMD prompt. (Type exit
to get out.)
Hitting the Windows key and running PowerShell
will bring up the PowerShell prompt. (Type exit
to get out.)
Git174 is a source code control system. And it’s great. Tons of people use it. You should install it.
When you install it, it installs a bash shell called gitbash that you can use.
The Windows Subsystem for Linux is an awesome piece of kit. It unobtrusively puts a little Linux install on your Windows machine. And then you can use the Linux command line.
To install Follow the WSL setup instructions175
There’s a recommendation in there to also install Windows Terminal, an alternate terminal to the existing ones. It’s a good choice.
After installing, update the system:
sudo apt update sudo apt -y upgrade
and Python should be there; running this should get you the version number:
python3 --version
You can open a File Explorer window with:
iexplore.exe .
(Yes, that’s a period after a space at the end.)
Macs come with a terminal built-in. Run the Terminal
app and you’ll be presented with a bash shell prompt.
All Unix-likes come with a variety of terminals and shells. Google for your distribution.
There are a lot of IDEs out there, but a good free one is VS Code.
Visit the Visual Studio Code176 website for downloads.
Let’s get it installed!
When you install:
If you’re using WSL, first run VS Code from outside WSL, and install the “Remote WSL” extension. Then you should be able to run it from inside WSL.
Just install it. No special instructions.
Linux and Unix-like users can use their package manager to install VS Code. Google for ubuntu vscode install
, substituting the name of your distro for ubuntu
.
If you already have a code editor you prefer using (Vim, Emacs, Sublime, Atom, PyCharm, etc.) feel free to use that, no problem!
In the shell of your choice, if all has gone well, you should be able to type:
code
and have it launch VS Code.
Once launched, click the icon on the left bar of VS Code to manage extensions.
Not all development environments are integrated. Some programmers use standalone editors and debuggers to get the work done.
A typical cycle is:
We’re going to do this old-school. Programmers have been using the command line for over 10,000 years, and it has staying power for a reason.
Launch a terminal and bring up a shell. You can use another shell if you want, but I’ll be talking bash/zsh here.
At the prompt, type the following commands, one per line:
cd
mkdir bgpython
cd bgpython ls -la
These commands do four amazing things:
cd
means change directory. (A directory is the same as a folder.) On a line by itself, it means “change to my home directory”.
mkdir
means make directory. We’re creating a new folder called bgpython
.
cd bgpython
to change into that directory.
ls -la
to get a long directory listing (i.e. all the files in that folder.)
At this point you should see something like this:
$ ls -la
total 0
drwxr-xr-x 2 beej staff 64 Nov 18 23:14 . drwxr-xr-x+ 123 beej staff 3936 Nov 18 23:14 ..
This is showing you all the files you have. Namely, there are two of them: .
and ..
. These mean “this directory” and “parent directory”, respectively. (You know how folders can be inside other folders? The outer folder is called the “parent” folder, which is what the parent directory is. If you want to get back to your home directory from here, you can type cd ..
.)
You should think of the shell as “being” in a certain directory at any particular time. You can
cd
into directories, orcd ..
back into the parent, orcd
to get to your home directory from anywhere. It’s like the folder you have open that has focus in a GUI.
(The remaining information on each line tells you the permissions on the file, who owns it, how big it is, when it was modified, and so on. We can worry about that later.)
Other than those there are no other files. We’ll soon fix that! Let’s add a Python program and run it!
Usually launching an editor to edit a file is as simple as typing the editor name directly followed by the filename on the command line.
For example, to launch VS Code to edit the file hello.py
:
code hello.py
But wait–isn’t VS Code a full-fledged IDE? Yes, it is. Another popular editor is Vim:
vim hello.py
But in any case, you’re in the editor and ready to type code.
This is your canvas! This is where the magic happens!
If you get in Vim and have no idea how to get out, hit the
ESC
key and then type:q!
and hitRETURN
—this will exit without saving. If you want to save and exit, hitESC
then typeZZ
in caps.Vim is a complex editor that is hard to learn. But after you learn it, I maintain it’s the fastest editor on the planet. I’m using it to type this very sentence right now.
To learn it, I recommend OpenVim’s interactive Vim tutorial and this reference of Vim commands from beginner to expert.
Type the following177 into your editor (the line numbers, below, are for reference only and shouldn’t be typed in):
Pull down File
→Save
to save the file.
Pop back into your terminal window and type ls -la
to get a directory listing:
$ ls -la
total 8
drwxr-xr-x 3 beej staff 96 Nov 18 23:27 .
drwxr-xr-x+ 123 beej staff 3936 Nov 18 23:14 .. -rw-r--r-- 1 beej staff 87 Nov 18 23:27 hello.py
There it is! hello.py
clocking in at 87 bytes (characters, roughly) in size.
Let’s run this puppy. Remember how the program is just a recipe for doing a thing—what do you think our hello.py
program does? Guess!
Then type this to run it (if python
doesn’t work, try python3
or py
depending on your system):
python hello.py
and hit RETURN
! [Angelic Chorus!]
$ python hello.py
Hello, world! My name's Beej and this is (possibly) my first program!
You just wrote some instructions and the computer carried it out!
Next up: write a Quake III clone!
Okay, so maybe there might be a small number of in between things that I skimmed over, but, as Obi-Wan Kenobi once said, “You’ve taken your first step into a larger world.”
Remember to use the four problem-solving steps to solve these problems: understand the problem, devise a plan, carry it out, look back to see what you could have done better.
dijkstra.py
that prints out your three favorite Edsger Dijkstra quotes178.https://docs.python.org/3/↩︎
http://www.catb.org/~esr/faqs/smart-questions.html↩︎
https://www.youtube.com/watch?v=ry55–J4_VQ↩︎
https://www.python.org/downloads/↩︎
https://www.python.org/downloads/↩︎
https://beej.us/guide/bgpython/source/examples/hello.py↩︎
https://en.wikiquote.org/wiki/Edsger_W._Dijkstra↩︎
https://beej.us/guide/bgpython/source/examples/vartest.py↩︎
More generally speaking, variables refer to objects, but since all we have for now is numeric values, let’s just go with values.↩︎
Later we’ll learn that other types of buckets can hold more than one thing.↩︎
https://en.wikipedia.org/wiki/Form_1040↩︎
Integer division truncates the part of the number after the decimal point.↩︎
https://en.wikipedia.org/wiki/Order_of_operations↩︎
We call shorthand like this syntactic sugar because it makes things that much sweeter for the developers.↩︎
input()
is what we call a built-in in Python. It comes with the language and we get to make use of it. Later we’ll learn to write our own functions from scratch!↩︎
https://beej.us/guide/bgpython/source/examples/inputtest.py↩︎
This is the way most computers represent numbers with a decimal point in them, such as \(3.14159265358979\). When you see “floating point” or “float”, think “number with a decimal point in it” as opposed to “integer”.↩︎
https://beej.us/guide/bgpython/source/examples/twosum.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_twosumfloat.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_twosumdiff.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_threesumdiff.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_xsquared.py↩︎
https://en.wikipedia.org/wiki/Quadratic_formula↩︎
https://beej.us/guide/bgpython/source/examples/ex_quadratic.py↩︎
https://en.wikipedia.org/wiki/Scientific_notation↩︎
https://beej.us/guide/bgpython/source/examples/ex_quadratic2.py↩︎
https://en.wikipedia.org/wiki/George_Boole↩︎
Ooooo! 1
s and 0
s! Binary! For just a moment, here, we’re getting a glimpse of the deep workings of the machine.↩︎
https://beej.us/guide/bgpython/source/examples/booltest.py↩︎
https://beej.us/guide/bgpython/source/examples/ifelse1.py↩︎
https://beej.us/guide/bgpython/source/examples/while.py↩︎
We could have also tested i <= 29
.↩︎
Have two loops!↩︎
https://beej.us/guide/bgpython/source/examples/hashast.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_10ksum.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_xfourth.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_ntimes10.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_oddsbetween.py↩︎
A number x
is divisible by 3
if x % 3 == 0
.↩︎
This is a famous interview problem for junior devs.↩︎
https://beej.us/guide/bgpython/source/examples/ex_fizzbuzz.py↩︎
https://en.wikipedia.org/wiki/Turing_completeness↩︎
We’ll talk about regular expressions, or regexes, later.↩︎
More on lists in upcoming chapters.↩︎
Spaces, tabs, and newlines.↩︎
https://docs.python.org/3/library/stdtypes.html#string-methods↩︎
Also called squirrely braces.↩︎
https://beej.us/guide/bgpython/source/examples/multtable.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_fstring.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_goatcount.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_uppervowel.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_charat.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_sliceat.py↩︎
Technically, lists and dictionaries are objects, so we’re being a bit redundant.↩︎
Technically we can use it to iterate over anything that’s iterable, which is quite a number of things.↩︎
Idiomatic means “the standard, accepted way of doing a thing in a language”.↩︎
https://beej.us/guide/bgpython/source/examples/listdouble.py↩︎
Remember that a method is a function that you call on a particular object with the dot (.
) operator.↩︎
https://beej.us/guide/bgpython/source/examples/listops.py↩︎
https://beej.us/guide/bgpython/source/examples/fiblist.py↩︎
We’re using a technique here generally called bottom-up dynamic programming. But that’s a story for another time. Probably involving Fibonacci again.↩︎
Names can be made up of letters, digits, and underscores, as long as they don’t start with a digit.↩︎
https://en.wikipedia.org/wiki/Roguelike↩︎
Well, not at this point in our learning, anyway.↩︎
https://en.wikipedia.org/wiki/ANSI_escape_code↩︎
https://www.youtube.com/watch?v=q3fx6TugN7g↩︎
https://docs.python.org/3/howto/curses.html↩︎
https://beej.us/guide/bgpython/source/examples/adv1.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_refval.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_listsum.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_listchange.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_replist.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_listcompx5.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_listcompcap.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_listmult.py↩︎
This is all about family trees. Did you know I’m related to Queen Elizabeth II (by marriage)? I’m her mother’s sister’s husband’s father’s father’s sister’s daughter’s husband’s wife’s (drama!) sister’s husband’s father’s brother’s son’s son’s daughter’s son’s daughter’s son. For realsies! I’m willing to bet that you’re related to Queen Elizabeth II, as well. That makes us cousins!↩︎
Except via Queen Elizabeth II, like the rest of us.↩︎
https://en.wikipedia.org/wiki/JSON↩︎
Time complexity enthusiasts will recognize this as \(O(1)\), or constant time.↩︎
If you haven’t seen it before or need a refresher, this is a value that represents “no value”. It’s a placeholder (what we call a sentinel value) to indicate a no-value condition.↩︎
Or what we call lexicographically sorted. It’s like alphabetical, but on steroids so that it can handle letters, numbers, punctuation and so on, all of which are all numbers deep down.↩︎
That’s Noughts and Crosses, to some of you.↩︎
It’s not unusual.↩︎
https://en.wikipedia.org/wiki/Universally_unique_identifier↩︎
https://beej.us/guide/bgpython/source/examples/familytree.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_printkeys.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_printkeysvals.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_list2dict.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_list2dictsort.py↩︎
https://en.wikipedia.org/wiki/Pythagorean_theorem#Euclidean_distance↩︎
Purists will point out exceptions to this, like with __add__()
, but let’s skip that for now.↩︎
https://en.wikipedia.org/wiki/Don%27t_repeat_yourself↩︎
https://docs.python.org/3/library/functions.html↩︎
https://www.defcon.org/html/defcon-2/defcon-2.html↩︎
This happens all the time in development.↩︎
https://en.wikipedia.org/wiki/Evaluation_strategy↩︎
https://en.wikipedia.org/wiki/C_(programming_language)↩︎
https://en.wikipedia.org/wiki/Fortran↩︎
https://en.wikipedia.org/wiki/C%2B%2B↩︎
https://beej.us/guide/bgpython/source/examples/shipdist.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_max.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_ennum.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_grav.py↩︎
This technique is called parallel arrays or, in the case of Python, parallel lists. It’s not a popular technique today, having been made obsolete by the very thing we’re talking about in this chapter.↩︎
https://en.wikipedia.org/wiki/Camel_case↩︎
In fact, I think the choice to use all those underscores in __init__()
is one of the few bad design choices in the language.↩︎
A lot of other languages use the variable name this
instead of self
.↩︎
Technically, even the methods are attributes, but we’ll get into the pedantic details another time.↩︎
https://beej.us/guide/bgpython/source/examples/moviesign.py↩︎
https://beej.us/guide/bgpython/source/examples/moviesign2.py↩︎
https://docs.python.org/3/library/datetime.html↩︎
https://beej.us/guide/bgpython/source/examples/ex_car.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_subway.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_adv2.py↩︎
https://beej.us/guide/bgpython/source/examples/example.zip↩︎
https://docs.python.org/3/library/index.html↩︎
https://pypi.org/↩︎
https://docs.python.org/3/library/sys.html↩︎
https://docs.python.org/3/library/calendar.html↩︎
https://docs.python.org/3/library/index.html↩︎
https://en.wikipedia.org/wiki/Comma-separated_values↩︎
https://docs.python.org/3/library/zipfile.html↩︎
https://en.wikipedia.org/wiki/Cyclic_redundancy_check↩︎
https://beej.us/guide/bgpython/source/examples/zipdir.py↩︎
https://beej.us/guide/bgpython/source/examples/example.zip↩︎
https://docs.python.org/3/library/os.html↩︎
https://beej.us/guide/bgpython/source/examples/ex_printpid.py↩︎
https://docs.python.org/3/library/uuid.html↩︎
https://beej.us/guide/bgpython/source/examples/ex_uuidgen.py↩︎
https://docs.python.org/3/library/textwrap.html#textwrap.TextWrapper↩︎
https://beej.us/guide/bgpython/source/examples/ex_wrap.py↩︎
https://docs.python.org/3/library/random.html↩︎
https://beej.us/guide/bgpython/source/examples/ex_rand1000.py↩︎
https://docs.python.org/3/library/time.html↩︎
https://beej.us/guide/bgpython/source/examples/ex_curdate.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_zipextract.py↩︎
The Operating System is like Linux, Windows, MacOS, Unix, etc. It’s a program that helps you, the user, interface with the hardware on the system, like keyboards, screens, and disks.↩︎
It’s a level of polish that other devs will expect. If they see you’re not thorough enough to close any files you open, they’ll wonder what else you’ve missed.↩︎
https://beej.us/guide/bgpython/source/examples/fileread1.py↩︎
Meaning, the One Right-ish Way to do something.↩︎
https://beej.us/guide/bgpython/source/examples/lines.txt↩︎
A function stub is a callable function that takes all the same arguments and, if necessary, returns a sensible value. But it doesn’t, in fact, do anything of use. It’s a good way to test that your overall call flow is working right. And it gives you a nice, easy TODO spot to fill out.↩︎
https://en.wikipedia.org/wiki/Off-by-one_error↩︎
https://beej.us/guide/bgpython/source/examples/lineedit.py↩︎
True story. This was back when I was in the 7th grade and computers were a bit of a novelty. We had a daisywheel printer at home which was fairly indistinguishable from a typewriter. In real life, though, I didn’t write a program to generate the lines. I told dad about the task, and he rolled his eyes and showed me how to copy and paste in WordStar.↩︎
https://beej.us/guide/bgpython/source/examples/ex_writelines.py↩︎
https://beej.us/guide/bgpython/source/examples/games.csv↩︎
https://en.wikipedia.org/wiki/M.U.L.E.↩︎
https://beej.us/guide/bgpython/source/examples/ex_simplecsv.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_multtablefile.py↩︎
https://docs.python.org/3/library/stdtypes.html#str.split↩︎
https://beej.us/guide/bgpython/source/examples/wargames.txt↩︎
https://beej.us/guide/bgpython/source/examples/ex_wc.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_sort.py↩︎
https://docs.python.org/3/library/exceptions.html↩︎
https://docs.python.org/3/library/exceptions.html↩︎
A traceback, also known as a stack trace, is a list of all the function calls that have taken place to get to this point. It’s really useful information for debugging.↩︎
https://docs.python.org/3/library/exceptions.html↩︎
https://docs.python.org/3/library/exceptions.html↩︎
https://beej.us/guide/bgpython/source/examples/head.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_catchorder.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_listadd2.py↩︎
https://beej.us/guide/bgpython/source/examples/ex_listadd.py↩︎
https://www.youtube.com/watch?v=XO0pcWxcROI↩︎
Apart from the lazy factor, that is.↩︎
https://en.wikipedia.org/wiki/Imaginary_number↩︎
https://en.wikipedia.org/wiki/Natural_logarithm↩︎
https://en.wikipedia.org/wiki/Big_O_notation↩︎
https://docs.python.org/3/tutorial/floatingpoint.html#tut-fp-issues↩︎
Which should serve as a not-so-gentle reminder that you should document your code.↩︎
https://en.wikipedia.org/wiki/End-of-file↩︎
This is a bit of a pun around the original Bourne Shell from back in the day. Bash improves on it a bit.↩︎
https://git-scm.com/↩︎
https://git-scm.com/downloads↩︎
https://learn.microsoft.com/en-us/windows/wsl/install↩︎
https://code.visualstudio.com/↩︎
https://beej.us/guide/bgpython/source/examples/hello.py↩︎
https://en.wikiquote.org/wiki/Edsger_W._Dijkstra↩︎