Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Variables for Numbers | Variables in Python
Python Tutorial for Beginners
course content

Conteúdo do Curso

Python Tutorial for Beginners

Python Tutorial for Beginners

1. Introduction to Programming
2. Variables in Python
3. Comments
4. If...Else Statement

Variables for Numbers

But we can not only assign strings to a variable. You can also assign a number.

1
coins = 70
copy

After writing the code above, every time you call the coins variable in your code, Python will know that you mean 70. You can use this variable in math and more.

Let's see how to add 20 to this variable.

1
print(coins + 20)
copy

Python remembers that coins refers to 70, so it will return 90 (70 + 20 = 90).

Unlike a string, a number does not need to be quoted. This is how Python understands that it is a number with which it can calculate mathematical expressions, and not a text string.

Python can also perform calculations that consist only of variables. For example

1234
price_of_tangerines = 15 price_of_lemon = 7 total_price = price_of_tangerines + price_of_lemon print(total_price)
copy

The variable total_price now has a value of 22.

12
original_number = 17 original_number = original_number + 8
copy

Let's look at very intresting example.

123
price_1 = "100" new_price = price_1 + 17 print(new_price)
copy

This code throws an error because Python cannot sum a string and a number.

It's time to practice.

Tarefa

Assign a number (35) to the variable number. Add 5 to the variable number and assign the result to the same variable. Display the result on the screen using print statement.

Tarefa

Assign a number (35) to the variable number. Add 5 to the variable number and assign the result to the same variable. Display the result on the screen using print statement.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 2. Capítulo 3
toggle bottom row

Variables for Numbers

But we can not only assign strings to a variable. You can also assign a number.

1
coins = 70
copy

After writing the code above, every time you call the coins variable in your code, Python will know that you mean 70. You can use this variable in math and more.

Let's see how to add 20 to this variable.

1
print(coins + 20)
copy

Python remembers that coins refers to 70, so it will return 90 (70 + 20 = 90).

Unlike a string, a number does not need to be quoted. This is how Python understands that it is a number with which it can calculate mathematical expressions, and not a text string.

Python can also perform calculations that consist only of variables. For example

1234
price_of_tangerines = 15 price_of_lemon = 7 total_price = price_of_tangerines + price_of_lemon print(total_price)
copy

The variable total_price now has a value of 22.

12
original_number = 17 original_number = original_number + 8
copy

Let's look at very intresting example.

123
price_1 = "100" new_price = price_1 + 17 print(new_price)
copy

This code throws an error because Python cannot sum a string and a number.

It's time to practice.

Tarefa

Assign a number (35) to the variable number. Add 5 to the variable number and assign the result to the same variable. Display the result on the screen using print statement.

Tarefa

Assign a number (35) to the variable number. Add 5 to the variable number and assign the result to the same variable. Display the result on the screen using print statement.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 2. Capítulo 3
toggle bottom row

Variables for Numbers

But we can not only assign strings to a variable. You can also assign a number.

1
coins = 70
copy

After writing the code above, every time you call the coins variable in your code, Python will know that you mean 70. You can use this variable in math and more.

Let's see how to add 20 to this variable.

1
print(coins + 20)
copy

Python remembers that coins refers to 70, so it will return 90 (70 + 20 = 90).

Unlike a string, a number does not need to be quoted. This is how Python understands that it is a number with which it can calculate mathematical expressions, and not a text string.

Python can also perform calculations that consist only of variables. For example

1234
price_of_tangerines = 15 price_of_lemon = 7 total_price = price_of_tangerines + price_of_lemon print(total_price)
copy

The variable total_price now has a value of 22.

12
original_number = 17 original_number = original_number + 8
copy

Let's look at very intresting example.

123
price_1 = "100" new_price = price_1 + 17 print(new_price)
copy

This code throws an error because Python cannot sum a string and a number.

It's time to practice.

Tarefa

Assign a number (35) to the variable number. Add 5 to the variable number and assign the result to the same variable. Display the result on the screen using print statement.

Tarefa

Assign a number (35) to the variable number. Add 5 to the variable number and assign the result to the same variable. Display the result on the screen using print statement.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

But we can not only assign strings to a variable. You can also assign a number.

1
coins = 70
copy

After writing the code above, every time you call the coins variable in your code, Python will know that you mean 70. You can use this variable in math and more.

Let's see how to add 20 to this variable.

1
print(coins + 20)
copy

Python remembers that coins refers to 70, so it will return 90 (70 + 20 = 90).

Unlike a string, a number does not need to be quoted. This is how Python understands that it is a number with which it can calculate mathematical expressions, and not a text string.

Python can also perform calculations that consist only of variables. For example

1234
price_of_tangerines = 15 price_of_lemon = 7 total_price = price_of_tangerines + price_of_lemon print(total_price)
copy

The variable total_price now has a value of 22.

12
original_number = 17 original_number = original_number + 8
copy

Let's look at very intresting example.

123
price_1 = "100" new_price = price_1 + 17 print(new_price)
copy

This code throws an error because Python cannot sum a string and a number.

It's time to practice.

Tarefa

Assign a number (35) to the variable number. Add 5 to the variable number and assign the result to the same variable. Display the result on the screen using print statement.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 2. Capítulo 3
Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt