DataCamp Python Flashcards
If you wanna use a package and you want to use it in your code what do you do?
first, import it at the beginning of your code
then if you want to use it write it with a dot at the end and then attach what you want to extract from the package
say you want to extract pi from the math package,
math.pi
How do you bring add something to your script from package in a general way?
from math import pi
Why is the NumPy array useful ?
You can perform calculations over entire arrays
How can you simply a package name for use in your script
write as after your import
e.g. import numpy as np
Whats the difference between when you add lists normally and when you add them using NumPy?
When you add them normally, it just combines the lists, one after the other.
With NumPy the lists will add
What is one thing you can’t do with numpy arrays which you can do with regular arrays?
numpy arrays cannot contain arrays with different types.
If you try to build such a list, some of the elements’ types are changed to end up with a homogeneous list. This is known as type coercion.
what package do you use to plot in python?
And how would you add it to your code?
import matplotlib.pyplot as plt
How do you create a plot in python with matplotlib imported?
and how do you print it ?
plt. plot()
plt. show
(remember plt is a subset of matplot lib, you have to call it when doing operations)
How do you convert an axis on a plot into log ?
plt.xscale(‘log’)
what are the inputs of the histogram function?
hist(x, bins = ‘ ‘)
x is the list of values
bins refer to the list should be divided, if you don’t specify it will be 10 by default. Remember you need to write bins = ‘your value’.
How to add labels to plots python?
plt.xlabel()
How do you customise the range/length of the
plot. yticks()
plot. xticks()
how would you customise the value of plt.yticks() shows?
You have an array after you ticks array to specify the value each tick should represent.
plt.yticks( [0,1,2,3,4], [‘0’,’1B’, ‘2B’, ‘3B’, ‘4B’, ‘5B’]
how do you add text to a plot?
plt.text(1550, 71, ‘India’)
corresponding coordinates and name
How do you create dictionaries?
use { }
Use a : to separate the key and the value.
call out a value using your_dict_name[key]
How do you get all the keys from a dictionary?
europe.keys()
europe being your dictionary in this case.
Why can you not use a list as a dictionary key?
because it isn’t an immutable object
it’s mutable - in other words it can be changed.
When to use a dictionary and when to use a list?
Use a list when you have a collection of values were order matters and you want to be able to select entire subsets.
Use a dictionary when you want a table that allows you to lookup up values very easily and quickly using unique keys.
How do you add a value in a dictionary?
europe[‘iceland’] = ‘reykjavik’
dict[key] = ‘what you want to set the key to’
How do you create a table starting from dictionaries and using pandas?
create your dict, remember keys are the column labels and the values are the data for each column.
what is csv short for ?
comma separated values.
How can you read a csv file using panda
what do you really need to remember?
that the csv file name or directory needs to be written within qoutes.
How can you change the index of csv file you are reading ?
cars = pd.read_csv(‘cars.csv’, index_col = 0 )
this sets the first column is used as the row labels