Basics Flashcards

1
Q

what are 4 different types of data in python?

A

str = string

int = integer

float = number with decimal values

bool = boolean, True or False

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

Can a list contain different types of data?

A

Yes

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

With what starting number are lists indexed?

A

zero going forward

-1 starting from the end of the list

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

List slicing syntax, e.g. 1:3. Is the 3 included?

A

start : end

No, the end is not included

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

What happens if you don’t specify the start or end of a list when list slicing?

e.g. mylist[:4] or mylist[2:]

A

If don’t specify start, it starts at index zero

If don’t specify end, includes the start and rest of the list

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

x = [2, 3, 4]

y = x

y[0] = 1

How do you prevent the first element changing to 1 in the original list x?

A

Instead of y=x, use y=list(x) or y=x[:]. Then changes to y will not affect x.

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

Can a numpy array contain elements with different types?

A

No. If you try, some of the elements’ types are changed to end up with a homogenous list (type coersion)

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

Create a 2D NumPy Array

How are rows and columns indexed in a 2D NumPy Array?

How do you subset a 2D NumPy Array?

A

np_2d = np.array([[1, 2, 3, 4],

[5, 6, 7, 8]])

Both rows and columns are indexed starting at 0 from the top left corner.

np_2d[0,2] or np_2d[0][2]: the first number is the row and the second the column

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

np_heights = array([191, 184, 185])

np_positions = array([‘GK’, ‘M’, ‘A’])

gk_heights = np_heights[np_positions == ‘GK’]

What will the gk_heights array contain?

A

It will contain 191.

np_positions == ‘GK’ creates a boolean array. Using it to index np_heights causes the program to return heights that correspond to “True” values in the boolean array

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

How do you make an x, y line plot?

A

Use matplotlib

import matplotlib.pyplot as plt

year = [1950, 1970, 1990, 2010]

pop = [2.5, 3.7, 5.3, 7.0]

plt. plot(year, pop)
plt. show()

Note that need plt.show() to see the chart

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

How do you create a scatter plot?

A

import matplotlib.pyplot as plt

year = [1950, 1970, 1990, 2010]

pop = [2.5, 3.7, 5.3, 7.0]

plt. scatter(year, pop)
plt. show()

Note that need plt.show() to see the chart

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

How do you make a histogram?

A

import matplotlib.pyplot as plt

values = [1, 2, 3, 4]

plt. hist(values, bins=2)
plt. show()

Note that bins is the number of bars you want the data to be divided into. The program automatically calculates appropriate boundaries for your data.

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

How do you “clean up” a plot to start afresh?

A

plot.clf()

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

How do you add names to a plot’s axes when using matplotlib.pyplot?

How do you add a title?

A

plt. xlabel(‘label1’)
plt. ylabel(‘label2’)
plt. title(‘title’)

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

How do you specify the numbers or ticks to display on an axis?

How do you change the name of the ticks on an axis?

A

plt. yticks([0, 2, 4, 6, 8, 10])
plt. yticks([0, 2, 4, 6, 8, 10], [‘0’, ‘2B’, ‘4B’, ‘6B’, ‘8B’, ‘10B’])

note that the names have to correspond to the ticks listed

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

How do you change an axis to logarithmic scale?

A

plt.scatter(x, y)

plt.xscale(‘log’)

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

how do you change the size of the dots on a scatter plot to reflect a third variable?

A

plt.scatter(x, y, s=z)

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

How do you add gridlines?

A

plt.grid(True)

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

How do you create a dictionary?

Two facts about keys

A

dictionaryname = {key1:value1, key2:value2, … }

When you type in dictionaryname[key2], you get value2

Keys:

  • Keys in a dictionary must be unique. If state same key twice, dictionary will just retain the last value stated
  • Keys have to be “immutable” objects, e.g. cannot be changed after they’re created. Strings, booleans, integers, and floats are immutable.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

how do you access the “keys” in a dictionary?

A

dictionaryname.keys()

Note that it takes no arguments

This will list all the keys in the dictionary

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

eHow do you add a key:value pair to a dictionary?

How do you change a key:value pair in a dictionary?

How do you delete a key:value pair?

A

dictionaryname[keytobeadded] = valuetobeadded

dictionaryname[key] = newvalueforkey

del(dictionaryname[key])

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

How do you get a value that is in a nested dictionary?

e.g.

countries = {‘spain’:{‘capital’:’madrid’, ‘population’:46.77}

‘france’:{‘capital’:’paris’, ‘population’: 66.04}}

