top of page

What is a variable in Python?

  • NalediumConcept Staff
  • Feb 3, 2024
  • 2 min read

Variable is any name refereeing to a value.


For example:


b = 27,


b is the variable and 27 is the value.


The process of binding b to 27 with an equal sign is called assignment, so by writing b = 27, 27 is assigned to b in the address of the computer memory.


There are rules to naming a variable in python.


1. Naming a variable should be self-explanatory


The name or variable should be self-explanatory. That is the variable should be descriptive. For instance, a programmer that wants to show the sum of all the cars can write:


a = 10


alternatively


cars = 10


alternatively


total_cars = 10


As you can see the third variable is more descriptive and this is best practice. The point here is you want to remove ambiguity to naming your variables so that you or any other programmer that comes in contact with you code can understand you. This is particularly important when you are collaborating on a project.



2. Naming a variable should use snake case to remove space


The other naming rule is the use snake case

Following the example above,


total_cars = 10


is a snake case of


total cars = 10 (would give you a syntax error)



3. Naming a variable in Python is case sensitive



For example:

Numbers = 27 and numbers = 27 are not the same variable even though they hold the same value.


Now that you know how to assign value to a variable in Python and why it is preferred for variable to be descriptive, there are other things to avoid that can cause problems because you would get syntax error.


- Variable name cannot start with a number

- Do not include space in your variable name

- Avoid using special characters Include special characters

- Avoid using Python keywords such as and, for, return, if etc we will learn more about these later.


 
 
 

Recent Posts

See All
My Python Journey so far

Please be aware that this is a new site and still testing things out. Write up may contain unintentional grammatical errors. A few years...

 
 
 

Comments


Top Stories

Thanks for subscribing!

  • Instagram
  • X

© 2024 Naledium Concept 

bottom of page