Literals in Python

You must first complete Display output in Python 3 | print() function in Python 3 before viewing this Lesson

Literals in Python

Literal may be defined as a quantity whose value can’t be changed during the execution of a Program.

So values of Python Literals don’t change during execution of program.




Types of Python Literals

Python Literals are categorized into five types as:

1. String literals

2. Numeric Literals

3. Boolean

4. None

5. Sequence

1. String Literals in Python

String literal is a collection of multiple characters .Types of string literal are:

Single Line String Literal

It is the collection of multiple characters within a pair of single quotes(’) or double quotes(“).

These literals don’t go across multiple lines.

Examples:

“India” , ’Rs100’ , “A” , ‘Grade’ , “Father’s name”, ‘1-x-5-k-9’ etc.

Multiline String Literal

It is the collection of multiple characters across multiple lines.

It can be created as given in following examples:

Example1:

Using Python IDLE Using Spyder
>>> str=’love\
jot’
>>> print(str)
lovejot.
In [1]: str=’love\
jot”In [2]:print(str)
lovejot

Note: Back slash allows to continue string at next line

Example2:

Using Python IDLE Using Spyder
>>> str = ”’Love
Jot”’>>> print(Str)
Love
Jot
In [3]: str=”’Love
: jot”’In [4]:print(str)
Love
jot

Note: Triple single quotes at beginning and end allow string to continue at multiple lines.

Example 3:

Using Python IDLE Using Spyder
>>> Str = “””Love
Jot”””>>> print(Str)
Love
Jot
In [5]: str=”””Love
jot”””In [6]:print(str)
Love
jot

Note: Triple double quotes at beginning and end allow string to continue at multiple lines.


2. Numeric Literals in Python

Numeric literals are those literals which can contain digits only . Types of numeric literal are:

  • Integer Literals
  • Floating Point Literals
  • Complex Literals

Integer Literals

Integer literals are those numeric literals which can contain numbers without decimal point.

They can contain negative as well as positive values. Types of integer literal are:

a. Decimal:

It can contain digits from 0 to 9. So base is 10.

Examples:

1234 , 55 ,98 are decimal integer literals.

We can display a decimal literal as:

Using Python IDLE Using Spyder
>>> print(10) #10 is decimal literal here
10
In [6]: print(10)
10

b. Octal:

It can contain digits from 0 to 7. Octal values are prefixed with 0o(Zero followed by letter ‘o’) in Python.

Examples:

0o12 ,0o53 are octal literals.

We can display an octal literal as:

Using Python IDLE Using Spyder
>>> print(0o123)
83
In [7]: print(0o123)
83

#output will be displayed as decimal form of octal number 123 i.e. 83

c. Hexadecimal:

It can contain digits from 0 to 9 and alphabets from A to F where A=10, B=11, C=12, D=13, E=14 and F=15.

It is prefixed with 0X (Zero followed by letter ‘x’).

Examples:

0X12 ,0XF9A are hexadecimal literals.

We can display a octal literal as:

Using Python IDLE Using Spyder
>> print(0X1F)
31
In [8]: print(0X1F)
31

#output will be displayed as decimal form of hexadecimal number 0X1F i.e. 31

d. Binary:

It can contain only two digits 0 and 1.

It is prefixed with 0b (Zero followed by letter ‘b’). where b refers to binary.

Examples:

0b101 ,0b1101 are binary literals.

We can display a binary literal as:

Using Python IDLE Using Spyder
>> print(0b101)
5
In [8]: print(0b101)
5

#output will be displayed as decimal form of binary number 0b101 i.e. 5



Floating Point Literals

They are also known as real literals. They can contain decimal point in them. They can be written in two ways:

  • Fractional Form
  • Exponent Form

a. Fractional Form

In fractional form, real number contains a whole number followed by a decimal point(.) which is further followed by fractional part.

We can omit digits before or after the decimal point. They can contain both negative and positive values.

Examples:

12.33, 0.345, -45.344 are floating point literals in fractional form.

We can display a floating point literal in Python as:

