Python

[Basic Grammar] Python Variables: Variable Names

grace21110 2023. 10. 15. 07:00
반응형

Rules for Python variables: 

  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)
  • A variable name cannot be any of the Python keywords

 

For better understanding, please refer to the following example. 

 

Example 

Legal variable names:

myvar = "Will"
my_var = "Will"
_my_var = "Will"
myVar = "Will"
MYVAR = "Will"
myvar2 = "Will"

Ilegal variable names: 

2myvar = "Kim"
my-var = "Kim"
my var = "Kim"

 

 

  • Techniques to make it more readeable
  • Camel Case 

Each word, except the first, starts with a capital letter:

myVariableName = "David"
  • Pascal Case 

Each word starts with a capital letter:

MyVariableName = "John"
  • Snake Case 
my_variable_name = "Berry"