Variables and Values

Computer can not only compute numbers, but also remember them. Variables work like containers that can hold values such as numbers, strings, and other types of data.

Simply put, a variable is an name that refers to a value.

Start a new line in the code editor, then type and run the following code:

x = 10
print(x)

Console result:

10

The first line assigns the integer value 10 to a variable x. x is the variable name and does not require quotation marks because it is not a string. It now holds the value 10, and when we input it to print(), 10 is printed out.

Assignment

You can create a new variable and assign any value to it like this:

variable_name = value

The variable is on the left of the equal sign. On the right is the value you want to assign to the variable, such as a number or a string.

Variable names can include letters, numbers, and underscores, such as y, a3, my_number, and _stuff. A variable name cannot start with a number. For example, 1candy is not allowed.

Next let's assign a string value "Bob" to a variable called name:

name = "Bob"
print(name)

Console result:

Bob

As we continue the lesson, you'll see Python has many other types of data. We can assign them to variables just like strings or integers.

results matching ""

    No results matching ""