Numbers & operators · Números y operadores
Two kinds of number
- An integer (
int) is a whole number, like7or-3. - A float is a number with a decimal point, like
2.5. - Division with
/always gives a float, even4 / 2→2.0.
Dos tipos de números
- Un entero (
int) es un número entero, como7o-3. - Un float (punto flotante) es un número con punto decimal, como
2.5. - La división con
/siempre devuelve un float, incluso4 / 2→2.0.
Add, subtract, multiply, divide
+adds,-subtracts,*multiplies,/divides.- You can use them with variables or with plain numbers.
- Spaces around operators are optional but make code easier to read.
Sumar, restar, multiplicar, dividir
+suma,-resta,*multiplica,/divide.- Se pueden usar con variables o con números simples.
- Los espacios alrededor de los operadores son opcionales, pero facilitan la lectura del código.
print(7 + 2)
print(7 - 2)
print(7 * 2)
print(7 / 2)
Whole-number division, remainder, powers
//divides and keeps only the whole part (floor division).%gives the remainder (called modulo).**raises to a power:2 ** 3is 2×2×2.
División de enteros, residuo y potencias
//divide y conserva solo la parte entera (división entera).%da el residuo (llamado módulo).**eleva a una potencia:2 ** 3es 2×2×2.
print(7 // 2)
print(7 % 2)
print(2 ** 5)
Order of operations
- Python does
**first, then*///%, then+-. - This is the same order you use in maths.
- Use parentheses
(...)to change the order.
Orden de las operaciones
- Python ejecuta primero
**, luego*///%, y finalmente+-. - Este es el mismo orden que se usa en matemáticas.
- Use paréntesis
(...)para cambiar el orden.
print(2 + 3 * 4)
print((2 + 3) * 4)
Common mistakes
/always gives a float (4 / 2is2.0); use//when you want a whole number.- Power is
**, not^. "3" + "4"joins text into"34"— it does not add to7.
Errores comunes
/siempre devuelve un float (4 / 2es2.0); use//cuando desee un número entero.- La potencia se escribe con
**, no con^. "3" + "4"une textos para formar"34"— no suma para dar7.
Now you try
- Store each answer in the variable named in the task.
- Press Check answer to test your code.
Ahora tú lo intentas
- Almacena cada respuesta en la variable cuyo nombre se indica en la tarea.
- Pulsa Comprobar respuesta para probar tu código.
Order of operations · Orden de operaciones
Python evaluates × and ÷ before + and −, left to right. · Python evalúa × y ÷ antes que + y −, de izquierda a derecha.
Set · Conjunto length to · hasta 8 and · y width to · hasta 5. Then store their product in a variable called area. · Asigna length el valor 8 y width el valor 5. Luego almacena su producto en una variable llamada area.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
You share 17 sweets among 5 children. Store how many each · cada child gets in each (use //), and how many are left over in · hacia adentro left (use %). · Repartes 17 dulces entre 5 niños. Almacena cuántos recibe cada niño en each (usa //) y cuántos sobran en left (usa %).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Store 3 ** 2 + 1 in · hacia adentro result. Then use parentheses to store (3 + 1) ** 2 in · hacia adentro grouped. · Almacena 3 ** 2 + 1 en result. Luego usa paréntesis para almacenar (3 + 1) ** 2 en grouped.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
celsius is 37. Store the temperature in Fahrenheit in fahrenheit, using the formula celsius × 9 ÷ 5 + 32. · celsius es 37. Almacena la temperatura en Fahrenheit en fahrenheit, usando la fórmula celsius × 9 ÷ 5 + 32.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.