What is a Variable?
A variable is a “container” or “label used to store data in memory. It gives your data a name so you can reuse and update it in your program.
Baisc Data Types
Data Type | Description | Example |
---|---|---|
Numbers | Whole numbers (integers) or decimals (floats). |
|
Strings | Text enclosed in quotes. |
|
Booleans | True or False values. |
|
You can create variables by assigning values with the =
sign. This is called Initialization or Initializing a variable. To display to the terminal in Python, you use the print() function. You can print Strings and Variables together by separating them by a comma as shown below.
Creating and Printing Variables
name = "Alice"
age = 25
print("My name is", name)
print("I am", age, "years old")
Example:
Reassigning Variables
Variables can be updated by assigning them a new value. This is super useful as you can update variables to reflect changes in the data, such as a new favorite food. Additionally, Python is a Dynamically Typed Language, meaning variables are determined at runtime. This allows you to reassign variables to different data types.
Additional Information:
Runtime - Phase when the program is running and being executed by the computer.
favorite_food = "Pizza"
favorite_food = "Sushi"
print("Now I love", favorite_food)
my_variable = 10 # Initially an integer
my_variable = "Now it's a string" # Reassigned as a string
print(my_variable)
Example:
Download the starter file here.
Open the file in VS Code by hitting File —> Open and then opening the starter file you just downloaded.
Try creating a variable for:
Your favorite color.
Your current mood.
A number of your choice.
Print all three variables in one sentence!