- Producing Python Script Output
- Formatting Scripts for Readability
- Understanding Python Variables
- Assigning Value to Python Variables
- Learning About Python Data Types
- Allowing Python Script Input
- Summary
- Q&A
- Workshop
Assigning Value to Python Variables
Assigning a value to a Python variable is fairly straightforward. You put the variable name first, then an equal sign (=), and finish up with the value you are assigning to the variable. This is the syntax:
variable = value
Listing 4.13 creates the variable coffee_cup and assigns a value to it.
LISTING 4.13 Assigning a Value to a Python Variable
>>> coffee_cup = 'coffee' >>> print (coffee_cup) coffee >>>
As you see in Listing 4.13, the print function can output the value of the variable without any quotation marks around it. You can take output a step further by putting a string and a variable together as two print function arguments. The print function knows they are two separate arguments because they are separated by a comma (,), as shown in Listing 4.14.
LISTING 4.14 Displaying Text and a Variable
>>> print ("My coffee cup is full of", coffee_cup) My coffee cup is full of coffee >>>
Formatting Variable and String Output
Using variables adds additional formatting issues. For example, the print function automatically inserts a space whenever it encounters a comma in a statement. This is why you do not need to add a space at the end of My coffee cup is full of, as shown in Listing 4.14. There may be times, however, when you want something else besides a space to separate a string of characters from a variable in the output. In such a case, you can use a separator in your statement. Listing 4.15 uses the sep separator to place an asterisk (*) in the output instead of a space.
LISTING 4.15 Using Separators in Output
>>> coffee_cup = 'coffee' >>> print ("I love my", coffee_cup, "!", sep='*') I love my*coffee*! >>>
Notice you can also put variables in between various strings in your print statements. In Listing 4.15, four arguments are given to the print function:
- The string "I love my"
- The variable coffee_cup
- The string "!"
- The separator designation '*'
The variable coffee_cup is between two strings. Thus, you get two asterisks (*), one between each argument to the print function. Mixing strings and variables in the print function gives you a lot of flexibility in your script’s output.
Avoiding Unassigned Variables
You cannot use a variable until you have assigned a value to it. A variable is created when it is assigned a value and not before. Listing 4.16 shows an example of this.
LISTING 4.16 Behavior of an Unassigned Variable
>>> print (glass) Traceback (most recent call last): File "<stdin>", line 1, in <module> Name Error: name 'glass' is not defined >>> >>> glass = 'water' >>> print (glass) water >>>
Assigning Long String Values to Variables
If you need to assign a long string value to a variable, you can break it up onto multiple lines by using a couple methods. Earlier in the hour, in the “Formatting Scripts for Readability” section, you looked at using the print function with multiple lines of outputted text. The concept here is very similar.
The first method involves using string concatenation (+) to put the strings together and an escape character (\) to keep a linefeed from being inserted. You can see in Listing 4.17 that two long lines of text were concatenated together in the assignment of the variable long_string.
LISTING 4.17 Concatenating Text in Variable Assignment
>>> long_string = "This is a really long line of text" +\ ... " that I need to display!" >>> print (long_string) This is a really long line of text that I need to display! >>>
Another method is to use parentheses to enclose your variable’s value. Listing 4.18 eliminates the +\ and uses parentheses on either side of the entire long string in order to make it into one long string of characters.
LISTING 4.18 Combining Text in Variable Assignment
>>> long_string = ("This is a really long line of text" ... " that I need to display!") >>> print (long_string) This is a really long line of text that I need to display! >>>
The method used in Listing 4.18 is a much cleaner method. It also helps improve the readability of the script.
More Variable Assignments
The value of a variable does not have to only be a string of characters; it can also be a number. In Listing 4.19, the number of cups consumed of a particular beverage are assigned to the variable cups_consumed.
LISTING 4.19 Assigning a Numeric Value to a Variable
>>> coffee_cup = 'coffee' >>> cups_consumed = 3 >>> print ("I had", cups_consumed, "cups of", ... coffee_cup, "today!") I had 3 cups of coffee today! >>>
You can also assign the result of an expression to a variable. The equation 3+1 is completed in Listing 4.20, and then the value 4 is assigned to the variable cups_consumed.
LISTING 4.20 Assigning an Expression Result to a Variable
>>> coffee_cup = 'coffee' >>> cups_consumed = 3 + 1 >>> print ("I had", cups_consumed, "cups of", ... coffee_cup, "today!") I had 4 cups of coffee today! >>>
You will learn more about performing mathematical operations in Python scripts in Hour 5, “Using Arithmetic in Your Programs.”
Reassigning Values to a Variable
After you assign a value to a variable, the variable is not stuck with that value. It can be reassigned. Variables are called variables because their values can be varied. (Say that three times fast.)
In Listing 4.21, the variable coffee_cup has its value changed from coffee to tea. To reassign a value, you simply enter the assignment syntax with a new value at the end of it.
LISTING 4.21 Reassigning a Variable
>>> coffee_cup = 'coffee' >>> print ("My cup is full of", coffee_cup) My cup is full of coffee >>> coffee_cup = 'tea' >>> print ("My cup is full of", coffee_cup) My cup is full of tea >>>