If you haven’t yet got python, the latest official installation packagescan be found here:
Python 3 is preferable, being the newest version out!
Note
On Windows, you’ll want to addPython to your PATH, so itcan be found by other programs. With Python 3.5 or later, there should beand option to do this in the installer. Otherwise, you can navigate to yourinstallation directory (C:\Python34\
), open theTools
,thenScripts
folder, and run thewin_add2path.py
file by double clicking on it.
A code editor helps with reading and writing programming code. There aremany around, and it is one of the most personal choices a programmer canmake - Like a tennis-player choosing their racket, or a chef choosing theirfavourite knife. To start off with, you’ll just want a basic, easy-to-use onethat doesn’t get in your way, but is still effective at writing python code.Here are some suggestions for those:
If you’d like our recommendation, try out Sublime Text first.
Tip
Wordpad, TextEdit, Notepad, and Word arenot suitable code editors.
Ok, so python is this thing called aprogramming language. It takes text thatyou’ve written (usually referred to ascode), turns it into instructions foryour computer, and runs those instructions. We’ll be learning how to write codeto do cool and useful stuff. No longer will you be bound to useothers’programs to do things with your computer - you can make your own!
Practically, Python is just another program on your computer. The first thing tolearn is how to use and interact with it. There are in fact many ways to do this;the first one to learn is to interact with python’s interpreter,using youroperating system’s (OS) console.
Aconsole (or ‘terminal’, or ‘command prompt’) is atextual way tointeract with your OS, just as the ‘desktop’, in conjunction with your mouse,is thegraphical way to interact your system.
OS X’s standard console is a program calledTerminal. Open Terminal bynavigating to Applications, then Utilities, then double-click theTerminal program. You can also easily search for it in the systemsearch tool in the top right.
The command line Terminal is a tool for interacting with yourcomputer. A window will open with a command line prompt message,something like this:
mycomputer:~ myusername$
Different linux distributions (e.g Ubuntu, Fedora, Mint) may have differentconsole programs, usually referred to as a terminal. The exact terminalyou start up, and how, can depend on your distribution. On Ubuntu, you willlikely want to openGnome Terminal. It should present a prompt like this:
myusername@mycomputer:~$
Window’s console is called the Command Prompt, namedcmd. An easyway to get to it is by using the key combinationWindows+R
(Windows
meaning the windows logo button), which should open aRun dialog. Then typecmd and hitEnter
orclickOk. You can also search for it from the start menu. It shouldlook like:
C:\Users\myusername>
Window’s Command Prompt is not quite as powerful as its counterparts on Linuxand OS X, so you might like to start the Python Interpreter (see just below)directly, or using theIDLE program that Python comes with.You can find these in the Start menu.
The python program that you have installed will by default act as somethingcalled aninterpreter. An interpreter takes text commands and runsthem as you enter them - very handy for trying things out.
Just typepython at your console, hitEnter
, and you shouldenter Python’s Interpreter.
To find out which version of python you’re running,instead typepython-V
in your console to tell you.
After Python opens, it will show you some contextual information similar to this:
Python 3.5.0 (default, Sep 20 2015, 11:28:25)[GCC 5.2.0] on linuxType "help", "copyright", "credits" or "license" for more information.>>>
Note
The prompt>>> on the last line indicates that you are now in aninteractive Python interpeter session, also called the “Python shell”.This is different from the normal terminal command prompt!
You can now enter some code for python to run. Try:
print("Hello world")
PressEnter
and see what happens. After showing the results, Pythonwill bring you back to the interactive prompt, where you could enteranother command:
>>>print("Hello world")Hello world>>>(1+4)*210
An extremely useful command ishelp()
, which enters a help functionalityto explore all the stuff python lets you do, right from the interpreter.Pressq
to close the help window and return to the Python prompt.
To leave the interactive shell and go back to the console (thesystem shell),pressCtrl-Z
and thenEnter
on Windows, orCtrl-D
onOS X or Linux. Alternatively, you could also run the python commandexit()
!
Just above we demonstrated entering a command to figure out some math. Trysome math commands of your own! What operations does python know? Get itto tell you what 239 and 588 added together, and then squared is.
Here are some ways you might have got the answer:
>>>239+588827>>>827*827683929
>>>(239+588)*(239+588)683929
>>>(239+588)**2683929
When you have a lot of python code to run, you will want to save it intoa file, so for instance, you can modify small parts of it (fix a bug) andre-run the code without having to repeatedly re-type the rest.Instead of typing commands in one-by-one you can save your code to afile and pass the file name to thepython program.It will execute that file’s code instead oflaunching its interactive interpreter.
Let’s try that! Create a filehello.py
in your current directorywith your favorite code editor and write the print command from above. Nowsave that file. On Linux or OS X, you can also runtouchhello.py
to createan empty file to edit. To run this file with python, it’s pretty easy:
$python hello.py
Note
Make sure you are at your system command prompt, which will have$
or>
at the end,not at python’s (which has>>>
instead)!
On Windows you should also be able to double-click the Python file to run it.
When pressingEnter
now, the file is executed and you see the outputas before. But this time, after Python finished executing all commands fromthat file it exits back to the system command prompt, instead of going backto the interactive shell.
And now we are all set and can get started with turtle!
Note
Not getting “Hello world” but some crazy error about “can’t openfile” or “No such file or directory?” Your command line might not berunning in the directory that you saved the file in. You can changethe working directory of your current command line with thecd command, which stands for “change directory”. On Windows,you might want something like:
> cd Desktop\Python_Exercises
On Linux or OS X, you might want something like:
$cdDesktop/Python_Exercises
This changes to the directory Python_Exercises under the Desktop folder(yours might be somewhere different). If you don’t know the locationof the directory where you saved the file, you can simply drag thedirectory to the command line window. If you don’t know whichdirectory your shell is currently running in usepwd,which stands for “print working directory”.
Warning
When playing around with turtle, avoid naming your fileturtle.py
— rather use more appropriate names such assquare.py
orrectangle.py
. Otherwise, whenever you refer toturtle
, Pythonwill pick upyour file instead of the standard Python turtle module.