The Python Book,, The Ultimate Guide To Coding With Python


Download Read Online

                          Introduction 


The Python Book

If you were using a graphical editor such as edit, then you would only have to do the last step of making the file executable.


You should only have to mark the file as executable once. You can freely edit the file once it is executable.


Interpreted vs compiled languages

An interpreted language such as Python is one where the source code is converted to machine code and then executed each time the program runs.


This is different from a compiled language such as C, where the source code is only converted to machine code once – the resulting machine code is then executed each time the program runs.


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 directory.


It’s probably a good idea to make a directory for the fi les 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 fi le using the command ‘touch’ followed by the fi rename. Our expert used the command touch hello_world.py.


The final and most important part of setting up the fi le is making it executable. This allows us to run code inside the hello_world.py fi le.


We do this with the command chmod +x hello_world.py. Now that we have our fi le set up, we can go ahead and open it up in nano, or any text editor of your choice.


Edit 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.


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 hash bang) followed 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 from the console.


You can save the changes you’ve just made to the fi le in nano using the key combination Ctrl+O, followed by Enter. Use Ctrl+X to exit nano.

Customer Reviews