In this chapter 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.
What you will learn in this:
- How to produce output from a script
- Making a script readable, print function
- 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.
- 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:
- About Standard Library of Functions:
- The print function is called a built-in function because it is part of the Python standard functions library. 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 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.
Using a Pair of Single Quotes to Enclose Characters:
In above it shows the use of double quotes with the print function.
Using a Pair of Double Quotes to Enclose Characters:
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.
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.