What are Literals in Python and explain about different Literals?

A literal in python source code represents a fixed value for primitive data types. There are 5 types of literals in Python-

String literals

A string literal is created by assigning some text enclosed in single of double quotes to a variable. To create multiline literals, assign the multiline text enclosed in triple quotes. E.g., Name = ‘Tanya’

A character literal

It is created by assigning a single in triple quotes. e.g., A = ‘p’.

Numeric literal

It include numeric values that can be either integer, floating point value, or a complex number. e.g., A = 40.

Boolean literal

These can be 2 values – True or False.

Literal Collections

These are of 4 types.

A) – List collections – e.g. A = [1, 3, 5, ‘Amit’]

B) – Tuple literals – e.g. A = (4, 5, 6, 7).

C) – Dictionary literals e.g., dict = {1: ‘apple’, 2: ‘mango’, 3:’banana’}.

D) – Set literals – e.g. {‘Tanya’, ‘Rohit’, ‘Sandesh’}.

Special literal

Python has 1 special literal None which is used to return a null variable.

Popular Posts

Spread the knowledge
 
  

Leave a Reply

Your email address will not be published. Required fields are marked *