Unit 2 · Python mode... Activate! · Lesson 12 of 57
Varying Variables
The First of Many
I lied to you!
We actually won't get into inputs right away, because you need to learn something very important first.
It's a……………………………… variable!
A variable, at its core, is something we use to store information. You pick a name, put an = after it, and then give it the value you want to store.
Say I need you to store information about CyberAcad. Can you do that for me?
Of course you can! (Kind of.)
Strings hold text
If I asked you to store CyberAcad's name, you'd store it as a string, which is just text. Strings always go inside quotes:
name = "CyberAcad"
Integers hold whole numbers
If I asked you to store CyberAcad's age in months, you'd store it as an integer, a fancy term for a number that doesn't have a decimal point. Numbers don't get quotes:
age = 7
Floats hold decimals
What about Pie-....no.... Pi? It has a decimal point, so it gets stored as something called a float:
pi = 3.14
Booleans hold True or False
What if you wanted to store something that's either true or false?
karl_is_super_cool = True
This is called a boolean, or bool for short. A bool can only ever be True or False.
Notice how the T in
Trueand the F inFalseare capitalized? That's important! Python won't know what you're talking about if they aren't.
Cheat sheet
Make these four variables:
year, with the integer value1991creator, with the string valueGuido van Rossumversion, with the float value1.0knowsPython, with the bool valueTrue
Heads up: storing a variable doesn't print anything, so it's totally normal if the console stays empty when you press run. Press Check my work to see if you got it!
Python exercise
Write the program in an editor next to the lesson, run it, and have it checked automatically. Create a free account to do this part.
Do this lesson for real
With a free account you get the terminals, challenges and checks that go with the lessons in Intro to Python, and your progress is saved as you go. CyberAcad is a 501(c)(3) nonprofit, so there is nothing to pay.