Cambridge University Press
978-1-316-61782-3 — Cambridge IGCSE® and O Level Computer Science Programming Book for Python
Chris Roffey
Excerpt
More Information
www.cambridge.org
© in this web service Cambridge University Press
Chapter 1:
Python 3
Learning objectives
By the end of this chapter you will understand how to:
obtain a simple IDE to support your programming
use both interactive mode and script mode in Python
program and save a text-based application in script mode.
1
Cambridge University Press
978-1-316-61782-3 — Cambridge IGCSE® and O Level Computer Science Programming Book for Python
Chris Roffey
Excerpt
More Information
www.cambridge.org
© in this web service Cambridge University Press
1.01 Getting Python 3 and IDLE For Your Computer
Python 3 is the latest version of the Python programming language. It is a loosely typed
script language. Loosely typed means that it is usually not necessary to declare variable
types; the interpreter looks aer this. Script languages do not have a compiler. This means
that, in general, Python programs cannot run as quickly as compiled languages; however,
this brings numerous advantages, such as fast and agile development.
Python is a powerful, modern programming language used by many famous organisations
such as YouTube and National Aeronautics and Space Administration (NASA) and it is one of
the three programming languages that can be used to develop Google Apps.
There are installers for Windows and Apple computers available at https://www.python.org/
downloads/. You should choose the latest stable version of Python 3 (Python 3.5.0 at time of
writing). If you have a Raspberry Pi, then two versions of Python are already installed.
On the Raspberry Pi you can start programming in interactive mode straight away by
selecting Python 3 from Programming in the main Menu in the task bar (Figure 1.01).
Figure 1.01 Starting Python 3 on a Raspberry Pi
2
Cambridge IGCSE and O Level Programming Book
Cambridge University Press
978-1-316-61782-3 — Cambridge IGCSE® and O Level Computer Science Programming Book for Python
Chris Roffey
Excerpt
More Information
www.cambridge.org
© in this web service Cambridge University Press
This opens IDLE which is the IDE (Integrated Development Environment) that comes
packaged with Python (Figure 1.02).
Figure 1.02 IDLE’s Python Shell – working in interactive mode on a Raspberry Pi
KEY TERMS
Interactive mode: When writing and running code in the Python Shell window, interactive mode
allows us to try out snippets of code without saving.
IDE: An Interactive Development Environment is a special text editor with useful built-in tools for
programmers.
A er installing Python 3 on Apple computers, IDLE can be found in the main Python 3 folder
in your Applications folder.
On Windows computers, once installed, IDLE can be opened by looking for the Python 3.5
folder found in All Programs when opening the Start menu. From the Python 3.5 folder
choose IDLE.
In all cases this opens a window containing the Python Shell. This can run simple programs
at the >>> prompt. Executing small programs in the Shell window is referred to as working in
interactive mode. It provides a very useful environment for running short code experiments
when developing larger programs in script mode. Throughout this book you will be
prompted to try out code snippets and run short experiments so that you get used to new
functions and syntax in interactive sessions. These sessions can be accessed extremely
quickly by opening IDLE and typing directly into the Shell window.
KEY TERMS
Script mode: When writing code in a new window in IDLE that will be saved in a file as a Python
script.
To create a script that can contain more complex programs and, more significantly, can be
saved and reused, you should obtain a new window by selecting New File from the File menu.
This opens a blank script window into which you can type and save your code (always with
3
Chapter 1: Python 3
Cambridge University Press
978-1-316-61782-3 — Cambridge IGCSE® and O Level Computer Science Programming Book for Python
Chris Roffey
Excerpt
More Information
www.cambridge.org
© in this web service Cambridge University Press
the extension .py). IDLE provides help with code colouring and auto-indenting in whichever
mode you are working.
In script mode, the Shell window takes on a new role as a console. Text output from your
programs appears in this window (see Figure 1.03). It is also where users provide input,
and error messages appear. The console is still available as a Shell window to use for quick
experiments while developing your scripts.
To run your scripts, you should save your file to a sensibly named folder in your Documents
folder and then select Run Module from the Run menu, or press F5 on your keyboard.
Figure 1.03 IDLE’s Python Shell and a script window open on a Raspberry Pi
1.02 Other Integrated Development Environments
IDLE is perfectly adequate for performing all the tasks required in this book. However, if you
have been programming with IDLE for a number of years, you might like to try one of the
other many IDEs available.
The one that is used for the remainder of the screenshots in this chapter, and occasionally
later in the book, is Wing IDE 101 (Figure 1.04). This is a free version of a commercial IDE
that provides a carefully selected set of facilities that are useful for students. It can be
downloaded from
http://wingware.com/downloads/wingide-101 where brief introductory
videos and installation instructions are available. Please be aware that the Raspberry Pi is
currently not powerful enough to run this or most other commercial IDEs satisfactorily. Wing
IDE 101 is available for Windows, Apple computers, Ubuntu and other versions of Linux.
4
Cambridge IGCSE and O Level Programming Book
Cambridge University Press
978-1-316-61782-3 — Cambridge IGCSE® and O Level Computer Science Programming Book for Python
Chris Roffey
Excerpt
More Information
www.cambridge.org
© in this web service Cambridge University Press
Figure 1.04 Wing IDE 101 Integrated Development Environment.
The large panel in the middle of the application is where you write your scripts. Interactive
sessions can be run in the Shell tab below this window.
There are two ways to run a program in Wing IDE. Clicking the run button (
) will access the
Python Shell as shown in Figure 1.04. An alternative – and recommended – way of running
your scripts is to click on the bug (
) to the right of the run button (Figure 1.05) This opens
the Debug I/O panel and now provides error messages in the Exceptions tab on the right.
Figure 1.05 Wing IDE 101 showing input and output a er pressing the bug button
5
Chapter 1: Python 3
Cambridge University Press
978-1-316-61782-3 — Cambridge IGCSE® and O Level Computer Science Programming Book for Python
Chris Roffey
Excerpt
More Information
www.cambridge.org
© in this web service Cambridge University Press
1.03 Make Your First Program Using Interactive Mode
In IDLE’s interactive mode window or in the Python Shell tab in Wing IDE, type out the
following line of code at the >>> prompt and then press return.
INTERACTIVE SESSION
>>> print('Hello world!')
You have now run your first interactive mode program. Your code told the computer to print
the text ‘Hello world!’ to the screen. It executed your code when you pressed the return key
on your keyboard. You can also use interactive mode as a simple calculator. Try entering this
sum and press return:
INTERACTIVE SESSION
>>> 3*4
1.04 Make Your First Program Using Script Mode
Working in IDLE select New File from the File menu to open a new window into which you
can type your code and then save it as a script. In Wing IDE, simply type into the main script
panel. Whichever IDE you are using, copy the following code and then save your file as
hello.py to a new folder called Python Code in your Documents folder.
# hello.py
print('Hello world!')
If using IDLE, run the script by selecting Run Module from the Run menu or by pressing F5. In
Wing IDE click the bug button.
Any code preceded by a hash symbol (#) is called a comment. This is ignored by the
computer when executing the script and is purely for the programmer. It can be useful to
include the file name in its own comment at the top of a script.
1.05 Graphical user interface Applications
Although not required by the syllabus, your Python scripts are not limited to text-based
applications. By importing the tkinter module, it is easy to produce visually rich graphical
user interfaces (GUIs) and attach your algorithms to buttons in windows.
TIP
Interactive sessions are used to illustrate simple concepts or to show the correct use of some new
syntax. It is a good idea to start your own interactive session and try the code yourself. You may
well want to experiment further to deepen your understanding.
Producing GUI
based applications
is outside the
syllabus.
6
Cambridge IGCSE and O Level Programming Book
Cambridge University Press
978-1-316-61782-3 — Cambridge IGCSE® and O Level Computer Science Programming Book for Python
Chris Roffey
Excerpt
More Information
www.cambridge.org
© in this web service Cambridge University Press
KEY TERMS
tkinter: An example of a GUI toolkit which is provided as part of the standard library when you install
Python.
graphical user interface (GUI): Graphical user interfaces contain items like buttons, text entry
boxes and radio buttons.
Chapter 5, GUI applications, is an optional chapter included in this book. In it, you will learn
how to build your own GUIs and how to repurpose your algorithm solutions to work with
them. From Chapter 5 onwards, there will be some tasks provided that include making
GUIs. Although these are not required by the Cambridge IGCSE and O Level Computer
Science syllabus, repurposing your solutions to work with GUIs will make you a more flexible
programmer and allow you to produce more professional looking applications.
1.06 Additional Support
The intention of this book is to introduce programming concepts that make use of the
non-language-specific formats included in the syllabus. Python 3 is used to provide the
opportunity for you to use a real programming language to develop your understanding of
these concepts. The oicial documentation for the Python programming language can be
accessed at https://docs.python.org/3/.
A simple syntax reference guide that can be printed out and fits in your pocket is available
from the Coding Club website at
http://codingclub.co.uk/codecards/.
This textbook also has its own companion website at [companion website URL].
Summary
Python 3 is a loosely typed programming language that is designed to encourage easily
read code.
Python 3 comes with a simple Integrated Development Environment called IDLE.
There are many other IDEs available, such as Wing IDE 101, which is specifically designed
for students.
There are three main styles of programming in Python 3:
interactive mode: quick tests and trials that can be programmed in the Python Shell
text-based: in script mode, text-based scripts can be saved so that your applications can
be reused
GUI applications: full, visually rich applications that can be produced in script mode.
7
Chapter 1: Python 3