Variable Names
In Python, a variable is like a storage box that holds information. It can have a short name, like a or b
, or a more descriptive name, such as year
, dayname
, or total_
area. Choosing a meaningful name for your variables helps you and others understand what information the variable is storing.
- Variable names must begin with a alphabet letter (a to z, A to Z) or an underscore (
_
). - After the first letter, you can use letters, numbers, or underscores in your variable names. Avoid using spaces or special characters.
- Variable names can’t begin with a number. For example,
5total
is not a valid variable name, buttotal5
is. - Python is case-sensitive, meaning
year
andYear
would be considered different variables. - Don’t use Python reserved words (keywords) as variable names. For example, you can’t use
if
,else
,while
, etc., as variable names. - Variable names cannot contain spaces. If you need to represent multiple words, use underscores or capitalize each word (e.g.,
user_age
ortotalArea
). - Python variable only contains Alphabet( a to z, A to Z), Numbers(0 to 9) and Underscore( _ ).
- Pick names that give a clear idea of what the variable represents. For instance, instead of
x
, use something liketotal_count
if the variable is counting something.
Here’s an example explains these rules:
# Valid variable names
year = 25
first_name = "Ronny"
total_area = 100.5
Invalid variable names:
# Invalid variable names (examples)
5total = 50 # Cannot start with a number
my-variable = 10 # Cannot contain hyphens
class = "Python" # Avoid using Python keywords
By following these rules, you ensure that your Python code is readable, understandable, and free of naming conflicts or duplications.
Multi Word Variable Names
This is an method to construct variable in better human readable form.
Python gives us several method to specify the variables. For human more than one words variable can be difficult to read. That’s why python have several technique.
Snake Case(Underscore Separation)
In this method, words are separated by underscores, and the entire variable name is in lowercase.
total_volume = 100.5
user_age = 25
number_of_college_graduates = 5
This style, known as snake case, is widely used in Python and is recommended in the official PEP 8 style guide.
Camel Case
In camel case, the first word starts with a lowercase letter, and each next word starts with an uppercase letter.
totalVolume = 100.5
userAge = 25
numberOfCollegeGraduates = 5
Pascal Case(Upper Camel Case)
Similar to camel case, all the first letter of a word starts with an uppercase letter.
TotalVolume = 100.5
UserAge = 25
NumberOfCollegeGraduates = 5
Usage
Common Usage of Snake Case
- Snake case is the most widely used naming convention in Python.
- Typically used for variable names, function names, and module-level names.
Common Usage of Camel Case
- Camel case is often used for naming classes in Python, especially when following Pascal Case (Upper Camel Case) for class names.
Common Usage of Camel Case
- Pascal case is commonly used for class names in Python.