Variables
So what is a variable?
A variable can best be described as a box.
We have a label on the box and that's the variable's name,
Then we have something in the box and that's the variable's data or content.
So when we want to make a new variable we need to give it a name and then what ever data we want to store...
my_name = "Beanzilla"
In this example, we have a variable called my_name that is assigned the data "Beanzilla".
So how do we access it?
print(my_name)
Beanzilla
Ok what else can we do?
my_fav_number = 9
my_least_fav_number = 1
sum = my_fav_number + my_least_fav_number
print(sum)
10
In the above example we now show you can add 2 (or more) variables together to make a 3rd variable...
But thats not all!
myself = {"name": "Beanzilla", "pets_name": "Fishstick"}
print(myself["name"])
Beanzilla
What is this?
Let's create a list of some data types in python:
- my_string = ""
- my_int = 0
- my_dict = {}
- my_list = []
- my_float = 0.0
And of course there is more data types out there in python.
So the above example is a dict or dictonary, it stores key and value...
myself = {"name": "Beanzilla", "pets_name": "Fishstick"}
# We have 2 keys, name and pets_name
# We have 1 value for each key
# name = Beanzilla
# pets_name = Fishstick
print(myself)
{'name': 'Beanzilla', 'pets_name': 'Fishstick'}
Ugly... but if we give it a key it will print it nicely...
print(myself["pets_name"])
Fishstick
Up next... String Format and F String