What are literals in python?

Literals in Python refer to fixed, constant values that are directly used within your code. They represent various data types, such as numbers, strings, and Boolean values. Here's an explanation of different types of literals with examples:

Numeric Literals

Numeric literals represent numbers and can be integers, floating-point numbers, or complex numbers.

integer_literal = 42 float_literal = 3.14 complex_literal = 2 + 3j

String Literals

String literals represent sequences of characters enclosed in single, double, or triple quotes.

single_quoted = 'Hello, world!' double_quoted = "Python is awesome" triple_quoted = '''This is a multi-line string literal'''

Boolean Literals

Boolean literals represent truth values and can only be True or False.

true_literal = True false_literal = False

None Literal

The None literal represents the absence of a value. It is often used to indicate that a variable has no value assigned.

empty_value = None

List Literals

List literals represent ordered collections of items enclosed in square brackets.

number_list = [1, 2, 3, 4] string_list = ['apple', 'banana', 'cherry']

Tuple Literals

Tuple literals represent ordered collections of items enclosed in parentheses.

point = (10, 20) colors = ('red', 'green', 'blue')

Dictionary Literals

Dictionary literals represent key-value pairs enclosed in curly braces.

person = {'name': 'Alice', 'age': 30} fruits = {'apple': 2, 'banana': 3, 'cherry': 5}

Set Literals

Set literals represent unordered collections of unique items enclosed in curly braces.

prime_numbers = {2, 3, 5, 7, 11} vowels = {'a', 'e', 'i', 'o', 'u'}


Literals are fundamental building blocks in Python programs, providing values that can be used in calculations, comparisons, and various operations. They help represent data in a concise and straightforward manner.