Computers have been around us for many years now. They are placed on our desks, inside our phones, and inside our cars and washing machines. Computers do their work by following instructions. These instructions are bundled together, as computer programs. The programs are then executed, which means that the instructions are followed, sometimes one by one and sometimes more than one at the same time, and the computer does what the instructions tells it to do.
The act of creating computer programs is referred to as programming, or if you wish to make it more clear, computer programming. This book has the goal of giving you an introduction to computer programming. It will do so by describing the basic elements of a programming language, and how these elements can be used to create programs.
Before learning how to do the actual programming, it is important to gain experience with the practical usage of computers. I will assume that you have such practical experience, and most likely you are using a computer, or perhaps another type of electronic reading device, right now, as you read this text.
If you read the book on the web, you are typically using a browser program, such as Firefox or Opera. If you use another reading device, such as a tablet computer, you may be using some kind of reader software, like a Kindle reader program or an iBooks program.
There are many types of computer programs, used in different scenarios, such as programs for word processing, program for making numerical calculations, programs for reading e-books, and programs for controlling the speed of your car or the temperature of your washing machine.
The programs described in this book will be small compared to a browser program, and they will also be small compared to a car control program, but they might still be useful for learning the important concepts.
The book describes how to write the program text, also referred to as source code. In order for a program to run, which is another word for execute, the program must be represented in a format that is understandable by the computer. This format is referred to as machine code. This means that the source code must be translated into machine code.
Although the book concentrates on how to write the source code, there will be references given, to places where more information on how to make actual programs run on your own computer can be found. It is advisable to follow along, and to make your own programs, as you read the book.
This is a Book with Views. This means that the book covers several programming languages at the same time. When you read the book, you read it one view at a time, and you can easily switch betwen the views. In the e-book version of the book there are links to the views at the end of each section, and in the web version of the book there are also links to the views in the left sidebar of each page. The view you are currently reading treats the programming language Python.
The book is written to give an introduction, and to give possibilities to practice writing programs. When doing this, you take on the role of a programmer. Perhaps you will do it as a hobby, or perhaps you will do it as a profession. In any case I hope the book might be of help in the task of learning programming.
The remained of this chapter will give an introduction to programming, in the form of a simple example. In the following chapter Names and values, we will start looking at variables.
It is customary to write a small program first, before making larger programs. A small program, with its only task being the display of a text message, saying "Hello, world", is often used. Such a program can be referred to as a Hello world program.
A Hello world program written in Python is shown in Figure 1.
print "Hello, world"
Figure 1. A hello world program in Python.
The program in Figure 1 contains only one line. This line is a print statement, saying that the string "Hello, world" shall be printed.
Figure 2. A computer, of a possible type that you may want to use for programming. It may also be a computer in the form of a mobile phone, or some other device where a computer resides inside.
When you program using the language Python you need a Python interpreter. When using Linux or Mac OS, a Python interpreter is most likely already present on your system. You can obtain a Python interpreter for Windows, as well as for Linux and Mac OS, from the Python official website.
This book has been produced using several open source software products and open standards. It is written using XML, and translated, using Python software, to html for the web version, epub for the e-book version, and LaTeX to be converted to pdf for the print version. The mobi version is currently created using Calibre. Emacs and PSGML are used for editing. The book contains images from the Open Clipart Library.