Python Flashcards
Name some features of tuples? As well provide an example of how one is constructed?
A tuple is a function that can return multiple values. Similar to a list but the values cannot be modified. An example of how one is constructed shown below:
even_nums = (2, 4, 6)
How does list slicing work in python?
List slicing has the following structure: [start (inclusive):end(exclusive)]
For example the command List[3:5] will return the elements 3 and 4 in the list but not the 5th element
What is the syntax used for the round function?
round(number to round, #decimal digits to round to)
For example: round( 1.68, 1)
equals 1.7
What are methods in python
Functions that belong to objects. Called using the following syntax:
object.method(). For example in order to find the index for a particular element in a list the following would be used:
List.index(element)
What does the append method do?
Adds a new element to an existing list. The new element is added to the end of the list.
How do you import just a single function from a library?
Use the following command at the beginning of the sub-procedure: from library (numpy) import function (array)
How do you turn a list into an array?
1.) Import numpy as np
2.) Convert a list such as “height” using the following syntax:
np_height=np.array(height)
What is the command for importing Matplotlib
import matplotlib.pyplot as plt
How to plot lists on a line graph?
plt.plot( List1 (x-axis), List2 (y-axis))
What method is used to display the graph?
The show method used as follows plt.show()
How to plot a histogram ?
Use the following command: plt.hist( list, bins = x)
the number of bins is equal the amount of bars in the histogram.
What are the commands for the labeling the x and y axis as well as the title?
plt. xlabel( ‘x axis label’)
plt. ylabel(‘y axis label’)
plt. title(‘graph title’)
How is a dictionary data structure constructed?
list = { “key1” : value1, “key2”: value2, “key3”:value3}
In order to access a value use the following command?
list[“key1”] = value1
How to create a data frame in python?
- ) import pandas as pd
2. ) variable =pd.dataframe(dict)
How to change index values in a dataframe?
dataframe.index = [“index1”, “index2”, “index3”……]