Variable Names in Python

Mridu Bhatnagar
InstructorMridu Bhatnagar

Share this video with your friends

Send Tweet
Published 4 months ago
Updated 3 months ago

Variable names in Python can be of any length. Variables names can have alphabets [A-Z][a-z], digits [0-9] and underscores. A variable name starting with a digit is an invalid variable name.

Mridu Bhatnagar: [0:00] We can do colors = { 'red', 'yellow', 'blue', 'green' }. We can have another variable name as rainbow_colors, and then assign this variable name to an object that has rainbow colors in it. Let us do sequentially 'violet', 'indigo', 'blue', 'green', then you have 'yellow', 'orange', and 'red'.

[0:53] A variable name that has underscore in it is also valid. Variable name can be of uppercase as well. I can do AGE = 30. Variable names are case sensitive. When you do age = 30, this would be considered as a new variable.

[1:27] We can also have rainbow_colors_7. That makes it a combination of alphabets, underscore as well as digit. This is also valid in Python. You can have a combination of underscore, digit as well as alphabets in it.

[1:53] Let us see what happens if we keep first letter of a variable name as a digit. Can I have a variable name as 7_rainbow_colors? Let us see if this name is valid or not. This time we got an error. That means variable name cannot start with a digit.