Home > Articles

Understanding Python Basics

  • Print
  • + Share This
In this chapter from Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours, 2nd Edition, you will learn some Python basics, such as using the print function to display output. You will read about using variables and how to assign values to variables, and you will gain an understanding of their data types. By the end of this chapter, you will know how to get data into a script by using the input function and write your first Python script!
This chapter is from the book

In this hour, you 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 values to variables, 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 write 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 get instant feedback on your Python statements from the Python interactive interpreter and can 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 simply enter a single Python statement to perform a task for you.

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)

The argument portion of the print function can be characters, such as ABC or 123. It also can be values stored in variables. You 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 resulting output in 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.
>>>

Sometimes you need to output a character string that contains a single quote mark to show possession or a contraction. In such a case, you should 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.
>>>

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

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 to use triple quotes to embed a linefeed character (via pressing the Enter key). When the output is displayed, each embedded linefeed character causes the next sentence to appear on the next line. Thus, the linefeed moves your output to the next new line. Notice that you cannot see the linefeed character embedded on each code line—you can see only 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 using triple quotes, you also can protect single and double quotes that need to be displayed in the output. Listing 4.6 shows triple quotes in action 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 character or 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 \n escape sequence. The \n escape sequence forces any characters listed after it onto the displayed output’s next line. This escape sequence is called a newline, and the formatting character it inserts is a linefeed. Listing 4.7 shows an example of using \n to insert a linefeed. Notice that it causes the output to be formatted exactly as it was in Listing 4.5 using triple quotes.

Listing 4.7 Using an Escape Sequence to Add a Linefeed

>>> print("This is line one.\nThis is line two.\nThis is line three.")
This is line one.
This is line two.
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 \n escape sequence addition.

You also can 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 form feed into the output

\n

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, but 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, “Setting Up a Programming Environment.” You can display Unicode characters by using the \u escape sequence. Each Unicode character is represented by a hexadecimal number. These hexadecimal numbers are found at www.unicode.org/charts. There are lots of Unicode characters!

The Unicode 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 π!
>>>
  • + Share This
  • 🔖 Save To Your Account