extract population of france

A

dictionaryname[key1][key2]

countries[‘france’][‘population’]

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

How do you create a Dataframe from a Dictionary?

How do you change the default row index numbers?

A

Make a dictionary where the keys will be the column labels (variables) of the Data frame, and the values are in list form:

dict ={‘country’:[‘Brazil’, ‘Russia’], ‘capital’:[‘Brasilia’, ‘Moscow’]}

Then,

import pandas as pd

dataframename = pd.DataFrame(dict)

For index change:

dataframename.index = [‘row1’, ‘row2’, ‘row3’}

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

How do you create a DataFrame from a CSV file?

A

dataframename = pandas.read_csv(“filepath.csv”, index_col = 0)

Note that the index_col is not necessary but use if first column in file contains the row labels

25
Q

How do you output values of an entire column of a DataFrame?

What type of values do you get?

How do you keep the data in a DataFrame?

How do you output values of two columns?

A

dataframename[‘columnname’]

panda series - it’s like a 1D labelled array

use double brackets: dataframename[[‘columnname’]]

dataframename[[‘column1’, ‘column2’]]

26
Q

How do you output the certain rows from a DataFrame?

A

dataframename[startrow:endrow]

*remember to use index NOT row label

*remember rows are indexed starting at zero

27
Q

How do you output a row from a DataFrame using loc (label-based)?

How do you select multiple rows?

A

dataframename. loc[[‘rowname’]]
dataframename. loc[[‘row1’, ‘row2’]]

*double brackets to keep type as DataFrame

28
Q

how do you output specific rows and specific columns from a DataFrame?

How do you select all rows but only a couple of columns?

A

dataframename. loc[[‘row1’, ‘row2’], [‘column1’, ‘column2’]]
dataframename. loc[:, [‘column1’, ‘column2’]]

29
Q

How do you output a row with iloc?

How do you select specific rows and specific columns with iloc?

A

dataframename. iloc[[indexposition]]
dataframename. iloc[[rowindex1, rowindex2], [columnindex1, columnindex2]]

30
Q

String operations

my_string = ‘thisStringisAwesome’

What will the following yield?

my_string*2

my_string + ‘Innit’

‘m’ in my_string

A

my_string*2 = ‘thisStringisAwesomethisStringisAwesome’

my_string + ‘Innit’ = ‘thisStringisAwesomeInnit’

‘m’ in my_string = True

31
Q

How do you subset lists of lists?

e.g.

my_list= [[4,5,6,7], [3,4,5,6]]

Select 7

A

my_list[indexlist1][indexlist2]

To select 7: my_list[1][3]

32
Q

List Operations

my_list = [‘my’, ‘list’, ‘is’, ‘nice’]

my_list2 = [[4,5,6,7], [3,4,5,6]]

What do the following yield?

my_list + my_list

my_list * 2

my_list2 > 4

A

my_list + my_list = [‘my’, ‘list’, ‘is’, ‘nice’, ‘my’, ‘list’, ‘is’, ‘nice’]

my_list * 2 = [‘my’, ‘list’, ‘is’, ‘nice’, ‘my’, ‘list’, ‘is’, ‘nice’]

my_list2 > 4 = True

33
Q

List Methods

How do you…

Get the index of an item in a list?

Count an item?

Reverse the list?

Sort the list?

A

my_list.index(item)

my_list.count(item)

my_list.reverse()

my_list.sort()

34
Q

List Methods

How do you…

Append an item at a time?

Append an item?

3 ways to remove an item?

Insert an item?

A

my_list.append(item)

my_list.extend(item)

my_list.remove(item)

my_list.pop(indexofitem)

del(my_list[0:1])

my_list.insert(index*, item)

*index of the item before which to insert

35
Q

String Methods

How do you…

Change string to uppercase?

Change string to lowercase?

Count string elements?

A

my_string.upper()

my_string.lower()

my_string.count(‘element’)

36
Q

How do you create a numpy array?

Use my_list = [1, 2, 3, 4]

A

my_array=numpy.array(my_list)

37
Q

Numpy array operations

my_array = numpy.array([1, 2, 3, 4])

What is the output of…

my_array > 3

my_array * 2

my_array + numpy.array([5, 6, 7, 8])

A

