General Python Flashcards

1
Q

Which library and what code is best to use when reading in csv files such as ‘train.csv’?

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

If you have a pandas dataframe called data, how would you select the top 10 rows?

A

head = data[:10]

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

For a pandas dataframe called data, how would you get the dimensions of the data set?

A

data.shape

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

How do you find out the type of a variable ‘unknown_var’?

A

type(unknown_var)

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

If

savings = 100 and result = savings * 1.1 ** 7

then does the following work? If not, what is the correct code?

print( “I started with $” + savings + “ and now have $” + result + “. Awesome!”)

A

No, the integers need to be converted to strings using str().

The correct code is:

print( “I started with $” + str(savings) + “ and now have $” + str(result) + “. Awesome!”)

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

What do floats represent?

A

real numbers

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

What does the int type represent?

A

integer numbers

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

What does the str type represent?

A

strings

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

What does the bool type represent?

A

True or False

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

Give a 3 point summary about python lists

A
  1. They name a collection of values
  2. They contain any type
  3. They can also contain different types
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the indexing of python lists?

A

Python lists are 0 indexed.

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

For lists, how are the start and end included/excluded when list slicing?

For example, when taking a list, ‘first_10_primes’ and coding first_10_primes[3:5], what do we get?

A

You’d get the start number specified, but the end number is excluded.

For the example used, you’d only get the 4th and 5th element of the list and the 6th is excluded (remember that python lists are 0-indexed.

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

What are the 3 main things that list manipulation allows you to do?

A
  1. Change list elements
  2. Add list elements
  3. Remove list elements
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you delete an element from a list? Say the 3rd element.

A

del( my_list[2] )

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

For a python list, x, if we run the code in the diagram, what is the outcome? Why?

A

The variable y does not contain each element in the list x but instead contains a reference/address to the list x.

Therefore, if we modify y, we modify x.

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

How would you copy the elements of a list x into a list y?

A

You can’t simply use an equals sign as that will copy the reference/address to x.

You must use a list function or list slicing.

17
Q

What function gets the length of a list?

A

len( my_list )

18
Q

What are methods?

A

Functions that belong to objects.

Strings have methods.

floats have methods.

lists have methods.

19
Q

How do you get the index of a string “unknown” from a list my_list?

A

my_list.index( “unknown” )

20
Q

How do you count how many occurances of the integer 4 appear in the list my_list?

A

my_list.count( 4 )

21
Q

If we have the following code:

place = “poolhouse”

place_up = place.upper()

print(place)

print(place_upper)

is the variable place changed to be all upper case?

A

No. If place was a list, then place_up would be pointing to the same address as place and therefore, place would be changed to upper case. However, this is not the case for string objects.

22
Q

What are methods that add, remove and reverse elements in a python list?

A

list. append()
list. remove()
list. reverse()

23
Q

Complete this sentence:

“Numpy arrays can only contain…”

A

“one type.”

24
Q
A