Python Masterclass
Always wanted to have a go at programming? No more excuses, because Python is the perfect way to get started!
Python is a great programming language for both beginners and experts. It is designed with code readability in mind, making it an excellent choice for beginners who are still getting used to various programming concepts. The language is popular and has plenty of libraries available, allowing programmers to get a lot done with relatively little code.
You can make all kinds of applications in Python: you could use the Pygame framework to write simple 2D games, you could use the GTK libraries to create a windowed application, or you could try something more ambitious like the app where we used Python's Bluetooth and input libraries to capture the input from a USB keyboard and relay the input events an Android phone.
For this tutorial, we're going to be using Python 2.X.
Hello World
Let's get stuck in, and what better way than with the programmer's best friend, the 'Hello World' application! Start by opening a terminal. Its current working directory will be your home. It's probably a good idea to make a directory for the files we'll be creating in this tutorial, rather than having them loose in your home directory. You can create a directory called Python using the command mkdir Python. You'll then want to change into that directory using the command cd python.
The next step is to create an empty file using the command 'touch' followed by the filename. Our expert used the command touch hello_world.py. The final and most important part of setting up the file is making it executable. This allows us to run code inside the hello_world.py file. We do this with the command chmod +x hello_world.py. Now that we have our file set up, we can go ahead and open it up in nano, or any text editor of your choice. Gedit is a great editor with syntax highlighting support that should be available on any distribution. You'll be able to install it using your package manager if you don't have it already.
[liam@liam-laptop -]$ mkdir Python
[liam@liam-laptop -]$ cd Python/
[liam@liam-laptop Python]$ touch hello_world.py
[liam@liam-laptop Python]$ chmod +x hello_ world.py
[liam@liam-laptop Python]$ nano hello_world.py
Our Hello World program is very simple, it only needs two lines. The first line begins with a 'shebang' (the symbol #! - also known as a hashbang) following by the path to the Python interpreter. The program loader uses this line to work out what the rest of the lines need to be interpreted with. If you're running this in an IDE like IDLE, you don't necessarily need to do this.
The code that is actually read by the Python interpreter is only a single line. We're passing the value Hello World to the print function by placing it in brackets immediately after we've called the print function. Hello World is enclosed in quotation marks to indicate that it is a literal value and should not be interpreted as source code. As expected, the print function in Python prints any value that gets passed to it to the console.
You can save the changes you've made to the file in nano using the key combination Ctrl+O, followed by Enter. Use Ctrl+X to exit nano.
#!/usr/bin/env python2
print("Hello World")
You can run the Hello World program by prefixing its filename with ./ - in this case you'd type ./hello_world.py.
[liam@liam-laptop Python]$ ./hello_world.py
Hello World
Variables and data types
A variable is a name in source code that is associatd with an area in memory that you can use to store data, which is then called upon throughout the code. This data can be one of many types, including:
Integer : Stores whole numbers
Float : Stores decimal numbers
Boolean : Can have a value of True or False
String : Stores a collection of characters. "Hello World" is a string
As well as these main data types, there are sequence types (technically, a string is a sequence type but is so commonly used we've classed it as a main data type):
List : Contains a collection of data in a specific order
Tuple : Contains a collection immutable data in a specific order
A tuple would be used for something like a co-ordinate, containing an x and y value stored as a single variable, whereas a list is typically used to store larger collections. The data stored in a tuple is immutable because you an't change values of individual elements in a tuple. However, you can do so in a list.
It will also be useful to know about Python's dictionary type. A dictionary is a mapped data type. It stores data in key-value pairs. This means that you access values stored in the dictionary using that value's corresponding key, which is different to how you would do it with a list. In a list, you would access an element of the list using that element's index (a number representing the element's position in the list).
Let's work out a little program we can use to demonstrate how to use variables and different data types. It's worth noting at this point that you don't always have to specify data types in Python - it will generally work out the correct data type for you. Feel free to create this file in any editor you like. Everything will work out just fine as long as you remember to make the file executable. We're going to call ours
variables.py.