Sloan Kelly 2016
Sloan Kelly Python, PyGame and Raspberry Pi Game Development 10.1007/978-1-4842-2517-2_1
1. What Is a Programming Language?
Sloan Kelly 1
(1)
Niagara Falls, Ontario, Canada
Electronic supplementary material
The online version of this chapter (doi: 10.1007/978-1-4842-2517-2_1 ) contains supplementary material, which is available to authorized users.
A computer program is a list of statements that a computer must carry out in order to complete a task. A computer language describes the arrangement or syntax of those statements. There are various computer languages out there, each suitable to one or more tasks.
Each language has its own syntax, but they all have commands that perform roughly the same types of actions:
Input
Output
Decision-Making
Loops
A command or keyword is a special phrase that is used by the language to perform an action whether it is to get input from the user, or display text on the screen. These commands are reserved words that cannot be used for any other purpose in your program.
The old definition of a computer program was a basic mathematical formula:
Program = Algorithm + Data
An algorithm is a step-by-step procedure for processing data. The algorithm solves a problem with the data that it has been supplied. What kind of problem? It could be anything from calculating the area of a rectangle or the volume of a room to deciding how an enemy should react to a player who just obtained a power up.
Are all computer programs written the same way? Is there a standard way to approach a given problem? Well, no. Not really. There are many ways to achieve the same result in computer programming! There is no correct way of solving a problem. So long as your program does what it is supposed to, thats just fine! You may want to tweak your code later to speed it up, but any optimization happens once you have the algorithm right. Your program must function as expected. This is of paramount importance.
It is not advisable to stick to one language, but rather experience as many languages as you can. This will enable you, the programmer, to decide which language is best for a given situation.
Your first language is a great choice. Python is a very powerful language that can be used for a variety of purposes and is perfect for the first-time programmer.
Sloan Kelly 2016
Sloan Kelly Python, PyGame and Raspberry Pi Game Development 10.1007/978-1-4842-2517-2_2
2. What Is Python?
Sloan Kelly 1
(1)
Niagara Falls, Ontario, Canada
Python is a modern programming language that supports object-oriented, functional, and imperative programming styles. It is ideal for the beginner because of its readability and ease of use. Python is first and foremost a scripting language, but can be compiled into computer-readable binary. The upside to all of this is that you can write programs in less lines of code than an equivalent C/C++ or Java program.
What on earth did I just say? Lets break that last paragraph down and make it a little more readable.
Programming Styles
Python is suitable for programming in the following styles:
Imperative
Object-Oriented
Functional
Imperative
Imperative programming was for the longest time the most common way to write computer code. It describes step by step how to perform changes to the data in a very linear manner.
For example, we have the following items:
Tea
Bag
Milk
Cup
Spoon
Kettle
Water
This is our data. We want to change this data to a different state. What state? Well, we want a cup of milky tea. How do we do that? We prescribe a series of operations that will transform this data into some other data. Like so:
Place tea bag in cup
Pour water into kettle
Boil the kettle
While the kettle is boiling, watch TV
Pour the water from the kettle to the cup
Pour milk into the cup
Stir the tea with the spoon
Serve
In code (not specifically Python code), this could be written as:
addTo(cup, tea_bag)
addTo(kettle, watter)
boil(kettle)
while isBoiling(kettle):
watchTV()
addTo(cup, getWaterFrom(kettle))
addTo(cup, milk)
stir(cup)
serve(cup)
These are the prescribed steps (process) to change our initial data (our input) and transform it into our output. See Figure .
Figure 2-1.
Input, process, output block diagram
Object-Oriented
Imperative programs separate the functionality (the algorithm) from the data. Object-oriented languages keep the functionality with the data. Objects contain the data and the instructions used to manipulate that data in one place.
There is an advantage to this; algorithms stored with it process your data. Lets take a pencil as an example. It has certain attributes that describe it:
Color
Hardness
Nib Size
Length
It also has certain actions or methods that can be applied to it:
These methods change the state of the object; remember that state is determined by the data. For example, when you write using a pencil, the nib length gets smaller and smaller. When you sharpen the pencil, its overall length gets shorter, but the nib size is reset to its maximum.
Functional
Functional programming is not new and was first developed in the 1930s. It has its roots in lambda calculus. Functional programming uses mathematical functions to perform calculations. No data is changed in these calculations; instead new values are calculated. This means that functional programs have no state.
Functional programming tends to be used for recursion (calling the same function from itself) and iteration through items.
In Python, Fibonacci numbers can be calculated with the following one line:
fib = lambda n: n if n < 2 else fib(n-1) + fib(n-2)
This was taken from a discussion on StackOverflow ( http://bit.ly/FibonacciPython ). To calculate a value, the programmer simply passes in an integer value:
fib(5)
Conclusion
Python is a modern, multi-paradigm programming language. It can be used for imperative, object-oriented, and functional programming. So, now that we know what Python is capable of, its time we took a look at the language itself.
Sloan Kelly 2016
Sloan Kelly Python, PyGame and Raspberry Pi Game Development 10.1007/978-1-4842-2517-2_3
3. Introducing Python
Sloan Kelly 1
(1)
Niagara Falls, Ontario, Canada
In this section we will introduce the Python language. At this stage were only interested in understanding the format or syntax of the Python language and its keywords. Python is an interpreted language, meaning that it requires another program called an interpreter to run any code that we write.