Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python Chapter 7 >>>
- ##Lists and Tuples
- **Sequence** - an object that holds multiple items of data stored one after the other. You can perform operations on a sequence to examine and manipulate the items stored in it. (Java has atomic data)
- **Lists and tuples are sequence types**
- **List** is an object that contains multiple data items. It is mutable is an Array - it is dynamic, items can be added and removed. You can use methods index, slice to work with them. It can hold different types.
- info = ['Jen', 7, 15.5]
- numbers = list(range(5))
- **element** - each item in a List
- **iteratable** - object?
- **Tuple** is immutable is a Hash?
- **repetition operator** - makes multiple copies of a list and joins them together
- list * n n is the repetition operator
- print function can be used to display an entire list
- list() function can convert certain types of objects to lists
- Difference between Lists and Arrays - Arrays are more limited
- ###Iterating over a list
- numbers = [1,2,3,4]
- for n in numbers:
- print(n)
- ### Concatenating Lists
- repetition operator
- p.297
- repetition operator
- list1 = [a,b,c]
- list2 = [1,2,3]
- list3 = list1 + list2
- print(list3)
- >> [a,b,c,1,2,3]
- **=+ (augmentation operator** - concatenates two lists and reassigns the variable on the left)
- ### List Slicing
- p.299
- When you slice a list you get a span of elements from within the list.
- list_name[start : end]
- start is inclusive
- end is exclusive
- ##Iterate over a list with a for loop
- mylist = [1,2,3]
- for element in mylist:
- print(element * element)
- ## Indexing
- first position - 0
- last position - n-1
- **try except structure block** - test to see if
- Length of List - len(mylist)
- mylist = [1,2,3,4,5,6,7,8,9]
- index = 10
- if (index , len(mylist):
- print(mylist[index])
- else:
- print("The index is too large.")
- **smallest negative index** - -n elements
- ## Finding Items in Lists with the *in operator*
- ## List methods and built-in functions
- **append(item)** - p.303 used to add items to a list
- **index(item)** - p.305
- **insert(index, item)** - p.307
- **sort()** - p.307
- **remove(item)** - p.308
- **reverse()** - p.309
- ## The del Statement
- *mylist = [1,2,3,4,5,6]
- print('Before deletion', mylist)
- del_mylist[2]
- print('After deletion', mylist)
- **** -
- **** -
- NUM_DAYS = 5
- sales = [0] * NUM_DAYS
- index = 0
- print("Enter the sales for the day: >>")
- while index < NUM_DAYS:
- print('Days' index + 1, ":", sep=' ', end=' ")
- sales[index] = float(input())
- index += 1
- print("Here are the sales for the days you entered: ")
- for value in sales:
- print(value)
- with a for loop
- NUM_DAYS = 5
- sales = [0] * NUM_DAYS
- index = 0
- print("Enter the sales for the day: >>")
- for index in range(0, NUM_DAYS):
- print('Days' index + 1, ":", sep=' ', end=' ")
- sales[index] = float(input())
- index += 1
- print("Here are the sales for the days you entered: ")
- for value in sales:
- print(value)
- Diana
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement