Understand Identifiers in Python

PythonBeginner
Practice Now

Introduction

In this lab, you will gain a comprehensive understanding of identifiers in Python. You will learn the fundamental rules governing how to name variables, functions, classes, and other objects in your Python code.

Through hands-on practice, you will identify valid and invalid identifier names, reinforcing the naming conventions. The lab will also cover special identifier conventions used in Python, equipping you with the knowledge to write clear, readable, and maintainable code.

Learn Identifier Naming Rules

In this step, we will learn about the rules for naming identifiers in Python. Identifiers are names used to identify variables, functions, classes, modules, and other objects in a program. Understanding the naming rules is crucial for writing readable and maintainable code.

As mentioned in the introduction, Python identifiers must follow these rules:

  • Identifiers can consist of letters (a-z, A-Z), digits (0-9), and underscores (_).
  • The first character of an identifier cannot be a digit.
  • Identifiers cannot contain spaces or special characters like @, %, $, *, etc.
  • Identifiers are case-sensitive. myVariable and myvariable are considered different identifiers.
  • You cannot use Python keywords or built-in function names as identifiers.

Let's create a simple Python file to practice defining some identifiers.

Open the integrated terminal in the WebIDE by clicking on Terminal -> New Terminal. Ensure you are in the ~/project directory.

Now, let's create a new Python file named identifier_rules.py in the ~/project directory using the VS Code editor. You can do this by right-clicking in the file explorer pane on the left and selecting New File, or by using the command palette (Ctrl+Shift+P or Cmd+Shift+P) and typing File: New File. Name the file identifier_rules.py.

In the identifier_rules.py file, type the following code:

## This is a valid identifier
my_variable = 10

## This is also a valid identifier
anotherVariable = "Hello"

## This is a valid identifier starting with an underscore
_private_variable = True

## This is an invalid identifier (starts with a digit)
## 1st_variable = 5

## This is an invalid identifier (contains a space)
## my variable = "invalid"

## This is an invalid identifier (contains a special character)
## my-variable = 20

## This is an invalid identifier (using a keyword)
## if = 30

print(my_variable)
print(anotherVariable)
print(_private_variable)

Save the file by pressing Ctrl+S (or Cmd+S).

Now, let's run this Python script from the terminal to see the output. In the terminal, make sure you are in the ~/project directory and execute the following command:

python identifier_rules.py

You should see the output of the print statements, confirming that the valid identifiers were processed correctly. The lines with invalid identifiers are commented out, so they won't cause errors when running the script.

10
Hello
True

This exercise demonstrates the basic rules for creating valid identifiers in Python. In the next steps, we will explore more about valid and invalid identifiers and common naming conventions.

Practice Valid Identifier Names

In this step, we will practice creating valid identifier names in Python based on the rules we learned in the previous step. Choosing meaningful and valid names for your variables, functions, and other code elements is a fundamental part of writing good code.

Let's create a new Python file named valid_identifiers.py in the ~/project directory using the VS Code editor.

In the valid_identifiers.py file, type the following code. This code defines several variables using valid identifier names and prints their values.

## Valid variable names
user_age = 30
total_count = 100
item_price_usd = 25.50
is_active = True
_internal_value = "secret"
MAX_RETRIES = 5

## Valid function name (we will learn about functions later)
def calculate_total():
    return user_age * total_count

## Valid class name (we will learn about classes later)
class UserProfile:
    def __init__(self, name):
        self.name = name

## Print the values of the variables
print("User Age:", user_age)
print("Total Count:", total_count)
print("Item Price (USD):", item_price_usd)
print("Is Active:", is_active)
print("Internal Value:", _internal_value)
print("Maximum Retries:", MAX_RETRIES)

## Call the function and print the result
## print("Calculated Total:", calculate_total())

## Create an instance of the class and print a property
## user = UserProfile("Alice")
## print("User Name:", user.name)

Save the file by pressing Ctrl+S (or Cmd+S).

Now, let's run this Python script from the terminal. Make sure you are in the ~/project directory and execute the following command:

python valid_identifiers.py

You should see the values of the variables printed to the console.

User Age: 30
Total Count: 100
Item Price (USD): 25.5
Is Active: True
Internal Value: secret
Maximum Retries: 5

Notice how the variable names use underscores (_) to separate words, which is a common convention in Python for variable and function names (snake_case). Class names, like UserProfile, typically use uppercase letters for the first letter of each word (PascalCase). Constants, like MAX_RETRIES, are often written in all uppercase with underscores.

By practicing with these examples, you become more familiar with creating valid and readable identifier names in Python.

Identify Invalid Identifier Names

In this step, we will focus on identifying invalid identifier names in Python. Understanding what makes an identifier invalid is just as important as knowing the rules for valid names. Attempting to use an invalid identifier will result in a SyntaxError when you try to run your Python code.

Let's create a new Python file named invalid_identifiers.py in the ~/project directory using the VS Code editor.

In the invalid_identifiers.py file, type the following code. This code contains examples of invalid identifier names. We will intentionally include these to see the errors they produce.

## Invalid identifier: starts with a digit
## 1variable = 10

## Invalid identifier: contains a space
## my variable = "hello"

## Invalid identifier: contains a special character (@)
## user@name = "Alice"

## Invalid identifier: contains a special character (-)
## product-id = "XYZ123"

## Invalid identifier: using a Python keyword
## class = "Math"

## Invalid identifier: using another Python keyword
## for = 100

## Invalid identifier: contains a special character ($)
## total$amount = 50.75

## Invalid identifier: contains a special character (%)
## discount%rate = 0.15

print("Attempting to define invalid identifiers will cause a SyntaxError.")

Save the file by pressing Ctrl+S (or Cmd+S).

Now, let's try to run this Python script from the terminal. Make sure you are in the ~/project directory and execute the following command:

python invalid_identifiers.py

Since all the invalid identifiers are commented out, the script will run without errors and print the message.

Attempting to define invalid identifiers will cause a SyntaxError.

Now, let's uncomment one of the invalid identifiers to see the error. Remove the # from the beginning of the line ## 1variable = 10. The line should now be 1variable = 10.

Save the file again.

Now, run the script again:

python invalid_identifiers.py

This time, you should see a SyntaxError indicating that the identifier is invalid because it starts with a digit.

  File "/home/labex/project/invalid_identifiers.py", line 4
    1variable = 10
    ^
SyntaxError: invalid decimal literal

You can try uncommenting other invalid identifiers one by one and running the script to see the different types of SyntaxError messages they produce. Remember to comment out the previous invalid identifier before uncommenting the next one to isolate the error.

This step helps you recognize common mistakes when naming identifiers and understand the importance of following the naming rules to avoid syntax errors.

Understand Special Identifier Conventions

In this step, we will explore some special conventions for using underscores in Python identifiers. These conventions don't necessarily affect whether an identifier is valid or invalid according to the basic rules, but they convey specific meanings about the intended use or visibility of the identifier.

Here are the common underscore conventions:

  • Single leading underscore (_name): This convention indicates that the identifier is intended for internal use within a module or class. It's a hint to other programmers that they shouldn't directly access this identifier from outside the module or class. However, Python does not strictly enforce this; you can still access it if you choose.
  • Double leading underscore (__name): This convention is used for name mangling in classes. When an identifier within a class starts with two underscores (and doesn't end with two underscores), Python internally changes the name to make it harder to access from outside the class directly. This helps prevent naming conflicts in inheritance.
  • Double leading and trailing underscores (__name__): These identifiers are reserved for special use by the Python interpreter. Examples include __init__ (constructor), __str__ (string representation), etc. You should avoid creating your own identifiers with this pattern unless you are implementing one of Python's special methods.
  • Single trailing underscore (name_): This convention is used to avoid naming conflicts with Python keywords. If you want to use a name that is a Python keyword (like class or for), you can append an underscore to make it a valid identifier (e.g., class_, for_).

Let's create a new Python file named underscore_conventions.py in the ~/project directory using the VS Code editor to see these conventions in action.

In the underscore_conventions.py file, type the following code:

## Single leading underscore: intended for internal use
_internal_data = "This is internal data"

## Double leading underscore: name mangling in classes
class MyClass:
    def __init__(self):
        self.__private_attribute = "This is a private attribute"

    def get_private_attribute(self):
        return self.__private_attribute

## Double leading and trailing underscores: special method
class MyString:
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return f"MyString object with value: {self.value}"

## Single trailing underscore: to avoid keyword conflict
import builtins
class_ = "This variable name avoids conflict with the 'class' keyword"

print("Internal data:", _internal_data)

## Accessing the private attribute (demonstrating name mangling)
obj = MyClass()
## print(obj.__private_attribute) ## This will cause an AttributeError
print("Accessing private attribute via method:", obj.get_private_attribute())
## You can technically access it using the mangled name, but it's not recommended
## print("Accessing mangled name:", obj._MyClass__private_attribute)

## Using the special __str__ method
my_str_obj = MyString("Hello World")
print(my_str_obj) ## This calls the __str__ method

print("Variable name avoiding keyword conflict:", class_)

Save the file by pressing Ctrl+S (or Cmd+S).

Now, run the script from the terminal:

python underscore_conventions.py

You should see the output demonstrating the use of these conventions.

Internal data: This is internal data
Accessing private attribute via method: This is a private attribute
MyString object with value: Hello World
Variable name avoiding keyword conflict: This variable name avoids conflict with the 'class' keyword

Notice that attempting to directly access obj.__private_attribute would result in an AttributeError due to name mangling. Accessing it through the get_private_attribute method or the mangled name _MyClass__private_attribute works, but the convention is to use the method. The print(my_str_obj) call automatically uses the __str__ method because it's a special method for string representation.

Understanding these conventions helps you interpret the intent behind identifier names in Python code and write code that follows common practices.

Summary

In this lab, we learned the fundamental rules for naming identifiers in Python. We practiced creating valid identifier names using letters, digits, and underscores, while adhering to the rule that identifiers cannot start with a digit. We also identified invalid identifier names that contained spaces, special characters, or were Python keywords. Furthermore, we explored special identifier conventions, such as using a leading underscore for "private" variables.