29

Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

  • Upload
    lecong

  • View
    222

  • Download
    10

Embed Size (px)

Citation preview

Page 1: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is
Page 2: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

[“Alice”, “Bob”, “Carol”, “Dave”]

[4, 8, 15, 16, 23, 42]

[“Emily”, 42, 13, “Fred”, True]

[]

Page 3: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

[“Alice”, “Bob”, “Carol”, “Dave”]

[4, 8, 15, 16, 23, 42]

[“Emily”, 42, 13, “Fred”, True]

[]

friends = [“Alice”, “Bob”, “Carol”, “Dave”]

numbers = [4, 8, 15, 16, 23, 42]

Page 4: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

friends = [“Alice”, “Bob”, “Carol”, “Dave”]

print(friends[0]) # Output: Alice

print(friends[1]) # Output: Bob

friends[3] = “Daniella” # Replaces Dave with Daniella

Page 5: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is
Page 6: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

colours = [“red”, “yellow”, “pink”, “green”, “orange”, “purple”, “blue”]print(colours[0])print(colours[2])print(colours[6])

Page 7: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

numbers = [4, 8, 15, 16, 23, 42]

print(numbers[2:4]) # Output: [15, 16]

Page 8: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

[“cat”, “dog”] + [“fish”, “rabbit”, “hamster”] ==[“cat”, “dog”, “fish”, “rabbit”, “hamster”]

[“cat”, “dog”] * 3 ==[“cat”, “dog”, “cat”, “dog”, “cat”, “dog”]

Page 9: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is
Page 10: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

colours = [“red”, “yellow”, “pink”, “green”, “orange”, “purple”, “blue”]newcolour = input(“Enter a new colour: “)colours = colours + [newcolour] # Could also use +=print(colours)

Page 11: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

len([“cat”, “dog”]) # Value: 2

“fish” in [“cat”, “dog”, “fish”, “rabbit”, “hamster”] == True

“sheep” in [“cat”, “dog”, “fish”, “rabbit”, “hamster”] == False

Page 12: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is
Page 13: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

colours = [“red”, “yellow”, “pink”, “green”, “orange”, “purple”, “blue”]testcolour = input(“Enter a colour: “)if testcolour in colours: print(“That colour is in the list.”)else: print(“That colour is not in the list.”)

Page 14: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

x, y, z = [4, “red”, “yellow”]print(x) # Outputs 4print(y) # Outputs redprint(z) # Outputs yellow

Page 15: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

while

animals = [“cat”, “dog”, “fish”, “rabbit”, “hamster”]i = 0while i < len(animals): print(animals[i]) i += 1

Page 16: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

for variable in list: ... code block ...

animals = [“cat”, “dog”, “fish”, “rabbit”, “hamster”]for x in animals:

print(x)

Page 17: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is
Page 18: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

numbers = [4, 8, 15, 16, 23, 42]total = 0for x in numbers: total += xprint(total)

Page 19: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

range(10) ==[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

for x in range(10): print(x)

Page 20: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

numbers = {1, 2, 4, 8, 2, 3, 2, 4}print(numbers)# Output: {8, 1, 2, 3, 4}

Page 21: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

parameters = (“Alex”, 14)

Page 22: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

parameters = (“Alex”, 14)

parameters = (“Alex”, 14)print(“My name is %s and I am %d years old.” % parameters)

Page 23: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

ages = {“Alice”: 26, “Bob”: 60, “Carol”: 30, “Dave”: 16}print(ages[“Bob”])# Output: 60

Page 24: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

ages = {“Alice”: 26, “Bob”: 60, “Carol”: 30, “Dave”: 16}print(ages[“Bob”])# Output: 60

for (name, age) in ages.items(): print(name)

Page 25: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is
Page 26: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

list1 = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]list2 = [3, 6, 9, 12, 15, 18]print(“Items in both lists:”)for x in list1: if x in list2: print(x)print(“Items in list 1 only:”)for x in list1: if not (x in list2): print(x)print(“Items in list 2 only:”)for x in list2: if not (x in list1): print(x)

Page 27: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is
Page 28: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

# The list to sortmylist = [6, 2, 7, 3, 42, 6, 1]

# Repeat until the list is sorteddone = Falsewhile not done: swapped = False # Iterate over each item in the list except the last for i in range(len(mylist) - 1): # Compare this element and the next one if mylist[i] > mylist[i+1]: # The elements are out of order - swap them temp = mylist[i] mylist[i] = mylist[i+1] mylist[i+1] = temp swapped = True # If no swaps were performed then we are finished done = not swapped

# Output the resultprint(mylist)

Page 29: Alice”, “Bob”, “Carol”, “Dave”] [“Emily”, 42, 13, “Fred ... - Compound Data... · # The list to sort mylist = [6, 2, 7, 3, 42, 6, 1] # Repeat until the list is

●●●