Be Globally Certified In Python

Python is a great text coding programming language for kids!

Variable rules

In Python, variable names must adhere to certain rules to be valid. Remember that following these rules and using descriptive variable names improves code readability and maintainability, making your code easier to understand by both you and others who may read it.

Rules for Naming Variables:

1. Variable names must begin with a letter (a-z, A-Z) or an underscore (_).

    • Correct: my_var, _data, Name
    • Incorrect: 123abc, @value, 1stPlace
  1. Variable names can contain letters, numbers, and underscores.
    • Correct: my_var_123, data_value, Name2
    • Incorrect: my-var, data@value, my variable
  2. Variable names are case-sensitive.
    • Correct: count, Count, and COUNT are treated as different variables.
    • Incorrect: count and Count are the same variable.
  3. Variable names should not be the same as Python keywords or reserved words (e.g., if, else, for, while, import, def, etc.).
    • Correct: my_var, value_1
    • Incorrect: if, import, class, etc.
  4. Variable names should be descriptive and meaningful to improve code readability.
    • Correct: total_count, user_input, student_age
    • Incorrect: x, temp, var
  5. Conventionally, variable names should use lowercase letters and underscores for readability (snake_case).
    • Correct: user_age, data_value
    • Incorrect: userAge, DataValue
  6. Avoid using single-letter variable names unless for temporary or loop variables.
    • Correct (for temporary or loop variables): i, j, temp
    • Incorrect (for general variables): x, y, a

Correct Variable Names:

Incorrect Variable Names: