Introduction

(Last modified 20th May 2020.)


Back to Tutorial contents.


In this chapter:

  1. What is programming?
  2. The Python programming language.
  3. A first program.

What is programming?

In simple words, programming is the art of writing instructions (that
usually control a computer). A set of instructions (also called code)
that you write to do some particular task is a program. The language
that used to write the program is called a programming language.

Most programming languages are usually made of some English words
and a few symbols. So all you really need to write a program is
a text editor (or even a pen or pencil and a piece of paper).

The aim of writing any program is to make the computer do some task
exactly as intended by the author, also called the programmer.
Feel free to skip the rest of this section. If you want to know more
about the process of programming, read on. You can always come back
later.

When writing a program, first you would think up what you want
to accomplish overall. Then you would refer to the programmer's manuals
(also known more popularly as the documentation) of the system or
language of your choice to find ways to do the different steps of your
program. You can also look at examples by other programmers.

Then you write (a part of) your program and try to run it. Next you fix
any mistakes and run again. Add a feature. Run again. You keep doing this
until you are satisfied with the result.

When your program gets bigger, you would split it into many sections
and even multiple files. This property of programming languages
is called modularity. This makes it easier to reuse some code
that you wrote for one project in another project that needs the same
capability.

In fact, many programming languages have large collections
of code libraries contributed by programmers. If you want
to do something common, chances are somebody else already wrote that
beautifully and has it available for anyone that needs it.

If you write something particularly tricky and want to understand what
you did when you read your program a while later, you would want to
describe that in plain English, with so called comments, right there
in the program. This is also useful when working in a team.

You may also write you own documentation in an external file to describe
what the program does to people who use (or even modify) it.
This is very handy for large projects.

This is still not a complete picture of programming. We will look at
the different elements in more detail in later chapters.


The Python programming language.

Python is a high level, interpreted programming language.
Being high level, reading Python code is close to reading English.

Almost any programming language requires some processing before it can
be run on a computer. The human readable programming language
is first converted into the computer's language - machine language.

Being an interpreted language, Python code is automatically converted
to machine code almost each time the program is run. This makes writing
and testing Python programs a quick process. Just change your code.
Then run the program again. You can test small pieces of code by typing
it into the interpreter and looking at the results live. This is a great
way to learn Python.


A first program.

(Take your time to read this section, especially if you are a beginner.)

You can install the latest release of Python 3 from the website
at python.org. If you're on a Linux distribution, you probably already
have Python installed. Once installed, open up a Terminal (Command Prompt
or PowerShell on Windows) and launch the interpreter by typing 'python'
or 'python3' (on Linux) and hitting Enter.

The interpreter shows some version and build information. Make sure you
are running Python 3 not Python 2 by checking the version number.

$ python3
Python 3.8.2 (default, Apr 8 2020, 14:31:25)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Now type in this line of code and press Enter:

print('Welcome to Python programming!')

You will see that the message is printed back to you:

>>> print('Welcome to Python programming!')
Welcome to Python programming!
>>>

Here 'print' is a built-in function. A function is a named section
(or block) of code that performs some useful action. What you just did
was to call print by placing a pair of brackets (parentheses)
after the function's name. A function can be called any number of times.

The message is a parameter to print. Parameters give additional
information to functions. Why not try some other messages?

>>> print('Another message.')
Another message.
>>> print('Python is not named after the snake.')
Python is not named after the snake.
>>> print("It was named after Monty Python's Flying Circus, a TV show.")
It was named after Monty Python's Flying Circus, a TV show.
>>>

When some code is repeated a considerable number of times in a program,
it can probably be rewritten as a function. Functions make our code more
readable, easier to edit and additionally saves us some storage space.

Note that the message is quoted. It is a string, which is any sequence
of characters. Python supports strings with characters from any language.
You put strings in either single (') or double (") quotes.

Use the 'exit' function to quit the interpreter:

exit()

And once again, Welcome to Python programming!