Introduction to Python
Run the hidden code cell below to import the data used in this course.
1 hidden cell
Take Notes
Add notes about the concepts you've learned and code cells with code you want to keep.
Add your notes here
# Add your code snippets here
x = "1"
y = "1"
print(bool("python"))
print(bool("0.0"))
Explore Datasets
Use the arrays imported in the first cell to explore the data and practice your skills!
- Print out the weight of the first ten baseball players.
- What is the median weight of all baseball players in the data?
- Print out the names of all players with a height greater than 80 (heights are in inches).
- Who is taller on average? Baseball players or soccer players? Keep in mind that baseball heights are stored in inches!
- The values in
soccer_shootingare decimals. Convert them to whole numbers (e.g., 0.98 becomes 98). - Do taller players get higher ratings? Calculate the correlation between
soccer_ratingsandsoccer_heightsto find out! - What is the average rating for attacking players (
'A')?
Python Lists
Python Lists x = [a, b, c] #list x = [3:] # the list is going to be spliced from element 3 to the end x = a[:] # The list 'a' is going to be duplicated to list x, however any direct change to x will not affect a x = a # the list x is directly linked to a, so any direct change will affect x x = a + [b, c] # the elemeents b and c have been added to list a and renamed x
Functions and Packages
Functions Functions are methods that carry out actions on objects.
Packages Packages are a directory of python scripts. There are thousands that can be found on the internet. Numpy is a package used to efficiently work with arrays, Matplotlib for data visualization, and scikit-learn for machine learning.
In order to use a package, you first have to install them into your own system. If you want to install them on your own system, you'll want to use pip, a package maintenance system for Python. If you go to this URL, you can download the file get-pip-dot-py. Next, you go to the terminal, and execute python3 get-pip-dot-py. Now you can use pip to actually install a Python package of your choosing. Suppose we want to install the numpy package, which you'll learn about in the next chapter. You type pip3 install numpy. You have to use the commands python3 and pip3 here to tell our system that we're working with Python version 3. Now that the package is installed, you can actually start using it in one of your Python scripts.
Objects
Everything in Python is an object. Depending on the type of the object, there are different methods associated with it. For example, a string has the methods capitalize(), a float has the method bit_length(). To call a method on a object, you can use dot notation. this means you say the name of the object, insert a dot ".", then insert the method. Make sure that if the method requires inputs you provide the necessary variables.
For example, a list called apples has elements "apple 1, apple 2, apple 3". If we want to find the index of apple 3 in the list apples, we use the method index(). So, it would look like: apples.index("apple 3"). If we only do apples.index(), we might receive an error.
Numpy
Doing calculations between different lists would require going through each element in both lists, and doing the calculation. Converting lists/arrays to numpy allows numpy to do the calculations between lists easily!
In order to convert an array, you first import Numpy at the top of the script. Then you create a new variable, such as np_height. You make it equal to numpy.array(insert array name here).
This works easily because Numpy assumes that the numpy array has only one type throughout the array, and that it is the appropriate type.