0 ratings 0% found this document useful (0 votes) 34 views 217 pages cambridge_igcse_and_o_level_computer_science_programming_book_for_pythonpdf
The document is a programming book for Python aimed at Cambridge IGCSE™ and O Level students, covering essential topics such as variables, algorithms, GUI applications, and iteration. It introduces Python 3 as a powerful, loosely typed scripting language and provides guidance on using the IDLE IDE for programming. The book includes practical exercises and explanations to help learners understand and apply Python programming concepts effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Carousel Previous Carousel Next
Save cambridge_igcse_and_o_level_computer_science_progr... For Later CAMBRIDGE
UNIVERSITY PRESS
Computer Science
for Cambridge IGCSE™ & O Level
PROGRAMMING BOOK FOR PYTHON
Chris Roffey> _ CAMBRIDGE IGCSE™ & 0 LEVEL COMPUTER SCIENCE: PROGRAMMIN
>» Contents
‘The items in orange are available on the digital edition
that accompanies this book
Introduction
How to use this book
How to use this series
1 Python 3
14 Getting Python 3 and IDLE.
1.2 Other Integrated Development
Environments (IDEs)
1.3. Turtle graphics
1.4 Graphical user interface (GUD)
applications
1.5 Additional support
2. Variables and arithmetic
operators
21 Variables and constants
22 Typesof data
2.3 Pseudo numbers
2.4 Naming conventions in Python
2.5 Arithmetic operators
2.6 Programming tasks
27 Python modules
2.8 Random and Round
3. Algorithm design tools
34 Programming constructs
32 Design tools
3.3. Flowcharts
3.4 Pseudocode
3.5 Effective use of flowcharts
and pseudocode
4 Subroutines
4.1 Subroutines
4.2 Programming a function
4.3 Programming a procedure
8
2
wy
28
2»
1
M
2
4
|G BOOK
5 GUI applications
5.1 Make your first application in a
window with a button
5.2 Other tkinter widgets you can use
in your applications
5.3 Choosing a text-based or
GUI application
6 Sequence and strings
61 Flowcharts and sequence
6.2 Pseudocode and sequence
63 Use of flowcharts and pseudocode
in programming
6.4 String manipulation in Python
7 Selection
7.1 IF statements
7.2 Logical operators
7.3 Coding IF statements in Python
7.4 Multiple IF statements
7.5 Nested IF statements
7.6 CASE statements
7.7 Drawing flowcharts for
CASE statements
7.8 Boolean operators
8 Iteration
81 Types iteration
82 FOR loops
83 WHILE loops
8.4 REPEAT..UNTIL loops
8.5 Gathering experimental data
aa £6
22Contents:
9 System design 12.6 — Breakpoints, variable tracing
9.4. | aap Rowden sand stepping through code 161
9.2 Structure diagrams a60 12.7 Beta testing 168
pe 105 43 Files and databases
94 The complete design process 109 43.4 files 168
10 Arrays 13.2. Databases im
10.1 Declaring an array 13 13.3 Querying databases 173,
10.2 Initialising arrays 4 13.4 Python and SQL 178
10.3. wanes , 13.5 More advanced SQL 179
10-4 ger ean 126 14 Programming scenario task
10.5 Two-dimensional arrays 128 Thai ereetintrmicemte a
10.6 ae tae sO 14.2 Constructing a skeleton answer 185
in Python 3 :
eh re 14.3 Filling in the details 186
11 Checking inputs 14.4 Putting it all together 188
414 Validation 136 14.5 Final thoughts 189
ie 137 45 Examination practice 194,
14.3. Programming validation into :
your systems 137 Appendix 1: Turtle reference 205
2 Teste Appendix 2: Tkinter reference 207
12.1 Whento test 152 Glossary 2
sins 152
122) a Acknowledgements 214
12.3 IDE debugging tools
and diagnostics 1540 Colutions
12.4 Identifying logical errors 156,
125 Dry running 156.
> Chapter 1
Pytho a) %> CAMBRIDGE IGCSE™ & © LEVEL COMPUTER SCIENCE: PROGRAMMING BOOK
Introduction
Python is a modern, powerful programming language used by many organisations such
as YouTube, Wikipedia, Google, Dropbox, CERN and NASA. At the time of writing,
Python is listed as the third most popular programming language in the world,
Python 3 is the latest version of the Python programming language. It is a loosely
typed script language. Loosely typed means that itis usually not necessary to declare
variable types; the interpreter looks after this. A compiler converts instructions into
‘machine code that can be read and executed by a computer. 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 sich as fast and
agile development
1.1 Getting Python 3 and IDLE
There are Python 3 installers for most types of computer available on the python.org
website. You should choose the latest stable version of Python 3 (Python 3.8.1 at
time of writing). When downloaded and installed, you will find Python comes with a
perfectly good IDE called IDLE, Starting up IDLE will enable you to run a program
straight away
An Integrated Development Environment (IDE) isa piece of software that is similar
toa word processor but for writing programs. IDEs provide special tools that help
programmers do their jobs more efficiently. They usually have an easy way of running
the programs during the development stage —such as a Run button. There are many
IDEs that can be used with Python. Some of them are very complicated to use, with
‘many specialist tools for teams of developers that work on very large projects. IDLE is
an IDE that has all the tools a learner requires and very litte to get in your way.
Everything that you are asked to do in this book can be done with IDLE. Figure 1.1
shows what you are presented with when you first open IDLE.
Python 3.8.1 (v3.8.1:1b293»6006, Dec 18 2019, 14:08:53)
[lang 6.0" (clang-600.0.57)) on darwin,
[type “heip", "copyright", “eredita™ or "License()* for more information
Figure 1.1: IDLE's Python shell on an Apple computer
loosely typed:
programming
languages, such as
Python, where the
programmer does
not have to declare
the variable type
when initialising or
declaring variables.
ad
Integrated
Development
Environment (IDE):
software that helps
programmers to
design, create and
test program code.
IDLE: the IDE
provided when
Python is installed.Figure 1.1 shows the Python shell, which is the first thing that opens when you start
HDLE. This is an unusual feature in Python, In the shell, we ean write Python
‘commands and code snippets and run them without having to save a file In this book,
‘we Will refer to typing code into the shell as an ‘interactive session’. Interactive sessions
aire great for experimenting and trying out new things that you learn about Python,
When you are presented with an interactive session, it is expected that you will open a
Python shell and type in what is shown, When you have done this, you are encouraged
to then try out your own ideas until you feel confident with the new feature. Open
IDLE from your Python instal folder now and follow the instructions in the
interactive session below
Open a Python shell and type in the following code after the >>> prompt:
>>> print (‘Hello world
Press return,
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:
>>> 3%4
Interactive sessions are used to illustrate simple concepts or to show the
correct use of some new syntax. Itis a good idea to start your own interactive
session and follow along with the book. You may then want to experiment
further to deepen your understanding
Sometimes we want to save our programs; this is not possible in the Python shell. To
do this we open a file, type in our code and then save the file with a .py extension.
In this book, we refer to this as working in script mode. In Python we can have the
Python shell open at the same time as a script window. This means that while writi
4 program, you can still swap into the Python shell to try out something before
continuing to write your program.
The Python shell serves another purpose. It is where you can type any input a program
asks for and it is where any output appears. In IDLE the two windows are separate and
you are free to arrange your desktop as you wish, To open a new file for programming
in script mode you click on File and then New Fite as shown in Figure 1.2 on the
following page.
1 Python 3
Python shell: a
window that allows
Python programmers
to write and run
code a line at a time
without having to
save the code in a
file. Itis also where
users can provide
input and where
output is sent.
interactive mode:
when writing and
running code in the
Python shell window,
interactive mode
allows us to try out
snippets of code
without saving.
execute: another
word for run
Programmers tend to
prefer to talk about
a program executing
rather than running,
but they mean the
same thing
script mede: Python
scripts are written
ina text editor or
IDE and saved with
the .py extension.
Script mode enables
programmers to write
longer programs that
can be edited or run
at any time in the
future