Reserved words in Python

Reaserved keywords in Python Python has a list of keywords which it uses for its syntax and internal processing. We can’t use these reserved words in our program as variable names or function names or as identifiers.

List of 35 reserved keywords in Python

This list may change with different versions of Python
FalseNoneTrueandas
assertasyncawaitbreakclass
continuedefdelelifelse
exceptfinallyforfromglobal
ifimportinislambda
nonlocalnotorpassraise
returntrywhilewithyield


Python reserved words list using keyword module with methods to check words

How to know if the name is included in the reserved key word list or not ?

We are checking the key word ( variable name ) nonlocal
import keyword
print(keyword.iskeyword('nonlocal')) # True

Display the full list of reserved keywords

We can display full list of reserved keywords in Python ( current version )
import keyword 
print(keyword.kwlist)
To know the number of words in the reserved keyword list
print(len(keyword.kwlist)) # 35  ( may change ) 
If we use any of the above listed words as variable then we will get the error message SyntaxError: invalid syntax

def is a reserved word ( for defining functions )
This line will generate error
def=45
SyntaxError: invalid syntax

Python variables are case sensitive, so break is a reserved keyword but Break is not. Check the outputs below.
import keyword
print(keyword.iskeyword('break')) # True
print(keyword.iskeyword('Break')) # False

Common Python Keywords and Their Purpose

Keyword Purpose
if Used for conditional branching
for Used to create a loop that iterates over a sequence
def Defines a function
class Used to define a class
return Exits a function and optionally returns a value

Reserved Words and Syntax Highlighting:

Mostlikely reserved words are typically highlighted in different Integrated Development Environments (IDEs) and text editors. This helps in syntax highlighting for code readability and error prevention.

Understanding why they are reserved:

These reserved words have specific meanings and functionalities in Python, like controlling control flow, defining loops, handling data types, etc. Using them for other purposes would clash with their existing roles and create confusion. Remember, using descriptive variable names and avoiding reserved words makes your code more readable and maintainable for yourself and others.

Difference between Keywords and Buitin functions

Difference Between Python Keywords and Built-in Functions

Python Keywords
  • Definition: Reserved words that define Python's syntax and structure.
  • Cannot be used as variable names.
  • Examples: if, else, for, def, class
  • Purpose: Control flow, function definitions, classes, etc.
Python Built-in Functions
  • Definition: Predefined functions provided by Python.
  • Used to perform specific tasks or operations.
  • Examples: print(), len(), input(), type()
  • Purpose: Perform actions like input/output, conversions, math, etc.
Example Code
if True:
    print("Hello, Python!")

Explanation: if and True are keywords, while print() is a built-in function.


Built in Functions in Python


Questions

  • Python reserved words listing and checking #python #reserwords
    14-Feb-2024 quixote
Check all our Live sessions on basics of Python

variables & identifiers in Python Built in Functions in Python

Podcast on Python Basics


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer