Python Variables and Identifiers: Rules, Examples, and Practice

0

Variables and identifiers are fundamental to programming in Python. In this guide, you’ll learn how to name variables correctly, avoid syntax errors, and follow Python’s naming conventions. We’ll also explore reserved keywords and practice with hands-on exercises.

Variables and Identifiers in Python

What Are Variables?

Variables are containers for storing data. They are created using an identifier (name) and the assignment operator =.

age = 25
name = "Mourad"
user_name = 'admin'
server_ip = '192.168.1.1'

Rules for Naming Identifiers

  • Can include letters, numbers, and underscores.
  • Cannot start with a number.
  • Cannot use reserved keywords (e.g., def, class).
  • Case-sensitive (Ageage).

Invalid Identifiers Example

#def = 5  # SyntaxError: "def" is a reserved keyword

Reserved Keywords

Python has predefined keywords that cannot be used as identifiers. View them using the keyword module:

import keyword
print(keyword.kwlist)

Practice Work

Exercise 1: Fix Invalid Identifiers

Correct the invalid identifiers in the code below:

2nd_name = "Ali"
class = "Python101"
user-email = "test@example.com"
Solution:

second_name = "Ali"
course_class = "Python101"
user_email = "test@example.com"

Exercise 2: Create Valid Identifiers

Write variables for the following data:

  1. A constant for maximum login attempts (value: 3).
  2. A string storing a server’s domain name (“api.example.com”).
  3. A boolean indicating whether a user is active (True/False).
Solution:

MAX_LOGIN_ATTEMPTS = 3
server_domain = "api.example.com"
is_user_active = True

Exercise 3: Reserved Keywords Check

Write a script to check if the word "async" is a reserved keyword in Python.

Solution:

import keyword
print("async" in keyword.kwlist)  # Output: True (in Python 3.7+)

Workshop: Real-World Practice

Workshop 1: User Registration Script

Create a script that asks for a user’s first name, last name, and age. Store the data in variables and print a summary. Follow these rules:

  • Use snake_case for variable names.
  • Avoid reserved keywords.
first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")
user_age = int(input("Enter your age: "))

print(f"User: {first_name} {last_name}, Age: {user_age}")

Workshop 2: Data Processing

Calculate the total price of items in a shopping cart using variables:

item1_price = 20.5
item2_price = 15.75
item3_price = 5.0
total = item1_price + item2_price + item3_price
print(f"Total: ${total:.2f}")

Best Practices

  • Use Descriptive Names: user_age instead of a.
  • Follow Case Conventions:
    • snake_case for variables (e.g., server_ip).
    • UPPER_CASE for constants (e.g., MAX_USERS).
  • Avoid Abbreviations: first_name is clearer than fn.

Conclusion

Mastering variables and identifiers is the first step to writing clean, maintainable Python code. Practice the exercises and workshops to solidify your understanding, and always follow Python’s naming conventions.

Want more? Check out our post on Python Data Types or Python Functions.

 

Choose your Reaction!
Leave a Comment

Your email address will not be published.