Saral Code logo
Saral Code
    HomeBlogTutorialsMCQs

No Related Topics Available


Python is a widely-used programming language that is known for its simplicity and ease of use. In this tutorial, we will take a look at the rules and conventions for writing variable names in Python.

Letters, Numbers, and Underscores

In Python, variable names can be written using letters, numbers, and underscores.

  • The only limitation is that variable names cannot start with a number.

For example, the following are valid variable names:

Python
my_variable = 10
x = 20
variable_1 = "Hello"

Reserved Words

Python has certain words that are reserved and cannot be used as variable names.

These words include print, if, else, while, for, and many others. Using a reserved word as a variable name will result in a syntax error.

Python
# this will result in a syntax error
if = 10
while = "hello"
for = 23.3

Descriptive and Meaningful Names

It is a good practice to give descriptive and meaningful names to the variables that you create in your code.

For example, instead of using a variable name like x, you can use a name like customer_name or product_price which makes it clear what the variable represents.

Python
customer_name = "John Doe"
product_price = 19.99
customer_age = 25

Snake_case, CamelCase, PascalCase

  1. Snake Case: In snake case, all letters are lowercase and words are separated by an underscore.
    For example, snake_case_example in Python.
  2. Camel Case: In camel case, the first letter of each word is capitalized except for the first word, which is written in lowercase.
    For example, camelCaseExample in Python.
  3. Pascal Case: In Pascal case, the first letter of each word is capitalized, including the first word.
    For example, PascalCaseExample in Python.

These different cases are mainly used for naming conventions in Python. Camel case is commonly used for variable and function names, while snake case is often used for constants and file names. Pascal case is also used for class and module names.

Python
customer_name = "John Doe" # snake_case
customerName = "John Doe" # camelCase
CustomerName = "John Doe" # PascalCase

Case Sensitivity

It is important to note that Python is case-sensitive, so the variable names name and Name are different.

Python
# Declaring two variables with different capitalization
name = "Ankit"
Name = "Aayush"

# Printing the values of the variables
print(name) # Output: "Ankit"
print(Name) # Output: "Aayush"

Conclusion

  1. Variable names must start with a letter or an underscore (_).
  2. Variable names can only contain letters, numbers, and underscores (_).
  3. Variable names cannot contain spaces or special characters, such as !, @, #, etc.
  4. Variable names cannot be a Python keyword, such as if, else, while, etc.
  5. Variable names should be descriptive and meaningful so that it's easy to understand what the variable represents.
  6. Variable names should be written in lowercase, with words separated by underscores(_) to make them readable.
  7. Variable names should avoid using common Python built-in functions or module names such as str, list, len, etc.
  8. Avoid using single-letter variable names, they are not meaningful and could lead to confusion.
  9. Be consistent with naming conventions throughout the code, whether it's snake_case, camelCase, or PascalCase.

In conclusion, writing variable names in Python is easy and straightforward. By following the rules and conventions outlined in this, you can make your code more readable and maintainable. Remember to use descriptive and meaningful names, snake_case, camelCase, and PascalCase, and be aware of the reserved words and case sensitivity. Happy coding!