Basic Programming in Python

Our adventure with Python, we will begin with the classic example of a “Hello World” program. The program will simply print ‘Hello World’ on the screen. Perhaps, you may be thinking that it seems pointless to make computer print Hello World and it is a valid argument. This program doesn't really do anything particularly interesting as it doesn't provide us with any new information. However, it makes us aware of the language structure. For instance, in natural languages, e.g. Spanish, there are certain rules in writing that one needs to learn and obey in order to express themselves correctly e.g. ¡Hola!. Similar kind of rules occur in Python (or in fact, in any programming language), but this time, the communication is between you and computer as oppose to other person. Let’s go straight to it. Once we have IDLE running let’s type:

Yes, the syntax error is inevitable here. Python has absolutely no idea what to do with this, so it throws an error. However, there is a way around it by putting quotes around the phrase like so:

This is a way but it is not particularly good. Notice that in the listing 1.1 and 1.2, we don’t really tell the computer what to do. There is no ‘instruction’ or ‘command’ that would tell the computer to display ‘Hello world’ and we are relying on the Python interpreter to understand what we are trying to do. Additionally, we can’t get rid of the quotes. Therefore, in order to make our program fully reliable we need to make it completely unambiguous and use a special instruction.

In Python, there exists a special keyword that tells the computer to print text and it is called print(unsurprisingly).

Again, we encounter the same problem of ambiguity in our code. How does Python know that Hello isn't just another instruction? The same thing applies to the word World.

We can deal with this problem by adding quotes. Python deals with the problem is by recognising quotes. If we put anything in quotes then Python sees it as ‘OK, here I am dealing with text, so I am not going to try to look for the ‘keywords’.

Everything inside quotes is called a string or more precisely: a string of characters. We can join different strings of characters into one, like this:

Or my wish to use comma "," like this:

Again, you may be wondering why would you want to join different strings together. Imagine we wish to print Welcome to Python in a new line. One way we do this is by printing a special character 'n'.

Separating different strings makes the text more readable. Literally, you can look at the instruction and read 'print Hello world and add a new line plus Welcome to Python'. Otherwise, we would have to write it like this, which is slightly less readable:

Further in this course you will see other advantages of splitting strings. In python it doesn't matter whether you are using single quotes (') or double ones ("), either will be fine.

Leave Comment

Your email address will not be published.

Please type the characters of this captcha image in the input box

Please type the characters of this captcha image in the input box