Menu iconMenu icon
Python y SQL Biblia

Capítulo 2: Bloques de construcción de Python

2.4 Ejercicios Prácticos

Para reforzar los conceptos que hemos discutido en este capítulo, prueba los siguientes ejercicios. Las soluciones se proporcionan después de cada pregunta, pero intenta completar los ejercicios por tu cuenta antes de ver las respuestas.

Ejercicio 1: Crea un programa en Python que tome dos números como entradas del usuario, realice todas las operaciones aritméticas básicas con estos números e imprima los resultados.

Solución:

# taking two numbers as inputs from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# performing arithmetic operations
add = num1 + num2
subtract = num1 - num2
multiply = num1 * num2
divide = num1 / num2

# printing the results
print(f"The sum of {num1} and {num2} is: {add}")
print(f"The difference between {num1} and {num2} is: {subtract}")
print(f"The product of {num1} and {num2} is: {multiply}")
print(f"The quotient of {num1} and {num2} is: {divide}")

Ejercicio 2: Crea un programa en Python que solicite al usuario un número y luego imprima si el número es par o impar.

Solución:

# asking the user for a number
num = int(input("Enter a number: "))

# checking whether the number is even or odd
if num % 2 == 0:
    print(f"{num} is an even number.")
else:
    print(f"{num} is an odd number.")

Ejercicio 3: Crea un programa en Python que utilice operadores de comparación para comparar dos números proporcionados por el usuario e imprima si son iguales y, si no lo son, cuál es el más grande.

Solución:

# taking two numbers as inputs from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# using comparison operators to compare the numbers
if num1 == num2:
    print("The numbers are equal.")
elif num1 > num2:
    print(f"The number {num1} is larger.")
else:
    print(f"The number {num2} is larger.")

Ejercicio 4: Crea un programa en Python que utilice operadores lógicos para determinar si un número ingresado por el usuario está dentro de un rango específico.

Solución:

# taking a number as input from the user
num = float(input("Enter a number: "))

# specifying the range
lower_bound = 10
upper_bound = 20

# checking whether the number is within the range
if num >= lower_bound and num <= upper_bound:
    print(f"The number {num} is within the range of {lower_bound} and {upper_bound}.")
else:
    print(f"The number {num} is outside the range of {lower_bound} and {upper_bound}.")

Estos ejercicios te ayudarán a practicar la sintaxis de Python, trabajar con variables y diferentes tipos de datos, y usar los operadores básicos de Python. Recuerda siempre que la práctica es clave cuando se aprende un nuevo lenguaje de programación.


Si deseas practicar Python y eventualmente convertirte en un experto, te recomendamos nuestro libro, "Python Practice Makes a Master: 120 'Real World' Python Exercises with more than 220 Concepts Explained.” Será una gran adición a tu viaje. Puedes encontrarlo en Amazon.

https://www.amazon.com/dp/B0BZF8R4BZ


2.4 Ejercicios Prácticos

Para reforzar los conceptos que hemos discutido en este capítulo, prueba los siguientes ejercicios. Las soluciones se proporcionan después de cada pregunta, pero intenta completar los ejercicios por tu cuenta antes de ver las respuestas.

Ejercicio 1: Crea un programa en Python que tome dos números como entradas del usuario, realice todas las operaciones aritméticas básicas con estos números e imprima los resultados.

Solución:

# taking two numbers as inputs from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# performing arithmetic operations
add = num1 + num2
subtract = num1 - num2
multiply = num1 * num2
divide = num1 / num2

# printing the results
print(f"The sum of {num1} and {num2} is: {add}")
print(f"The difference between {num1} and {num2} is: {subtract}")
print(f"The product of {num1} and {num2} is: {multiply}")
print(f"The quotient of {num1} and {num2} is: {divide}")

Ejercicio 2: Crea un programa en Python que solicite al usuario un número y luego imprima si el número es par o impar.

Solución:

# asking the user for a number
num = int(input("Enter a number: "))

# checking whether the number is even or odd
if num % 2 == 0:
    print(f"{num} is an even number.")
else:
    print(f"{num} is an odd number.")

Ejercicio 3: Crea un programa en Python que utilice operadores de comparación para comparar dos números proporcionados por el usuario e imprima si son iguales y, si no lo son, cuál es el más grande.

Solución:

# taking two numbers as inputs from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# using comparison operators to compare the numbers
if num1 == num2:
    print("The numbers are equal.")
elif num1 > num2:
    print(f"The number {num1} is larger.")
else:
    print(f"The number {num2} is larger.")

Ejercicio 4: Crea un programa en Python que utilice operadores lógicos para determinar si un número ingresado por el usuario está dentro de un rango específico.

Solución:

# taking a number as input from the user
num = float(input("Enter a number: "))

# specifying the range
lower_bound = 10
upper_bound = 20

# checking whether the number is within the range
if num >= lower_bound and num <= upper_bound:
    print(f"The number {num} is within the range of {lower_bound} and {upper_bound}.")
else:
    print(f"The number {num} is outside the range of {lower_bound} and {upper_bound}.")

Estos ejercicios te ayudarán a practicar la sintaxis de Python, trabajar con variables y diferentes tipos de datos, y usar los operadores básicos de Python. Recuerda siempre que la práctica es clave cuando se aprende un nuevo lenguaje de programación.


Si deseas practicar Python y eventualmente convertirte en un experto, te recomendamos nuestro libro, "Python Practice Makes a Master: 120 'Real World' Python Exercises with more than 220 Concepts Explained.” Será una gran adición a tu viaje. Puedes encontrarlo en Amazon.

