Python - Things to know before proceed - Literals Tutorial
Literal is a value given to entities like variables or constants.
The following type of literals in python are:-
1] Numeric Literals
Numeric Literals are immutable ( unchangeable). Numeric literals belong to 3 different types of numeric types i.e Integer, float, and complex.
Example:-
b = 0b101010 #Binary Literals
d = 123 #Decimal Literal
h = 0x12a #Hexadecimal Literal
o = 0o215 #Octal Literal
#Float Literal
float1 = 12.7
float2 = 3.5e2
#Complex Literal
z = 1 + 3.14j
print(b, d, h, o)
print(float1, float2)
print(z, z.imag, z.real)
2] String Literals
String literal is a sequence of string which can be formed by enclosing a string inside quotes in a different sequence. It can include single, double, or triple double-quotes.
Some of the examples of string literals are:-
i) Single Line String – For a Single-line string we can use a text between single quotes or double-quotes.
Example:-
Str1 = ‘Welcome to fresherbell’
Str2 = “Welcome to fresherbell”
ii) Multi-Line String – For multi-line string, we can use a text between triple double quotes or adding a backslash at the end of each line.
Using Triple Quote
Example:-
str1 = """Welcome
to
Fresherbell"""
print(str1);
Using Back Slash after each Line
Example:-
str2 = "Welcome \
to \
Fresherbell"
print(str2);
3] Boolean Literals
Boolean literals can have two values: true or false. depending upon the condition.
In python, True represents a value 1, and False represents a value 0.
x = (1 == True) #it will check whether 1 is equal to True or not
y = (1 == False) #it will check whether 1 is equal to False or not
a = True + 1 #true = 1
b = False + 3 #false = 0
print("x =", x)
print("y =", y)
print("a =", a)
print("b =", b)
4] Special Literal
In Python, there is one special literal i.e None. None value can be used to specify that the field has not been created.
It can also be used to end the list.
For example-
a=1
b=None
print(a)
print(b)
5] Literal Collections
There are four types of literal collection in python. Those are List Literal, Tuple Literal, Dict Literal, and Set Literal.
For example:-
list_1=['Fresherbell',2, 2.33] #List
tuple_1=(1,2,['Fresherbell',4,'Tutorial']) #Tuple
dict_1={1:'Hello',2:'World',3:'in',4:'Python'} #Dictionary
set_1={'Welcome','To','Fresherbell'} #Set
print(list_1)
print(tuple_1)
print(dict_1)
print(set_1) #Whenerver you will print set, everytime order will change