DataCamp Python Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

If you wanna use a package and you want to use it in your code what do you do?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you bring add something to your script from package in a general way?

A

from math import pi

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why is the NumPy array useful ?

A

You can perform calculations over entire arrays

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you simply a package name for use in your script

A

write as after your import

e.g. import numpy as np

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Whats the difference between when you add lists normally and when you add them using NumPy?

A

When you add them normally, it just combines the lists, one after the other.

With NumPy the lists will add

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is one thing you can’t do with numpy arrays which you can do with regular arrays?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what package do you use to plot in python?

And how would you add it to your code?

A

import matplotlib.pyplot as plt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you create a plot in python with matplotlib imported?

and how do you print it ?

A

plt. plot()
plt. show

(remember plt is a subset of matplot lib, you have to call it when doing operations)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you convert an axis on a plot into log ?

A

plt.xscale(‘log’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what are the inputs of the histogram function?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to add labels to plots python?

A

plt.xlabel()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you customise the range/length of the

A

plot. yticks()
plot. xticks()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

how would you customise the value of plt.yticks() shows?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

how do you add text to a plot?

A

plt.text(1550, 71, ‘India’)

corresponding coordinates and name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you create dictionaries?

A

use { }

Use a : to separate the key and the value.

call out a value using your_dict_name[key]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you get all the keys from a dictionary?

A

europe.keys()

europe being your dictionary in this case.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Why can you not use a list as a dictionary key?

A

because it isn’t an immutable object

it’s mutable - in other words it can be changed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

When to use a dictionary and when to use a list?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you add a value in a dictionary?

A

europe[‘iceland’] = ‘reykjavik’

dict[key] = ‘what you want to set the key to’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How do you create a table starting from dictionaries and using pandas?

A

create your dict, remember keys are the column labels and the values are the data for each column.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

what is csv short for ?

A

comma separated values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How can you read a csv file using panda

what do you really need to remember?

A

that the csv file name or directory needs to be written within qoutes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How can you change the index of csv file you are reading ?

A

cars = pd.read_csv(‘cars.csv’, index_col = 0 )

this sets the first column is used as the row labels

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the diff between loc and iloc

A

only difference is how you refer to columns and rows

iloc uses integers

25
Q

Why would you double brackets when indexing panda ?

A

not putting double brackets gives a series in terms of one column, even rows are converted into columns, which can be inconvenient.

Double brackets allow for a dataframe, and rows will remain rows.

26
Q

How to filter pandas dataframe?

A

Select your desired column(s) or row(s) and perform a operation, to filter, a comparison.

e.g. is_huge = brics[“area”] > 8

You need to store it in a variable so that you can index the dataframe, as follows:

brics[is_huge]

27
Q

in a for loop, what function do you have to use to get the index ?

A

enumerator

28
Q

what do you have to use to iterate a dictionary in a for loop?

What is this thing specified as?

A

for key, value in your_dict.items()

a method

29
Q

What do you have to use to iterate through all elements in a numpy array ?

What is this thing referred as ?

A

np.nditor(my_array)

function

30
Q

how do you iterate a panda dataframe in a for loop?

A

for lab, row in you_dataframe.iterrows()

31
Q

what is the efficient way of adding a column to a panda dataframe based of the data of another column?

A

your_dataframe[“new_column_name”] = your_dataframe[“selected_column”].apply(function)

32
Q

How do you create random numbers from numpy

A

np.random.rand()

33
Q

How to create random integer?

A

np.random.randint()

34
Q

How to set seed manually when creating random numbers?

A

np.random.seed(your_value)

35
Q

what sort of type does print() return ?

A

nonetype

36
Q

how to define functions in python?

A

def you

37
Q

Why is returning values generally more desirable than printing them out?

A

the type returned from printing is nonetype

38
Q

What is a tuple and what are its properties ?

A

essentially it is list that allows function to return multiple values

Like a list it can contain multiple values.

It is immutable - you can’t modify values !

constructed with parentheses

You can access values in a tuple in the same way you would with a list, by indexing.

39
Q

How do you use a tuple to return two values from a function?

A

define to output variables,

put them into a tuple

print the tuple

40
Q

How do you unpack a tuple?

A

a,b,c = your_tuple

unpacks to the elements of your tuple into the varibles a,b,c

41
Q

Why would equate a function to more than one variable?

A

If the function returns more than variable maybe you want to print them individually and not in bracket form after each other

42
Q

Explain this code.

A

dataframe saved in df variable

There is empty dictionary defined as langs_count

the ‘lang’ column is saved as col

The for loop goes through every value in the col

For each value we test whether the value is in the dictionary if it is, add one to the value the to the associated key, else update the dictionary and add the entry and add one to its value.

43
Q

How do you add something to a dictionary?

A

your_dict.update({key : value})

44
Q

How to change a key value in a dictionary?

A

your_dict[key] = value

45
Q

How do you make a variable global when it is in a local environment ?

A

write

global your_variable

46
Q

If you change a variable in a function what sort of scope is it?

A

local scope.

47
Q

How does python go through finding variables in nested functions?

A

First it looks locally in the nested function

If it does not find the variable, it goes to the function the previous function is nested in and searches locally there.

If it still can’t find it, it searches globally.

48
Q

How do you change a variable in nested function (all the functions) without changing it globally?

A

use

nonlocal

49
Q

Why are nested functions necessary?

A

They are necessary to scale programs, if there is code that needs to be repeated multiple times in a function, it is better to write a function to replace that code.

50
Q

How would you use nested functions to concatenate three input words ?

A
51
Q

How are the scopes searched?

A

Local Scope

Enclosing Functions

Global

Built-in

52
Q

How does this code work?

A

The outer function determines the number of copies

The inner function takes in a word and multiplies it by n, the value is then returned to outer

A value for n is assigned to twice and thrice

Both Twice and Thrice are input ‘hello’ and the print function is called on them.

With a value of n associated with Twice and Thrice, the inner function returns to the concatenated strings which are then princted.

53
Q

What are default arguments in a function?

A

Default argument is a variable already defined in the function

in the case that the user does not personally define the variable, the default value is used.

This can be useful when a value for the variable is required.

54
Q

What are flexible arguments and how are they defined?

A

Flexible arguments allow for any number of inputs from the user.

It is defined within the function brackets with a * in front of the variable

e.g. *args

55
Q

what do **kwargs do ?

A

they allow for an arbitrary amount of key,value pairs defined by the user.

you can define any kwarg by placing a ** in front.

56
Q

when you have used **kwargs how do you loop through the values ?

A

for key, value in kwargs.items():

57
Q

what do lambda functions allow?

A

Allow you to write functions quick

58
Q

What does the map() function do ?

A

The map() function applies a given function to each item of an iterable (list, tuple etc.) and returns a list of the results.

59
Q

a function that allows you to remove elements from a list that don’t match your criteria

A

filter()