Home Python Tutorial Python Keywords and Identifiers

Python Keywords and Identifiers

by anupmaurya
4 minutes read

In this article, you’ll learn about Python Keywords and Identifiers and more.

Python, a versatile and widely-used programming language, relies on specific keywords and identifiers to construct its code.

Python Keywords

  • Keywords are the reserved words in Python.
  • We cannot use a keyword as a variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language.
  • In Python, keywords are case sensitive.

It’s difficult to neatly categorize all 35 Python keywords into a table because they serve vastly different purposes. However, Here we grouped them by general functionality to give you a better overview:

CategoryKeywordsDescription
Conditional Statementsif, elif, elseControl the flow of execution based on conditions.
Loopsfor, while, break, continue, passIterate over sequences or execute blocks of code repeatedly. break exits a loop, continue skips to the next iteration, and pass is a placeholder.
Functionsdef, return, lambdaDefine and return values from functions. lambda creates anonymous functions.
Classes and Objectsclass, isinstance, issubclassDefine classes and check object relationships.
Exception Handlingtry, except, finally, raiseHandle errors and exceptions during program execution.
Boolean Values and OperatorsTrue, False, and, or, not, in, isRepresent truth values and perform logical/membership/identity comparisons.
Imports and Modulesimport, from, asImport modules and specific objects from modules. as is used for aliasing.
Context ManagementwithManage resources (like files) efficiently.
GeneratorsyieldCreate iterators that produce values on demand.
Variable Scopeglobal, nonlocalControl the scope of variables within functions.
DeletiondelDelete objects or references.
Null ValueNoneRepresents a null or empty value.

All the keywords except TrueFalse and None are in lowercase.

Example

if x > 10:  # 'if' is a keyword
    print("x is greater than 10")
else:  # 'else' is a keyword
    print("x is not greater than 10")

for i in range(5):  # 'for' and 'in' are keywords
    print(i)

def greet(name):  # 'def' is a keyword
    return "Hello, " + name  # 'return' is a keyword

is_active = True  # 'True' is a keyword

class MyClass: # 'class' is a keyword
    pass # 'pass' is a keyword

Python Identifiers

An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate one entity from another.

Rules for Identifiers

  • Character Set: Identifiers can contain letters (a-z, A-Z), digits (0-9), and underscores (_).
  • First Character: The first character of an identifier cannot be a digit. It must be a letter or an underscore.
  • Case Sensitivity: Python is case-sensitive. my_variable and My_Variable are considered different identifiers.
  • No Keywords: Identifiers cannot be the same as any of Python’s keywords.
  • No Special Characters: Identifiers cannot contain special characters like !, @, #, $, %, etc.
  • Length: While there’s no official limit on the length of an identifier, it’s best to keep them reasonably short and descriptive.

Example

name = "Alice"  # 'name' is an identifier
age = 30  # 'age' is an identifier

def calculate_sum(x, y):  # 'calculate_sum', 'x', and 'y' are identifiers
    total = x + y  # 'total' is an identifier
    return total

result = calculate_sum(5, 10)  # 'result' is an identifier
print(result)

Things to Remember

  • Python is a case-sensitive language. This means, Variable and variable are not the same.
  • Always give the identifiers a name that makes sense. While c = 10 is a valid name, writing count = 10 would make more sense, and it would be easier to figure out what it represents when you look at your code after a long gap.
  • Multiple words can be separated using an underscore, like this_is_a_long_variable.

Best Practices for Identifiers

  • Be Descriptive: Choose names that clearly indicate the purpose of the variable or function.
  • Follow Conventions: Adhere to common naming conventions (e.g., snake_case for variables and functions, CamelCase for classes).
  • Be Consistent: Use the same naming style throughout your code.
  • Avoid Single-Character Names (except for simple loop counters): i, j, k are acceptable for short loops, but more descriptive names are better in most cases.
  • Use Meaningful Abbreviations: If you need to shorten a name, use abbreviations that are easily understood.

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.