-
Book Overview & Buying
-
Table Of Contents
Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)
By :
Now we will look at writing a Python script in a file and executing it. For this we will use IDLE as it will provide syntax highlighting on the code. However, any text editor (for example, LeafPad, GEdit, nano, vim) can be used to write the Python files.

time module and prints a string containing the current time to the terminal.import time print "The current time is: " + time.ctime()
Keep in mind that the indentation level in Python is very important as this defines the scope that a line of code fits into. This will become clearer later on in the book when we start writing more complex codes.
time.py in the home directory by selecting Save from the File menu.Now that the script is saved, we can execute it using the Python executable at the command line.
python time.py
This will give the following output to the terminal:

One small improvement that could be made to this process is to include a shebang in the Python script that will tell the shell what to use to execute the script. This way we do not have to explicitly include the Python command when we run the script.
#!/usr/bin/env python
chmod a+x time.py
./time.py
This gives the following output on the terminal:

Change the font size
Change margin width
Change background colour