Python Tutorials

Python Keywords

Keywords in Python are reserved words that have a special meaning and specific purpose in the programming language. You cannot use a keyword as a variable name, function name, class name, or any other identifier.

They are the fundamental building blocks of a Python program, used to define the syntax and structure of the code.

Characteristics of Python Keywords

Before memorizing the keywords, you should know these strict rules regarding their usage:

  1. Reserved Words: Every keyword is reserved for a specific task. For example, the def keyword is only used to create a function.
  2. Case-Sensitive: Python is a case-sensitive language, and so are its keywords.
  3. Capitalization: Out of all the standard keywords, only three start with a capital letter: True, False, and None. Every other keyword is written entirely in lowercase letters (e.g., if, for, while).

How to Check Python Keywords

If you are wondering how many keywords in python exist, the exact number can change depending on the version of Python you are using. In Python 3.13+, there are exactly 35 keywords.

You do not need to memorize the entire list. You can print all the current keywords directly in your Python terminal using the built-in keyword module.

# Importing the keyword module
import keyword

# Printing the total number of keywords
print("Total Keywords:", len(keyword.kwlist))

# Printing the list of all keywords
print(keyword.kwlist)
Code language: PHP (php)

Output:

Total Keywords: 35
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
Code language: CSS (css)

Important Python Keyword Categories

To make learning easier, we can categorize Python keywords based on their functionality.

1. Value Keywords (True, False, None)

These keywords represent specific data values.

  • True and False: Used to represent boolean truth values. They are the result of comparison operations.
  • None: Represents the absence of a value or a null value.
x = 10
y = 5

print(x > y)  # Returns True
Code language: PHP (php)

2. Logical Operator Keywords (and, or, not)

These keywords are used to combine conditional statements.

  • and: Returns True if both statements are true.
  • or: Returns True if at least one statement is true.
  • not: Reverses the result (returns False if the result is true).
age = 20
print(age > 18 and age < 30)  # Returns True because both conditions match
Code language: PHP (php)

3. Control Flow Keywords (if, elif, else)

These keywords are used to make decisions in the code based on certain conditions.

marks = 85

if marks >= 90:
    print("Grade A")
elif marks >= 80:
    print("Grade B")
else:
    print("Grade C")
Code language: PHP (php)

4. Iteration / Looping Keywords (for, while, break, continue, pass)

These are used to execute a block of code multiple times.

  • for: Used to iterate over a sequence (like a list or string).
  • while: Used to execute a block as long as a condition is true.
  • break: Stops the loop entirely.
  • continue: Skips the current iteration and moves to the next one.
for i in range(1, 4):
    print("Number:", i)
Code language: PHP (php)

5. Function and Class Keywords (def, return, class, lambda)

  • def: Used to define a standard user-defined function.
  • return: Used to exit a function and output a specific value.
  • class: Used to define an object-oriented class.
  • lambda: Used to create small, anonymous functions.
def add_numbers(a, b):
    return a + b

print(add_numbers(5, 10))
Code language: PHP (php)

6. Exception Handling Keywords (try, except, finally, raise)

When your code encounters an error, it crashes. These keywords are used to handle errors gracefully.

  • try: Defines a block of code to test for errors.
  • except: Defines a block of code to execute if an error occurs.
  • finally: Defines a block of code that executes regardless of the try-except result.

Keywords as Variable Names

It is a strict rule in Python that you cannot use keywords as identifiers (variable names, function names, etc.). Because keywords have predefined compiler meanings, assigning a new value to them results in a SyntaxError.

Incorrect Usage:

# Trying to use a keyword as a variable
True = 100
for = "Python"
Code language: PHP (php)

Output:

SyntaxError: cannot assign to True
SyntaxError: invalid syntax
Code language: HTTP (http)

If you ever need a variable name that sounds like a keyword, a common industry practice is to add an underscore at the end (for example, class_ or return_).

Keywords vs Identifiers

In Python, you will frequently hear the terms “keywords” and “identifiers”. It is important to know the clear difference between them:

  • Keywords: These are predefined, reserved words built into the Python language (like if, for, def). You cannot change their meaning or redefine them.
  • Identifiers: These are user-defined names given to entities like variables, functions, or classes (like age, calculate_sum, Student). You create them based on specific naming rules.

You can think of keywords as the grammar rules of Python, while identifiers are the custom vocabulary you create for your specific program.

Variables vs Keywords

A variable is the most common type of identifier. Beginners often get confused between variables and keywords. Here is a direct comparison:

FeatureKeywordsVariables
DefinitionReserved words with predefined meanings.User-defined containers used to store data in memory.
CreationBuilt into the Python language.Created by the programmer using an assignment operator (=).
CharactersAlways written in alphabetical characters (mostly lowercase).Can contain letters, numbers, and underscores.
UsageUsed to define program structure and execution logic.Used to hold values that can change during program execution.

Tags: #can you use keywords as variable names in python #categories of python keywords explained #difference between python keywords and identifiers #examples of control flow keywords in python #exception handling keywords in python explained #how to print all keywords in python #how to use the built-in keyword module in python #python 3.13 total keywords list #python keywords list and their uses #python keywords vs variables explanation #python reserved words tutorial for beginners #what are value keywords in python

Leave a Comment