Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- players = ['shoxie', 'lucky', 'almanek', 'kennys']
- # Here we are defining the variable player and associating each list value to
- # our player variable and then printing each in a loop until finished
- for player in players:
- print(player)
- # Python will store the last value that was assigned to our variable created in the loop
- # So in this example player will retain the value of 'kennys'
- print(player)
- # Expanding on what we can do with a for loop
- for player in players:
- print(f"You are such an amazing player {player.title()}")
- print(f"I really hope you make it far in the major {player.title()}.\n")
- # Python cares about white space so an indent literally defines whats in a for loop
- # Unlike java where we use {} to contain the loop contents
- # The next non indented line will be after the for loop
- print("Everyone did amazing! Keep it up!")
- # Try it yourself example
- pizzas = ['meatlovers', 'pineapple', 'cheese']
- for pizza in pizzas:
- print(f"I like {pizza}")
- print("\nBoy, I sure do love pizza!\n")
- # Try it yourself example 2
- animals = ['cat', 'dog', 'rabbit']
- for animal in animals:
- print(f"A {animal} would make a great pet!")
- print("\nAll of these animals have fur!\n")
- # We can use range to print a list of numbers
- for value in range(1, 6):
- print(value)
- # We can also use list() along with range() to create an actual list of numbers
- # We can also pass a third argument to range() to have it count by x as shown below
- even_numbers = list(range(2, 11, 2))
- print(even_numbers)
- # For example, if we wanted to get the squared value of 1-10 it'd look like below
- squares = []
- for value in range(1, 11):
- squares.append(value ** 2)
- print(squares)
- # If needed we can find certain values of a list like min, max, sum, etc
- digits = [1, 2, 3, 4, 5]
- print(min(digits))
- print(max(digits))
- print(sum(digits))
- # List comprehension allows us to build the same for loop/list in one line
- squares = [value**2 for value in range(1, 11)]
- print(squares)
- # Try it yourself example
- for numbers in range(1, 21):
- print(numbers)
- # for nums in range(1, 1000001):
- # print(nums)
- # Create a list of 1-1million and then confirm the contents, and print its min/max/sum
- oneMillion = []
- for nums in range(1, 1000001):
- oneMillion.append(nums)
- print(f"There are: {len(oneMillion)} numbers in our list!")
- print(min(oneMillion))
- print(max(oneMillion))
- print(sum(oneMillion))
- # Use the range function to make a list of odd numbers 1-20
- odds = []
- for numbers in range(1, 20, 3):
- odds.append(numbers)
- print(numbers)
- # Make a list of multiples of 3 from 3 to 30
- multiples = []
- for threes in range(3, 31):
- tmp = threes * 3
- multiples.append(tmp)
- print(multiples)
- # Or we can do it in one line like shown below
- test = [numTest*3 for numTest in range(3, 31)]
- print(test)
- # We can work with a portion of a list called a slice as shown below
- players = ['shoxie', 'lucky', 'almanek', 'kennys']
- print(players[0:2])
- # If we don't specify a starting point python will automatically start at the first index
- print(players[:3])
- # We can also do it in reverse so here it prints index 2 through the end of the list
- print(players[2:])
- # We can also use a negative value to only get the last x amount of values from the list
- print(players[-3:])
- # We can work with a subset of elements in a list, in a for loop like below
- for player in players[:2]:
- print(player)
- # We can copy a list using a splice if needed
- # Make sure to include [:] or else we will just point friends_foods to my_foods instead of making a copy
- my_foods = ['pasta', 'shrimp', 'pizza', 'toast']
- friends_foods = my_foods[:]
- print(friends_foods)
- # Try it yourself examples
- # Print the first 3 items in a list using a slice
- sliced = [1, 2, 3, 4, 5]
- print(sliced[0:3])
- # Print 3 items from the middle of the list
- print(sliced[1:4])
- # Print the last three items in the list
- print(sliced[-3:])
- # Create a list of your favorite pizzas, make a copy called friends_pizza
- # Add a pizza to your list and your friends list
- # Use a for loop to print out both people's favorite pizzas to prove the lists are different
- pizzas = ['meatlovers', 'pineapple', 'cheese']
- friends_pizza = pizzas[:]
- pizzas.append('sausage')
- friends_pizza.append('vegan')
- for pizza in pizzas:
- print(f"My favorite pizzas are: {pizza}.")
- for pizza in friends_pizza:
- print(f"My friend's favorite pizzas are: {pizza}.")
- # A tuple is a list of immutable values or a value that cant be changed
- # You create a tuple just like a list but use () instead of []
- dimensions = (200, 50)
- print(dimensions[0])
- print(dimensions[1])
- # We can loop through tuples the same way as lists with a for loop
- for number in dimensions:
- print(number)
- # Although we can can't change the original tuple values, we can re-create it
- dimensions = (400, 100)
- print(dimensions[0])
- print(dimensions[1])
- # Try it yourself exercises
- # Create a list of five food items and store them in a tuple
- food_menu = ('pasta', 'burger', 'potato', 'fries', 'yogurt')
- for item in food_menu:
- print(item)
- # Replace two of the items with different foods by creating a new tuple and
- # print the menu
- food_menu = ('pasta', 'burger', 'potato', 'chicken nuggets', 'cheese')
- for item in food_menu:
- print(item)
- # Set hard wrap to 80 characters to adhere to PEP 8 formatting
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement