In this section, we will take a quick look at the syntax of Python, and in particular how it differs from other common programming languages. We will use a simple Python program as an example:
def fibonacci(n): a,b = 0,1 for i in range(n): print(a) a, b = b, a + b fibonacci(10)
This code defines a function that prints the first n Fibonacci numbers. The code then calls the function to print the first 10 numbers.
We can start to get a flavour of Python just from these few lines of code. The observations below are covered in more detail in the rest of the topics.
Python declares functions using the def keyword, and uses a colon at the end of function signature.
Unlike many languages, Python uses indentation to specify the program structure. The body of a function, or if statement or loop is indented by a fixed number of spaces.
Python doesn't use curly brackets or begin...end around blocks of code, it relies purely on indentation to define the program structure.
If you use Java or similar languages, you probably indent your code like that anyway, to make it more readable. The difference with Python is that you must indent your code correctly because indentation has meaning. This may seem strange at first, but it removes a lot of clutter from your code when you get used to it.
A second thing you might notice is that the function parameter n is not given a type:
In Python, variables do not have a type. Data has a type, for example int or string, but any variable can hold any type of data.
On a related point, the function doesn't specify a return type. This is because:
A function can return a value of any type. All functions return a value, if you don't specify a return value, a value of None is returned by default.
None is Python's equivalent of void or nil. A None value is simply an object of type None.
This next line declares and assigns values to variables a (set to 0) and b (set to 1). Notice again the variables do not have a type, but the values (0 and 1) are both ints.
a,b = 0,1
Another thing to notice here is that there is no semicolon at the end of the line:
Python doesn't require semicolons, provided you put each statement on its own line.
Here is a Python for loop:
for i in range(n):
This loops n times, with i counting from 0 to n - 1. It is equivalent to the C-like loop:
for (int i = 0; i < n; i++)
The Python loop is arguably more readable. As you will see later, it also provides a very consistent way of looping over lists and other sequences.
print is a built in function, providing an easy way to print to the console. Python can be used to create quick scripts or command line utilities.
Here is how you swap the values of two variables in Python:
a, b = b, a + b
This uses a technique called tuple packing which is very useful for handling multiple values like this.
Anything which is outside of any function definition is executed by Python:
There is no need for a main function. This makes Python great for creating scripts, you can just type in the code you need, with no boiler plate.
Copyright (c) Axlesoft Ltd 2020