Advertisement
Guest User

Untitled

a guest
May 29th, 2015
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. # Python Chapter 7 >>>
  2.  
  3. ##Lists and Tuples
  4.  
  5. **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)
  6.  
  7. **Lists and tuples are sequence types**
  8.  
  9. **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.
  10.  
  11. info = ['Jen', 7, 15.5]
  12.  
  13. numbers = list(range(5))
  14.  
  15. **element** - each item in a List
  16.  
  17. **iteratable** - object?
  18.  
  19. **Tuple** is immutable is a Hash?
  20.  
  21. **repetition operator** - makes multiple copies of a list and joins them together
  22. list * n n is the repetition operator
  23.  
  24. print function can be used to display an entire list
  25.  
  26. list() function can convert certain types of objects to lists
  27.  
  28. Difference between Lists and Arrays - Arrays are more limited
  29.  
  30. ###Iterating over a list
  31.  
  32. numbers = [1,2,3,4]
  33. for n in numbers:
  34. print(n)
  35.  
  36. ### Concatenating Lists
  37.  
  38. repetition operator
  39.  
  40. p.297
  41.  
  42. repetition operator
  43. list1 = [a,b,c]
  44. list2 = [1,2,3]
  45. list3 = list1 + list2
  46.  
  47. print(list3)
  48.  
  49. >> [a,b,c,1,2,3]
  50.  
  51. **=+ (augmentation operator** - concatenates two lists and reassigns the variable on the left)
  52.  
  53. ### List Slicing
  54.  
  55. p.299
  56. When you slice a list you get a span of elements from within the list.
  57.  
  58. list_name[start : end]
  59.  
  60. start is inclusive
  61. end is exclusive
  62.  
  63. ##Iterate over a list with a for loop
  64.  
  65. mylist = [1,2,3]
  66. for element in mylist:
  67. print(element * element)
  68.  
  69. ## Indexing
  70.  
  71. first position - 0
  72. last position - n-1
  73.  
  74. **try except structure block** - test to see if
  75.  
  76. Length of List - len(mylist)
  77.  
  78. mylist = [1,2,3,4,5,6,7,8,9]
  79. index = 10
  80. if (index , len(mylist):
  81. print(mylist[index])
  82. else:
  83. print("The index is too large.")
  84.  
  85. **smallest negative index** - -n elements
  86.  
  87. ## Finding Items in Lists with the *in operator*
  88.  
  89. ## List methods and built-in functions
  90.  
  91. **append(item)** - p.303 used to add items to a list
  92.  
  93. **index(item)** - p.305
  94.  
  95. **insert(index, item)** - p.307
  96.  
  97. **sort()** - p.307
  98.  
  99. **remove(item)** - p.308
  100.  
  101. **reverse()** - p.309
  102.  
  103. ## The del Statement
  104.  
  105. *mylist = [1,2,3,4,5,6]
  106. print('Before deletion', mylist)
  107. del_mylist[2]
  108. print('After deletion', mylist)
  109.  
  110. **** -
  111.  
  112. **** -
  113.  
  114. NUM_DAYS = 5
  115. sales = [0] * NUM_DAYS
  116. index = 0
  117. print("Enter the sales for the day: >>")
  118. while index < NUM_DAYS:
  119. print('Days' index + 1, ":", sep=' ', end=' ")
  120. sales[index] = float(input())
  121. index += 1
  122.  
  123. print("Here are the sales for the days you entered: ")
  124. for value in sales:
  125. print(value)
  126.  
  127. with a for loop
  128.  
  129. NUM_DAYS = 5
  130. sales = [0] * NUM_DAYS
  131. index = 0
  132.  
  133. print("Enter the sales for the day: >>")
  134.  
  135. for index in range(0, NUM_DAYS):
  136. print('Days' index + 1, ":", sep=' ', end=' ")
  137. sales[index] = float(input())
  138. index += 1
  139.  
  140. print("Here are the sales for the days you entered: ")
  141. for value in sales:
  142. print(value)
  143. Diana
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement