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.