Capítulo 9: Biblioteca Estándar de Python
9.4 Ejercicios Prácticos
Ejercicio 1: Explorando la Biblioteca Math
La biblioteca math de Python tiene varias funciones que se pueden utilizar para operaciones matemáticas. Intenta usar la función sqrt()
para encontrar la raíz cuadrada de un número y las funciones ceil()
y floor()
para redondear un número de punto flotante hacia arriba y hacia abajo, respectivamente.
import math
# Find the square root of a number
print(math.sqrt(16))
# Round a floating-point number up and down
print(math.ceil(4.7))
print(math.floor(4.7))
Ejercicio 2: Manipulación de Datos con Pandas
Crea un DataFrame utilizando la biblioteca pandas con cualquier conjunto de datos de tu elección. Intenta agregar nuevas filas y columnas, y utiliza la función describe()
para obtener un resumen estadístico de los datos.
import pandas as pd
# Creating a pandas DataFrame
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'Occupation': ['Engineer', 'Doctor', 'Teacher']
})
# Adding a new column
df['Salary'] = [70000, 80000, 60000]
# Adding a new row
df = df.append({'Name': 'David', 'Age': 40, 'Occupation': 'Lawyer', 'Salary': 90000}, ignore_index=True)
# Getting a statistical summary of the data
print(df.describe(include='all'))
Ejercicio 3: Operaciones de Archivos con las Bibliotecas os y shutil
Utilizando las bibliotecas os
y shutil
, crea un nuevo directorio, escribe un archivo de texto en ese directorio y luego copia ese archivo a un directorio diferente.
import os
import shutil
# Creating a new directory
os.mkdir('new_directory')
# Writing a text file in the new directory
with open('new_directory/text_file.txt', 'w') as file:
file.write("This is some text.")
# Creating a second directory
os.mkdir('second_directory')
# Copying the text file to the second directory
shutil.copy('new_directory/text_file.txt', 'second_directory/text_file.txt')
Estos ejercicios te ayudarán a comprender y familiarizarte con la biblioteca estándar de Python, así como con bibliotecas clave como pandas, os y shutil.
9.4 Ejercicios Prácticos
Ejercicio 1: Explorando la Biblioteca Math
La biblioteca math de Python tiene varias funciones que se pueden utilizar para operaciones matemáticas. Intenta usar la función sqrt()
para encontrar la raíz cuadrada de un número y las funciones ceil()
y floor()
para redondear un número de punto flotante hacia arriba y hacia abajo, respectivamente.
import math
# Find the square root of a number
print(math.sqrt(16))
# Round a floating-point number up and down
print(math.ceil(4.7))
print(math.floor(4.7))
Ejercicio 2: Manipulación de Datos con Pandas
Crea un DataFrame utilizando la biblioteca pandas con cualquier conjunto de datos de tu elección. Intenta agregar nuevas filas y columnas, y utiliza la función describe()
para obtener un resumen estadístico de los datos.
import pandas as pd
# Creating a pandas DataFrame
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'Occupation': ['Engineer', 'Doctor', 'Teacher']
})
# Adding a new column
df['Salary'] = [70000, 80000, 60000]
# Adding a new row
df = df.append({'Name': 'David', 'Age': 40, 'Occupation': 'Lawyer', 'Salary': 90000}, ignore_index=True)
# Getting a statistical summary of the data
print(df.describe(include='all'))
Ejercicio 3: Operaciones de Archivos con las Bibliotecas os y shutil
Utilizando las bibliotecas os
y shutil
, crea un nuevo directorio, escribe un archivo de texto en ese directorio y luego copia ese archivo a un directorio diferente.
import os
import shutil
# Creating a new directory
os.mkdir('new_directory')
# Writing a text file in the new directory
with open('new_directory/text_file.txt', 'w') as file:
file.write("This is some text.")
# Creating a second directory
os.mkdir('second_directory')
# Copying the text file to the second directory
shutil.copy('new_directory/text_file.txt', 'second_directory/text_file.txt')
Estos ejercicios te ayudarán a comprender y familiarizarte con la biblioteca estándar de Python, así como con bibliotecas clave como pandas, os y shutil.
9.4 Ejercicios Prácticos
Ejercicio 1: Explorando la Biblioteca Math
La biblioteca math de Python tiene varias funciones que se pueden utilizar para operaciones matemáticas. Intenta usar la función sqrt()
para encontrar la raíz cuadrada de un número y las funciones ceil()
y floor()
para redondear un número de punto flotante hacia arriba y hacia abajo, respectivamente.
import math
# Find the square root of a number
print(math.sqrt(16))
# Round a floating-point number up and down
print(math.ceil(4.7))
print(math.floor(4.7))
Ejercicio 2: Manipulación de Datos con Pandas
Crea un DataFrame utilizando la biblioteca pandas con cualquier conjunto de datos de tu elección. Intenta agregar nuevas filas y columnas, y utiliza la función describe()
para obtener un resumen estadístico de los datos.
import pandas as pd
# Creating a pandas DataFrame
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'Occupation': ['Engineer', 'Doctor', 'Teacher']
})
# Adding a new column
df['Salary'] = [70000, 80000, 60000]
# Adding a new row
df = df.append({'Name': 'David', 'Age': 40, 'Occupation': 'Lawyer', 'Salary': 90000}, ignore_index=True)
# Getting a statistical summary of the data
print(df.describe(include='all'))
Ejercicio 3: Operaciones de Archivos con las Bibliotecas os y shutil
Utilizando las bibliotecas os
y shutil
, crea un nuevo directorio, escribe un archivo de texto en ese directorio y luego copia ese archivo a un directorio diferente.
import os
import shutil
# Creating a new directory
os.mkdir('new_directory')
# Writing a text file in the new directory
with open('new_directory/text_file.txt', 'w') as file:
file.write("This is some text.")
# Creating a second directory
os.mkdir('second_directory')
# Copying the text file to the second directory
shutil.copy('new_directory/text_file.txt', 'second_directory/text_file.txt')
Estos ejercicios te ayudarán a comprender y familiarizarte con la biblioteca estándar de Python, así como con bibliotecas clave como pandas, os y shutil.
9.4 Ejercicios Prácticos
Ejercicio 1: Explorando la Biblioteca Math
La biblioteca math de Python tiene varias funciones que se pueden utilizar para operaciones matemáticas. Intenta usar la función sqrt()
para encontrar la raíz cuadrada de un número y las funciones ceil()
y floor()
para redondear un número de punto flotante hacia arriba y hacia abajo, respectivamente.
import math
# Find the square root of a number
print(math.sqrt(16))
# Round a floating-point number up and down
print(math.ceil(4.7))
print(math.floor(4.7))
Ejercicio 2: Manipulación de Datos con Pandas
Crea un DataFrame utilizando la biblioteca pandas con cualquier conjunto de datos de tu elección. Intenta agregar nuevas filas y columnas, y utiliza la función describe()
para obtener un resumen estadístico de los datos.
import pandas as pd
# Creating a pandas DataFrame
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'Occupation': ['Engineer', 'Doctor', 'Teacher']
})
# Adding a new column
df['Salary'] = [70000, 80000, 60000]
# Adding a new row
df = df.append({'Name': 'David', 'Age': 40, 'Occupation': 'Lawyer', 'Salary': 90000}, ignore_index=True)
# Getting a statistical summary of the data
print(df.describe(include='all'))
Ejercicio 3: Operaciones de Archivos con las Bibliotecas os y shutil
Utilizando las bibliotecas os
y shutil
, crea un nuevo directorio, escribe un archivo de texto en ese directorio y luego copia ese archivo a un directorio diferente.
import os
import shutil
# Creating a new directory
os.mkdir('new_directory')
# Writing a text file in the new directory
with open('new_directory/text_file.txt', 'w') as file:
file.write("This is some text.")
# Creating a second directory
os.mkdir('second_directory')
# Copying the text file to the second directory
shutil.copy('new_directory/text_file.txt', 'second_directory/text_file.txt')
Estos ejercicios te ayudarán a comprender y familiarizarte con la biblioteca estándar de Python, así como con bibliotecas clave como pandas, os y shutil.