General Python Flashcards
Which library and what code is best to use when reading in csv files such as ‘train.csv’?
If you have a pandas dataframe called data, how would you select the top 10 rows?
head = data[:10]
For a pandas dataframe called data, how would you get the dimensions of the data set?
data.shape
How do you find out the type of a variable ‘unknown_var’?
type(unknown_var)
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!”)
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!”)
What do floats represent?
real numbers
What does the int type represent?
integer numbers
What does the str type represent?
strings
What does the bool type represent?
True or False
Give a 3 point summary about python lists
- They name a collection of values
- They contain any type
- They can also contain different types
What is the indexing of python lists?
Python lists are 0 indexed.
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?
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.
What are the 3 main things that list manipulation allows you to do?
- Change list elements
- Add list elements
- Remove list elements
How do you delete an element from a list? Say the 3rd element.
del( my_list[2] )
For a python list, x, if we run the code in the diagram, what is the outcome? Why?
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 would you copy the elements of a list x into a list y?
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.
What function gets the length of a list?
len( my_list )
What are methods?
Functions that belong to objects.
Strings have methods.
floats have methods.
lists have methods.
How do you get the index of a string “unknown” from a list my_list?
my_list.index( “unknown” )
How do you count how many occurances of the integer 4 appear in the list my_list?
my_list.count( 4 )
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?
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.
What are methods that add, remove and reverse elements in a python list?
list. append()
list. remove()
list. reverse()
Complete this sentence:
“Numpy arrays can only contain…”
“one type.”