Python Basics - Python (2016)

Python (2016)

CHAPTER 5: Python Basics

The Shell

There are many ways to call it—command prompt, shell, prompt, command line, CLI—but they all pertain to the same entity. This is one of the mediums of communication between the user and the Python interpreter. For a good part of this book, most of the Python codes seen in the examples will be done in the shell.

When starting up the shell, do the same thing you did for testing whether or not your machine has Python on it—namely, pull up the CLI and enter “python”. You will see the details we laid out above—the version number, date of build, compiler used for the build, the computer architecture, as well as the operating system. The information will be useful for debugging, as well as for checking which version you have on hand.

From the initial text display, one of the things you will need to pay attention to is the set of three greater than signs (“>>>”). This will tell you that a prompt is present, asking for input. This prompt will always be shown in the examples we will be using, so it is not something you will have to type out.

For example, here is a code with the prompt displayed:

>>>print(“Hello, world!”)

Hello, world!

As with the information above, the new line that appears without the prompt is the output. You will also notice that the prompt will change into an ellipsis (“...”) when you enter a command that has indentations. You will have to be careful not to see this as part of the actual code:

>>>if True:

... Print(“Hello, world!”

...

Hello, world!

The Script

Once you have got the hang of Python programming, you will find that you find it tedious to repeatedly type the same programs into the shell. Like all its sibling interpreted programming languages, you will be able to store and execute the Python code directly from a file.

There are two different types of Python files—plain text and bytecode files. “Plain text” files are so-called since they are simple files that are created and edited using any simple text editor. This may be the Windows-standard Notepad, or a downloaded utility such as Notepad++, Sublime, Kate, or even Python’s very own IDLE or IDE. The bytecode file, o the other hand, is a compiled CPython file. We will go into more detail about these files later on in the book.

Then, there is the third type of file—the Python script. This is usually suffixed with the extension “.py”. It is as executable as anything you type in the Python shell when it is executed by itself—this goes for most operating systems (Windows, Mac, and major Linux distributions included). Here is an example of a very simple script that we can title “greeting.py”:

Print(“Hi there! I am a Python script!”)

Input(“Press enter to continue...”)

Take note that things may be a little different depending on whether you are using Linux, Mac, or Windows. For users of Linux, Mac, and other operating systems derived from UNIX, you will need to include a “shebang” at the beginning of the script. For Python, you will need to include the following line:

#!/usr/bin/env python

This line tells the shell where the interpreter lies. One good thing about this is that it starts with the hash tag (#). This is ignored as a comment on other operating systems whose scripts do not require the use of a shebang. This means that you can take any code written in Mac or Linux and execute it in Windows without worrying about the Windows interpreter misinterpreting the shebang!

For Windows users, you will have to remember that Python files have to be executed with py.exe. This will start by default with the Python 2.X branch, if installed on your computer. Executing “py” should tell you which version you have running. If you currently have 2.X but are in need of the 3.X branch, you will need to us a switch—“-3”.

In order to make Python 3.X as the default version, you will need to open up the control panel, then create an environment variable “PY_PYTHON”. Set the value of this variable to 3. Rolling back to 2.X is as simple as deleting PY_PYTHON and changing the value to 2.

Running the Python script can be done either by clicking on it or by running it from the system’s shell, regardless of which operating system you are using. The script sample above, for example, should give you the lines “Hi there, I am a Python script!” and “Press enter to continue...”. The script will then pause up until you actually press the Enter key.

If you tried this, then congratulations! You have just built your very first Python script!

Another note on saving Python scripts in Windows, there are some users who might not be allowed to save directly to a “.py” extension. In this case, you can create a text file then just save it with .py afterwards. Or, you can also change the “Hide extensions for known file types” toggle in your Control Panel. This can be found under the Folder Options. Other more advanced options are available, but these should suffice for our current purpose.

IDLE

Earlier, we talked about IDLE, or the Integrated Development Environment. This is the default IDE that is installed when one gets CPython. On the Windows OS, this may be accessed by right-clicking any Python scrips, then selecting “Edit with IDLE” on the context menu. You can also open up IDLE directly from the CLI by typing “python -m idlelib”.

When using IDLE, a couple of things will be noticeable. First, you will find that the code is colored—strings are green, comments are in red, functions (built-in) are purple, and so forth. This will allow you to have a visual cue of certain parts of the code, a significant advantage over mono-colored text. Second, you will also find that IDLE would make automatic indentations where needed. It will also modify the function of the Tab key—inserting four spaces when pressed.

For those just starting out, these default settings are perfect for learning the ropes. However, your preferences may change when you have reached the intermediate to advanced stages. To meet these changing requirements, the creators of IDLE made it into a very customizable module. For example, the red-and-green distinction may not work too well for a color-blind person, so the colors can be swapped out for something else. The default settings can be changed in the Options menu, in the menubar.

Basics and Jargon

Now that you know the very basics and the tools available to you, it is time to tackle some of the more commonly-mentioned jargons and other stuff. More advanced items will be tackled later on, but pay close attention to these starters:

1. In Python, a “comment” is any text preceded by a hashtag (#). This is meant to be a sentence that is readable by humans (e.g., a note for yourself or for a fellow programmer) but unreadable by Python. For example, writing something that says “# For your eyes only” will appear on the code but will be ignored by Python upon interpretation.

2. Once you start seeing an ellipsis (...) or three greater-than signs (>>>), then this will mean that you are dealing with the Python shell.

3. “Variables” are namespaces such as “somevar” and “var” (or anything you define). It can hold Python data types, such as numbers or strings. More about this will be discussed later.

4. If you are following the steps in this book to the letter but keep on seeing error messages, double-back and check the Python version you are using. Remember that the exercises here are meant to work on Python 3.X.

5. Don’t confuse the term “Python script” with the actual programming we are about to do. Essentially, the Python script is just a plain text file that has a Python code in it.