Concepts / Defining Functions with def

Defining Functions with def

The word def is reserved. It has a specific meaning to Python and therefore cannot be selected as a variable name.

  • Programming

Two Kinds of Names

When you write Python, some words already belong to the language. Other names are created by you for the data your program works with. The word def belongs to Python's vocabulary. It is a reserved word, so it cannot also be used as the name of a variable.

Python vocabularyreserved wordsifreserved wordscorecreated namedefreserved wordProgrammervocabularyvariable namestotalcreated nameTruereserved wordincludesincludesincludesincludesincludes
What is the difference between Python's fixed vocabulary and the names a programmer creates?

Reserved Words in Python

Definition

Reserved words are a fixed set of 35 words that have unchangeable, singular meanings to Python. They form the core vocabulary of the language.

A reserved word is not an ordinary name that you can redefine for your own use. Python recognizes it as part of the language itself. The set is finite and well-defined. Examples include control-structure words such as if, for, while, def, and class, as well as the special values True, False, and None.

Key point

The word def is reserved. It has a specific meaning to Python and therefore cannot be selected as a variable name.

When a Name Conflicts

Suppose you try to create a variable whose name is a reserved word. Python does not wait until the program is running to discover the conflict. It rejects the program before execution begins, so the program does not run at all. This early rejection prevents the same word from being treated both as a language instruction and as a programmer-created variable name.

defreserved word used as avariable namescoreprogrammer-created variablenameProgramrejected before executionProgramnot blocked by this namingconflictconflict causes rejectiondifferent name avoids conflict
What changes when a programmer replaces a reserved word with an available variable name?
Worked example

Choosing a non-conflicting name

A programmer wants to name a variable def.

  1. 1
    Check the proposed name

    def is one of Python's reserved words, so it already has an unchangeable meaning in the language.

  2. 2
    Predict Python's response

    Python will reject the program before execution begins rather than allowing def to serve as a variable name.

  3. 3
    Choose another name

    The programmer can create a different name, such as score, because variables are names created by the programmer and are allowed as long as they are not reserved words.

Result

Use a different variable name instead of def.

Reading Names in Code

As you read Python, separate words supplied by the language from names supplied by the programmer. Reserved words tell Python what to do. Variables are names created by the programmer to store and work with data. Both appear in a Python program, but they perform different roles.

Many code editors and Python environments highlight reserved words in a special color, often blue or purple. This visual distinction helps you recognize which words belong to Python and which names you have created. Treat the highlighting as a useful reading signal, but remember the underlying rule: a reserved word cannot be used as a variable name.

Naming Practice

EASY

Classify each proposed name as either a reserved word or a programmer-created variable name: if, total, None, and score.

Hints
  • The source identifies if and None as reserved words.
  • A variable is a name created by the programmer.
Worked example

Checking four proposed names

Determine which names belong to Python and which can serve as programmer-created variable names.

  1. 1
    if

    if is a reserved word and belongs to Python's fixed vocabulary.

  2. 2
    total

    total is presented as a programmer-created name, so it represents the variable side of the distinction.

  3. 3
    None

    None is a reserved word and has a special meaning to Python.

  4. 4
    score

    score is presented as a programmer-created name, so it represents a variable name rather than a reserved word.

Result

if and None are reserved words; total and score are programmer-created variable names in this generated example.

  • Common mistake

    Treating def as an available variable name because it looks like a short ordinary word.

    What it looks like

    Choosing def for a variable.

    Why it fails

    def is one of Python's reserved words and has a fixed meaning to the language.

    Do this instead

    Choose a different name that is not a reserved word.

  • Common mistake

    Assuming Python will run first and report the naming conflict later.

    What it looks like

    Expecting a program with a reserved-word variable name to begin execution.

    Why it fails

    Python catches this mistake before execution begins and rejects the program.

    Do this instead

    Check proposed names before running the program.

  • Common mistake

    Confusing a reserved word with a variable.

    What it looks like

    Thinking that if, for, or None are names created by the programmer.

    Why it fails

    These words belong to Python's fixed vocabulary.

    Do this instead

    Remember that reserved words tell Python what to do, while variables are names you create for data.

Key Takeaways

  1. 1
    Python has a fixed set of 35 reserved words with unchangeable, singular meanings.
  2. 2
    def is a reserved word, so it cannot be used as a variable name.
  3. 3
    Reserved words form Python's vocabulary; variables form the programmer's vocabulary.
  4. 4
    Python rejects a program that uses a reserved word as a variable name before execution begins.
  5. 5
    When a proposed variable name conflicts with a reserved word, choose a different name.

At a glance

  • Reserved words are Python's fixed vocabulary, consisting of 35 words with unchangeable meanings.
  • The word def is reserved and cannot be used as a variable name.
  • Variables are names created by the programmer, with the restriction that they cannot be reserved words.
  • Python detects a reserved-word naming conflict before execution and rejects the program.
  • Recognizing the difference between Python's words and your names helps you choose valid variable names.

Related concepts

More concepts starting with D