This follows the lesson plan at https://edube.org
Program 51 – Conjunctions, Disjunctions, and Negations
not (p and q) == (not p) or (not q)
not (p or q) == (not p) and (not q)
can be used in the abbreviated form known as op=
& (ampersand) - bitwise conjunction;
| (bar) - bitwise disjunction;
~ (tilde) - bitwise negation;
^ (caret) - bitwise exclusive or (xor)
Let's make it easier:
& requires exactly two 1s to provide 1 as the result;
| requires at least one 1 to provide 1 as the result;
^ requires exactly one 1 to provide 1 as the result.
Program 52 – Arrays / Lists
numbers = [10, 5, 7, 2, 1]
print("List content:", numbers) # Printing original list content.
numbers[0] = 45
print("List content:", numbers) # Printing original list content.
print(len(numbers))
del numbers[3]
print(numbers)
print(numbers[3])
numbers = [10, 5, 7, 2, 1]
print("Original list content:", numbers) # Printing original list content.
numbers[0] = 111
print("\nPrevious list content:", numbers) # Printing previous list content.
numbers[1] = numbers[4] # Copying value of the fifth element to the second.
print("Previous list content:", numbers) # Printing previous list content.
print("\nList's length:", len(numbers)) # Printing previous list length.
###
del numbers[1] # Removing the second element from the list.
print("New list's length:", len(numbers)) # Printing new list length.
print("\nNew list content:", numbers) # Printing current list content.
###
print(numbers[-1])
This will print the LAST number in a list.
print(numbers[-2])
Similarly, the element with an index equal to -2 is the
next to the last element in the list.
Program 53 – More on Lists
hat = [1, 2, 3, 4, 5]
print(hat)
q = int(input("Change number 3 to any other number: "))
hat[2] = q
del(hat[4])
print(hat)
print(len(hat))
print(hat)
Program 54 – Functions and Methods
result = function(arg) # Passes something back to the data
result = data.method(arg) # Method does something to create a value
list.append(value) # appends to the END of the list
list.insert(location, value) # inserts SOMEWHERE in the list
numbers = [111, 7, 2, 1]
print(len(numbers))
print(numbers)
###
numbers.append(4)
print(len(numbers))
print(numbers)
###
numbers.insert(0, 222) # NOTE: This does not use brackets
print(len(numbers))
print(numbers)
numbers.insert(1, 333) # NOTE: This does not use brackets
print(numbers)
Program 55 – Working with Lists
my_list = [] # Creating an empty list.
for i in range(5):
my_list.append(i + 1)
# appends to the end
print(my_list)
my_list2 = [] # Creating an empty list.
for i in range(5):
my_list2.insert(0, i + 1) # inserts as first char
print(my_list2)
Program 56 – USING len() or just for loop
my_list = [10, 1, 8, 3, 5]
total = 0
for i in my_list:
total += i
print(total)
my_list = [10, 1, 8, 3, 5]
total = 0
for i in range(len(my_list)):
total += my_list[i]
print(total)
Program 57 – Append, Delete, Insert
# step 1
beatles = []
print("Step 1:", beatles)
# step 2
beatles.append("John")
beatles.append("Paul")
beatles.append("George")
print("Step 2:", beatles)
# step 3
for i in range(2):
beatles.append(input("Names Stu and Pete: "))
print("Step 3:", beatles)
# step 4
del beatles[4]
del beatles[3]
print("Step 4:",beatles)
# step 5
beatles.insert(0, "Ringo") # inserts as first char
print("Step 5:", beatles)
# testing list legth
print("The Fab", len(beatles))
# Ad Hoc Test
print(beatles[3]) # outputs: George
# Ad Hoc Test
beatles.insert(0, "Steve")
beatles.append("Bob")
print(beatles)
Program 58 – Bubble Sort
my_list = [8, 10, 6, 2, 4, 9, 15, 22, 3, 5] # list to sort
swapped = True # It's a little fake, we need it to enter the while loop.
print(my_list)
while swapped:
swapped = False # no swaps so far
for i in range(len(my_list) - 1):
if my_list[i] > my_list[i + 1]:
swapped = True # a swap occurred!
my_list[i], my_list[i + 1] = my_list[i + 1], my_list[i]
print(my_list)
# OR
simple_sort = [8, 10, 6, 2, 4, 9, 15, 22, 3, 5]
print(simple_sort)
simple_sort.sort()
print(simple_sort)
simple_sort.reverse()
print(simple_sort)
Program 59 – SORT STORED IN MEMORY SPACE – CAUTION
list_1 = [1]
list_2 = list_1
list_1[0] = 2
print(list_2) # PRINTS OUT A 2
You could say that:
1. the name of an ordinary variable is the name of its content;
2. the name of a list is the name of a memory location where the list is stored.
list_1 = [1]
list_2 = list_1[:]
list_1[0] = 2
print(list_2) # PRINTS OUT A 1
This is called a slice. It copies the contents, not the memory location.
new_list = my_list[:] # Copies entire list contents
my_list[0:end] is the same as my_list[:end]
my_list[:3] copies data from 0, 1, and 2, but not 3
my_list[start:] is the same as my_list[start:len(my_list)]
new_list = my_list[3:] copies everything from the 4th value to the end
omitting both start and end makes a copy of the whole list:
my_list = [10, 8, 6, 4, 2]
new_list = my_list[:]
You can delete with slices as well
my_list = [10, 8, 6, 4, 2]
del my_list[1:3]
print(my_list)
Deleting all the elements at once is possible too:
my_list = [10, 8, 6, 4, 2]
del my_list[:]
print(my_list)
The list becomes empty, and the output is: []
Program 60 – Does a list contain X Y Z
elem in my_list # returns True
elem not in my_list # returns False
my_list = [0, 3, 12, 8, 2]
print(5 in my_list)
print(5 not in my_list)
print(12 in my_list)
# Find largest value in a list
my_list = [17, 3, 11, 34, 5, 1, 9, 7, 15, 13]
largest = my_list[0]
for i in range(1, len(my_list)):
if my_list[i] > largest:
largest = my_list[i]
print(largest)