How do I name variables?

QuestionsQuestions8 SkillsProYour First Python LabNov, 23 2025
059

When naming variables in Python, you should follow these guidelines:

  1. Use Descriptive Names: Choose names that clearly describe the purpose of the variable. For example, use total_price instead of tp.

  2. Use Lowercase Letters: Variable names should start with a lowercase letter and can include letters, numbers, and underscores. For example, user_age, item_count.

  3. Avoid Special Characters: Variable names cannot contain special characters (like @, #, $, etc.) or spaces.

  4. No Starting with a Number: Variable names cannot start with a number. For example, 1st_item is invalid, but item1 is valid.

  5. Use Underscores for Readability: If a variable name consists of multiple words, separate them with underscores for better readability. For example, first_name, total_score.

  6. Case Sensitivity: Variable names are case-sensitive. For example, myVariable and myvariable are considered different variables.

Example:

# Valid variable names
user_name = "Alice"
total_score = 95
is_active = True

# Invalid variable names
# 1st_item = "apple"  # Invalid: starts with a number
# total-price = 100    # Invalid: contains a special character

By following these conventions, you can create clear and maintainable variable names in your Python code.

0 Comments

no data
Be the first to share your comment!