Python Literals
Literal is raw data that is mainly assigned to a variable or constant.
In Python, there are many types of literals that are as follows:
Numeric Literals:
Numeric Literals are unchangeable (immutable), meaning the value once assigned cannot be changed.
Numeric literals belong to 4 different numerical types:
- Numeric Literals - Numeric Literals: Numeric Literals are unchangeable which means the value once assigned cannot be changed. Numeric literals belong to 3 different numerical types: Integer, Float, and Complex.
- Long - Unlimited size followed by “L .“
- Float - Real Numbers with or without decimals.
- Complex - In the form of a + bi, where bi = imaginary part and a = real part.
NOTE:- Whenever you print the variables, all the literals are converted into decimal values.
String literals:
A string literal is a sequence of characters that are surrounded by quotes. We can use single and double or triple quotes to create a String.
The string is of two types:-
- Single-line string - A line that terminates in a single line is called a single-line string.
- Multi-line string - Texts which are written in multiple lines are called Multi-line string.
We can create a Multi-line string in two ways:-
By adding a slash “ \ ” at the end of the line.
E.g., var1 = “Hello \
World1”
By using triple quotation “ ‘“ “ marks
E.g., var2 = ‘“ Hello
World2 ’”
String literals:
Boolean literals:
This type of literals can have only two types of values:
Special literals:
Python contains one type of particular literal that is None
. We have been using it to specify that the field has not been created and used to end the list in Python.
Literal Collection
In Python, there are four literal collections:-
- List literal
- Their elements may have different data types as the list accepts different data types for a single variable.
- The list can be modified.
- Items in the list are separated by comma (,).
- The list is enclosed with a square bracket, [ ]
- Tuple literal
- Their elements may have different data types as the tuple accepts different data types for a single variable.
- The tuples can not be modified.
- Items in the tuples are separated by comma (,).
- The tuple is enclosed with parentheses, ()
- ⦁ Dict literal
- Python dictionary uses key-value pair to store the data inside its curly brackets, {}.
- The pair of the dictionary are separated by comma (,).
- Python Dict uses a colon (:) to separate the key and the value pair.
- Set literal
- Python contains the unordered dataset collection.
- Set uses curly brackets to enclose its data, {}.
- Elements in the set are separated by comma (,).
Example No.1:
x = 24
y = 24.3
z = 2+3j
print(x, y, z)
OUTPUT:
Example No.2:
s = 'Knowledge2life'
t = "Knowledge2life"
m = '''Knowledge2life'''
print(s)
print(t)
print(m)
OUTPUT:
Knowledge2life
Knowledge2life
Knowledge2life