GeeksBro

FREE EXCLUSIVE TIPS

Python Tutorials, Tricks & Tips, Projects, etc..,

Python Literals | Different Types Of Literals in Python

In this tutorial, you are going to learn about Python Literals. What are Python Literals and different types of Python Literals. Python Literals are Boolean Literals, Number Literals, String Literals, Special Literal and Literal Collections.

Python Literals

Literals are the values given to a variable or constant. Python supports different type of Literals.

python literals

Let's start.....

#1. Boolean Literals

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

Program:

boolean1 = (1 == True)
boolean2 = (0 != False)
x = False + 0
y = True + 2
print(true)
print(false)
print(x)
print(y)

Output:

True
False
0
3

In the above program, the value of boolean1 is True and the value of boolean2 is False. In Python True is equalled to 1 and False is equalled to 0.

You can also use Boolean literals in the Mathematical expressions. See the Statements 3 and 4 to get an Idea. The value of x is 0 and the value of y is 3. Because the True == 1 and False == 0.

#2. Number

In Python, Numerical Literals belongs to 3 different types ie.., Int type, Float type, Complex type.

Program:

#int literals
a = 0b01 #binary literal
b = 1 #decimal literal
c = 0o05 #octal literal
d = 0x0F #hexadecimal literal

#float literals
x = 1.35
y = 3.145

#complex literals
p = 3 + 4.5j

print(a, b, c, d)
print(x, y)
print(p, p.real, p.imag)

Output:

1 1 5 15
1.35 3.145
(3+4.5j) 3.0 4.5

#3. Strings

String Literals are the set of characters enclosed in quotes. You can use both single or double or triple quotes for the string literals.

Program:

string = "I am GeeksBro"
char = "G"
triple_quotes_string = """This is a triple quotes string"""
unicode = u"\u00dc\u00f6"
raw_string = r"raw \n string"
print(string)
print(char)
print(triple_quotes_string)
print(unicode)
print(raw_string)

Output:

I am GeeksBro
G
This is a triple quotes string
Üö
raw \n string

In the above program, I am GeeksBro is a String literal and G is a Character literal. The value of triple_quotes_string is a Multiple string literal and unicode is Unicode literal which supports the characters other than English. raw \n string is a Raw string literal.

#4. Special Literal

In Python, you have one Special Literal None. None is used to define a field that is not created.

Program:

a = None
if a == None:
    print('this is none')

Output:

this is none

In the above program, we assigned None to a variable. You can use it anywhere in the program for conditions, even for loops in data structures, etc..,

#5. Literal Collections

In Python, you have Literal Collections like List Collections, Tuple Collections, Dictionary Collections and Set Collections.

Program:

#list
languages = ['Python', 'JS', 'C#']
#tuple
numbers = (1, 2, 3)
#dictionary
android_versions = {'m' : 'marshmellow', 'n' : 'nougat', 'o' : 'oreo'}
#set
vowels = {'a', 'e', 'i', 'o', 'u'}
print(languages)
print(numbers)
print(android_versions)
print(vowels0

Output:

['Python', 'JS', 'C#']
(1, 2, 3)
{'m': 'marshmellow', 'n': 'nougat', 'o': 'oreo'}
{'o', 'e', 'u', 'i', 'a'}

EndNote

If you have any Doubts Comment below or Contact us at GeeksBro Facebook Page.


Post a Comment