Concepts / Defining Functions with def

Defining Functions with def

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

  • 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.

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

Reserved Words in Python

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.

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.

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

Choosing a non-conflicting name

A programmer wants to name a variable def.

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

Predict Python's response: Python will reject the program before execution begins rather than allowing def to serve as a variable name.

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.

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.

Checking four proposed names

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

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

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

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

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

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

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

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

    Fix: Choose a different name that is not a reserved word.

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

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

    Fix: Check proposed names before running the program.

  • Confusing a reserved word with a variable.

    These words belong to Python's fixed vocabulary.

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

Key Takeaways

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

Key Takeaways

  • 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.