Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Python Essentials
  • Table Of Contents Toc
  • Feedback & Rating feedback
Python Essentials

Python Essentials

By : Steven F. Lott
4.5 (2)
close
close
Python Essentials

Python Essentials

4.5 (2)
By: Steven F. Lott

Overview of this book

This book is designed for Python 2 developers who want to get to grips with Python 3 in a short period of time. It covers the key features of Python, assuming you are familiar with the fundamentals of Python 2.
Table of Contents (17 chapters)
close
close
16
Index

Creating simple script files

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:

  • The content must be plain text. While ASCII encoding is preferred by some, Python 3 can easily handle UTF-8 and most OS-specific variations such as Mac OS Roman or Windows CP-1252. A portable encoding like UTF-8 is strongly suggested.
  • Python can cope with Mac OS X, Linux newline (\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.
  • The filename should be a legal Python identifier. This is not a requirement, but it gives us considerable flexibility if we follow this suggestion. The Language Reference Manual, section 2.3, provides the details of what constitutes an identifier. A summary of these rules is that identifiers must begin with a letter (or a Unicode character that normalizes to a letter) or _. 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 _.
  • The filename extension should be .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.

Simplified syntax rules

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:

  • Almost all statements begin with a Python keyword such as pass, if, and def. The expression statement and the assignment statement are the exceptions.
  • Python has two kinds of statements—one-line simple statements and multiline compound statements.
  • Simple statements must be complete within a single line. An assignment statement is a simple statement. It begins with one or more user-provided identifiers and includes the = assignment symbol or an augmented variant like +=. An expression statement is also simple.
  • Compound statements use indentation to show the suite of statements embedded within the overall statement. The standard indentation is four spaces. Most developers set their editor to replace tabs with four spaces. Inconsistent use of spaces and tabs will lead to syntax errors that can be hard to see because tabs and spaces are both invisible by default. Avoiding tab characters in general makes it easier to debug problems.
  • Compound statements include class and function definitions—the body of the definition is indented. If statements and for and while loops are examples of compound statements that contain an indented suite of statements that are executed conditionally or repeatedly.
  • The ( 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
)

Tip

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.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Python Essentials
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon