Get a wonderful overview of Python basics, including output and formatting output from Python; creating legal variable names and assigning values to variables; and various data types and when they are assigned by Python. Learn how Python can handle input from the keyboard and how to convert the data types of the variables receiving that input.



What You’ll Learn in This Hour: How to produce output from a script

Making a script readable

How to use variables

Assigning value to variables

Types of data

How to put information into a script

In this hour, you will get a chance to learn some Python basics, such as using the print function to display output. You will read about using variables and how to assign them values, and you will gain an understanding of their data types. By the end of the hour, you will know how to get data into a script by using the input function, and you will be writing your first Python script!

Producing Python Script Output

Understanding how to produce output from a Python script is a good starting point for those who are new to the Python programming language. You can get instant feedback on your Python statements from the Python interactive interpreter and gently experiment with proper syntax. The print function, which you met in Hour 3, “Setting Up a Programming Environment,” is a good place to focus your attention.

Exploring the print Function

A function is a group of python statements that are put together as a unit to perform a specific task. You can simply enter a single Python statement to perform a task for you.

By the Way: The “New” print Function In Python v2, print is not a function. It became a function when Python v3 was created.

The print function’s task is to output items. The “items” to output are correctly called an argument. The basic syntax of the print function is as follows:

print (argument)

Did You Know: Standard Library of Functions The print function is called a built-in function because it is part of the Python standard library of functions. You don’t need to do anything special to get this function. It is provided for your use when you install Python.

The argument portion of the print function can be characters, such as ABC or 123 . It can also be values stored in variables. You will learn about variables later in this hour.

Using Characters as print Function Arguments

To display characters (also called string literals) using the print function, you need to enclose the characters in either a set of single quotes or double quotes. Listing 4.1 shows using a pair of single quotes to enclose characters (a sentence) so it can be used as a print function argument.

LISTING 4.1 Using a Pair of Single Quotes to Enclose Characters

>>> print ('This is an example of using single quotes.') This is an example of using single quotes. >>>

Listing 4.2 shows the use of double quotes with the print function. You can see that the output that results from both Listing 4.1 and Listing 4.2 does not contain the quotation marks, only the characters.

LISTING 4.2 Using a Pair of Double Quotes to Enclose Characters

>>> print ("This is an example of using double quotes.") This is an example of using double quotes. >>>

By the Way: Choose One Type of Quotes and Stick with It If you like to use single quotation marks to enclose string literals in a print function argument, then consistently use them. If you prefer double quotation marks, then consistently use them. Even though Python doesn’t care, it is considered poor form to use single quotes on one print function argument and then double quotes on the next. This makes the code hard for humans to read.

Sometimes you need to output a string of characters that contain a single quote to show possession or a contraction. In such a case, you use double quotes around the print function argument, as shown in Listing 4.3.

LISTING 4.3 Protecting a Single Quote with Double Quotes

>>> print ("This example protects the output's single quote.") This example protects the output's single quote. >>>

At other times, you need to output a string of characters that contain double quotes, such as for a quotation. Listing 4.4 shows an example of protecting a quote, using single quotes in the argument.

LISTING 4.4 Protecting a Double Quote with Single Quotes

>>> print ('I said, "I need to protect my quotation!" and did so.') I said, "I need to protect my quotation!" and did so. >>>

Did You Know: Protecting Single Quotes with Single Quotes You can also embed single quotes within single quote marks and double quotes within double quote marks. However, when you do, you need to use something called an “escape sequence,” which is covered later in this hour.

Formatting Output with the print Function

You can perform various output formatting features by using the print function. For example, you can insert a single blank line by using the print function with no arguments, like this:

print ()

The screen in Figure 4.1 shows a short Python script that inserts a blank line between two other lines of output.

FIGURE 4.1 Adding a blank line in script output.

Another way to format output using the print function is via triple quotes. Triple quotes are simply three sets of double quotes.

