Python – Literals
Python – Literals
In this tutorial you will learn python in detail.
In Python literal is a data that is given in a variable or constant. Python supports various literals. They are.
- String literals
- Numeric literals
- Boolean literals
- Special literals
- Literal collections
Let us study in details about them
- String literal
When a sequence of characters is enclosed in quotes, it forms string literal. A string can use single, double or triple quotes. A character literal has a single character enclosed in quotes.
Example:
string = "This is Python"
char = "C"
multiline_str = """This is a multiline string with more than one line code."""
unicode = u"\u00dcnic\u00f6de"
raw_
print(string)
str = r"raw \n string"
print(char)
print(multiline_str)
print(unicode)
print(raw_str)
Output:
This is Python
CThis is a multiline string with more than one line code.
Ünicöde
raw \n string
In the above program, string literal is “This is Python” and a character literal is “C”..
The value assigned to the “multiline_str” in triple-quotes “”” is a multi-line string literal.
Unicode literal is u”\u00dcnic\u00f6de” which supports characters other than English. In this case, \u00dc represents Ü and \u00f6 represents ö.
Raw string literal is r”raw \n string”.
- Numeric literals
These literals are immutable i.e., unchangeable. Numeric literals can belong to three different numerical types: they are Integer, Float, and Complex.
Examples:
a = 0b1010 #Binary Literals
b = 100 #Decimal Literal
c = 0o310 #Octal Literal
d = 0x12c #Hexadecimal Literal
#Float Literal
float_1 = 10.5
float_2 = 1.5e2
#Complex Literal
x = 2.15j
print(a, b, c, d)
print(float_1, float_2)
print(x, x.imag, x.real)
Output:
10 100 200 300
10.5 150.0
2.15j 2.15 0.0
- In the above program a variable is assigned with binary literal, b is assigned with decimal literal, c variable is assigned with octal literal and d is a hexadecimal literal.
- float_1 and float_2 variables are floating point literals.
- Complex literal 2.15j is assigned to variable x. this complex value has an imaginary and real literal.
- Boolean literals
A Boolean literal can have either True or False
Example:
x = (1 == True)
y = (1 == False)
a = True + 5
b = False + 20
print ("x is", x)
print ("y is", y)
print ("a:", a)
print ("b:", b)
Output:
x is True
y is False
a: 6
b: 20
The value of True represents 1 and value of False represents 0
- Special literal
Python provides one special literal called None. It is used to specify that the field has not been created.
Example:
x1=10
x2=None
print(x1)
print(x2)
Output:
10
None
- Literal collections
There are four different literal collections in python such as List literals, Tuple literals, Dict literals, and Set literals.
List:
List literal can have values of different datatypes. They are mutable and are separated by comma (,). They are enclosed in square brackets [].
Example:
list=['David',673,10.4,'Peter']
list1=[476,'Rose']
print(list)
print (list + list1)
Output:
[‘David’, 673, 10.4, ‘Peter’]
[‘David’, 673, 10.4, ‘Peter’, 476, ‘Rose’]
Tuple:
Tuple is also a collection of different data-types. It is immutable, separated by comma (,), and is enclosed in parentheses ().
Example:
tup = (10,20,"Sri",[3,4,5])
print (tup)
Output:
(10, 20, 'Sri', [3, 4, 5])
Dictionary:
Dictionary stores data in key-value pair, enclosed by curly braces {} and each pair is separated by comma (,).
Example:
Dict = {‘name’: ‘David’, ‘Roll_no’: 120, ‘Age’: 30}
Print (Dict)
Output:
{‘name’: ‘David’ , ‘Roll_no’: 120, ‘Age’: 30}
Set:
Sets are a collection of unordered datasets. They enclosed by {} and each element is separated by (,).
Example:
set = {'apple','grapes','guava','papaya'}
print(set)
Output:
{'guava', 'apple', 'grapes', 'papaya'}