https://www.amazon.com/dp/B0BZF8R4BZ


2.4 Ejercicios Prácticos

Para reforzar los conceptos que hemos discutido en este capítulo, prueba los siguientes ejercicios. Las soluciones se proporcionan después de cada pregunta, pero intenta completar los ejercicios por tu cuenta antes de ver las respuestas.

Ejercicio 1: Crea un programa en Python que tome dos números como entradas del usuario, realice todas las operaciones aritméticas básicas con estos números e imprima los resultados.

Solución:

# taking two numbers as inputs from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# performing arithmetic operations
add = num1 + num2
subtract = num1 - num2
multiply = num1 * num2
divide = num1 / num2

# printing the results
print(f"The sum of {num1} and {num2} is: {add}")
print(f"The difference between {num1} and {num2} is: {subtract}")
print(f"The product of {num1} and {num2} is: {multiply}")
print(f"The quotient of {num1} and {num2} is: {divide}")

Ejercicio 2: Crea un programa en Python que solicite al usuario un número y luego imprima si el número es par o impar.

Solución:

# asking the user for a number
num = int(input("Enter a number: "))

# checking whether the number is even or odd
if num % 2 == 0:
    print(f"{num} is an even number.")
else:
    print(f"{num} is an odd number.")

Ejercicio 3: Crea un programa en Python que utilice operadores de comparación para comparar dos números proporcionados por el usuario e imprima si son iguales y, si no lo son, cuál es el más grande.

Solución:

# taking two numbers as inputs from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# using comparison operators to compare the numbers
if num1 == num2:
    print("The numbers are equal.")
elif num1 > num2:
    print(f"The number {num1} is larger.")
else:
    print(f"The number {num2} is larger.")

Ejercicio 4: Crea un programa en Python que utilice operadores lógicos para determinar si un número ingresado por el usuario está dentro de un rango específico.

Solución:

# taking a number as input from the user
num = float(input("Enter a number: "))

# specifying the range
lower_bound = 10
upper_bound = 20

# checking whether the number is within the range
if num >= lower_bound and num <= upper_bound:
    print(f"The number {num} is within the range of {lower_bound} and {upper_bound}.")
else:
    print(f"The number {num} is outside the range of {lower_bound} and {upper_bound}.")

Estos ejercicios te ayudarán a practicar la sintaxis de Python, trabajar con variables y diferentes tipos de datos, y usar los operadores básicos de Python. Recuerda siempre que la práctica es clave cuando se aprende un nuevo lenguaje de programación.


Si deseas practicar Python y eventualmente convertirte en un experto, te recomendamos nuestro libro, "Python Practice Makes a Master: 120 'Real World' Python Exercises with more than 220 Concepts Explained.” Será una gran adición a tu viaje. Puedes encontrarlo en Amazon.

https://www.amazon.com/dp/B0BZF8R4BZ


2.4 Ejercicios Prácticos

Para reforzar los conceptos que hemos discutido en este capítulo, prueba los siguientes ejercicios. Las soluciones se proporcionan después de cada pregunta, pero intenta completar los ejercicios por tu cuenta antes de ver las respuestas.

Ejercicio 1: Crea un programa en Python que tome dos números como entradas del usuario, realice todas las operaciones aritméticas básicas con estos números e imprima los resultados.

Solución:

# taking two numbers as inputs from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# performing arithmetic operations
add = num1 + num2
subtract = num1 - num2
multiply = num1 * num2
divide = num1 / num2

# printing the results
print(f"The sum of {num1} and {num2} is: {add}")
print(f"The difference between {num1} and {num2} is: {subtract}")
print(f"The product of {num1} and {num2} is: {multiply}")
print(f"The quotient of {num1} and {num2} is: {divide}")

Ejercicio 2: Crea un programa en Python que solicite al usuario un número y luego imprima si el número es par o impar.

Solución:

# asking the user for a number
num = int(input("Enter a number: "))

# checking whether the number is even or odd
if num % 2 == 0:
    print(f"{num} is an even number.")
else:
    print(f"{num} is an odd number.")

Ejercicio 3: Crea un programa en Python que utilice operadores de comparación para comparar dos números proporcionados por el usuario e imprima si son iguales y, si no lo son, cuál es el más grande.

Solución:

# taking two numbers as inputs from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# using comparison operators to compare the numbers
if num1 == num2:
    print("The numbers are equal.")
elif num1 > num2:
    print(f"The number {num1} is larger.")
else:
    print(f"The number {num2} is larger.")

Ejercicio 4: Crea un programa en Python que utilice operadores lógicos para determinar si un número ingresado por el usuario está dentro de un rango específico.

Solución:

# taking a number as input from the user
num = float(input("Enter a number: "))

# specifying the range
lower_bound = 10
upper_bound = 20

# checking whether the number is within the range
if num >= lower_bound and num <= upper_bound:
    print(f"The number {num} is within the range of {lower_bound} and {upper_bound}.")
else:
    print(f"The number {num} is outside the range of {lower_bound} and {upper_bound}.")

Estos ejercicios te ayudarán a practicar la sintaxis de Python, trabajar con variables y diferentes tipos de datos, y usar los operadores básicos de Python. Recuerda siempre que la práctica es clave cuando se aprende un nuevo lenguaje de programación.


Si deseas practicar Python y eventualmente convertirte en un experto, te recomendamos nuestro libro, "Python Practice Makes a Master: 120 'Real World' Python Exercises with more than 220 Concepts Explained.” Será una gran adición a tu viaje. Puedes encontrarlo en Amazon.

https://www.amazon.com/dp/B0BZF8R4BZ