Variables

Python Variables

If you have any experince with computer programming at all, in any way, shape or form, then you should at least have the basic understanding of what a variable is. For people who don’t know what a variable in coding is, it is a simple way to store information. For example, if you want to store the value 3 somewhere to use it in multiple places in your program, that is when you would use a variable. This simple article will help you understand what a variable is in Python, how to define one, and how to properly use it in your code.

Like I said before, a variable in Python is used for storing any type of information. Wether it is a number, a string(of letters/words) or practically anything else, a variable can be used. To define a variable in Python, you must write the name of the variable, followed by an equal to sign and then whatever must be stored inside the variable:

In this example we used the variable to store a number, however, if you want to store a string, you should surround it with quotation marks for it to be valid:

You can essentially name your variable anything you like, however it is best to name it something unique and easy to remember.

So now that we know how to define variables, now we can talk about how to actually use them in our programs. We can use variables in several different situations: we use variables to store information and be referenced to, and even manipulated in our programs. It also helps to label our information with a discriptive name so that our programs can be clean, organized and easilly understood. Here is an example on how we can use variables in a simple beginner program:

If you do the math on your own, you’ll be able to figure out that after we run this program, the computer should return this as our answer:

I hope this tutorial helped in understand Python Variables! As always, leave any questions or concerns down in the comment section.

Close Bitnami banner
Bitnami