Storing values in variables
are containers for storing
. There are many different types of values, but some simple ones are strings (any combination of characters, like “apple” or “100%”), numbers (ex: 4 or 0.5), and booleans (true or false).
To start off, you can declare a variable with a specific name and a value.
is_the_sky_blue = True
favorite_fruit = "apple"
number_of_variables = 3
After it’s declared, you can assign a different value to the variable, or you can use the current value of the variable.
print(favorite_fruit) # prints "apple"
favorite_fruit = "kiwi"
print(favorite_fruit) # prints "kiwi" now
function allows you to collect input from the user.
name = input("What's your name?")
print("Hi, " + name + "!")
Here, it’s helpful to use a variable to store whatever the user inputs.
is a combination of values, variables, operators (ex: math operators like + - * /), and function calls (to be explained in a later section!) that can be evaluated to a single value.
Examples of expressions include:
4 * 5 # evaluates to 20
"tri" + "angle" # evaluates to "triangle"
number_of_variables - 1 # evaluates to 2
is like an instruction. We’ve already seen examples of statements: declaring and assigning a variable is a statement, and using
is also a statement.
This is different than an expression because a statement does something. However, you can use expressions in your statements. Here is an example of declaring a variable and assigning it to the evaluation of an expression:
new_variable = number_of_variables * 2
In the upcoming sections, we will learn more about the other types of statements!