Advent of Code 2024

aoc-2024

Leer más » Comentar

Python GIL

python-gil

Leer más » Comentar

Fobia

fobia

Leer más » Comentar

Rendimiento

rendimiento

Leer más » Comentar

Lenguaje Eco-friendly

eco-friendly

Leer más » Comentar

Python Lento

Python Lento

Leer más » Comentar

Python3

Leer más » Comentar

Calcula si un número es par o impar en Python

Uno de los problemas que propongo a mis alumnos cuando están aprendiendo condicionales en un lenguaje de programación es calcular si un número entero es par o impar.

Aquí os dejo el enunciado del problema y una propuesta de solución en Python. Espero que os sirva tanto si es para la enseñanza como para el aprendizaje.

Problema:

Imprime en pantalla si un número entero dado es par o impar.

Solución propuesta (didáctico):

La clave está en utilizar la operación módulo, que calcula el resto de una división entera: si un entero es divisible por dos, es par; si no, impar.

import sys

a = int(sys.argv[1])

if a % 2 == 0:
    print('El número', a, 'es par.')
else:
    print('El número', a, 'es impar.')

Leer más » Comentar

Aprende a usar variables en Python

Aquí os dejo un código mío de ejemplo para empezar a aprender a usar variables de tipo básico en Python.

Es recomendable copiar el código y ejecutarlo en un intérprete de Python y, a continuación, realizar los cambios que consideremos oportunos para comprender el funcionamiento.

# This is a comment

'''
This is a comment
on multiple lines
'''

# VARIABLES
#
# A variable is a symbol that represents a quantity that may vary.
#
# $identifier = value;

age = 25 # The value 25 is assigned to variable age

# BASIC DATA TYPES
age = 25 # Integer
temperature = -3.82 # Real number
name = 'Nacho López' # String
has_car = True # Boolean (only two values: True or False)

# ARITHMETIC OPERATIONS WITH NUMBERS
x = 5
y = 2

z = x + y # Addition. Result: 7.
z = x - y # Subtraction. Result: 3.
z = x * y # Multiplication. Result: 10.
z = x / y # Division. Result: 2.5.
z = x % y # Modulo (remainder of the integer division). Result: 1.

z = z + 1 # Increase the value of z by 1. Result: 2.
z = z - 1 # Decrease the value of z by 1. Result: 1.

z = 50 - x * 6 / -0.5 #
z = (50 - x) * 6 / -0.5 # The order of operations is as in mathematics
z = (50 - x * 6) / -0.5 #

z = 2 * z + 3 # Remember: the symbol = assigns a value to the variable

# BASIC OPERATIONS WITH STRINGS
a = 'GNU/'
b = 'Linux'
c = a + b # Concatenation Result: 'GNU/Linux'.
c = a * 3 # Repetition Result: 'GNU/GNU/GNU/'.

# PRINT VARIABLES ON SCREEN

print('Hello, world!') # Prints on screen: Hello, world!
print(x) # Prints the variable x

# You can print on screen strings and variables
print('I have bought', x, 'oranges and', y, 'lemons.')

# DATA TYPE CONVERSION

height = '95.4'
print(type(height)) # Prints the current data type
height = float(height) # Convert a string to a real number
print(type(height))

altitude = -544.432
print(type(altitude))
altitude = str(altitude) # Convert a real number to string
print(type(altitude))
read more

Leer más » Comentar

Python2

Leer más » Comentar