Python literals are fixed values typed directly into code, such as 42, "Hello, World!", and True. Learn how number, string, and Boolean literals differ from variables—and how these small building blocks shape calculations, comparisons, and everyday Python programs.

Multiple Choice

What does the term 'literals' refer to in programming?

The term 'literals' in programming refers to fixed values that are explicitly written in the code. These values can represent various types of data such as numbers, strings, characters, and boolean values, among others. For instance, when you write a number like `42`, a string like `"Hello, World!"`, or a boolean like `True` directly in your code, you are using literals. Literals are distinguished from variables because they do not change throughout the execution of the program; they are static values that directly represent data. This concept is fundamental to programming because it allows developers to define constant values that can be used in computations, comparisons, and other operations within the program. In contrast, the other options refer to different concepts in programming. Values used in computations may include literals, but they also encompass variables and expressions. Variables are not literals because they can store values that may change during program execution. Lastly, function names in a module do not represent values themselves but rather callable blocks of code that may operate on literals or variables.

A number typed straight into a line of Python code is more than a symbol on the screen: it’s a value the program can use. The same goes for a string such as "Hello, world!" or the Boolean value True. These values, written directly in the code, are called literals.

Literals are one of programming’s simplest building blocks. They give instructions a concrete starting point: a price to calculate, a message to display, or a setting to check. To understand how Python uses them, it helps to see how they differ from names such as variables and functions.

A value, written right there

Consider this short Python statement:


print("Hello, world!")

The text "Hello, world!" is a string literal. It appears directly in the code, enclosed in quotation marks, and Python treats it as a specific piece of text. The name print, by contrast, refers to a built-in function. It tells Python what action to perform.

That difference matters. A literal represents data; a function name refers to code that can do something with data. In this example, the function receives the string literal and displays it.

A literal doesn’t need to be stored in a variable before it can be used. You can add two numeric literals directly:


total = 12 + 8

Here, 12 and 8 are integer literals. Python adds their values and assigns the result to the variable total. The variable is a name that can refer to a value; the literals are the specific values written into the expression.

The main kinds of Python literals

Python has literals for several common data types. Each kind follows its own rules, but the basic idea stays the same: the value is written directly in the code.

Numbers

Whole numbers, such as 42 or -7, are integer literals. Numbers with a decimal point, such as 3.14 or 0.5, are floating-point literals.


items = 4

temperature = 18.5

In this example, 4 and 18.5 are literals. items and temperature are variable names. The distinction can seem small, but it helps explain what Python is handling at each point in a program.

Python also lets you write numbers in forms other than ordinary decimal notation. For example, 0b1010 is a binary integer literal, and 0xFF is a hexadecimal one. They use different number systems, but they still represent numeric values written directly in the code.

Strings

A string literal is a sequence of characters enclosed in quotes. Python accepts either single quotes or double quotes:


name = "Mina"

city = 'Dunedin'

Both "Mina" and 'Dunedin' are string literals. The quote marks show where the text begins and ends; they are part of the way Python recognizes the value.

For longer text, triple quotes can span several lines:


note = """The meeting starts

at nine o'clock."""

This is useful for paragraphs, notes, or documentation. It’s a small convenience, but one that makes a program feel less cramped when a message needs room to breathe.

Boolean values

Python has two Boolean literals: True and False. They represent truth values and are often used in conditions.


is_open = True

The literal True is not a string, even though it’s written as a word. That means "True" and True are different values: the first is text, while the second is a Boolean. The capital letters in Python’s Boolean literals matter, too. Writing true in lowercase doesn’t mean the same thing to Python.

The special value None

Python’s None literal represents the absence of a value. For instance, a variable may hold None when a result hasn’t been supplied or there is nothing to return.


selected_item = None

It’s not the same as 0, an empty string, or False. Those are values in their own right. None has a distinct role, much like an empty chair reserved for something that isn’t there yet.

Literals and variables: related, but not the same

Literals often appear beside variables, so it’s easy to blur the two. Compare these lines:


score = 10

score = score + 5

In the first line, 10 is an integer literal, and score is a variable name. In the second, 5 is another literal. The name score refers to a value that can change as the program runs.

That’s the key difference: a literal is written directly as a specific value, while a variable is a name that refers to a value. The value associated with a variable may change; the literal written in the source code remains what it is. The number 5 in the statement doesn’t turn into 6 just because the program later updates score.

Still, literals can be part of changing computations. In score = score + 5, the program reads the current value of score, adds five, and assigns a new value back to the name. So a literal is fixed as written, but it can take part in an operation whose result changes.

Expressions do more than literals alone

An expression can combine literals, variables, operators, and function calls. For example:


subtotal = 20 * quantity

The number 20 is a literal. quantity is a variable, and 20 * quantity is an expression. Its result depends on the current value stored under quantity.

This is why “values used in computations” is broader than “literals.” A computation may use literals, variables, or both. The literal is the directly written value; the whole expression is the larger piece Python evaluates.

The same idea appears in comparisons:


if temperature < 0:

print("Below freezing")

Here, 0 is an integer literal and "Below freezing" is a string literal. Python compares the variable’s value with zero, then may pass the string to print. Each literal supplies a particular value for the program to work with.

Why small details matter

Literals make code readable because they put important values where they’re used. A line like attempts = 3 is easy to scan: the reader can see the value immediately. But if the same value appears many times, a named constant can sometimes make the code easier to maintain:


MAX_ATTEMPTS = 3

Now MAX_ATTEMPTS is a name, not a literal. Its value is assigned using the integer literal 3. Python doesn’t enforce constants in the same way some languages do, but uppercase names commonly signal that a value is intended to stay unchanged.

This approach can also make a program’s purpose clearer. The number 60 might mean seconds in a minute, a time limit, or a score threshold. A well-chosen name gives that number context. There’s no need to replace every literal, though; straightforward values are often clearer when written where they’re needed.

A small concept with a big role

Literals are fixed values written directly in source code: numbers, strings, Booleans, and special values such as None. They can stand alone, or they can be combined with variables and operators in expressions. They are not variable names or function names, though they often appear alongside both.

Once that distinction clicks, everyday Python code becomes easier to read. You can spot the value, the name that refers to data, and the operation that brings them together. It’s a modest idea, but nearly every program relies on it.