Variables
How to modify and manipulate variables in Python
The vast majority of applications, regardless of platform, store data. Variables are one such method of storing data, and they are helpful because an application can constantly manipulate and reference them. One common example is a score in a video game. The score is stored in a variable, and whenever you defeat an enemy or pass a level, the variable’s value increases.
In python, a variable is created with two components:
• Name - you use the name to reference the variable later on
• Value - this is the data that is being stored
Let’s first store a variable named
number
that has the value
5
. Then, let’s use the print function we learned in the “Hello World!” article to see the variable’s value. Here is what it would look like:
Code:
``````number = 5;
print(number);``````
Output:
> 5
Notice that unlike in the “Hello World!” article, we don’t have quotation marks around
number
in the print statement. This is because quotation marks indicate the computer should print the words between them. We don’t want to print the actual word “number,” we want the computer to print the value of the variable named
number
. Here’s what would happen if we surrounded number with quotation marks:
Code:
``````number = 5
print("number")``````
Output:
> number
Math using variables
Now that we know how to create variables, we can manipulate and modify them. For example, here is how we manipulate them using the four basic arithmetic operations:
Code:
``````a = 5
b = 5
print(a + b)
print(a - b)
print(a * b)
print(a / b)``````
Output:
> 10
> 0
> 25
> 1.0
Note: Notice only the last number ends with
.0
even though all four numbers technically end with
.0
. This is because integers and numbers with decimals are stored differently. Python originally stored
a
and
b
as integers but automatically converted them to a number with a decimal when the
/
division operator was used. Thus, when it was printed, it included its decimal despite not having a non-zero decimal.
We modify the value of an already-created variable with a line of code in the form “name of variable = new value of the variable.” If we wanted to change
number
from the previous example to have the value
10
, this is what it would look like:
Code:
``````number = 5
number = 10
print(number)``````
Output:
> 10
In programming, the equals sign does not have the same purpose as in math. Instead, equal signs are used to set the variable on the LEFT equal to the value on the RIGHT. The value on the right is first calculated, then the variable on the left is assigned that value.
You can also use arithmetic when modifying a variable …
Code:
``````number = 5
number = 10 + 5
print(number)``````
Output:
> 15
… and even use the variable itself when modifying it.
Code:
``````number = 5
number = number + 2
print(number)``````
Output:
> 7
If you plan to use the variable itself when modifying it, you can use the shortcut below. This code does the same thing as the previous example but takes less space. You can also use this shortcut with any arithmetic operation by replacing
+
in the expression with the desired operation (
-
,
*
,
/
).
Code:
``````number = 5
number += 2
print(number)``````
Output:
> 7
Variables are an extremely powerful tool that every programmer utilizes daily.
Warning: You may have noticed in our examples that, unlike Java and C++, Python does not require the data type
int
. Compared to Java and C++, Python is a higher-level language, meaning its syntax is closer to how humans communicate than Java and C++. The way Python combines variable data types is just one example; humans, like Python, can interpret context clues while communicating to figure out what is being communicated. However, Python’s flexibility comes at a cost. Python is still a computer program that needs exact instructions and cannot magically figure out what data type a variable is-- behind the scenes, it is manually checking every data type against the value of the variable in order to identify the data type from context clues. It’s doing a lot of unnecessary processing that significantly slows it down compared to Java and C++. This is the tradeoff of being a higher-level language, and it is something all programmers must consider when choosing a language.
You can play with all the code we've used in this article on Replit:
Join our discord server for updates, announcements, future event planning, to ask questions, or just for fun!
Copyright ©2021 Howard County Hour of Code