-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Python Essentials
By :
While we can use all of Python from the REPL, this is not a good way to produce a final application. Most of what we do with Python will be done via script files. We'll look at script files in detail in Chapter 12, Scripts, Modules, Packages, Libraries, and Applications. For now, we'll look at a few features.
A script file has to follow a few rules:
\n), as well as Windows CR-LF (\r\n). Only a few Windows tools, such as Notepad, insist on CR-LF line endings; most other programming editors discern the line endings flexibly. Unless you really must use Notepad, it's often best to use Unix-style newline line endings._. It continues with letters, digits, and the _ character. What's important is that we should avoid characters that are Python operators or delimiters in filenames. In particular, we should avoid the hyphen (-), which can become a problem in some Python contexts. OS filenames have much more flexible rules than Python identifiers, and the OS has ways to escape the meaning of OS-related punctuation; we are happiest when we limit our filenames to valid Python identifiers – letters, digits, and _..py. Again, this is not required, but it is very helpful to follow this rule.For example, we'll try to focus on names such as test_1_2.py. We can't as easily use a file named test-1.2.py; the base name isn't a valid identifier—this name looks like a Python expression. While the second name is acceptable for a top-level script, it won't work as a module or package.
We'll look at some Python syntax rules in the next section. For now, we can create a simple script file named ex_1.py that has one line:
print("π≈", 355/113)We can also use "\u03c0\u2248" instead of "π≈". The string "\N{GREEK SMALL LETTER PI}\N{ALMOST EQUAL TO}" will also work.
Once we have this file, we can have Python execute the file as follows:
MacBookPro-SLott:Chapter_1 slott$ python3 ex_1.py π≈ 3.1415929203539825
We've provided a filename, ex_1.py, as the positional argument to the python3 program. Python reads the file and executes each line. The output that we see is the text printed to the console by the print() function.
The file is found by Python using ordinary OS rules for locating files, starting with the current working directory. This will work with any kind of filename.
If we followed the naming rules for our file—the filename is an identifier and the extension is .py—we can also use the following command to execute a Python module:
MacBookPro-SLott:Chapter_1 slott$ python3 -m ex_1 π≈ 3.1415929203539825
The -m ex_1 option forces Python to search for a module named ex_1. The file associated with this module is named ex_1.py. Python has a search path that it uses to find the requested module. Unless special arrangements are made, Python will search the local directory first, and then will search the library directories. This allows us to run our scripts and Python's built-in applications with a simple, uniform syntax. It also allows us to add our own applications and modules by modifying the PYTHONPATH environment variable.
We'll look at the search path in Chapter 12, Scripts, Modules, Packages, Libraries, and Applications. The detailed documentation for the search path is part of the site package.
The syntax rules for Python are defined in section 2 of the Python Language Reference manual. We'll look at the rules in detail in Chapter 3, Expressions and Output.
Python has about 20 kinds of statements. Here's a quick summary of the rules:
pass, if, and def. The expression statement and the assignment statement are the exceptions.= assignment symbol or an augmented variant like +=. An expression statement is also simple.( and ) characters must match. A single statement on a logical line may span multiple physical lines until the ( and ) characters match.In effect, Python programs consist of one-statement-one-line. The end of a line is the statement terminator. We have a few techniques for extending a statement. The most common technique is based on Python's requirement that the ( and ) characters must balance.
We can, for example, write code like this:
print( "Hello world", "π≈", 355/113 )
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
We've spread a single logical line to four physical lines using ( and ). One consequence of this is that a simple statement that we enter at the REPL must not be indented. A leading space will cause problems because leading spaces are used to show which statements are inside a compound statement.
Another consequence of this is less direct. Python executes a script file one statement at a time from start to finish. This means that complex Python programs will have a number of definitions first, and the "main" part of the processing will generally be last.
A Python comment starts with # and ends at the end of the line. This follows the same rules as the various Linux shells. Because of the way Python documentation strings are processed by pydoc and help(), most documentation is actually presented in separate string literals at the start of a package, module, class, or function definition. We'll look at these documentation strings in Chapter 14, Fit and Finish – Unit Testing, Packaging, and Documentation. The # comment is used sparingly.