Literals

A literal is data whose values are determined by the literal itself. 

What does this mean?  It means that literals are a visible way to write a value.  You can use literals to encode data and insert them into your code.  Below we will look at different types of literals and how they can be used in our programs.

We are going to look at two numeric types in Python, integers and floats.

  • Please note, if you are working with negative numbers use the "-" symbol in front of your numeric value, ex. print(-2).  Positive numeric values do not require a "+" symbol, but you can use them if you choose, ex. print(+5) or print(5) will output to the same positive value.


An integer is a whole number, positive or negative, without decimals.  For example 5, 4025698, and -123 are all examples of integers.  Two other types of integers include octal numbers and hexadecimal numbers.

Octal Numbers: The octal constant in Python 3 is 0o (zero-o) and means that numbers are represented in base 16.

Try this: print(0o34)

*The compiler calculates this input by multiplying the first digit by 8 and then adding the second digit.

Hexadecimal numbers: The hexadecimal constant in Python 3 is 0x (zero-x) and means that numbers are represented in base 16.

Try this: print(0x23)

*The compiler calculates this input by multiplying the first digit by 16 (the hexadecimal base) and then adding the second digit.


A float is a number, positive or negative, containing one or more decimals.  The numeric values 2.40, 1.0, and -42.84 are all examples of floats.  Floats may also be scientific notation, which is represented by an "E" or "e" to indicate the power of 10. 

Try this: print(2.5E4)

print(2.5E4) → 2.5 x104 → 25000

Although we have already worked with strings in this course so far, it is important to note that a string literal can be displayed with the print() function.  String literals in Python are surrounded by double quotation marks.

Example:

Input: print("Hello world")

Output: Hello world

Just remember that anything written within the double quotation marks are taken literally (as exactly written) by the Python compiler.

In Python, boolean values are represented by one of two variables: True or False.  They are used to represent truth values, meaning that a boolean expression will evaluate if a statement is true or false (two possible states). 

* Please notes that capitalization is important, since 'true' and 'false' are not boolean values (remember that Python is case sensitive).

6 Common Comparison Operators


Operator
Meaning
x = = y *
x is equal to y
x != y
x is not equal to y
x > y
x is greater than y
x < y
x is less than y
x >= y
x is greater than or equal to y
x <= y
x is less than or equal to y
*Keep in mind that the equality operator, ==, compares two values and produces a boolean value.  Don't confuse it with  a single equal sign, =, which assigns a value to a variable.


Practice

Objectives:

  • Become familiar with using literals in Python code.


Classify each example below as an integer or a float:

a) 10.25

b) 4532

c)  -4

d)  12E3

Predict each output for the following inputs:

a)  print(0o25)

b) print(1.2E3)

c) print(0x26)

Compare two values using the Boolean operators above. Predict the outcomes prior to running your input.