Skip to content

Literals in Python

  • Python

Literals are the most basic kind of expression in Python, representing constant values that are fixed during the program execution.
Python supports several types of literals, which correspond to different data types.
Understanding literals is fundamental to mastering Python as they form the building blocks of data representation.

Types of Literals

Numeric Literals

Numeric literals are used to represent numbers. They are immutable and come in various forms:

Integer Literals

Integers are whole numbers without a fractional part. They can be represented in decimal, binary, octal, and hexadecimal forms.

decimal = 10       # Decimal (Base 10)
binary = 0b1010    # Binary (Base 2)
octal = 0o12       # Octal (Base 8)
hexadecimal = 0xA  # Hexadecimal (Base 16)

print(decimal)      # Output: 10
print(binary)       # Output: 10
print(octal)        # Output: 10
print(hexadecimal)  # Output: 10

Floating-point Literals

Floating-point literals represent real numbers with a fractional part. They can be written in decimal form or exponential form.

float1 = 10.5
float2 = 1.05e1   # Equivalent to 1.05 * 10^1

print(float1)     # Output: 10.5
print(float2)     # Output: 10.5

Complex Literals

Complex literals have a real part and an imaginary part, denoted by j or J.

complex_num = 3 + 4j
print(complex_num)       # Output: (3+4j)
print(complex_num.real)  # Output: 3.0
print(complex_num.imag)  # Output: 4.0

String Literals

String literals are sequences of characters enclosed in quotes. They can be single, double, triple-single, or triple-double quotes.

single = 'Hello'
double = "World"
triple_single = '''Hello
World'''
triple_double = """Hello
World"""

print(single)        # Output: Hello
print(double)        # Output: World
print(triple_single) # Output: Hello\nWorld
print(triple_double) # Output: Hello\nWorld

Boolean Literals

Boolean literals have only two values: True and False. They are used to represent truth values.

is_true = True
is_false = False

print(is_true)  # Output: True
print(is_false) # Output: False

Special Literals

Python has a special literal None which represents the absence of a value or a null value.

nothing = None
print(nothing)  # Output: None

Collection Literals

Collection literals include lists, tuples, dictionaries, and sets. These are used to store multiple items in a single variable.

List Literals

Lists are ordered, mutable collections of items enclosed in square brackets.

list_example = [1, 2, 3, 'four', 5.0]
print(list_example)  # Output: [1, 2, 3, 'four', 5.0]

Tuple Literals

Tuples are ordered, immutable collections of items enclosed in parentheses.

tuple_example = (1, 2, 3, 'four', 5.0)
print(tuple_example)  # Output: (1, 2, 3, 'four', 5.0)

Dictionary Literals

Dictionaries are unordered collections of key-value pairs enclosed in curly braces.

dict_example = {'one': 1, 'two': 2, 'three': 3}
print(dict_example)  # Output: {'one': 1, 'two': 2, 'three': 3}

Set Literals

Sets are unordered collections of unique items enclosed in curly braces.

set_example = {1, 2, 3, 4, 5}
print(set_example)  # Output: {1, 2, 3, 4, 5}

Conclusion

Literals are fundamental to programming in Python, representing fixed values of various types. Understanding the different types of literals and how to use them effectively can enhance your ability to write clear, efficient, and maintainable code.
Whether you’re working with numbers, strings, booleans, or collections, literals provide a straightforward way to express constant values in your programs.