Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1

LISTING 4.18 Combining Text in Variable Assignment


Click here to view code image


>>> 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.


By the Way: Assigning Short Strings to Variables
You can use parentheses for assigning short strings to variables, too! This is especially
useful if it helps you improve the readability of your Python 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


Click here to view code image


>>> 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


Click here to view code image


>>> 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

Free download pdf