Intro to Python
Share
Explore
Learn Python

Variables, Expressions, and Statements

This section covers:
Storing values in variables
Evaluating expressions
Statements
Variables
Variables
are containers for storing
values
. 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

Input Function
Python’s
input
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.
Expressions
An
expression
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
Statements
A
statement
is like an instruction. We’ve already seen examples of statements: declaring and assigning a variable is a statement, and using
print
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!
Task
Status
1
Learn about variables
Not Started
2
Learn about expressions
Not Started
3
Learn about statements
Not Started
No results from filter
Next Section ➡️


Share
 
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.