Skip to content

Introduction to Python

๐Ÿ‘‹ Welcome to your new workspace! Here, you can experiment with the data you used in Introduction to Python and practice your newly learned skills with some challenges. You can find out more about DataCamp Workspace here.

On average, we expect users to take approximately 15 minutes to complete the content in this workspace. However, you are free to experiment and practice in it as long as you would like!

1. Get Started

Below is a code cell. It is used to execute Python code. To get you started, there is already pre-written Python code for you to run.

๐ŸƒTo execute the code, click inside the cell to select it and click "Run" or the โ–บ icon. You can also use Shift-Enter to run a selected cell.

# Create two variables
x = 2
y = 3

# Multiply and print their product
print("Multiplying x and y returns", x * y)

2. Load in the Data

The code below imports packages you used in Introduction to Python: NumPy and Math. It also imports pandas, a package introduced in Intermediate Python to help load in data as NumPy arrays.

Reminder: To execute the code, click inside the cell to select it and click "Run" or the โ–บ icon. You can also use Shift-Enter to run a selected cell.

# Import the course packages
import numpy as np
import math
import pandas as pd

# Read in the file
baseball_df = pd.read_csv("datasets/baseball.csv")

# Separate into arrays
baseball_names = baseball_df["Name"].to_numpy()
baseball_heights = baseball_df["Height"].to_numpy()
baseball_weights = baseball_df["Weight"].to_numpy()
baseball_ages = baseball_df["Age"].to_numpy()

# Print out first array
baseball_names

3. Challenge Yourself

After running the cell above, you have created four NumPy arrays: baseball_names, baseball_heights, baseball_weights, and baseball_ages.

Add code to the code cells below to try one (or more) of the following challenges:

  1. Print out the names of the first ten baseball players in baseball_names. If you're stuck, try reviewing this video.
  2. What is the median weight of all baseball players in baseball_weights? If you're stuck, try reviewing this video.
  3. Print out the names of all players with a height greater than 80 (heights are in inches) using baseball_names and baseball_heights. If you're stuck, try reviewing this video.

Be sure to check out the Answer Key at the end to see one way to solve each problem. Did you try something similar?

Reminder: To execute the code you add to a cell, click inside the cell to select it and click "Run" or the โ–บ icon. You can also use Shift-Enter to run a selected cell.

# 1. Print out the names of the first ten baseball players
# 2. Print out the median weight of all baseball players
# 3. Print out the names of all players with a height greater than 80

4. Next Steps

Feeling confident about your skills? Continue on to Intermediate Python! This course will introduce you to some powerful libraries for visualizing and working with data: Matplotlib and pandas!

If you're still keen to practice, you can also use the code below to load in the soccer data you used in the final exercise of Introduction to Python.

# Read in the file
soccer_df = pd.read_csv("datasets/baseball.csv")

# Separate into Numpy arrays
soccer_names = soccer_df["Name"].to_numpy()
soccer_heights = soccer_df["Height"].to_numpy()
soccer_weights = soccer_df["Weight"].to_numpy()
soccer_ages = soccer_df["Age"].to_numpy()
soccer_positions = soccer_df["PosCategory"].to_numpy()

# Print the first array
print(soccer_names)

๐Ÿ’ก After running the cell above, you can access the arrays soccer_names, soccer_heights, soccer_weights, soccer_ages, and soccer_positions. Try adding a new code cell to inspect and explore these arrays!

5. Answer Key

Below are potential solutions to the challenges shown earlier. Try them out and see how they compare to how you approached the problem!

# 1. Print out the names of the first ten baseball players
print(baseball_names[0:10])
# 2. Print out the median weight of all baseball players
print(np.median(baseball_weights))
โ€Œ
โ€Œ
โ€Œ