What will happen? Run this code on your computer and on Python Tutor
What will happen below? Will vegetables be mutated?
What will happen? Run this code on your computer and on Python Tutor the difference between the two chunks of code (the one above and the one below)?
def combine_lists(list_1, list_2):
for item in list_2:
list_1.append(item)
return list_1
if __name__ == "__main__":
vegetables = ["kale", "carrot", "peas", "celery"]
fruits = ["banana", "tomato", "pear"]
combine_lists(vegetables, fruits)
print(vegetables)
['kale', 'carrot', 'peas', 'celery', 'banana', 'tomato', 'pear']
What will happen? Run this code on your computer and on Python Tutor the difference between the two chunks of code (the one above and the one below)?
def combine_lists(list_1, list_2):
list_1.extend(list_2)
return list_1
if __name__ == "__main__":
vegetables = ["kale", "carrot", "peas", "celery"]
fruits = ["banana", "tomato", "pear"]
combine_lists(vegetables, fruits)
print(vegetables)
['kale', 'carrot', 'peas', 'celery', 'banana', 'tomato', 'pear']
.append(value)
.insert(index, value)
.pop(index)
.remove(value)
.extend(list)
Indicate when an error occurs.
numbers = [1, 2, 31, 0, 2]
numbers.append(40)
numbers # evaluate this at this point
numbers.insert(3, 10)
numbers # evaluate this at this point
numbers.remove(2)
numbers # evaluate this at this point
numbers.pop(2)
numbers # evaluate this at this point
new_list = [40, 50, 60]
numbers.extend(new_list)
numbers # evaluate this at this point
[1, 2, 31, 0, 2, 40]
remove_and_append
original_list
, to_remove_list
, to_append_list
original_list
removing values found in to_remove_list
and then appending the values in to_append_list
Test case:
def remove_and_append(original_list, to_remove_list, to_append_list):
for item in to_remove_list:
if item in original_list:
original_list.remove(item)
original_list.extend(to_append_list)
return original_list
if __name__ == "__main__":
test_list = [1, 2, 3]
remove_and_append(test_list, [2, 3, 4], [10, 11])
assert test_list == [1, 10, 11]
print(test_list) # [1, 10, 11]
[1, 10, 11]
Write python code that determines if a number is prime or not.
A number n
is NOT prime if it is divisible by any number between 2 and n / 2
.
The integer 1 is not a prime number.
Your code should have different functions to deal with lists of numbers in different ways (see test cases on next slide)
Test cases:
if __name__ == "__main__":
original_list = [1, 2, 453, 3, 32, 5, 7]
result = create_dictionary(original_list)
assert result == {1: False, 2: True, 453: False, 3: True, 32: False, 5: True, 7: True}
original_list = [102, 250, 509, 345, 2, 4, 547, 563]
result = create_dictionary(original_list)
assert result == {102: False, 250: False, 509: True, 345: False, 2: True,
4: False, 547: True, 563: True}
original_list = [102, 250, 509, 345, 2, 4, 547, 563]
mutate_list(original_list)
assert original_list == [False, False, True, False, True, False, True, True]
original_list = [102, 250, 509, 345, 2, 4, 547, 563]
remove_primes(original_list)
assert original_list == [102, 250, 345, 4]
Submit your code to gradescope for attendance. Name your file prime_numbers.py