my_array > 3 : array([False, False, False, True]

my_array * 2 : array([2, 4, 6, 8])

my_array + numpy.array([5, 6, 7, 8]) : array([6, 8, 10, 12])

38
Q

Numpy Array Functions

How do you…

Get the dimensions of an array?

Get the mean of the array?

Get the median of the array?

Get the correlation coefficient of the array?

Get the standard deviation of the array?

A

my_array.shape

numpy. mean(my_array)
numpy. median(my_array)

my_array.corrcoef()

numpy.std(my_array)

39
Q

Numpy Array Functions

How do you…

Append items to an array?

Insert items in an array?

Delete subarray in an array?

A

numpy. append(other_array)
numpy. insert(my_array, index*, valuestoinsert)

*index before which value is inserted

numpy.delete(my_array, indexofarraytodelete)

40
Q

What happens when you do

‘carl’ < ‘chris’

A

‘carl’ comes first in alphabet, you get True

41
Q

Less than or equal to

Greater than or equal to

equal to

not equal to

A

<=

>=

*Note =< is not correct syntax

==

!=

42
Q

What are the numpy array equivalents of the boolean operators and, or, and not?

A

numpy. logical_and()
numpy. logical_or()
numpy. logical_not()
eg. numpy.logical_and(arrayname > 21, arrayname < 22)

*note that these also work on Pandas DataFrame

43
Q

if statement

A

if condition:

expression1

espression2

*The indentation of the expression lets python know that the expression is part of the if statement. To exit if statement, simply write code without indentation.

44
Q

if else statement

A

if condition:

expression

else:

expression

45
Q

elif statement

What happens with the elif statement if the condition of the if statement is true?

A

if condition:

expression

elif condition:

expression

else:

expression

If condition for if is true, the output is the if’s expression. elif is never reached

46
Q

while loop statement

A

while condition:

expression

47
Q

fofor loop statement

A

for var in seq:

expression

*var is a variable you create

*seq is a list of values

48
Q

enumerate function

A

enumerate(seq)

Returns an enumerate object that produces a sequence of tuples, and each of the tuples is an index-value pair

e.g.

fam = [1, 2, 3, 4]

for index, height in enumerate(fam):

print(‘index “ + str(index) + “:” + str(height))

Output:

index 0: 1

index 1: 2

index 2: 3

index 3: 4

49
Q

Loop over string

What is the output of the following?

for c in “family”:

print (c.capitalize())

A

F
A
M
I
L
Y

50
Q

Using the following dictionary, print out each key and value using the for loop.

world = {‘afghanistan’:30.55, ‘albania’:2.77, ‘algeria’:39.21}

A

for key, value in world.items():

print (key + str(value))

*Note that need the .items() to use for loop with the dictionary

51
Q

How do you print out every element of a 2D Numpy Array (using for loop)?

np_height = np.array([1, 2, 3, 4])

np_weight = np.array([1, 2, 3, 4])

meas = np.array([np_height, np_weight])

A

for val in np.nditer(meas):

print(val)

*This will print out all the heights, then all the weights

52
Q

For loop with Pandas Dataframe

How do you print out every single element of a DataFrame?

Use dataset “countries”

A

for rowlabel, rowvalues in countries.iterrows():

print(rowlabel)

print(rowvalues)

This will print the rowlabel followed by all the column names with row values. e.g.

BR
country Brazil
capital Brasilia
area 8.516
population 200.4

etc

53
Q

For loops with Pandas DataFrame

How do you print out row labels with only the capital cities for the dataset countries?

A

for rowlabel, rowvalues in countries.iterrows():

print(rowlabel + “: “ + rowvalues[“capital”])

This will print:

BR: Brasilia

RU: Moscow

etc

54
Q

What function gives you the length of a string?

A

len(‘string’)

55
Q

Using the apply function, how do you create a new column with the countries’ name length in the DataFrame countries?

A

countries[“name_length”] = countries[“country”].apply(len)

56
Q

Which numpy function generates random numbers? How do you manually choose a seed?

A

numpy.random.rand()

This generates psedurandom numbers between 0 and 1 starting from a seed

numpy.random.seed(number)

*seed can be any number. If don’t specify, python will automatically pick a seed. You can set the seed so that results are reproducible between simulations (same seed will generate same random numbers)

57
Q

Simulate a coin toss

A

coin = numpy.random.randint(lowest, high)

*lowest is the lowest number and high is one above the highest number

coin = numpy.random.randint(0, 2)

if coin == 0:

print(‘heads’)

else:

print(‘tails’)

58
Q
A