Python Literals


Literal can be defined as raw data given to a variable or constant. There are different types of literals:

  • Numeric literals
  • String literals
  • Boolean literals
  • Special literals
  • Literal Collections

In the below sections, we will learn more about every different Python literals.


Numeric Literals

Numeric Literals are immutable (unchangeable). Numeric Literals can belong to 3 different numerical types:

  • Integer
  • Float
  • Complex

How to use Numeric Literals in Python?

In the following example, we will use different Numeric Literals.

a = 0b1011 #Binary Literals
b = 100 #Decimal Literal 
c = 0o324 #Octal Literal
d = 0x11b #Hexadecimal Literal

#Float Literal
float_1 = 11.5 
float_2 = 2.6e2

#Complex Literal 
x = 3.14j

print(a, b, c, d)
print(float_1, float_2)
print(x, x.imag, x.real)

Output

11 100 212 283 
11.5 260.0 
3.14j 3.14 0.0

In the above code,

  • Integer literals are assigned into variables a (binary literal), b (decimal literal), c (octal literal), d (hexadecimal literal).
  • All the Numeric Literals are converted into decimal values when printed.
  • 11.5 and 2.6e2 are floating-point literals. 2.6e2 is represented with exponential and is equal to 2.6 * 10^2.
  • 3.14j is a complex literal that we assigned to variable X. We used imaginary literal (x.imag) and real literal (x.real) to build imaginary and real parts of complex numbers.

String Literals

A string literal is a sequence of characters surrounded by quotes. We can use single, double, or triple quotes for a String.

A character literal is a single character surrounded by single or double-quotes.


How to use String Literals in Python?

In the following example, we will use different ways to declare String Literals.

text = "Hello World!"
char = "A"
multiline_text = """This is a multiline
text with more than one line of code"""
unicode_text = u"\u00dcnic\u00f6de"
raw_text = r"raw \n text"

print(text)
print(char)
print(multiline_text)
print(unicode_text)
print(raw_text)

Output:

Hello World! 
A 
This is a multi-line text with more than one line of code 
Ünicöde 
raw \n text

In the above code:

  • We assigned a string literal Hello World! to a variable named text.
  • We assigned a character literal A to a variable named char.
  • We used a triple-quotes """ to assign a multi-line string literal to a multiline_text variable.
  • The text u"\u00dcnic\u00f6de" is a Unicode Literal that supports characters other than English.
  • We assigned a raw string literal r"raw \n text to raw_text variable.

Boolean Literals

A Boolean Literal can have one of the two values: True or False.


How to use Boolean Literals in Python?

a = (True == 1)
b = (False == 1)
x = True + 5
y = False + 11

print("a is", a)
print("b is", b)
print("x:", x)
print("y:", y)

Output:

a is True
b is False
x: 6
y: 11

In the above code:

  • We use boolean literal True and False. In Python, True represents the value as 1 and False as 0.
  • The value of the a variable is True because 1 is equal to True. And the value of b is False because 1 is not equal to False.
  • We can also use the True and False in numeric expressions as the value. True is interpreted as 1 and False as 0. This is why the value of x is 6 (because True + 5 is equal to 1 + 5) and y is 12 (beacause False + 11 is equal to 0 + 11).

Special Literals

Python contains one special literal, which is None.

None is used to specify that the field has not been created. It is also used for the end of lists in Python.


How to use Special Literals in Python?

In the following example, we assigned None to b variable.

a = "Hello"
b = None 

print(a)
print(b)

Output:

Hello
None

Literal Collections

Python provides four different literal collections: List literals, Dictionary literals, Tuple literals, Set literals.


How to use Literals Collections in Python?

In the following example, we will create a list of vegetables, a tuple of numbers, a dictionary vegetables_dict, and a set of vowels.

vegetables = ["carrots", "broccoli", "mushrooms"] #list
alphabets = {'c':'carrots', 'b':'broccoli', 'm':'mushrooms'} #dictionary
numbers = (5, 6, 7) #tuple
vowels = {'a', 'e', 'i' , 'o', 'u'} #set

print(vegetables)
print(numbers)
print(alphabets)
print(vowels)

After executing the above code, the output will be as follows:

['carrots', 'broccoli', 'mushrooms'] 
(5, 6, 7) 
{'c': 'carrots', 'b': 'broccoli', 'm': 'mushrooms'} 
{'i', 'u', 'a', 'e', 'o'}

If you want to learn more about literal collections, you can check Python Data Types.



ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.