Listing 4.5 shows how you can use triple quotes to embed a linefeed character by pressing the Enter key. When the output is displayed, each embedded linefeed character causes the next sentence to appear on the next line. Thus, linefeed moves your output to the next new line. Notice that you cannot see the linefeed character embedded on each line in the code; you can only see its effect in the output.

LISTING 4.5 Using Triple Quotes

>>> print ("""This is line one. ... This is line two. ... This is line three.""") This is line one. This is line two. This is line three. >>>

By the Way: But I Prefer Single Quotes Triple quotes don’t have to be three sets of double quotes. You can use three sets of single quotes instead to get the same result!

By using triple quotes, you can also protect single and double quotes that you need to be displayed in the output. Listing 4.6 shows the use of triple quotes to protect both single and double quotes in the same character string.

LISTING 4.6 Using Triple Quotes to Protect Single and Double Quotes

>>> print ("""Raz said, "I didn't know about triple quotes!" and laughed.""") Raz said, "I didn't know about triple quotes!" and laughed. >>>

Controlling Output with Escape Sequences

An escape sequence is a series of characters that allow a Python statement to “escape” from normal behavior. The new behavior can be the addition of special formatting for the output or the protection of characters typically used in syntax. Escape sequences all begin with the backslash ( \ ) character.

An example of using an escape sequence to add special formatting for output is the

escape sequence. The

escape sequence forces any characters listed after it onto the next line of displayed output. This is called a newline, and the formatting character it inserts is a linefeed. Listing 4.7 shows an example of using the

escape sequence to insert a linefeed. Notice that it causes the output to be formatted exactly as it was Listing 4.5, with triple quotes.

LISTING 4.7 Using an Escape Sequence to Add a Linefeed

>>> print ("This is line one.

This is line two.

And this is line three.") This is line one. This is line two. And this is line three. >>>

Typically, the print function puts a linefeed only at the end of displayed output. However, the print function in Listing 4.7 is forced to “escape” its normal formatting behavior because of the addition of the

escape sequence.

Did You Know: Quotes and Escape Sequences Escape sequences work whether you use single quotes, double quotes, or triple quotes to surround your print function argument.

You can also use escape sequences to protect various characters used in syntax. Listing 4.8 shows the backslash ( \ ) character used to protect a single quote so that it will not be used in the print function’s syntax. Instead, the quote is displayed in the output.

LISTING 4.8 Using an Escape Sequence to Protect Quotes

>>> print ('Use backslash, so the single quote isn\'t noticed.') Use backslash, so the single quote isn't noticed. >>>

You can use many different escape sequences in your Python scripts. Table 4.1 shows a few of the available sequences.

TABLE 4.1 A Few Python Escape Sequences

Escape Sequence Description \' Displays a single quote in output. \" Displays a double quote in output. \\ Displays a single backslash in output. \a Produces a “bell” sound with output. \f Inserts a formfeed into the output.

Inserts a linefeed into the output. \t Inserts a horizontal tab into the output. \u#### Displays the Unicode character denoted by the character’s four hexadecimal digits (####).

Notice in Table 4.1 that not only can you insert formatting into your output, you can produce sound as well! Another interesting escape sequence involves displaying Unicode characters in your output.

Now for Something Fun!

Thanks to the Unicode escape sequence, you can print all kinds of characters in your output. You learned a little about Unicode in Hour 3. You can display Unicode characters by using the \u escape sequence. Each Unicode character is represented by a hexadecimal number. You can find these hexadecimal numbers at www.unicode.org/charts. There are lots of Unicode characters!

The hexadecimal number for the pi (∏) symbol is 03c0. To display this symbol using the Unicode escape sequence, you must precede the number with \u in your print function argument. Listing 4.9 displays the pi symbol to output.

LISTING 4.9 Using a Unicode Escape Sequence

>>> print ("I love my Raspberry \u03c0!") I love my Raspberry π! >>>