Understanding Literals in Python

Understanding Literals in Python

7 mins read2.5K Views Comment
clickHere
Updated on Oct 13, 2023 14:05 IST

When starting with Python programming, it is important to get the basics right before we get into coding. In your very first class of Python, you will be introduced to phrases like keywords, variables, operators, literals, etc.

2022_07_Understanding-Literals-in-Python.jpg

In this article, we are going to focus on the concepts for the definition and usage of literals. We will also be deep-diving into the types of literals in Python. So, without further ado, let’s get started!

Explore free Python courses

Explore How to Find an Armstrong Number Using Python

We will be covering the following sections today:

Check out: Top Python Online Courses and Certifications

What are Python Literals?

Python literals constant or fixed values in the source code that do not change during the execution of a program. We can also define Literals as the raw values that are given to Python variables. 

Let’s consider the below examples for better understanding:

daigram1

Literals are a way of representing data types in Python. They can be anything from numbers to textual data, Boolean values, etc. Let’s discuss the various types of literals –

Check out- Advantages of Python

Types of Python Literals

The usage of Python literals varies according to the type. Let’s look at the five types of literals Python has to offer:

types

Now, let’s talk about each of them in detail:

Check out- How to install Python

Numeric Literals

These literals contain only immutable numeric values. By immutable, it implies that one cannot changes its values during runtime. 

From Python 3 onwards, numeric literals have three subtypes:

  • Integers
  • Float
  • Complex

Let’s look at each of them in detail –

Integers

All the natural numbers – negative, zero, or positive are integers. We can also categorize them into the following types:

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

Consider the below Python examples of integer literals:

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Integer literals\n \n
\n \n
\n \n <span style="color: #808080;font-style: italic">\n \n #Positive\n \n
x \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: #ff4500">\n 734 \n
\n
\n <span style="color: #808080;font-style: italic">\n #Negative \n
y \n <span style="color: #66cc66">\n = - \n <span style="color: #ff4500">\n 8672 \n
\n
\n <span style="color: #808080;font-style: italic">\n #Binary \n
a \n <span style="color: #66cc66">\n = \n <span style="color: #ff4500">\n 0b111001100 \n
\n
\n <span style="color: #808080;font-style: italic">\n #Decimal \n
b \n <span style="color: #66cc66">\n = \n <span style="color: #ff4500">\n 21 \n
\n
\n <span style="color: #808080;font-style: italic">\n #Octal \n
c \n <span style="color: #66cc66">\n = \n <span style="color: #ff4500">\n 0o544 \n
\n
\n <span style="color: #808080;font-style: italic">\n #Hexadecimal \n
d \n <span style="color: #66cc66">\n = \n <span style="color: #ff4500">\n 0xC9 \n
\n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (x \n <span style="color: #66cc66">\n ,y \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (a \n <span style="color: #66cc66">\n , b \n <span style="color: #66cc66">\n , c \n <span style="color: #66cc66">\n , d \n <span style="color: black">\n ) \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #66cc66"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: #ff4500">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

output1

Float

Float literals or floating-point literals contain numeric values in decimal points. We also sometimes call them by the name of real literals. Developers classify them into two main types:

  • Fractional – contain whole numbers and decimal points.
 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Fractional float literals\n \n
\n \n
\n \n <span style="color: #808080;font-style: italic">\n \n #Positive\n \n
x \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: #ff4500">\n 1090 \n
\n
\n <span style="color: #808080;font-style: italic">\n #Negative \n
y \n <span style="color: #66cc66">\n = - \n <span style="color: #ff4500">\n 227 \n
\n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (x \n <span style="color: #66cc66">\n ,y \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: #ff4500">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

output3
  • Exponential – these values are represented in the powers of 10, represented by the letter e or E. The digits before the symbol E in an exponential literal are known as the mantissa and the digits succeeding the symbol E are the exponent. The exponent value describes the placement of the decimal point.
 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Exponential float literals\n \n
\n \n
\n \n <span style="color: #808080;font-style: italic">\n \n #Positive\n \n
\n \n <span style="color: #ff7700;font-weight:bold">\n \n print\n \n <span style="color: black">\n ( \n <span style="color: #ff4500">\n 25.53E3 \n <span style="color: black">\n ) \n <span style="color: #808080;font-style: italic">\n #Decimal point will jump 3 places \n
\n
\n <span style="color: #808080;font-style: italic">\n #Negative \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (- \n <span style="color: #ff4500">\n 105.03E5 \n <span style="color: black">\n ) \n <span style="color: #808080;font-style: italic">\n #Decimal point will jump 5 places \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: black">\n \n </span style="color: #ff7700;font-weight:bold">\n \n </span style="color: #808080;font-style: italic">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

output4

Complex

Complex literals are represented as X+Yj, where X is the real part of the value and Yj is the imaginary or complex part. Here, j= -1. 

Consider the below examples of complex literals:

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Complex literals\n \n
\n \n
\n \n <span style="color: #808080;font-style: italic">\n \n #Positive\n \n
\n \n <span style="color: #ff7700;font-weight:bold">\n \n print\n \n <span style="color: black">\n ( \n <span style="color: #ff4500">\n 12 + 8j \n <span style="color: black">\n ) \n
\n
\n <span style="color: #808080;font-style: italic">\n #Negative \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (-5j \n <span style="color: black">\n ) \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: black">\n \n </span style="color: #ff7700;font-weight:bold">\n \n </span style="color: #808080;font-style: italic">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

output5

String Literals

These literals contain textual data in the form of characters, enclosed within quotes. Based on quotes, we divide string literals into two subtypes:

  • Single-line
  • Multi-line

Let’s look at both of them in detail –

Single-line String Literals

Single-line string literals are enclosed within a single quote (‘ ‘) or double quotes (” “). Consider the below examples to understand how they work:

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Single line string literals\n \n
\n \n
\n \n <span style="color: #ff7700;font-weight:bold">\n \n print\n \n <span style="color: black">\n \n (\n \n <span style="color: #483d8b">\n 'Python Tutorial' \n <span style="color: black">\n ) \n
\n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Shiksha Online" \n <span style="color: black">\n ) \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b">\n \n </span style="color: black">\n \n </span style="color: #ff7700;font-weight:bold">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

output6

Multi-line String Literals

Multi-line string literals are a collection of characters that span multiple lines. In Python, we can declare them in following in two ways:

  • Backlashes – using a backlash at the end of each line.
 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Multi-line string literals\n \n
\n \n
\n \n <span style="color: #008000">\n \n str \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: #483d8b">\n "Welcome \n <span style="color: #000099;font-weight: bold"> \n
to \n <span style="color: #000099;font-weight: bold"> \n
Naukri \n <span style="color: #000099;font-weight: bold"> \n
Learning" \n
\n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #008000">\n str \n <span style="color: black">\n ) \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #000099;font-weight: bold"> \n </span style="color: #000099;font-weight: bold"> \n </span style="color: #000099;font-weight: bold"> \n </span style="color: #483d8b">\n \n </span style="color: #66cc66">\n \n </span style="color: #008000">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

output7
  • Triple quotes – you can also enclose multi-line strings in triple quotes (“””). 
 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Multi-line string literals\n \n
\n \n
\n \n <span style="color: #008000">\n \n str \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: #483d8b">\n """This \n
is \n
a \n
Python \n
Tutorial""" \n
\n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #008000">\n str \n <span style="color: black">\n ) \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #483d8b">\n \n </span style="color: #66cc66">\n \n </span style="color: #008000">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

output8

Boolean Literals

These literals take only two values: True (aka 1) and False (aka 0).

Let’s look at the below example:

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Boolean Literals\n \n
\n \n
x \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: #ff4500">\n \n 1.0\n \n
\n \n
a \n \n <span style="color: #66cc66">\n = \n <span style="color: black">\n (x \n <span style="color: #66cc66">\n == \n <span style="color: #ff4500">\n 1 \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "a is" \n <span style="color: #66cc66">\n , a \n <span style="color: black">\n ) \n
\n
b \n <span style="color: #66cc66">\n = \n <span style="color: black">\n (x \n <span style="color: #66cc66">\n == \n <span style="color: #ff4500">\n 7 \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "b is" \n <span style="color: #66cc66">\n , b \n <span style="color: black">\n ) \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #66cc66">\n \n </span style="color: #ff4500">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

output8
Object-Oriented Programming with Python
Object-Oriented Programming with Python
A good understanding of OOPs concepts can help you make your Python programming journey smoother. In this article, we will explore the basics of object-oriented programming in Python.
Constructors in Python: Definition, Types, and Rules
Constructors in Python: Definition, Types, and Rules
Constructor in python is a special method that is called when an object is created. The purpose of a python constructor is to assign values to the data members within...read more
How To Use Indentation in Python
How To Use Indentation in Python
Indentation is basically the whitespace (spaces/tabs) that are inserted before a Python statement. In this article, we will learn how to indent your python code with the help of example....read more

Literal Collections

Developers use literal collections when they work with more than one value. They are of the following four subtypes:

  • List literals
  • Tuple literals
  • Dictionary literals
  • Set literals

Let’s look at each of them in detail –

List literals

We are already aware that lists are an immutable collection of different types of data. The elements of a list are separated by commas and enclosed in square brackets []. 

Let’s look at the following example. Here, the list contains literals of different data types such as string, integer, float, dictionary, and a nested inner list:

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #List Literals\n \n
\n \n
mylist \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #483d8b">\n 'Rosa' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 10 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Jake' \n <span style="color: #66cc66">\n , - \n <span style="color: #ff4500">\n 2.3 \n <span style="color: #66cc66">\n , \n <span style="color: black">\n { \n <span style="color: #483d8b">\n 'Amy': \n <span style="color: #483d8b">\n 'Winner' \n <span style="color: black">\n } \n <span style="color: #66cc66">\n , \n <span style="color: black">\n [ \n <span style="color: #483d8b">\n 'Hitchcock' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Sculy' \n <span style="color: black">\n ] \n <span style="color: black">\n ] \n
\n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (mylist \n <span style="color: black">\n ) \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

output10

Tuple literals

Tuples are similar to lists in the fact that they, too, are a collection of different types of data with elements separated by commas. 

However, they differ from lists in two aspects – tuples are mutable, and they are enclosed in simple brackets ().

Let’s look at the following example. Here, we have created a tuple with collection literals of different data types such as string, integer, float, dictionary, and a nested inner list:

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Tuple Literals\n \n
\n \n
tupe \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n (\n \n <span style="color: #483d8b">\n 'Rosa' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 10 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Jake' \n <span style="color: #66cc66">\n , - \n <span style="color: #ff4500">\n 2.3 \n <span style="color: #66cc66">\n , \n <span style="color: black">\n { \n <span style="color: #483d8b">\n 'Amy': \n <span style="color: #483d8b">\n 'Winner' \n <span style="color: black">\n } \n <span style="color: #66cc66">\n , \n <span style="color: black">\n [ \n <span style="color: #483d8b">\n 'Hitchcock' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Sculy' \n <span style="color: black">\n ] \n <span style="color: black">\n ) \n
\n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (tupe \n <span style="color: black">\n ) \n
\n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

output11

Dictionary literals

Dictionary literals store different types of data in a key-value format. These are enclosed in curly brackets {} and separated by commas. Dictionaries are mutable as well. 

Let’s look at the following example of dictionary literals:

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Dictionary Literals\n \n
\n \n
mydict \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n {\n \n <span style="color: #483d8b">\n 'a': \n <span style="color: #483d8b">\n 'apple' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'b': \n <span style="color: #483d8b">\n 'ball' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'c': \n <span style="color: #483d8b">\n 'cat' \n <span style="color: black">\n } \n
\n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (mydict \n <span style="color: black">\n ) \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #483d8b">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

output12

Set literals

Set literals are a collection of unordered data. These are enclosed in curly brackets {} and separated by commas. Sets are immutable.

Let’s look at the following example of set literals:

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Set Literals\n \n
\n \n
vowelset \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n {\n \n <span style="color: #483d8b">\n 'a' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'e' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'i' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'o' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'u' \n <span style="color: black">\n } \n
\n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Vowels: " \n <span style="color: #66cc66">\n , vowelset \n <span style="color: black">\n ) \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

output13

Special Literal

Python has a special literal known as None. It signifies that a particular field is not created. Developers can also use it for end of Python lists.

Let’s look at the below example:

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Special Literal\n \n
\n \n
my_list \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #483d8b">\n 'Dog' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Cat' \n <span style="color: #66cc66">\n , \n <span style="color: #008000">\n None \n <span style="color: black">\n ] \n
\n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (my_list \n <span style="color: black">\n [ \n <span style="color: #ff4500">\n 2 \n <span style="color: black">\n ] \n <span style="color: black">\n ) \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

output13

Endnotes

I hope this article was helpful for you to understanding literals in python and how to implement different types of literal. You can explore related articles here if you wish to learn more about Python and practice Python programming.

Explore programming courses

Check out more Trending Article on Python

Top Online Python Compiler | How to Check if a Python String is Palindrome | Feature Selection Technique | Conditional Statement in Python | How to Find Armstrong Number in Python | Data Types in Python | How to Find Second Occurrence of Sub-String in Python String | For Loop in Python |Prime Number | Inheritance in Python | Validating Password using Python Regex | Python List |Market Basket Analysis in Python | Python Dictionary | Python While Loop | Python Split Function | Rock Paper Scissor Game in Python | Python String | How to Generate Random Number in Python | Python Program to Check Leap Year | Slicing in Python

Recently completed any professional course/certification from the market? Tell us what liked or disliked in the course for more curated content.

Click here to submit its review.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio

Comments