Literals in Python

In Python, a literal is a value that is written exactly as-is in your code. It represents a fixed value that doesn’t change during the execution of a program. Literals are used to assign values to variables, as arguments in functions, or to create data structures like lists, dictionaries, and more.

Python supports several types of literals, each with its own syntax and purpose. Let’s take a closer look at some of the most common types of literals:

 In the below PDF we discuss about Literals in Python in detail in simple language, Hope this will help in better understanding.


Types of Literals:

1. Numeric Literals:

Python supports various types of numeric literals, including

  • Integer Literals: These are whole numbers without a decimal point. For example, 42 and -123 are integer literals.
  • Floating-Point Literals: These are numbers with a decimal point or in exponential form. For example, 3.14 or 2.5e-3 are floating-point literals.
  • Complex Literals: Complex numbers are written as real + imagj, where real and imag are the real and imaginary parts. For example, 3 + 2j is a complex literal.

2. String Literals:

String literals in Python are sequences of characters enclosed in either single (‘) or double (“) quotes. For example:

message = 'Hello,

 World!’Python also supports triple-quoted strings, which are useful for multi-line strings or docstrings:

description = """Python is a versatile programming language 

that is both powerful and beginner-friendly.”””

3. Boolean Literals:

Boolean literals represent one of two values: True or False. They are often used in conditional statements and comparisons to control the flow of a program. For example:

is_student = True

is_teacher = False

4. None Literal:

The None literal is a special value in Python that represents the absence of a value or a null value. It is often used to initialize variables before they are assigned a meaningful value or to indicate that a function doesn’t return anything.

result = None

5. List and Dictionary Literals:

Lists and dictionaries are two commonly used data structures in Python. You can create them using literals as follows:

fruits = ["apple", "banana", "cherry"]

person = {“name”: “Alice”, “age”: 30}


Related Question

Literals in Python are constant values that are used to represent data. They are used to initialize variables and provide data directly in the code.

Integer literals can be represented without a decimal point. For example: 42, -123, 0.

Floating-point literals have a decimal point or use scientific notation. For example: 3.14, 2.0, 1e-3.

A set literal in Python is a collection of unique elements enclosed in curly braces. For example: {1, 2, 3}.

Complex number literals in Python have an imaginary part represented with a ‘j’ or ‘J’ suffix. For example: 2 + 3j, 1.5 – 2j.


PYTHON interview Questions and Answers

Destructor in Python A destructor

Constructor in Python In object-oriented

Python Modules Python is a

File Handling in Python Python

Exception Handling in Python Exception

Data Abstraction in Python Data

Leave a Comment

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

// Sticky ads