Variables · Variáveis
A box with a name
- A variable is a named box that holds a value.
- You put a value in the box, then use the name later.
- The value can be a number or a string.
Uma caixa com nome
- Uma variável é uma caixa nomeada que armazena um valor.
- Você coloca um valor na caixa, depois usa o nome depois.
- O valor pode ser um número ou uma string.
Make a variable
- Write
name = valueto store a value. - The name is on the left. The value is on the right.
=means "store this value", not "is equal to".
Criar uma variável
- Escreva
name = valuepara armazenar um valor. - O nome está à esquerda. O valor está à direita.
=significa "armazene este valor", não "é igual a".
city = "Paris"
print(city)
Naming rules
- Use letters, digits, and the underscore
_. - A name cannot start with a digit and cannot have spaces.
- Names are case-sensitive:
scoreandScoreare different. - Pick clear names like
ageortotal_score.
Regras de nomenclatura
- Use letras, dígitos e o underline
_. - Um nome não pode começar com um dígito e não pode conter espaços.
- Os nomes são sensíveis a maiúsculas/minúsculas:
scoreeScoresão diferentes. - Escolha nomes claros como
ageoutotal_score.
Change a variable
- You can store a new value at any time.
- The new value replaces the old one.
- The name still points to one value — the latest one.
Alterar uma variável
- Você pode armazenar um novo valor a qualquer momento.
- O novo valor substitui o antigo.
- O nome ainda aponta para um único valor — o mais recente.
A variable is a labelled box · Uma variável é uma caixa rotulada
Step through the program. Each line stores a value in a named box; reassigning count overwrites its box. One name, one current value. · Passo a passo pelo programa. Cada linha armazena um valor em uma caixa nomeada; reatribuir count sobrescreve sua caixa. Um nome, um valor atual.
count = 1
count = 2
print(count)
Constants
- A constant is a value that should not change.
- Python has no special keyword for it.
- By convention, we write constant names in
UPPERCASE.
Constantes
- Uma constante é um valor que não deve ser alterado.
- Python não possui uma palavra-chave especial para isso.
- Por convenção, escrevemos nomes de constantes em
UPPERCASE.
PI = 3.14159
print(PI)
Common mistakes
- Use
=to store a value: the name goes on the left, the value on the right. - Use a variable only after you have given it a value, or you get a NameError.
- Names cannot start with a number or contain spaces.
Erros comuns
- Use
=para armazenar um valor: o nome fica à esquerda, o valor à direita. - Use uma variável apenas depois de atribuir um valor a ela, caso contrário obterá um NameError.
- Nomes não podem começar com um número nem conter espaços.
Now you try
- Use a variable for each task below.
- Press Check answer to test your code.
Agora você tenta
- Use uma variável para cada tarefa abaixo.
- Clique em Check answer para testar seu código.
Create a variable called age and store the number 15 in it. · Crie uma variável chamada age e armazene o número 15 nela.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
The variable color starts as "red". Reassign it so that it becomes "blue". · A variável color começa como "red". Reatribua para que se torne "blue".
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Constants are written in UPPERCASE by convention. Create a constant MAX_SCORE and set it to 100. · Constantes são escritas em MAIÚSCULAS por convenção. Crie uma constante MAX_SCORE e defina-a como 100.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
a is 1 and · e b is 2. Swap them, so a becomes 2 and · e b becomes 1, without typing the numbers 1 or · ou 2 again. (Tip: a third variable can hold one value for a moment.) · a é 1 e b é 2. Troque-os, para que a vire 2 e b vire 1, sem digitar os números 1 ou 2 novamente. (Dica: uma terceira variável pode guardar um valor por um momento.)
Click Run to see the output here. · Clique em Executar para ver a saída aqui.