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.
Una caja con nombre
- Una variable es una caja con nombre que almacena un valor.
- Colocas un valor en la caja y luego usas el nombre más adelante.
- El valor puede ser un número o una cadena de texto.
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".
Crear una variable
- Escribe
name = valuepara almacenar un valor. - El nombre está a la izquierda. El valor está a la derecha.
=significa "almacena este valor", no "es 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.
Reglas de nombrado
- Usa letras, dígitos y el guion bajo
_. - Un nombre no puede comenzar con un dígito ni contener espacios.
- Los nombres distinguen entre mayúsculas y minúsculas:
scoreyScoreson diferentes. - Elige nombres claros como
ageototal_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.
Cambiar una variable
- Puedes almacenar un nuevo valor en cualquier momento.
- El nuevo valor reemplaza al anterior.
- El nombre sigue apuntando a un solo valor — el más reciente.
A variable is a labelled box · Una variable es una caja etiquetada
Step through the program. Each line stores a value in a named box; reassigning count overwrites its box. One name, one current value. · Recorre el programa. Cada línea almacena un valor en una caja con nombre; reasignar count sobrescribe su caja. Un nombre, un valor actual.
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
- Una constante es un valor que no debería cambiar.
- Python no tiene una palabra clave especial para ello.
- Por convención, escribimos los nombres de las constantes en
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.
Errores comunes
- Usa
=para almacenar un valor: el nombre va a la izquierda y el valor a la derecha. - Usa una variable únicamente después de haberle asignado un valor, o obtendrás un NameError.
- Los nombres no pueden comenzar con un número ni contener espacios.
Now you try
- Use a variable for each task below.
- Press Check answer to test your code.
Ahora te toca
- Usa una variable para cada tarea siguiente.
- Presiona Check answer para probar tu código.
Create a variable called age and store the number 15 in it. · Crea una variable llamada age y almacena el número 15 en ella.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
The variable color starts as "red". Reassign it so that it becomes "blue". · La variable color comienza como "red". Reasígnala para que se convierta en "blue".
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Constants are written in UPPERCASE by convention. Create a constant MAX_SCORE and set it to 100. · Por convención, las constantes se escriben en MAYÚSCULAS. Crea una constante MAX_SCORE y establécela en 100.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
a is 1 and · y b is 2. Swap them, so a becomes 2 and · y b becomes 1, without typing the numbers 1 or 2 again. (Tip: a third variable can hold one value for a moment.) · a es 1 y b es 2. Intercámbialos, de modo que a sea · mar 2 y b sea · mar 1, sin volver a escribir los números 1 o 2. (Consejo: una tercera variable puede guardar un valor momentáneamente.)
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.