A literal is a specific written value in your program. Literals get their name because they literally mean what they are. Numbers are literals in Pyhon and so are words and phrases written in a human language. In this lesson you will learn how to communicate numbers and words to your program. You will also learn about other important literals in the Python language.
You can write a number directly into your program. For example, if you want Python to print the number
10 you can execute this instruction:
Copy the print instruction into the cell below and run it:
expression enclosed in parentheses
) as input and displays the expression in a readable format. We will use the
Python understands integers, numbers with no decimal place and floating point numbers, which contain a decimal. Copy these statements into the cell below and run them:
Python also understands scientific notation. That’s where the nubmer is expressed as a decimal that is multiplied by a power of 10. For example 1 billion is:
\(1,000,000,000 = 1 x 10^9\)
You can print one billion in like this:
You can use the notation to represent fractions too.
print("A nanosecond is", 1e-9, "seconds")
Copy the two print statements into the cell below and run them. Make sure there is no error and that you see printed the correct numbers.
Strings are a group of letters. There can be any number of letters in a string (including zero). Python detects the beginning and end of a string by looking for a quote character. There are different kinds of quotes. They are discussed in Quotes. The most essential quotes are double quotes and single quotes and they are just two ways of doing the same thing. Copy these two examples into the cell below:
print("Double Quote") print('Single Quote')
Do you see any difference in the way Python prints the words? There isn’t! Double and single quotes do the same thing. Why have both? The double and single quote are very common in English so you sometimes need to pick quotes based on what you have to say. Enter these examples into the cell below:
print("I can't do that, Dave.") print('"Brevity is the soul of wit." --William Shakespeare')
What would happen if you changed double quotes to single quotes in the first print example?
True or False?¶
Python has special literals for the values
False. The use of true and false will become important later when we talk about conditions and loops. For now think of them as a special way to get the
False. Enter this example into the cell below:
print('Roses are', False, 'violets are', True)
All of the other literals you’ve learned so far exist in every other programming language. The
None literal is unique to Python. It’s means “no value.” Printing
None is exactly like telling Python to “print nothing.” See what happens when you print None in the cell below:
Literals and Jupyter¶
When you execute a cell in Jupyter the notebook automatically prints that last literal it sees as a convenience. In a lot of cases this means you don’t need to have a
"Roses are", False
Now try this:
print("Roses are", False)
Notice the difference?