Chapter 7 Lists and Tuples

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

*Plotting a pie function*

when you call the *pie* function, you pass a list of values as an argument -the sum of the values will be used as the value of the whole -each element in the list will become a slice of the pie chart -the size of a slice represents that elements value as a percentage of the whole

How to install *matplotlib*

Windows: open command prompt *pip install matplotlib* Mac: Open a terminal window *sudo pip3 install matplotlib* -see Appendix F in your textbook for more information about packages and the *pip* utility

Customize tick marks *xticks* and *yticks* functions

first argument is a list of tick mark locations second argument is a list of labels to display at the specific locations

Plotting a line graph with the *plot* function

use the *plot* function to create a line graph that connects a series of points with straight lines

*append(item)*

used to add items to a list - *item* is appended to the end of the existing list

*index(item)*

used to determine where an item is located in a list -returns the index of the first element in the list containing *item* -raises *ValueError* exception if *item* not in the list

*insert(index, item)*

used to insert *item* at position *index* in the list

*sort( )*

used to sort the elements of the list in ascending order

the default width of each bar in a bar graph is 0.8 along the X axis

you can change the bar width by passing a third argument to the *bar* function *bar_width = 5*

*print* function can be used to display an entire list

-*list( )* function can convert certain types of objects to lists

Element

an item in a list

To verify the package was installed start *IDLE* and enter

*import matplotlib* with no error messages

*matplotlib* contains a module named *pyplot* that we need to import in order to create all of the graphs

*import matplotlib.pyplot as plt*

Example of how to pass a tuple of color codes as a keyword argument

*plt.bar(left_edges, heights, color=('r', 'g', 'b', 'w', 'k'))* The First bar will be red Second bar green Third bar Blue Forth bar White Fifth bar Black

you can specify colors, by passing a tuple of color codes as an argument to the *pie* functions *colors* parameter

*plt.pie(values, colors=('r', 'g', 'b', 'w', 'k'))* Red Green Blue White Black = *k*

To make a copy of a list you must copy each element of the list *Two methods to do this*

-Creating a new empty list and using a *for* loop to add a copy of each element from the original list to the new list -Creating a new empty list and concatenating the old list to the new empty list

Advantages for using tuples over lists

-Processing tuples is faster than processing lists -tuples are safe -some operations in python require use of tuples

Tuples *do not* support the methods...

-append -remove -insert -reverse -sort

*bar* Function needs two lists:

-one with the X cooridinates of each bar's left edge -another with the heights of each bar, along the Y axis

the *pie* function automatically changes the color of the slices, in the following order:

Blue, green, red, cyan, magenta, yellow, black, and white

Python provides different types of sequences, including lists and tuples

List = mutable tuple = immutable

The *bar* function has a color parameter that you can use to change the colors of the bars

The argument that you pass into this parameter is a tuple containing a series of color codes

Plotting a Bar chart

Use the *bar* function in the *matplotlib.pyplot* module to create a bar chart

Two-dimensional list

a list that contains other lists as its elements *Aka Nested list* -common to have rows and columns -useful for multiple sets of data To process data needs to use two indexes -typically uses nested loops to process *pg. 376*

Slice

a span of items that are taken from a sequence -Format: *list[start : end]* Span is a list containing copies of elements from *start* up to, but not including, *end* -If *start* not specified, *0* is used for start index *[ , 3]* -If *end* not specified, *len(list)* is used for end index (last number) Slicing expressions can include a step value and negative indexes relative to end of list

List

an object that contains multiple data items Format: *list = [item1, item2, etc.] can hold items of different types

Sequence

an object that contains multiple items of data -the items are stored in sequence one after another

*min* and *max* functions

built-in functions that returns the item that has the lowest or highest value in a sequence -sequence passed as an argument

*plt.xlim(xmin=1, xmax=100)* *plt.ylim(ymin=10, ymax=50)*

causes the X axis to begin at 1 and end at 100 causes the Y axis to begin at 10 and end at 50

*tuple( )* function

converts list to tuple

*list( )* function

converts tuple to list

You can use the *in* operator to determine whether an item is contained in a list

format: *item in list* Returns *True* if item is in the list, or *False* if it is not in the list -similarly you can use the *not in* operator to determine whether an item is not in a list

Tuple

immutable sequence -similar to a list, but once created *cannot be changed* Format: *tuple_name = (item1, item2)* Tuples support operations as lists -subscript indexing for retrieving elements -methods such as *index* -Built in functions such as *len, min, max* -Slicing expressions -The *in,+, ** operators

Concatenate

join two things together -the *+* operator can be used to concatenate two lists -cannot concatenate a list with another data type, such as a number The *+=* augmented assignment operator can also be used to concatenate lists

Repetition operator

makes multiple copies of a list and joins them together -the * symbol is a repetition operator when applied to a sequence and an integer - sequence is left operand, number is right

Index

number specifying the position of an element in a list -enables access to individual element in list -index of first element in the list is 0, second is 1, and n'th element is n-1 Negative indexes identify positions relative to the end of the list -the index -1 identifies the last element, -2 identifies the next to last element, etc.

*del* statement

removes an element from a specific index in a list Format: *del list[ i ]*

*remove(item)*

removes the first occurrence of *item* in the list

*len* function

returns the length of a sequence such as a list Ex. *size = len(my_list)* -returns the number of elements in the list, so the index of last element is *len(list)-1* -Can be used to prevent an *IndexError* exception when iterating over a list with a loop

*reverse( )*

reverses the order of the elements in the list

*Plotting Data with matplotlib*

the *matplotlib* package is a library for creating two-dimensional charts and graphs -*NOT* part of the standard Python library, has to be installed separately

the *pie* function has a *labels* parameter that you can use to display labels for the slices in the pie chart

the argument that you pass into this parameter is a list containing the desired labels, as strings

Mutable sequence

the items in the sequence can be changed -list are mutable so their elements can be changed An expression such as: *list[1] = new_value* can be used to assign a new value to a list element -must use a valid index to prevent raising of an *IndexError* exception


संबंधित स्टडी सेट्स

bk 10 lesson 1 (vending machines)

View Set

ACG 2021 Paterson FSU Ch.11 Quiz

View Set

Chapter 11 -Unemployment and Efficiency Wages

View Set

3.7 Floating-point numbers (double)

View Set

Personal Finance Chapter 3 - Taxes in Your Financial Plan

View Set

Principles of Operations Management

View Set

1.Géneros y Formas Musicales del Barroco

View Set

Scientific Thinking: Can e-Cigarettes Help People Quit or Reduce Smoking?

View Set