Using Python IDLE Using Spyder
>>> print(2.5)
2.5
In [9]: print(2.5)
2.5

Note: 2.5 is a floating point literal in fractional form

b. Exponent Form

In this form a real number is represented in terms of power of 10. The power of 10 is represented by alphabets E or e.

The real number in exponent form has two parts:

Mantissa :- The digits before the symbol E form the Mantissa part.
Exponent :- The digits after the symbol E form the Exponent part. The exponent Part can only contain integers

Example:

1.33 х 105 can be represented as 1.33E5
We can display a floating point value in exponent form as:

Using Python IDLE Using Spyder
>>> print(2.533E5)
253300.0
In [10]: print(2.533E5)
253300.0

#Output will appear after multiplying 2.533 with 105 (100000).


Complex Literals

Complex literals are of the form A+Bj.

Here A and B are real values. A is termed as real part and B is termed as imaginary part.

j represents square root of -1. Which is an imaginary number.

Example:

2.5+3.1j is a complex literal.

we can display it at python shell as:

Using Python IDLE Using Spyder
>>> print(2.5+3.1j)
(2.5+3.1j)
In [11]: print(2.5+3.1j)
(2.5+3.1j)



3. Boolean Literals

It represents one of the two Boolean values i.e. True and False.

It is internally represented as an integer. True is manipulated as 1 and False is manipulated as 0.

We can display a boolean literal as:

Using Python IDLE Using Spyder
>>> print(True)
True
In [12]: print(True)
True



4. None Literal

None represents something that has not been created yet. It is represented as None.

Python doesn’t display anything when we try to display None.

When we use print statement to display a variable having no value stored in it, None will be displayed.

We can display None literal as:

Using Python IDLE Using Spyder
>>> print(None)
None
In [13]: print(None)
None



5. Sequence Literals

Sequence contains a group of values. They are of four types

  • List
  • Tuple
  • Dictionary
  • Set

a. List

List is a group of values separated by commas within the pair of square brackets. Values in a list can be of any data type.

Examples:

[1,2,3,4,5]

[‘a’, ‘e’, ‘i’ , ‘o’, ‘u’]

We can display above lists as:

 

Using Python IDLE Using Spyder
>>> print([1,2,3,4,5])
[1,2,3,4,5]
In [14]: print([1,2,3,4,5])
[1,2,3,4,5]

b. Tuple

Tuple is a group of values separated by commas within the pair of parenthesis. They can never be modified. Values in a tuple can be of any data type.

Examples:

(1,2,3,4,5)

(‘a’, ‘e’, ‘i’ , ‘o’, ‘u’)

We can display above tuples as:

Using Python IDLE Using Spyder
>>> print((1,2,3,4,5))
(1,2,3,4,5)
In [15]: print((1,2,3,4,5))
(1,2,3,4,5)

c. Dictionary

Dictionary is a group of values in the form of key:value pairs separated by commas within the pair of braces (Curly brackets). Keys and Values in a dictionary can be of any data type.

Example:

{1:’a’ ,2:’e’, 3:’i’, ,4:’o’, 5:’u’}

We can display above dictionary as:

Using Python IDLE Using Spyder
>>> print({1:’a’ ,2:’e’, 3:’i’,4:’o’, 5:’u’})
{1:’a’ ,2:’e’, 3:’i’,4:’o’, 5:’u’}
In [16]: print({1:’a’ ,2:’e’, 3:’i’,4:’o’, 5:’u’})
{1:’a’ ,2:’e’, 3:’i’,4:’o’, 5:’u’}

d. Set

Set is a group of values separated by commas within the pair of braces (curly brackets). They can never be modified

Examples:

{1,2,3,4}

We can display above dictionary as:

Using Python IDLE Using Spyder
>>> print({1,2,3,4})
{1,2,3,4}
In [17]: print({1,2,3,4})
{1,2,3,4}

Lesson tags: complex literal, constants in python, literals in python, None literal, python literals, real values in python, string lliteral, types of collectoin, types of strings in python, what is literal in python
Back to: Python Programming Tutorial