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
and2.6e2
are floating-point literals.2.6e2
is represented with exponential and is equal to2.6 * 10^2
.3.14j
is a complex literal that we assigned to variableX
. 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 namedtext
. - We assigned a character literal
A
to a variable namedchar
. - We used a triple-quotes
"""
to assign a multi-line string literal to amultiline_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
toraw_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
andFalse
. In Python,True
represents the value as1
andFalse
as0
. - The value of the
a
variable isTrue
because1
is equal toTrue
. And the value ofb
isFalse
because1
is not equal toFalse
. - We can also use the
True
andFalse
in numeric expressions as the value.True
is interpreted as1
andFalse
as0
. This is why the value ofx
is6
(because True + 5 is equal to 1 + 5) andy
is12
(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.