Course Content
Introduction to Python
3. Conditional Statements
Introduction to Python
Naming Rules
As we discussed in the last chapter, variables are essential for holding values that we'll use later on. When naming variables in Python, there are some rules and best practices to keep in mind:
- Variable names can include letters, numbers, or underscores:
myVariable1
number_2
data_frame3
Python4Life
_underscoreVar
- Variable names can't start with a number:
variable1
(Correct)1variable
(Incorrect - starts with a number)
- Variable names are case-sensitive:
MyVariable
andmyvariable
are different variables.Data
anddata
are different variables.Number1
andnumber1
are different variables.
- Apart from the underscore
_
, special characters aren't allowed:my_variable
(Correct)my-variable
(Incorrect - contains a hyphen)variable@name
(Incorrect - contains an@
)data*star
(Incorrect - contains an asterisk*
)
- Reserved words in Python can't be used as variable names:
forLoop
(Correct - not a reserved word)ifCondition
(Correct - not a reserved word)for
(Incorrect -for
is a reserved word)while
(Incorrect -while
is a reserved word)try
(Incorrect -try
is a reserved word)
Which of these would be valid variable names in Python?
Select a few correct answers
Note
If you're uncertain about why a particular variable name is considered correct or incorrect, you can refer back to the above rules and examples for clarification.
Everything was clear?
Section 2. Chapter 2