Names and Variables
In the previous lesson, we learned about basic math operations and how Python can help perform calculations.
In this lesson, we will learn more about how we can use variables to refer to objects.
DEFINITION
Variable: A variable is a name which is used to refer to a value. A variable can refer to a number, a string of text, or an object.
Warm up
To begin this lesson, paste the following 2 lines of code into the Python editor:
my_number = 24
print(my_number)What do you think will get printed here?
As you might have guessed, 24 will be printed here because we assigned my_number the value of 24.
There are 2 important details to know about a variable.
- Its name, which is how we as programmers will interact with the variable
- Its value, which in this case will be a number
Here's another example of how we can use a variable to store a number
sum = 5 + 6
print(sum)As you might expect, this program will print out 11 to the screen when run.
Lastly, we can use operations on variables as if they were the numbers themselves:
sum = 11
print(sum + 2)And this should print 13 as expected!
Exercises
- create a variable named
ageand set it equal to your age, and print that value + 2 to the screen.
For example, if you are 18 years old, the output of your code should be:
20- print a variable named
zerothat equals0
The output of your code should be:
0Submit here: https://forms.gle/vryURyEUuCXra9jw6
Recap
- We can use variables with human readable names to store, access numbers.
- These variables can be used in place of numbers in math expressions and equations