Python Literals: String, Numeric, Boolean & Collection Literals

In computer programming, a literal is a fixed value written directly into the source code text. While a variable is a named storage tag, the actual value assigned to that variable (such as 100, "Veewom", or True) is a literal. Python provides rich syntactic support for numeric, string, boolean, special, and collection literals.


1. Numeric Literals in Python

Numeric literals are divided into integers, floating-point numbers, and complex numbers:

A. Integer Literals (Decimal, Binary, Octal, Hexadecimal)

Python integers can be written in four different numeral bases:

# Integer Literals in different bases
decimal_num = 100        # Standard Base 10
binary_num = 0b1100100   # Base 2 (prefixed with 0b)
octal_num = 0o144        # Base 8 (prefixed with 0o)
hex_num = 0x64           # Base 16 (prefixed with 0x)

# Visual underscores as thousands separators (PEP 515):
budget = 10_000_000      # Ten Million

print(f"Dec: {decimal_num}, Bin: {binary_num}, Oct: {octal_num}, Hex: {hex_num}")
# All print: 100!

B. Floating-Point Literals

Floating-point literals represent real fractional numbers, written with a decimal point or using scientific exponential notation (e or E):

pi = 3.14159
avogadro = 6.022e23       # 6.022 x 10^23
small_charge = 1.6e-19    # 1.6 x 10^-19

C. Complex Number Literals

Python has native built-in support for complex numbers in the form a + bj, where j represents the imaginary unit (√-1):

z = 3 + 5j
print("Real Part:", z.real)        # 3.0
print("Imaginary Part:", z.imag)   # 5.0

2. String Literals & Multi-Line Text

String literals represent sequences of text characters enclosed in quotes:

• Single / Double Quotes: 'Hello' or "Hello" (both are identical in Python).

• Triple Quotes (''' or """): Allow multi-line strings spanning multiple physical lines without manual \n escape characters.

• Raw Strings (r""): Ignores backslash escape sequences, essential for Regular Expressions and Windows directory paths (r"C:\new\test").

# Multi-line string literal:
syllabus = """
1. Python Fundamentals
2. Control Flow & Loops
3. Data Structures
4. Object-Oriented Programming
"""
print(syllabus)

3. Boolean Literals & Special Literal None

Python has two Boolean literals: True and False. Internally in Python, True behaves as an integer 1 and False behaves as 0:

print(True + True)   # 2 (1 + 1)
print(False * 50)    # 0 (0 * 50)

# Special Literal: None
db_connection = None
if db_connection is None:
    print("No database connection active!")

4. Literal Collections (Lists, Tuples, Dictionaries, Sets)

Python provides direct syntactic literal notations for constructing collections without calling class constructors:

# Collection Literals:
marks_list = [85, 90, 78, 92]                # List Literal (square brackets)
coordinates = (28.6139, 77.2090)             # Tuple Literal (parentheses)
unique_tags = {"python", "bca", "coding"}     # Set Literal (curly brackets)
student_info = {"id": 101, "name": "Pooja"}  # Dictionary Literal (key: value pairs)

Frequently Asked Questions (FAQ)

What is the difference between a variable and a literal?

A variable is a symbolic name (an identifier) pointing to memory, while a literal is the concrete raw data value itself. In score = 95, score is the variable identifier and 95 is an integer literal.

How do you write a single-element tuple literal?

A single-element tuple requires a trailing comma: t = (50,). Writing t = (50) without a comma evaluates simply as an integer surrounded by parentheses!
Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!