Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. players = ['shoxie', 'lucky', 'almanek', 'kennys']
  2.  
  3. # Here we are defining the variable player and associating each list value to
  4. # our player variable and then printing each in a loop until finished
  5. for player in players:
  6. print(player)
  7.  
  8. # Python will store the last value that was assigned to our variable created in the loop
  9. # So in this example player will retain the value of 'kennys'
  10. print(player)
  11.  
  12. # Expanding on what we can do with a for loop
  13. for player in players:
  14. print(f"You are such an amazing player {player.title()}")
  15. print(f"I really hope you make it far in the major {player.title()}.\n")
  16.  
  17. # Python cares about white space so an indent literally defines whats in a for loop
  18. # Unlike java where we use {} to contain the loop contents
  19.  
  20. # The next non indented line will be after the for loop
  21. print("Everyone did amazing! Keep it up!")
  22.  
  23.  
  24. # Try it yourself example
  25. pizzas = ['meatlovers', 'pineapple', 'cheese']
  26.  
  27. for pizza in pizzas:
  28. print(f"I like {pizza}")
  29.  
  30. print("\nBoy, I sure do love pizza!\n")
  31.  
  32.  
  33. # Try it yourself example 2
  34. animals = ['cat', 'dog', 'rabbit']
  35.  
  36. for animal in animals:
  37. print(f"A {animal} would make a great pet!")
  38.  
  39. print("\nAll of these animals have fur!\n")
  40.  
  41. # We can use range to print a list of numbers
  42. for value in range(1, 6):
  43. print(value)
  44.  
  45. # We can also use list() along with range() to create an actual list of numbers
  46. # We can also pass a third argument to range() to have it count by x as shown below
  47. even_numbers = list(range(2, 11, 2))
  48. print(even_numbers)
  49.  
  50.  
  51. # For example, if we wanted to get the squared value of 1-10 it'd look like below
  52. squares = []
  53. for value in range(1, 11):
  54. squares.append(value ** 2)
  55. print(squares)
  56.  
  57. # If needed we can find certain values of a list like min, max, sum, etc
  58. digits = [1, 2, 3, 4, 5]
  59. print(min(digits))
  60. print(max(digits))
  61. print(sum(digits))
  62.  
  63. # List comprehension allows us to build the same for loop/list in one line
  64. squares = [value**2 for value in range(1, 11)]
  65. print(squares)
  66.  
  67.  
  68. # Try it yourself example
  69. for numbers in range(1, 21):
  70. print(numbers)
  71.  
  72. # for nums in range(1, 1000001):
  73. # print(nums)
  74.  
  75. # Create a list of 1-1million and then confirm the contents, and print its min/max/sum
  76. oneMillion = []
  77. for nums in range(1, 1000001):
  78. oneMillion.append(nums)
  79. print(f"There are: {len(oneMillion)} numbers in our list!")
  80. print(min(oneMillion))
  81. print(max(oneMillion))
  82. print(sum(oneMillion))
  83.  
  84. # Use the range function to make a list of odd numbers 1-20
  85. odds = []
  86. for numbers in range(1, 20, 3):
  87. odds.append(numbers)
  88. print(numbers)
  89.  
  90. # Make a list of multiples of 3 from 3 to 30
  91. multiples = []
  92. for threes in range(3, 31):
  93. tmp = threes * 3
  94. multiples.append(tmp)
  95. print(multiples)
  96. # Or we can do it in one line like shown below
  97. test = [numTest*3 for numTest in range(3, 31)]
  98. print(test)
  99.  
  100. # We can work with a portion of a list called a slice as shown below
  101. players = ['shoxie', 'lucky', 'almanek', 'kennys']
  102. print(players[0:2])
  103.  
  104. # If we don't specify a starting point python will automatically start at the first index
  105. print(players[:3])
  106.  
  107. # We can also do it in reverse so here it prints index 2 through the end of the list
  108. print(players[2:])
  109.  
  110. # We can also use a negative value to only get the last x amount of values from the list
  111. print(players[-3:])
  112.  
  113. # We can work with a subset of elements in a list, in a for loop like below
  114. for player in players[:2]:
  115. print(player)
  116.  
  117. # We can copy a list using a splice if needed
  118. # Make sure to include [:] or else we will just point friends_foods to my_foods instead of making a copy
  119. my_foods = ['pasta', 'shrimp', 'pizza', 'toast']
  120. friends_foods = my_foods[:]
  121. print(friends_foods)
  122.  
  123.  
  124. # Try it yourself examples
  125. # Print the first 3 items in a list using a slice
  126. sliced = [1, 2, 3, 4, 5]
  127. print(sliced[0:3])
  128.  
  129. # Print 3 items from the middle of the list
  130. print(sliced[1:4])
  131.  
  132. # Print the last three items in the list
  133. print(sliced[-3:])
  134.  
  135. # Create a list of your favorite pizzas, make a copy called friends_pizza
  136. # Add a pizza to your list and your friends list
  137. # Use a for loop to print out both people's favorite pizzas to prove the lists are different
  138. pizzas = ['meatlovers', 'pineapple', 'cheese']
  139. friends_pizza = pizzas[:]
  140. pizzas.append('sausage')
  141. friends_pizza.append('vegan')
  142.  
  143. for pizza in pizzas:
  144. print(f"My favorite pizzas are: {pizza}.")
  145.  
  146. for pizza in friends_pizza:
  147. print(f"My friend's favorite pizzas are: {pizza}.")
  148.  
  149. # A tuple is a list of immutable values or a value that cant be changed
  150. # You create a tuple just like a list but use () instead of []
  151. dimensions = (200, 50)
  152. print(dimensions[0])
  153. print(dimensions[1])
  154.  
  155. # We can loop through tuples the same way as lists with a for loop
  156. for number in dimensions:
  157. print(number)
  158.  
  159. # Although we can can't change the original tuple values, we can re-create it
  160. dimensions = (400, 100)
  161. print(dimensions[0])
  162. print(dimensions[1])
  163.  
  164. # Try it yourself exercises
  165. # Create a list of five food items and store them in a tuple
  166. food_menu = ('pasta', 'burger', 'potato', 'fries', 'yogurt')
  167. for item in food_menu:
  168. print(item)
  169.  
  170. # Replace two of the items with different foods by creating a new tuple and
  171. # print the menu
  172. food_menu = ('pasta', 'burger', 'potato', 'chicken nuggets', 'cheese')
  173. for item in food_menu:
  174. print(item)
  175.  
  176.  
  177. # Set hard wrap to 80 characters to adhere to PEP 8 formatting
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement