Friday, 5 January 2018

Understanding Python Basics



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!
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 at Infocampus
Exploring the print Function
A function is a group of Python statements that are put together as a unit to perform a specific task.
The print function’s task is to output items. The items to output are correctly called an argument.
The argument portion of the print function can be characters, such as ABC or 123. It also can be values stored in variables.
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.
Try It Yourself: Create Output with the print Function
This hour you have been reading about creating and formatting output by using the print function. Now it is your turn to try this versatile Python tool. Follow these steps:
  1. If you have not already done so, power up your Raspberry Pi and log in to the system.
  2. If you do not have the GUI started automatically at boot, start it now by typing startx and pressing Enter.
  3. Open a terminal by double-clicking the Terminal icon.
  4. At the command-line prompt, type python3 and press Enter. You are taken to the Python interactive shell, where you can type Python statements and see immediate results.
  5. At the Python interactive shell prompt (>>>), type print('I learned about the print function.') and press Enter.
  6. At the prompt, type print('I learned about single quotes.') and press Enter.
  7. At the prompt, type print("Double quotes can also be used.") and press Enter.
By the Way: Multiple Lines with Triple Double Quotes
In steps 8–10, you will not be completing the print function on one line. Instead, you will be using triple double quotes to enable multiple lines to be entered and displayed.
  1. At the prompt, type print("""I learned about things like... and press Enter.
  2. Type triple quotes, and press Enter.
  3. Type and displaying text on multiple lines.""") and press Enter. Notice that the Python interactive shell did not output the Python print statement’s argument until you had fully completed it with the closing parenthesis.
  4. At the prompt, type print('Single quotes protect "double quotes" in output.') and press Enter.
  5. At the prompt, type print("Double quotes protect 'single quotes' in output.") and press Enter.
  6. At the prompt, type print("A backslash protects \"double quotes\" in output.") and press Enter.
  7. At the prompt, type print('A backslash protects \'single quotes\' in output.') and press Enter. Using the backslash to protect either single or double quotes enables you to maintain your chosen method of consistently using single (or double) quotes around your print function argument.
  8. At the prompt, type print("The backslash character \\ is an escape character.") and press Enter.
  9. At the prompt, type print("Use escape sequences to \n insert a linefeed.") and press Enter. In the output, notice how part of the sentence, Use escape sequences to, is on one line and the end of the sentence, insert a linefeed., is on another line. This is due to your insertion of the escape sequence \n in the middle of the sentence.
  10. At the prompt, type print("Use escape sequences to \t\t insert two tabs or" and press Enter.
  11. At the . . . prompt, type "insert a check mark: \u2714") and press Enter.
You can do a lot with the print function to display and format output! In fact, you could spend this entire hour just playing with output formatting. However, there are additional important Python basics you need to learn, such as formatting scripts for readability.

No comments:

Post a Comment