Literals

1. Literals are notations for representing some fixed values in code. Python has various types of literals – for example, a literal can be a number (numeric literals, e.g., 123), or a string (string literals, e.g., “I am a literal.”).

2. The binary system is a system of numbers that employs 2 as the base. Therefore, a binary number is made up of 0s and 1s only, e.g., 1010 is 10 in decimal.

Octal and hexadecimal numeration systems, similarly, employ 8 and 16 as their bases respectively. The hexadecimal system uses the decimal numbers and six extra letters.

3. Integers (or simply ints) are one of the numerical types supported by Python. They are numbers written without a fractional component, e.g., 256, or -1 (negative integers).

4. Floating-point numbers (or simply floats) are another one of the numerical types supported by Python. They are numbers that contain (or are able to contain) a fractional component, e.g., 1.27.

5. To encode an apostrophe or a quote inside a string you can either use the escape character, e.g., 'I\'m happy.', or open and close the string using an opposite set of symbols to the ones you wish to encode, e.g., "I'm happy." to encode an apostrophe, and 'He said "Python", not "typhoon"' to encode a (double) quote.

6. Boolean values are the two constant objects True and False used to represent truth values (in numeric contexts 1 is True, while 0 is False.

 

EXTRA

There is one more, special literal that is used in Python: the None literal. This literal is a so-called NoneType object, and it is used to represent the absence of a value. We’ll tell you more about it soon.

 

Exercise 1

What types of literals are the following two examples?

"Hello ", "007"

They’re both strings/string literals.

 

Exercise 2

What types of literals are the following four examples?

"1.5", 2.0, 528, False

The first is a string, the second is a numerical literal (a float), the third is a numerical literal (an integer), and the fourth is a boolean literal.

 

Exercise 3

What is the decimal value of the following binary number?

1011

It’s 11, because (2**0) + (2**1) + (2**3) = 11

 

Ex. 4

Which of the following print statements displays the integer decimal 1000 (one thousand) on the screen?

print(1_0_0_0)

print(1,000)

print(01000)

print(1_000)

 

Ex. 5

What is the output of the following code snippet?

Correct answer

15

Ex. 6

What is the output of the following code snippet?


 

Correct answer

3 3 0.0