24.06.2020 Views

The Little Book of Algorithms 2.0

This workbook is designed to help those learning and teaching Computer Science. The aim of the book is to help students build fluency in their Python programming. The book would suit students who have already been introduced to the three basic programming constructs of structured programming, namely sequence, selection and iteration. The learning curve for programming can be quite steep and this book aims to ease this transition by encouraging practise and gradually introducing more complex concepts such as lists and 2D lists, file writing and using procedures and functions. Originally, the book was written for my 14-16 year old students studying for their GCSE Computer Science programming exam. However, a wide range of students and teachers will find this book useful. The Little Book of Algorithms concisely presents 18 problems which computer science students will commonly encounter. These problems are solved efficiently using programs written using Python. However, reading these programs is not enough, so this new version of the book now comes with 48 challenges so that you can apply what you have learnt in various ways: Writing your own programs Solving Parson’s puzzles Completing quizzes Tracing Gap fills This range of exercises will help you to become more fluent in Python and ensure that you are comfortable with any question format in a programming exam. After finishing this book, you should feel more familiar with: While loops and For loops Concatenating different data types Using procedures and functions Working with 1D and 2D lists and arrays File reading and writing This book will show you how to write better Python programs and will expose you to the key skills that are required to do well in any secondary school programming assignment or exam.

This workbook is designed to help those learning and teaching Computer Science. The aim of the book is to help students build fluency in their Python programming. The book would suit students who have already been introduced to the three basic programming constructs of structured programming, namely sequence, selection and iteration.

The learning curve for programming can be quite steep and this book aims to ease this transition by encouraging practise and gradually introducing more complex concepts such as lists and 2D lists, file writing and using procedures and functions. Originally, the book was written for my 14-16 year old students studying for their GCSE Computer Science programming exam. However, a wide range of students and teachers will find this book useful.

The Little Book of Algorithms concisely presents 18 problems which computer science students will commonly encounter. These problems are solved efficiently using programs written using Python. However, reading these programs is not enough, so this new version of the book now comes with 48 challenges so that you can apply what you have learnt in various ways:
Writing your own programs
Solving Parson’s puzzles
Completing quizzes
Tracing
Gap fills

This range of exercises will help you to become more fluent in Python and ensure that you are comfortable with any question format in a programming exam.

After finishing this book, you should feel more familiar with:
While loops and For loops
Concatenating different data types
Using procedures and functions
Working with 1D and 2D lists and arrays
File reading and writing

This book will show you how to write better Python programs and will expose you to the key skills that are required to do well in any secondary school programming assignment or exam.

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

...What happens if you wanted to use this lowest number later in

the program? In this case, it makes sense to use a function. The

key differentiator is that functions return values whereas

procedures do not.

1

2

3

4

5

6

7

8

9

10

11

12

13

def lower_num(num1,num2):

if num1 <= num2:

return num1

else:

return num2

first_num = int(input("Enter the first number: "))

second_num = int(input("Enter the second number: "))

lowest = lower_num(first_num,second_num)

print("The lowest number is " + str(lowest))

The function lower_num is defined on lines 1-5. We have to

define functions before we can call (use) them.

Usage of the function is demonstrated on lines 8-13. We still

take two numbers as integer inputs on Lines 8-9.

Line 11 calls the function lower_num with two arguments: the

contents of first_num and second_num variables.

These arguments are passed into the parameters num1 and

num2 respectively¹. The result is stored in the variable lowest.

As the returned value is an integer, it is cast to a string on line

13 using str(lowest) so that it can be concatenated (the

technical name for joining text) with the meaningful output

message.

¹ Arguments and parameters should have different names even if they seem to serve the

same purpose. In this case both num1 and first_num store the first number. However, the

argument stored in the variable first_num has global scope, it can be accessed and

changed anywhere in the program. The parameter num1 has local scope, it is a local

variable which can only be accessed in the subroutine.

8

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!