Concepts / Working with Numbers

Working with Numbers

A number like 5 or 1.23 is a real example of a literal constant -- a value that is used directly, exactly as written, and whose own value never changes.

  • CORE CONCEPT
  • Programming

What Is a Number Literal?

When you write the number 5 directly into your Python code, you are writing a literal constant. A literal constant is a value that is used directly, exactly as written, and whose own value never changes. The number 5 is always 5; it cannot become 6 or 3.5. This permanence is why we call it 'literal' — you use its value literally, exactly as it appears on the page.

Number literals are different from variables. When you write x = 5, you create a variable named x that holds the value 5. The variable x can change later — you could write x = 10 and now x holds a different value. But the literal 5 itself never changes. Understanding this distinction is crucial for writing clear, predictable code.

Literal Constants vs. Variables

To understand the difference between a literal constant and a variable, imagine two different ways to refer to a number in your code. When you write 5 directly, Python uses that value immediately and exactly as written. When you write x = 5 and then use x in your code, Python looks up what value x currently holds and uses that. If x changes, the behavior of code using x changes too. But code using the literal 5 always behaves the same way.

5literal constant5still 5, unchangedxholds 5xnow holds 10
When you write 5 in code, Python uses that exact value every time. When you write x = 5, Python creates a variable that can change. This diagram shows what happens when you use each one.

A literal constant's value is fixed and cannot change. A variable is a named container that can hold different values at different times. When you use a literal in your code, you always get the same value. When you use a variable, you get whatever value it currently holds.

Integer Literals

An integer is simply a whole number with no fractional part, such as 2, 100, or -47. When you write an integer directly in your code, like 42 or 0 or -15, you are writing an integer literal. Integer literals are the most straightforward number literals in Python — they are just whole numbers written as you would normally write them.

Because a literal number's value cannot be changed, it is called 'literal' — you use its value literally, as written. The integer literal 42 is always 42. You cannot modify it or reassign it. It is a constant value that Python recognizes and uses exactly as you have written it.

Recognizing Different Number Forms

Python recognizes different forms of number literals by how they are written. An integer literal contains only digits, optionally preceded by a minus sign for negative numbers. For example, 5, 100, and -42 are all integer literals. A floating-point literal includes a decimal point, such as 5.0, 3.14, or -2.5. The presence or absence of the decimal point tells Python which type of number you are writing.

5integer literal5.0floating-point literal-42integer literal-42.5floating-point literal1000integer literal3.14159floating-point literal
How can you tell an integer literal from a floating-point literal just by looking at it? The key difference is the decimal point.

The visual difference is simple: if the number has a decimal point, it is a floating-point literal. If it has no decimal point, it is an integer literal. Even 5.0, which represents the same quantity as the integer 5, is a floating-point literal because of the decimal point. Python treats these two forms differently internally, even though they represent the same mathematical value.

How Literals Work in Your Code

When you write a number literal in your code, Python reads it and understands it as a value. If you write print(5), Python sees the literal 5 and prints it. If you write x = 5 + 3, Python sees two integer literals, 5 and 3, adds them together, and stores the result 8 in the variable x. The literals themselves are not stored in variables — they are used directly, exactly as written.

This is different from writing x = 5 and then using x in a calculation. When you use x, Python looks up the current value of x and uses that. But when you use the literal 5, Python always uses 5. There is no lookup, no indirection — just the value itself.

Common Mistakes with Number Literals

  • Thinking a literal can change

    A literal constant's value is fixed by definition. The number 5 is always 5 in your code.

    Fix: If you need a value that can change, use a variable instead: x = 5, then later x = 6.

  • Confusing 5 and 5.0

    5 is an integer literal and 5.0 is a floating-point literal. Python treats them as different types internally.

    Fix: Use 5 for whole numbers and 5.0 (or any number with a decimal point) for floating-point values. Choose based on what your code needs.

  • Writing a number with spaces or commas

    Python does not recognize spaces or commas inside number literals. This will cause a syntax error.

    Fix: Write numbers without spaces or commas: 1000, not 1,000.

Why This Matters

Understanding the difference between a literal constant and a variable helps you write clearer code. When you see the number 5 in your code, you know it is always 5. When you see a variable name like x, you know it might hold different values at different times. This distinction makes your code easier to reason about and debug. It also helps you choose the right tool for the job — use a literal when you have a fixed value, and use a variable when you need to store or change a value.

Recognizing integer literals versus floating-point literals is also important because Python treats them differently. If you need exact whole-number arithmetic, use integers. If you need to represent fractional or decimal values, use floating-point literals. Being intentional about which form you use prevents confusion and bugs.

Practice

EASY

For each of the following, identify whether it is an integer literal, a floating-point literal, or a variable: (1) 42, (2) x, (3) 3.14, (4) -7, (5) y = 10.

Hints
  • Look for a decimal point to distinguish integers from floating-point numbers.
  • Remember that a variable is a name that holds a value, not the value itself.
  • An assignment like y = 10 creates a variable y; the 10 is an integer literal.
MEDIUM

Explain why the literal 5 and the variable x (after x = 5) behave differently in your code. What can change and what cannot?

Hints
  • Think about what happens if you later write x = 10.
  • Can you write 5 = 10? Why or why not?
  • The key difference is whether the value can be reassigned.

Key Takeaways

  • A literal constant is a value used directly, exactly as written, and its value never changes. The number 5 is always 5.
  • An integer literal is a whole number with no fractional part, such as 42 or -7. It has no decimal point.
  • A floating-point literal includes a decimal point, such as 5.0 or 3.14. The decimal point distinguishes it from an integer.
  • Literals are different from variables. A variable is a named container that can hold different values; a literal is a fixed value.
  • Recognizing the form of a number literal — integer or floating-point — helps you understand how Python will treat it and choose the right form for your code.