Introduction To Python
Introduction To Python
1 Python Basics
o
o
2 Python Lists
o
o
o
3 Functions and Packages
o 3.1 Functions
o 3.2 Methods
o 3.3 Packages
4 NumPy
o
o
o
Tran Thanh Dat - International University
25 February 2022
Introduction to Python
Hugo Bowne-Anderson - DataCamp
Course Description
Python is a general-purpose programming language that is becoming ever more popular for data science.
Companies worldwide are using Python to harvest insights from their data and gain a competitive edge. Unlike other
Python tutorials, this course focuses on Python specifically for data science. In our Introduction to Python course, you’ll
learn about powerful ways to store and manipulate data, and helpful data science tools to begin conducting your own
analyses. Start DataCamp’s online Python curriculum now. For any violation, please contact dattran.hcmiu@gmail.com.
1 Python Basics
An introduction to the basic concepts of Python. Learn how to use Python interactively and by using a script. Create
your first variables and acquaint yourself with Python’s basic data types.
1.1 Hello Python!
You can also use the IPython Shell interactively by simply typing commands and hitting Enter. When you work in
the shell directly, your code will not be checked for correctness so it is a great way to experiment.
## 0.625
Add another line of code to the Python script on the top-right (not in the Shell): print(7 + 10).
# Put code below here
print(7 + 10)
## 17
Hit Submit Answer to execute the Python script and receive feedback.
Great! On to the next one!
Your boss asks you to clean and analyze the results of the latest satisfaction survey.
1.1.3 Any comments?
Something that Hugo didn’t mention in his videos is that you can add comments to your Python scripts.
Comments are important to make sure that you and others can understand what your code is about.
To add comments to your Python script, you can use the # tag. These comments are not run as Python code, so they
will not influence your result. As an example, take the comment in the editor, # Division; it is completely ignored
during execution.
Above the print(7 + 10), add the comment
# Addition
# Division
print(5 / 8)
# Addition
## 0.625
print(7 + 10)
## 17
Great!
1.1.4 Python as a calculator
Python is perfectly suited to do basic calculations. Apart from addition, subtraction, multiplication and division,
there is also support for more advanced operations such as:
Suppose you have $100, which you can invest with a 10% return each year. After one year,
it’s 100×1.1=110100×1.1=110 dollars, and after two years it’s 100×1.1×1.1=121100×1.1×1.1=121. Add code to
calculate how much money you end up with after 7 years, and print the result.
# Addition, subtraction
print(5 + 5)
## 10
print(5 - 5)
## 0
print(3 * 5)
## 15
print(10 / 2)
## 5.0
print(18 % 7)
## 4
print(4 ** 2)
## 16
print(100 * 1.1 ** 7)
## 194.87171000000012
Time for another video!
1.2.1 Variable Assignment
In Python, a variable allows you to refer to a value with a name. To create a variable use =, like this example:
x = 5
You can now use the name of this variable, x, instead of the actual value, 5.
Remember, = in Python means assignment, it doesn’t test equality!
Create a variable savings with the value 100.
# Create a variable savings
savings = 100
## 100
Great! Let’s try to do some calculations with this variable now!
100 * 1.1 ** 7
Instead of calculating with the actual values, you can use variables instead. The savings variable you’ve created in
the previous exercise represents the $100 you started with. It’s up to you to create a new variable to represent 1.1 and
then redo the calculations!
Create a variable growth_multiplier, equal to 1.1.
# Create a variable growth_multiplier
growth_multiplier = 1.1
Create a variable, result, equal to the amount of money you saved after 7 years.
# Calculate result
result = savings * growth_multiplier ** 7
## 194.87171000000012
Great!
Next to numerical data types, there are two other very common data types:
str, or string: a type to represent text. You can use single or double
quotes to build a string.
bool, or boolean: a type to represent logical values. Can only
be True or False (the capitalization is important!).
## <class 'float'>
type(b)
## <class 'str'>
Correcto perfecto!
1.2.5 Operations with other types
Hugo mentioned that different types behave differently in Python.
When you sum two strings, for example, you’ll get different behavior than when you sum two integers or two
booleans.
In the script some variables with different types have already been created. It’s up to you to use them.
What do you think the resulting type will be? Find out by printing out the type of year1.
# Print the type of year1
print(type(year1))
## <class 'float'>
Calculate the sum of desc and desc and store the result in a new variable doubledesc.
# Assign sum of desc and desc to doubledesc
doubledesc = desc + desc
1.2.6 Type conversion
Using the + operator to paste together two strings can be very useful in building custom messages.
Suppose, for example, that you’ve calculated the return of your investment and want to summarize the results in a
string. Assuming the integer savings and float result are defined, you can try something like this:
print("I started with $" + savings + " and now have $" + result + ". Awesome!")
This will not work, though, as you cannot simply sum strings and integers/floats.
To fix the error, you’ll need to explicitly convert the types of your variables. More specifically, you’ll need str(), to
convert a value into a string. str(savings), for example, will convert the integer savings to a string.
Similar functions such as int(), float() and bool() will help you convert Python values into any type.
Hit Run Code to run the code. Try to understand the error message.
# Definition of savings and result
savings = 100
result = 100 * 1.10 ** 7
# Definition of pi_string
pi_string = "3.1415926"
Fix the code such that the printout runs without errors; use the function str() to convert the variables to strings.
# Definition of savings and result
savings = 100
result = 100 * 1.10 ** 7
Great! You have a profit of around $95; that’s pretty awesome indeed!
Correct! Because you’re not converting 2 to a string with str(), this will give an error.
2 Python Lists
Learn to store, access, and manipulate data in lists: the first step toward efficiently working with huge amounts of
data.
2.1 Python Lists
2.1.1 Create a list
As opposed to int, bool etc., a list is a compound data type; you can group values together:
a = "is"
b = "nice"
my_list = ["my", "list", a, b]
After measuring the height of your family, you decide to collect some information on the house you’re living in. The
areas of the different parts of your house are stored in separate variables for now, as shown in the script.
Create a list, areas, that contains the area of the hallway (hall), kitchen (kit), living room (liv), bedroom (bed)
and bathroom (bath), in this order. Use the predefined variables.
# Area variables (in square meters)
hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50
Print areas with the print() function.
Nice! A list is way better here, isn’t it?
The printout of the previous exercise wasn’t really satisfying. It’s just a list of numbers representing the areas, but
you can’t tell which area corresponds to which part of your house.
The code in the editor is the start of a solution. For some of the areas, the name of the corresponding room is
already placed in front. Pay attention here! “bathroom” is a string, while bath is a variable that represents the
float 9.50 you specified earlier.
Finish the code that creates the areas list. Build the list so that the list first contains the name of each room as a
string and then its area. In other words, add the strings “hallway”, “kitchen” and “bedroom” at the appropriate
locations.
# area variables (in square meters)
hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50
## ['hallway', 11.25, 'kitchen', 18.0, 'living room', 20.0, 'bedroom', 10.75, 'bathroom', 9.5]
Nice! This list contains both strings and floats, but that’s not a problem for Python!
A, B and C
B
B and C
C
Correct! As funny as they may look, all these commands are valid ways to build a Python list.
2.1.4 List of lists
As a data scientist, you’ll often be dealing with a lot of data, and it will make sense to group some of this data.
Instead of creating a flat list containing strings and floats, representing the names and areas of the rooms in your
house, you can create a list of lists. The script in the editor can already give you an idea.
Don’t get confused here: “hallway” is a string, while hall is a variable that represents the float 11.25 you
specified earlier.
Finish the list of lists so that it also contains the bedroom and bathroom data. Make sure you enter these in order!
# area variables (in square meters)
hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50
Print out house; does this way of structuring your data make more sense?
# Print out house
print(house)
## [['hallway', 11.25], ['kitchen', 18.0], ['living room', 20.0], ['bedroom', 10.75], ['bathroom',
9.5]]
Print out the type of house. Are you still dealing with a list?
# Print out the type of house
print(type(house))
## <class 'list'>
Great! Get ready to learn about list subsetting!
2.2 Subsetting Lists
## 11.25
Subset and print out the last element of areas, being 9.50. Using a negative index makes sense here!
# Print out last element from areas
print(areas[-1])
## 9.5
Select the number representing the area of the living room (20.0) and print it out.
# Print out the area of the living room
print(areas[5])
## 20.0
Good job!
## 28.75
Bellissimo!
2.2.3 Slicing and dicing
Selecting single values from a list is just one part of the story. It’s also possible to slice your list, which means
selecting multiple elements from your list. Use the following syntax:
my_list[start:end]
The start index will be included, while the end index is not.
The code sample below shows an example. A list with “b” and “c”, corresponding to indexes 1 and 2, are selected
from a list x:
x = ["a", "b", "c", "d"]
x[1:3]
The elements with index 1 and 2 are included, while the element with index 3 is not.
Use slicing to create a list, downstairs, that contains the first 6 elements of areas.
# Create the areas list
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]
Do a similar thing to create a new variable, upstairs, that contains the last 4 elements of areas.
# Use slicing to create upstairs
upstairs = areas[6:10]
Print both downstairs and upstairs using print().
# Print out downstairs and upstairs
print(downstairs)
print(upstairs)
my_list[begin:end]
However, it’s also possible not to specify these indexes. If you don’t specify the begin index, Python figures out that
you want to start your slice at the beginning of your list. If you don’t specify the end index, the slice will go all the way to
the last element of your list. To experiment with this, try the following commands in the IPython Shell:
x = ["a", "b", "c", "d"]
x[:2]
x[2:]
x[:]
Create downstairs again, as the first 6 elements of areas. This time, simplify the slicing by omitting
the begin index.
# Create the areas list
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]
Create upstairs again, as the last 4 elements of areas. This time, simplify the slicing by omitting the end index.
# Alternative slicing to create upstairs
upstairs = areas[6:]
Wonderful!
2.3 Manipulating Lists
Use the IPython Shell to experiment with the commands below. Can you tell what’s happening and why?
Sweet! As the code sample showed, you can also slice a list and replace it with another list to update multiple
elements in a single command.
2.3.2 Extend a list
If you can change elements in a list, you sure want to be able to add elements to it, right? You can use
the + operator:
x = ["a", "b", "c", "d"]
y = x + ["e", "f"]
You just won the lottery, awesome! You decide to build a poolhouse and a garage. Can you add the information to
the areas list?
Use the + operator to paste the list [“poolhouse”, 24.5] to the end of the areas list. Store the resulting list
as areas_1.
# Create the areas list (updated version)
areas = ["hallway", 11.25, "kitchen", 18.0, "chill zone", 20.0,
"bedroom", 10.75, "bathroom", 10.50]
Further extend areas_1 by adding data on your garage. Add the string “garage” and float 15.45. Name the
resulting list areas_2.
# Add garage data to areas_1, new list is areas_2
areas_2 = areas_1 + ["garage", 15.45]
The updated and extended version of areas that you’ve built in the previous exercises is coded below. You can copy
and paste this into the IPython Shell to play around with the result.
areas = ["hallway", 11.25, "kitchen", 18.0,
"chill zone", 20.0, "bedroom", 10.75,
"bathroom", 10.50, "poolhouse", 24.5,
"garage", 15.45]
There was a mistake! The amount you won with the lottery is not that big after all and it looks like the poolhouse
isn’t going to happen. You decide to remove the corresponding string and float from the areas list.
The ; sign is used to place commands on the same line. The following two code chunks are equivalent:
# Same line
command1; command2
# Separate lines
command1
command2
Which of the code chunks will do the job for us?
del(areas[10]); del(areas[11])
del(areas[10:11])
del(areas[-4:-2])
del(areas[-3]); del(areas[-4])
Correct! You’ll learn about easier ways to remove specific elements from Python lists later on.
The Python code in the script already creates a list with the name areas and a copy named areas_copy. Next, the
first element in the areas_copy list is changed and the areas list is printed out. If you hit Run Code you’ll see that,
although you’ve changed areas_copy, the change also takes effect in the areas list. That’s
because areas and areas_copy point to the same list.
If you want to prevent changes in areas_copy from also taking effect in areas, you’ll have to do a more explicit
copy of the areas list. You can do this with list() or by using [:].
Change the second command, that creates the variable areas_copy, such that areas_copy is an explicit copy
of areas. After your edit, changes made to areas_copy shouldn’t affect areas. Submit the answer to check this.
# Create list areas
areas = [11.25, 18.0, 20.0, 10.75, 9.50]
# Create areas_copy
areas_copy = areas
# Change areas_copy
areas_copy[0] = 5.0
# Print areas
print(areas)
# Create areas_copy
areas_copy = list(areas)
# Change areas_copy
areas_copy[0] = 5.0
# Print areas
print(areas)
3.1 Functions
3.1.1 Familiar functions
Out of the box, Python offers a bunch of built-in functions to make your life as a data scientist easier. You already
know two such functions: print() and type(). You’ve also used the functions str(), int(), bool() and float() to
switch between data types. These are built-in functions as well.
Calling a function is easy. To get the type of 3.0 and store the output as a new variable, result, you can use the
following:
result = type(3.0)
The general recipe for calling functions and saving the result to a variable is thus:
output = function_name(input)
Use print() in combination with type() to print out the type of var1.
# Create variables var1 and var2
var1 = [1, 2, 3, 4]
var2 = True
## <class 'list'>
Use len() to get the length of the list var1. Wrap it in a print() call to directly print it out.
# Print out length of var1
print(len(var1))
## 4
Use int() to convert var2 to an integer. Store the output as out2.
# Convert var2 to an integer: out2
out2 = int(var2)
Great job! The len() function is extremely useful; it also works on strings to count the number of characters!
3.1.2 Help!
Maybe you already know the name of a Python function, but you still have to figure out how to use it. Ironically, you
have to ask for information about a function with another function: help(). In IPython specifically, you can also
use ? before the function name.
To get help on the max() function, for example, you can use one of these calls:
help(max)
?max
Use the Shell to open up the documentation on complex(). Which of the following statements is true?
Perfect!
3.1.3 Multiple arguments
In the previous exercise, the square brackets around imag in the documentation showed us that the imag argument
is optional. But Python also uses a different way to tell users about arguments being optional.
Have a look at the documentation of sorted() by typing help(sorted) in the IPython Shell.
You’ll see that sorted() takes three arguments: iterable, key and reverse.
key=None means that if you don’t specify the key argument, it will be None. reverse=False means that if you
don’t specify the reverse argument, it will be False.
In this exercise, you’ll only have to specify iterable and reverse, not key. The first input you pass
to sorted() will be matched to the iterable argument, but what about the second input? To tell Python you want to
specify reverse without changing anything about key, you can use =:
sorted(___, reverse = ___)
Two lists have been created for you in the editor. Can you paste them together and sort them in descending order?
Note: For now, we can understand an iterable as being any collection of objects, e.g. a List.
3.2 Methods
3.2.1 String Methods
Strings come with a bunch of methods. Follow the instructions closely to discover some of them. If you want to
discover them in more detail, you can always type help(str) in the IPython Shell.
A string place has already been created for you to experiment with.
Use the upper() method on place and store the result in place_up. Use the syntax for calling methods that you
learned in the previous video.
# string to experiment with: place
place = "poolhouse"
## poolhouse
print(place_up)
## POOLHOUSE
Print out the number of o’s on the variable place by calling count() on place and passing the letter ‘o’ as an
input to the method. We’re talking about the variable place, not the word “place”!
# Print out the number of o's in place
print(place.count('o'))
## 3
Nice! Notice from the printouts that the upper() method does not change the object it is called on. This will be
different for lists in the next exercise!
3.2.2 List Methods
Strings are not the only Python types that have methods associated with them. Lists, floats, integers and booleans
are also types that come packaged with a bunch of useful methods. In this exercise, you’ll be experimenting with:
index(), to get the index of the first element of a list that matches its
input and
count(), to get the number of times an element appears in a list.
You’ll be working on the list with the area of different parts of a house: areas.
Use the index() method to get the index of the element in areas that is equal to 20.0. Print out this index.
# Create list areas
areas = [11.25, 18.0, 20.0, 10.75, 9.50]
## 2
Call count() on areas to find out how many times 9.50 appears in the list. Again, simply print out this number.
# Print out how often 9.50 appears in areas
print(areas.count(9.50))
## 1
Nice! These were examples of list methods that did not change the list they were called on.
You’ll be working on the list with the area of different parts of the house: areas.
Use append() twice to add the size of the poolhouse and the garage again: 24.5 and 15.45, respectively. Make sure
to add them in this order.
# Create list areas
areas = [11.25, 18.0, 20.0, 10.75, 9.50]
Print out areas
# Print out areas
print(areas)
3.3 Packages
3.3.1 Import package
As a data scientist, some notions of geometry never hurt. Let’s refresh some of the basics.
For a fancy clustering algorithm, you want to find the circumference, CC, and area, AA, of a circle. When the
radius of the circle is r, you can calculate CC and AA as:
C=2πrC=2πr
A=πr2A=πr2
To use the constant pi, you’ll need the math package. A variable r is already coded in the script. Fill in the code to
calculate C and A and see how the print() functions create some nice printouts.
Import the math package. Now you can access the constant pi with math.pi.
import math
# Definition of radius
r = 0.43
# Build printout
## Circumference: 2.701769682087222
Calculate the area of the circle and store it in A.
# Calculate A
A = math.pi * r ** 2
# Build printout
## Area: 0.5808804816487527
Nice! If you know how to deal with functions from packages, the power of a lot of Python programmers is at your
fingertips!
3.3.2 Selective import
General imports, like import math, make all functionality from the math package available to you. However, if
you decide to only use a specific part of a package, you can always make your import more selective:
from math import pi
Let’s say the Moon’s orbit around planet Earth is a perfect circle, with a radius r (in km) that is defined in the
script.
Perform a selective import from the math package where you only import the radians function.
# Definition of radius
r = 192500
Calculate the distance travelled by the Moon over 12 degrees of its orbit. Assign the result to dist. You can calculate
this as r * phi, where r is the radius and phi is the angle in radians. To convert an angle in degrees to an angle in
radians, use the radians() function, which you just imported.
# Travel distance of Moon over 12 degrees. Store in dist.
dist = r * radians(12)
Print out dist.
# Print out dist
print(dist)
## 40317.10572106901
Nice! Head over to the next exercise.
Suppose you want to use the function inv(), which is in the linalg subpackage of the scipy package. You want to
be able to use this function as follows:
my_inv([[1,2], [3,4]])
Which import statement will you need in order to run the above code without an error?
import scipy
import scipy.linalg
from scipy.linalg import my_inv
from scipy.linalg import inv as my_inv
Correct! The as word allows you to create a local name for the function you’re importing: inv() is now available
as my_inv().
4 NumPy
NumPy is a fundamental Python package to efficiently practice data science. Learn to work with powerful tools in
the NumPy array, and get started with data exploration.
4.1 Numpy
## <class 'numpy.ndarray'>
Great job!
# Import numpy
import numpy as np
Print np_height_in.
# Print out np_height_in
print(np_height_in)
# Import numpy
import numpy as np
Use np_height_m and np_weight_kg to calculate the BMI of each player. Use the following equation:
BMI=weight(kg)height(m)2BMI=weight(kg)height(m)2
Save the resulting numpy array as bmi.
# Calculate the BMI: bmi
bmi = np_weight_kg / np_height_m ** 2
Print out bmi.
# Print out bmi
print(bmi)
Create a boolean numpy array: the element of the array should be True if the corresponding baseball player’s BMI is
below 21. You can use the < operator for this. Name the array light.
# height_in and weight_lb are available as a regular lists
# Import numpy
import numpy as np
np.array([True, 1, 2, 3, 4, False])
np.array([4, 3, 0]) + np.array([0, 2, 2])
np.array([1, 1, 2]) + np.array([3, 4, -1])
np.array([0, 1, 2, 3, 4, 5])
np_x = np.array(x)
np_x[1]
The script in the editor already contains code that imports numpy as np, and stores both the height and weight of
the MLB players as numpy arrays.
Subset np_weight_lb by printing out the element at index 50.
# height_in and weight_lb are available as a regular lists
# Import numpy
import numpy as np
## 200
Print out a sub-array of np_height_in that contains the elements at index 100 up to and including index 110.
# Print out sub-array of np_height_in: index 100 up to and including index 110
print(np_height_in[100:111])
## [73 74 72 73 69 72 73 75 75 73 72]
Nice! Time to learn something new: 2D Numpy arrays!
# Import numpy
import numpy as np
## <class 'numpy.ndarray'>
Print out the shape attribute of np_baseball. Use np_baseball.shape.
# Print out the shape of np_baseball
print(np_baseball.shape)
## (4, 2)
Great! You’re ready to convert the actual MLB data to a 2D numpy array now!
## (4, 2)
Slick! Time to show off some killer features of multi-dimensional numpy arrays!
# numpy
import numpy as np
np_x = np.array(x)
np_x[:,0]
For regular Python lists, this is a real pain. For 2D numpy arrays, however, it’s pretty intuitive! The indexes before
the comma refer to the rows, while those after the comma refer to the columns. The : is for slicing; in this example, it
tells Python to include all rows.
The code that converts the pre-loaded baseball list to a 2D numpy array is already in the script. The first column
contains the players’ height in inches and the second column holds player weight, in pounds. Add some lines to make
the correct selections. Remember that in Python, the first element is at index 0!
Print out the 50th row of np_baseball.
# baseball is available as a regular list of lists
import pandas as pd
baseball = pd.read_csv("http://s3.amazonaws.com/assets.datacamp.com/course/intro_to_python/
baseball.csv")[['Height', 'Weight']]
## [ 70 195]
Make a new variable, np_weight_lb, containing the entire second column of np_baseball.
# Select the entire second column of np_baseball: np_weight_lb
np_weight_lb = np_baseball[:,1]
Select the height (first column) of the 124th baseball player in np_baseball and print it out.
# Print out height of 124th player
print(np_baseball[123, 0])
## 75
This is going well!
4.2.4 2D Arithmetic
Remember how you calculated the Body Mass Index for all baseball players? numpy was able to perform all
calculations element-wise (i.e. element by element). For 2D numpy arrays this isn’t any different! You can combine
matrices with single numbers, with vectors, and with other matrices.
Execute the code below in the IPython shell and see if you understand:
import numpy as np
np_mat = np.array([[1, 2],
[3, 4],
[5, 6]])
np_mat * 2
np_mat + np.array([10, 10])
np_mat + np_mat
np_baseball is coded for you; it’s again a 2D numpy array with 3 columns representing height (in inches), weight
(in pounds) and age (in years).
You managed to get hold of the changes in height, weight and age of all baseball players. It is available as a
2D numpy array, updated. Add np_baseball and updated and print out the result.
# baseball is available as a regular list of lists
# updated is available as 2D numpy array
import pandas as pd
import numpy as np
baseball = pd.read_csv("http://s3.amazonaws.com/assets.datacamp.com/course/intro_to_python/
baseball.csv")[['Height', 'Weight', 'Age']]
n = len(baseball)
updated = np.array(pd.read_csv("http://s3.amazonaws.com/assets.datacamp.com/course/intro_to_python/
update.csv", header = None))
# Import numpy
import numpy as np
## 73.6896551724138
Print out the median of np_height_in.
# Print out the median of np_height_in
print(np.median(np_height_in))
## 74.0
An average height of 1586 inches, that doesn’t sound right, does it? However, the median does not seem affected by
the outliers: 74 inches makes perfect sense. It’s always a good idea to check both the median and the mean, to get an
idea about the overall distribution of the entire dataset.
The code to print out the mean height is already included. Complete the code for the median height.
Replace None with the correct code.
# np_baseball is available
# Import numpy
import numpy as np
## Average: 73.6896551724138
med = np.median(np_baseball[:,0])
print("Median: " + str(med))
## Median: 74.0
Use np.std() on the first column of np_baseball to calculate stddev. Replace None with the correct code.
# Print out the standard deviation on height. Replace 'None'
stddev = np.std(np_baseball[:,0])
print("Standard Deviation: " + str(stddev))
You’ve contacted FIFA for some data and they handed you two lists. The lists are the following:
Extract all the heights of the goalkeepers. You can use a little trick here: use np_positions == ‘GK’ as an index
for np_heights. Assign the result to gk_heights.
# Heights of the goalkeepers: gk_heights
gk_heights = np_heights[np_positions == 'GK']
Extract all the heights of all the other players. This time use np_positions != ‘GK’ as an index for np_heights.
Assign the result to other_heights.
# Heights of the other players: other_heights
other_heights = np_heights[np_positions != 'GK']
Print out the median height of the goalkeepers using np.median(). Replace None with the correct code.
# Print out the median height of goalkeepers. Replace 'None'
print("Median height of goalkeepers: " + str(np.median(gk_heights)))
## Median height of goalkeepers: 188.0
Do the same for the other players. Print out their median height. Replace None with the correct code.
# Print out the median height of other players. Replace 'None'
print("Median height of other players: " + str(np.median(other_heights)))