All Courses
Python Literals

Introduction to Literals in Python

Python literals are a set/notation whose values ​​do not change during program execution.
Python literals are just a concise way to represent data types. Simply, it is a function that expresses a fixed value in the source code. They are either numbers, text, Boolean values, or other forms of data.
In this article, we’ll take a closer look at Python literals and their types.

Types of Python Literals

There are many different types of Python literals, and their usage is quite different. Then, Let’s take a look at them one by one.

  • String Literals
  • Numeric Literals
  • Boolean Literals
  • Literal Collections
  • Special Literals

1. String literals in Python

String literals can be created by writing text (a group of characters) enclosed in single quotation marks (`), double quotation marks (“), or triple quotation marks (”’).
Then, In Triple quotation marks, you can use the quotes to create and display multi-line strings in the way you want.

For Example:

"eMexo" , '12345'  , '''eMexo Technologies'''

Types of Strings:

There are two types of strings supported by Python:

Single-line String

A string that ends on one line is called a one-line string.

For Example:

#string literals
#single line literal
single_quotes_string='Welcome to'
double_quotes_string="eMexo Technologies"
print(single_quotes_string)
print(double_quotes_string)

Output:

Welcome to
eMexo Technologies

Multi-line String

A piece of text that is written in multiple lines is known as multiple lines string. There are two ways to create multiline strings:

1) Adding a black slash at the end of each line:

We can enable multi-line strings in Python by adding a backslash at the end of every line.

For Example:

#string literals
#multi line literal
str="Welcome\
to\
eMexo\
Technologies"
print(str)

Output:

WelcometoeMexoTechnologies

2) Using triple quotation marks:

You can literally easily create a multi-line string in Python by using the triple quotes at the beginning and end of the string.

For Example:

#string literals
#multi line literal
str="""Welcome
to
eMexo
Technologies"""
print(str)

Output:

Welcome
to
eMexo
Technologies

2. Numeric Literals in Python

Numeric literals are immutable. They are classified into four different numeric types:

  1. Integer
  2. Float
  3. Complex
  4. Long

Integer

Both positive and negative numbers, including 0. There should be no fractions.

The different types of integers are:

  • Decimal- It contains digits from 0 to 9. The base for decimal values is 10.
  • Binary- It contains only two digits- 0 and 1. The base for binary values is 2 and prefixed with “0b”.
  • Octal- It contains the digits from 0 to 7. The base for octal values is 8. In Python, such values are prefixed with “0o”.
  • Hexadecimal- It contains digits from 0 to 9 and alphabets from A to F.

For Example:

# integer literal

# Binary Literals
a = 0b10100

# Decimal Literal
b = 50

# Octal Literal
c = 0o320

# Hexadecimal Literal
d = 0x12b

print(a, b, c, d)

Output:

20 50 208 299

Float

Floating-point literals are also known as real literals. Unlike integers, they contain a decimal point.

For Example:

# Float Literal
e = 24.8
f = 45.0

print(e, f)

Output:

24.8 45.0

Complex

Complex literals are represented by A + Bj. Here, A is the real part. Also, the entire B part and j are imaginary or complex part. Where j represents the square root of -1. This is just iota or i used in mathematics.

For Example:

z = 7 + 5j

# real part is 0 here.
k = 7j

print(z, k)

Output:

(7+5j) 7j

Long

Long literals were just integers of unlimited length. Starting with Python 2.2, the integers used for overflow were automatically converted to long ints. Starting with Python 3.0, long literal has been removed. The Python 2 long data type is now the default Python 3 int type.
Long literals were represented by the suffix -l or L. We strongly recommend using L as l is very similar to the number 1.

For Example:

#usage long literal before it was depreciated
x=037467L 
print(x)

Output:

Success #stdin

Success #stdin #stdout 0.01s 7320KB
16183

3. Boolean literals in Python

Python Boolean literals are fairly simple and have only two values

  • True-True represents the value 1.
  • False-False represents the value 0.

For Example:

x = (1 == True)  
y = (2 == False)  
z = (3 == True)  
a = True + 10  
b = False + 10  
  
print("x is", x)  
print("y is", y)  
print("z is", z)  
print("a:", a)  
print("b:", b)  

Output:

x is True
y is False
z is False
a: 11
b: 10

4. Literal Collections in Python

If you want to handle multiple values, you can select a literal collection in Python. Python has four types of literal collections:

  1. List literals
  2. Tuple literals
  3. Dict literals
  4. Set literals

List Literals

The list contains elements of various data types. The list can be changed. The values ​​stored in the list are separated by commas (,) and enclosed in square brackets ([]). You can save different types of data in a list.

For Example:

# List literals
number = [1, 2, 3, 4, 5]
name = ['eMexo', 'Technologies', 'Bangalore', 2]
print(number)
print(name)

Output:

[1, 2, 3, 4, 5]
['eMexo', 'Technologies', 'Bangalore', 2]

Tuple Literals

Literals declared in parentheses () can hold any data type and are tuples. Commas separate the elements of the tuple. However, unlike lists, tuples are immutable.

For Example:

tup = (10,20,"eMexo",[2,3,4])  
print(tup)  

Output:

(10, 20, "eMexo", [2,3,4]) 

Dictionary Literals

The Python dictionary stores data in key-value pairs. Enclosed in curly braces {}, each pair is separated by a comma (,).

For Example:

# Dict literals
alphabets = {'a': 'apple', 'b': 'ball', 'c': 'cat'}
information = {'name': 'ajay', 'age': 21, 'ID': 27}

print(alphabets)
print(information)

Output:

{'a': 'apple', 'b': 'ball', 'c': 'cat'}
{'name': 'ajay', 'age': 21, 'ID': 27}

Set Literals

A set literal is a collection of unordered data that cannot be modified. It is enclosed in curly braces and separated by commas.

For Example:

set = {'apple','grapes','guava','papaya'}  
print(set)  

Output:

{'guava', 'apple', 'papaya', 'grapes'}

5. Special Literals in Python

At first, Python literals have a special literal called None. This literal in Python is used to indicate that a particular field is not created.
If you print a variable that has no value assigned to it, Python will print None. None is also used at the end of the Python list.

For Example:

val1=10    
val2=None    
print(val1)     
print(val2)  

Output:

10
None