In [140]:
# Adding together
fruit = ['apple', 'pear', 'orange']
vegetable = ['carrot', 'broccoli', 'celery']
food_fruit_first = fruit + vegetable
print(food_fruit_first)
food_vegetable_first = vegetable + fruit
print(food_vegetable_first)
In [141]:
# Repeating List as characters
na = 'na'
batman = 'batman'
meow = na * 2
nana = meow
print(meow)
print((nana + ' ') * 8 + batman)
nana_with_space = nana + ' '
print('{0}{0}{0}{0} {1}'.format(nana_with_space, batman))
In [142]:
# Repeating lists
values = [1,2,3,4,5]
def multiply_by_2(value):
return value * 2
print(values * 2)
print(list(map(multiply_by_2, values)))
In [143]:
# Removing elements from lists
bag = ['knife', 'chapstick', 'cards', 'plum', 'jacket']
print(bag)
del bag[0]
print(bag)
In [144]:
# Removing elements from lists -- this has an error, be ready
fruit = ['apple', 'pear', 'orange']
vegetable = ['carrot', 'broccoli', 'celery']
food = fruit + vegetable
def is_fruit(item):
return item in fruit
print('Food starts wtih', food)
# Remove all the fruit we've added -- or so we thought
for index in range(len(food)):
print('I\'m at list location', index)
print('Length of list', len(food))
if is_fruit(food[index]):
print('Deleted', food[index], 'index', index)
del food[index]
print('Food ends with', food)
In [145]:
# Better way of removing from the list
# Removing elements from lists
fruit = ['apple', 'pear', 'orange']
vegetable = ['carrot', 'broccoli', 'celery']
food = fruit + vegetable
print('Food starts wtih', food)
# Remove all the fruit we've added
for item in fruit:
if item in food:
food.remove(item)
print('Food ends with', food)
numbers = [1,2,3,4,1,2,3,4]
print(numbers)
numbers.remove(1)
print(numbers)
numbers.remove(1)
print(numbers)
numbers.remove(1)
In [146]:
# Looking at the is operator
def default_return():
pass
print(default_return() is None)
In [ ]:
In [147]:
first_list = []
second_list = []
print(first_list)
print(second_list)
print('Equal', first_list == second_list)
print('The same', first_list is second_list)
print(id(first_list), id(second_list))
In [148]:
first_a = 'a'
second_a = 'a'
print('Equal', first_a == second_a)
print('The same', first_a is second_a)
print(id(first_a), id(second_a))
In [149]:
first_int = 1
second_int = 1
print('Equal', first_int == second_int)
print('The same', first_int is second_int)
print(id(first_int), id(second_int))
In [150]:
def starts_with_a(word):
if type(word) is str:
return word.startswith('a')
foods_bar = ['apple', 'anise', 'blueberry', 'pizza', 'chips']
foods_foo = ['chicken', 'apricote', 'charmander']
foods_starting_with_a = []
for food in foods_bar:
if starts_with_a(food):
foods_starting_with_a.append(food)
for food in foods_foo:
if starts_with_a(food):
foods_starting_with_a.append(food)
print(foods_starting_with_a)
In [151]:
numbers = [1,2,3,4]
numbers_two = [5,6,7,8]
print(numbers)
print(numbers_two)
starting_numbers = [list(numbers), numbers_two]
print(starting_numbers)
print(id(starting_numbers))
print(id(numbers))
print(id(starting_numbers[0]))
numbers.append(200)
print(numbers)
print(starting_numbers)
In [152]:
from pprint import pprint
fruit = ['apple', 'pear', 'grape']
non_perishable = ['chips', 'soup', 'cans']
meat = ['bologna', 'beef', 'turkey']
warehouse_foods = [
fruit,
non_perishable
]
material = ['cotton', 'hide', 'denim']
made_goods = ['gloves', 'boots', 'jeans']
warehouse_goods = [
material,
made_goods
]
warehouse_foods_and_goods = warehouse_foods + warehouse_goods
# Print out the lists of all foods and goods
print('Warehouse of foods and goods')
pprint(warehouse_foods_and_goods)
print()
# Change the food wharehouse to have another set of food
warehouse_foods.append(meat)
print('Warehouse of foods')
pprint(warehouse_foods)
print()
# See what's in the food and goods wharehouse again
print('Warehouse of foods and goods')
pprint(warehouse_foods_and_goods)
print()
# Add more fruit
fruit.append('peach')
print('Fruits')
print(fruit)
print()
# See what's in the food warehouse
print('Food warehouse')
pprint(warehouse_foods)
print()
# See what's in the food and goods ware house
print('Food and goods')
pprint(warehouse_foods_and_goods)
print()
# Remove the materials from the warehouse_goods
del warehouse_goods[1]
print('Warehouse of goods')
pprint(warehouse_goods)
print()
# See what's left in the food and goods warehouse
print('Warehouse of Foods and Goods')
pprint(warehouse_foods_and_goods)
print()
In [153]:
my_list = [2, 5, 3, 1]
# run first iteration bubble
my_list = [2, 3, 5, 1]
# run second iteraiton bubble
my_list = [2, 3, 1, 5]
# run third iteration bubble
my_list = [2, 1, 3, 5]
# run fourth iteration bubble
my_list = [1, 2, 3, 5]
# run fifth iteration bubble
# DONE my_list is sorted
In [154]:
# Bubble Sort implementation:
def bubble_sort (items):
is_sorted = False
while is_sorted is False:
nswaps = 0
for index in range(1, len(items)):
previous_index = index - 1
print(items)
if items[previous_index] > items[index]:
current_item = items[index]
previous_item = items[previous_index]
items[previous_index] = current_item
items[index] = previous_item
nswaps =+ 1
print(items)
if nswaps is 0:
is_sorted = True
return items
list_1 = [1, 2, 3, 4, 5]
list_2 = [5, 4, 3, 2, 1]
list_3 = [4, 5, 3, 1, 2]
print(bubble_sort(list(list_1)))
print(bubble_sort(list(list_2)))
print(bubble_sort(list(list_3)))
assert bubble_sort(list(list_1)) == sorted(list(list_1))
assert bubble_sort(list(list_2)) == sorted(list(list_2))
assert bubble_sort(list(list_3)) == sorted(list(list_3))
In [ ]:
In [ ]: