Table of contents
- Introduction
- How do Variables Work?
- Defining Variables
- Altering Variables
- Displaying Variables
- Why Use Variables
- Works Cited
- “Variable.” Techopedia, 10 June 2022, www.techopedia.com/definition/25647/ variable-programming. Accessed 07 Oct. 2023.
- “Constants in Programming Language: What Are Constants?: Definition.” Toppr, 21 May 2021, www.toppr.com/guides/computer-science/introduction-to-c/data-types-variables-and-constants/constants-in-programming-language/. Accessed 07 Oct. 2023.
Introduction
Python variables are an easy way to store information temporarily (as long as your code is running) in RAM (Random Access Memory). Variables aren't just in Python, they are everywhere. Javascript, Java, C, C++, Ruby, and so many more. In this article, I will teach you how variables work, how to define them, and how to use them.
How do Variables Work?
Variable work in an interesting way. They use RAM to temporarily store information for use while your program is running. When your program ends, the variables stored in RAM and cleared out, as if they never existed. Little pieces in RAM hold an electrical charge that represents bits of data. If the charge isn't refreshed quickly, the charge slowly gets weaker and weaker, losing the data it holds. RAM is a piece of hardware in every computer. The motherboard connects to the RAM to use it.
Imagine RAM as the following one-column table.
RAM |
Now, let's say I have a variable named hello
in my Python code with the value of a string 'Hello!' When the program is run, the RAM will now look like this.
RAM |
Hello 'Hello!' |
But once my program ends, the RAM empties the variables out and just forgets it. So that's why you can't use a variable without defining it. Python will look in your RAM for that variable. The nice thing with variables is that you can change their values throughout the program. Taking our previous hello
variable, if I set it to 'Hello!' and then set it somewhere else to 'Hi!', the RAM will look like this.
RAM |
Hello |
Notice how the original is crossed out. When you are displaying RAM to some other person, it's good to cross old values out instead of erasing them so that even without the code, someone looking at a table like this will understand the values the variable has had or currently has. This is also known as 'tracing' a program. You are tracing the steps that execute in a program. You are acting like the compiler/interpreter. Now let's see how to define a variable in Python!
Defining Variables
If you have worked with Java or another similar language, you know that defining a variable takes a type, the name, and value, or null. Well, that's not how Python works! In Python, you don't need to set a variable to a specific type. The interpreter already understands the type of the variable when the sees the value. If there is no value, the interpreter also reads that. The benefit of not having a specific type in a variable is that you can change a variable with a string of 'Hello!' to an integer of 1. Here is how to define a variable in Python.
fullName = "Spewing Facts"
phone_number = 1111111111
age = 100.3463
PROGRAMMING_EXPERIENCE = True
The lines of code above show different ways to define a variable and how to store primitive types in them. The variable fullName
is in the format of camelCase and stores a string. Camel case is a variable name format where the first letter of each word in your variable is capitalized except the first one. All variable formats can still hold any data type, it's just a look. The second variable is in snake_case and stores an integer. Snake case is a variable format where each word is separated by an underscore. The third variable is just one word so nobody knows what case it's in. The variable stores a float. The final one is known as SCREAMING_SNAKE_CASE and is usually used to define a constant. In this format, every letter in the variable is capitalized and the words are separated by an underscore.
Altering Variables
As I said earlier, variables can be altered to have different values. And their type can also be altered. Here is how to change a variable through your program.
fullName = "Spewing Facts"
phone_number = 1111111111
age = 100.3463
PROGRAMMING_EXPERIENCE = True
# Altering to Different Value But Same Type
fullName = "Facts Spewing"
phone_number = 9999999999
age = 1.2932
PROGRAMMING_EXPERIENCE = False
# Altering to Different Value and Type
fullName = "Spewing Facts" # Changing the Type Wouldn't be Ideal Here
phone_number = 1111111111 # Changing the Type Wouldn't be Ideal Here
age = 39 # Changing the Variable to an Integer
PROGRAMMING_EXPERIENCE = "No" # Changing the Variable to a String
The lines of code above show different ways to alter the value of a variable. The first alter section is altering the variables but keeping their type the same. The second alter section shows altering the variable and changing the type. Changing the type of the fullName
and phone_number
variables wouldn't be ideal because why would you have an integer for a variable named fullName? And why would a phone number be a float or boolean? It's not ideal. This is why you have to make sure you keep variable names reasonable. You can't alter a variable name because the interpreter will just create a new variable.
Displaying Variables
Displaying variables is just like displaying a data type that's not stored in a variable. Just print it onto the console. Try and do that yourself right now before looking down below at the code!
fullName = "Spewing Facts"
phone_number = 1111111111
age = 100.3463
PROGRAMMING_EXPERIENCE = True
print(fullName, phone_number, age, PROGRAMMING_EXPERIENCE)
Printing the variables with a print method for each variable still works. This is just another way. Keep in mind that a print method adds a new line at the end of every data. So doing them each on a separate line will put them on different lines. But printing them out on the same line would just display them in the console on one line. Try printing out the variables, altering them, and then printing them out again!
fullName = "Spewing Facts"
phone_number = 1111111111
age = 100.3463
PROGRAMMING_EXPERIENCE = True
print(fullName, phone_number, age, PROGRAMMING_EXPERIENCE)
fullName = "Facts Spewing"
phone_number = 9999999999
age = 1.29384
PROGRAMMING_EXPERIENCE = False
print(fullName, phone_number, age, PROGRAMMING_EXPERIENCE)
When you run this code, you will see that the first original variables are printed and then the altered variables are printed.
Why Use Variables
Let's say you are working on a project with 1000 lines of code and some of the same text (data type) is written on almost every line of code and in different files. Now your boss says that the text that is everywhere should say something else. Do you want to change every line of code where that text is? I don't think so. The solution is to define a variable at the top of your program with your desired case and set it to that text. Now, if your boss says he/she wants the text changed, you just need to change the text in the variable definition! Variables are reusable and that's why every programmer has used them hundreds of times. Hope you learned something new today!