Stay organized with collections Save and categorize content based on your preferences.
This page explains how to set up Python on a machine so you can run and edit Python programs, and links to the exercise code to download. You can do this before starting the class, or you can leave it until you've gotten far enough in the class that you want to write some code. The Google Python Class uses a simple, standard Python installation, although more complex strategies are possible. Python is free and open source, available for all operating systems from python.org. In particular we want a Python install where you can do two things:
Both of the above are done quite a lot in the lecture videos, and it's definitely something you need to be able to do to solve the exercises.
Note: in all examples below, you'll seepython
invoked as python3
. This is not a mistake. On Linux, Mac OS X, and most other OSs, the python runtime is installed as python3
. If you're following along on Windows, the python executable can still be invoked as python
. The videos predate Python 3 (and there have been a lot of changes in the python language since then) so, when you see code in the video that doesn't work, check here in the docs to see the updated code examples. Download Google Python Exercises
As a first step, download the google-python-exercises.zip file and unzip it someplace where you can work on it. The resulting google-python-exercises directory contains many different python code exercises you can work on. In particular, google-python-exercises contains a simple hello.py file you can use in the next step to check that Python is working on your machine. Below are instructions for Windows and other operating systems.
Python on Linux, Mac OS X, and other OSMost operating systems other than Windows already have Python installed by default. To check that Python is installed, open a command line (typically by running the "Terminal" program), and cd to the google-python-exercises directory. Try the following to run the hello.py program (what you type is shown in bold):
~/google-python-exercises$ python3 hello.py Hello World ~/google-python-exercises$ python3 hello.py Alice Hello Alice
If python is not installed, see the Python.org download page. To run the Python interpreter interactively, just type python3
in the terminal:
~/google-python-exercises$ python3 Python 3.X.X (XXX, XXX XX XXXX, XX:XX:XX) [XXX] on XXX Type "help", "copyright", "credits" or "license" for more information. >>> 1 + 1 2 >>> you can type expressions here .. use ctrl-d to exit
The two lines python prints after you type python3
and before the >>> prompt tells you about the version of python you're using and where it was built. As long as the first thing printed is "Python 3.", these examples should work for you. This course is designed for Python 3.X or later.
The commands above are the simplest way to run python programs. If the "execute bit" is set on a .py file, it can be run by name without having to type python
first. Set the execute bit with the chmod
command like this:
~/google-python-exercises$ chmod +x hello.py ~/google-python-exercises$ ./hello.py ## now can run it as ./hello.py Hello WorldPython on Windows
To install Python on Windows, go to the python.org download page and download Python 3.X.X. Run the Python installer and accept all the defaults. This will install Python in the root directory and set up some file associations.
With Python installed, open a command prompt (Accessories > Command Prompt, or type cmd
into the run dialog). Cd to the google-python-exercises directory (from unzipping google-python-exercises.zip). You should be able to run the hello.py python program by typing python hello.py
(what you type is shown in bold):
C:\google-python-exercises> python hello.py Hello World C:\google-python-exercises> python hello.py Alice Hello Alice
If this works, Python is installed. Otherwise, see Python Windows FAQ for help.
To run the Python interpreter interactively, select the Run... command from the Start menu, and type python
-- this will launch Python interactively in its own window. On Windows, use Ctrl-Z to exit (on all other operating systems it's Ctrl-D to exit).
In the lecture videos, we generally run the Python programs with commands like ./hello.py
. On Windows, it's simplest to use the python hello.py
form.
A Python program is just a text file that you edit directly. As above, you should have a command line open, where you can type python3 hello.py Alice
to run whatever exercise you are working on. At the command line prompt, just hit the up-arrow key to recall previously typed commands, so it's easy to run previous commands without retyping them.
You want a text editor with a little understanding of code and indentation. There are many good free ones:
Following are some recommended settings for your text editor:
Here are the preferences to set for common editors to treat tabs and line-endings correctly for Python:
M-x set-variable(return) indent-tabs-mode(return) nil
.To try out your editor, edit the hello.py program. Change the word "Hello" in the code to the word "Howdy" (you don't need to understand all the other Python code in there - we'll explain it all in class). Save your edits and run the program to see its new output. Try adding a print('yay!')
just below the existing print and with the same indentation. Try running the program, to see that your edits work correctly. For class we want an edit/run workflow that allows you to switch between editing and running easily.
print
isn't followed by parenthesis "()"
. This is one of the language changes you'll see in the updated examples. Quick Python Style
One of the advantages of Python is that it makes it easy to type a little code and quickly see what it does. In class, we want a work setup that matches that: a text editor working on the current file.py, and a separate command line window where you can just press the up-arrow key to run file.py and see what it does.
Teaching philosophy aside: the interpreter is great for little experiments, as shown throughout the lectures. However, the exercises are structured as Python files that students edit. Since being able to write Python programs is the ultimate goal, it's best to be in that mode the whole time and use the interpreter just for little experiments.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-23 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-23 UTC."],[[["This guide provides instructions for setting up a Python environment on your machine to run and edit Python programs, primarily for the Google Python Class."],["Download the google-python-exercises.zip file to access practice code and verify your Python installation by running the hello.py program."],["Choose a suitable text editor with features like space-based indentation, auto-indentation, and Unix line endings for efficient Python development."],["Familiarize yourself with running Python programs from the command line and using the Python interpreter interactively for experimentation."],["The focus is on writing and executing Python programs, with the interpreter serving as a tool for quick tests and explorations."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4