Literals

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.

Numbers

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:

print(10)

Copy the print instruction into the cell below and run it:

[ ]:

The print function is given an expression enclosed in parentheses ( and ) as input and displays the expression in a readable format. We will use the print fucntion a lot in this class so it’s a good idea to start becoming familiar with the print function. The most important thing to notice right now is that what you want printed is placed in between the parentheses.

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:

print(100)
print(3.14)
[ ]:

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:

print(1e9)

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

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 True and 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 print function to print the words True and False. Enter this example into the cell below:

print('Roses are', False, 'violets are', True)
[ ]:

Literally Nothing!

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:

print(None)
[ ]:

Literals and Jupyter

When you execute a cell in Jupyter the notebook automatically prints that last literal it sees as a convinience. In a lot of cases this means you don’t need to have a print statement to see the value of something in the notebook. Try putting the code below into the next cell:

"Roses are", False
[ ]:

Now try this:

print("Roses are", False)
[ ]:

Notice the difference?