The code you submit on the CS Circles website is evaluated using a copy of Python running on our web server, which allows us to do automated grading. However, the more traditional way of programming is that you install a Python interpreter on your own personal computer. Running Python at home has several additional advantages: (i) you are not subject to sharing the limited resources on our web server; (ii) you can write interactive programs (see below); (iii) once the software is installed, you don't need an internet connection to run programs; (iv) you can access debug tools, sound, and graphics.
Free Python Tools
One easy place to get Python running on your computer, whether it is Windows, Mac, or Linux, is from the official Python.org website. Click here to visit the Download Python page. Select the most recent 3.x version and pick the installer that matches your computer (e.g., for 32-bit Windows, "Windows x86 MSI Installer"), then download and execute the installer. Once it is all installed, running the program called IDLE will open up an interactive Python prompt.
Note: if you are at school, you might not be able to install the software yourself depending on how the permissions are configured; it might be necessary to get the permission and help of the network administrator. Even so, there are some Python installations which can be installed & run on a USB drive, for example portablepython.com.
IDLE lacks some useful tools for debugging. Wing IDE has many debugging features including making breakpoints, doing stepped execution, watching variables and the call stack. Other options, which we found harder to install, are PyDev or PyCharm. For the rest of this page we only talk about IDLE.
Using The Interactive Prompt
When you open IDLE you get an interactive prompt (sometimes called a shell). Here, you can directly type individual commands and have them immediately executed. The sign
indicates that Python is awaiting your input. For example, if you type
print(2+3)and then the Enter key, you will see
>>> print(2+3) 5The first line is what you asked Python to execute, and the second line is Python's output: the result of executing the statement was that Python computed the number
5and printed it. In fact you can leave out the
>>> 2+3 5In other words, by default Python will print the value of the statements you ask it to execute. One thing that is useful is to check the version: if you enter the following two lines, you can see what exact version of Python we are using here at CS Circles.
(We teach major version 3 since it is easier for beginners. Major version 2 is used in a lot of industry, but version 3 is gradually being supported more.)
- At the interactive prompt, you can get documentation about built-in functions by using the
helpfunction. For example, type
help(print)and it will give you basic information about how the
- To stop an infinite loop or a computation that is taking too long, you can interrupt/break the program by pressing Ctrl-C. Try running the command
while (1 > 0): purpose = "Wasting Time"and then breaking it with Ctrl-C.
- If you are using Python as a calculator or doing some small experiments, here is a useful shortcut. The underscore character
_refers to the result of the previous expression which the interactive prompt evaluated. For example, if you type
2+3on one line, then
_*2on the next line, you will see the following:
>>> 2+3 5 >>> _*2 10
If you are working on a complex problem, you will need to write and edit a long file containing multiple commands, similar to what we have done in the CS Circles website. To do this in IDLE, go to the File menu and select New Window (or use the shortcut Ctrl-N). This will open up a new window that is a basic text editor (not an interactive prompt). Write some code in this window (for example
print("Hello, World")); note that it does not execute when you hit the Enter key. Save this file on your computer (in the File menu choose Save, or press Ctrl-S). Then you can execute it: go to the Run menu and select Run Module (or use the shortcut F5). You will see the output of your program in the interactive prompt window!
A major feature that becomes possible when you run Python at home, rather than on the CS Circles website, is that you can now use interactive input in the interpreter window. The
input() command will get a line of user input from the interactive interpreter, and that string becomes the return value of the
input() function. In addition, when you call input you can pass it a prompt string as an argument, which helps instruct the user on what to do. We illustrate with a small example... copy it into your IDLE editor, then save and execute it to try it out.
print("Good day!") username = input("What is your name? ") print("Nice to meet you,", username)
An Example of Interactivity: A Guessing Game
Here we give an example of something where interactive input makes a big difference: a guessing game where you play against the computer. We will use a function from the
random package: the function
random.randint(A, B) generates a random integer between
B (inclusive). Using these functions, here is a small game: copy-and-paste it to your editor, save and run!
import random secretNumber = random.randint(1, 100) print("I'm thinking of a secret number between 1 and 100...") while True: guess = int(input("What is your guess? ")) if (guess == secretNumber): break if (guess > secretNumber): print("Sorry, your guess is too high.") if (guess < secretNumber): print("Sorry, your guess is too low.") print("Dang, you got it!")
If you find running these programs at home interesting, then create more complex programs, such as an interactive program to convert between units of measurement. Here are some more things that you can do now that you are running Python at home:
- the Python turtle module allows you to create line graphics and geometric animations!
- you can write Python programs that read and write files;
- you can use Python to create musical beeps!
We recommend the free book Invent Your Own Computer Games With Python if you are looking for a resource that explains, in vivid detail, many fun and interesting ideas involving interactive Python.
|Using Pygame in Python 3 on Windows can be done with the extra libraries found here.|