Starting out with Python: Chapter 7 - List and Tuples
Tuples doesn't have the following
- append() - remove() - reverse() - insert() - sort() Mnemonic ar-rist (like artist, except no t, - to indicate a second r)
Properties of tuples
- supports indexing - methods such as index() - built-in functions such as len, min and max - Slicing expressions - in operator - the + and * operators
Why tuples vs list?
- tuples are processed faster (tuple is fixed-size 1 block and list are 2 blocks, the object and variable-size for elements) - safe since modification is not allowed
Two types of sequences in pythons
1. Lists 2. Tuples
What sort of graph does the plot function produce?
A line raph
element
each item stored in list
Module
files with the ". py" extension containing Python code that can be imported inside another Python Program
The importing as statement
import matplotlib.pyplot as plt plt.method() you can use the plt object vs having to write matplotlib.pyplot().method() everytime
Finding items in a list with the in Operator
item in list
slice
items taken from a sequence
list concatenation
joining lists with the + operator example 1 list3 = list1 + list2 example 2 list1 += list2
Tuple
like a list except immutable meaning values can't be changed
Convert list into a tuple
list = [elements] list = tuple(list) <- answer right here
How to create pie chart in Python
list = [values] plt.pie(list)
Repetition operator
makes multiple copies of a list and joins them together syntax list * n
Syntax, create tuple with x number of elements
my_tuple(x,) whatever x is, there will be that number of elements. x must be an integer
Generating a list with range(x, y, z) function
numbers = list(range(1, 10, 2)) 1: starting point 10: ending point 2: step value the list is [1, 3, 5, 7, 9]
Generating a list with the range() function
numbers = list(range(5)) The list is [0, 1, 2, 3, 4]
How to plot bar charts
plt.bar(left_edges, heights) - left_edges are the actual bars themselves and will align towards the left part of the x-axis tick - height is the height of the bar
How to plot bar chart specifying bar width
plt.bar(left_edges, heights, bar_width)
Bar color codes
plt.bar(left_edges, heights, color=('r', 'g', 'b', 'w', 'k')) - first bar is red - second bar is green - third bar is blue - fourth bar is white - fifth bar is black
Marker symbol codes
plt.plot(x_coordinates, y_coordinates, 'marker symbol')
Customizing the X and Y axes
plt.xlim(xmin=1, xmax=100) modifies min and max values for the x axes replace the x with a y to do the same for the y axes.
What functions do you use to add X and Y axes labels in a graph?
pyplot.xlabel(X_LabelName) pyplot.ylabel(Y_LabelName)
matplotlib
python *package for plotting data*
Repetition operator question what's the output of numbers = [0] * 5 print(numbers)
the [0] is the list the 5 shows how many copies to make of the elements in the list therefore the output will be [0, 0, 0, 0, 0]
Default x-axes and y-axes
the graph will show both x and y min and max values. x_coord = [0, 1, 2, 3, 4] The min shown will be 0 The max shown will be 4
to create a pie chart with the pie function, what argument must you pass?
the x and y coordinates
Convert tuple into list
tuple = (elements) tuple = list(tuple) <- answer right here
List and mutability
values within a list are able to be changed
Indexing a list
when you access an element within the list like so
To create a graph with the plot function, what two arguments must you pass?
x and y coordinates are needed recall you also need import matplotlib.pyplot() as plt
How do you change the lower and the upper limits of the X and Y axes in a graph?
xlim(xmin, xmax) ylim(ymin, ymax)
How do you customize the tick marks along the X and Y axes in a graph?
xticks(tick_list [elements], labels_list[elements]) yticks(tick_list [elements], labels_list[elements])
Can list have different data types?
yes
List vs Tuples
Lists are mutable (can be changed) and tuples are immutable (can't be changed)
pyplot
Module in matplolib, needed to create graphs import matplotlib.pyplot
What is a pie chart slice label?
What's underlined A label for each portion of the pie chart
sequence
an object that contains items of data
List
an object that contains multiple data items