Lesson Notes 3.8 and 3.10
Lesson Notes
iterable = [1, 2, 3, 4]
for num in iterable:
print(num)
The code above is an example of iteration, which is basically going over and over until the condition is met, then it stops. Or in this case it spits out everything that is in that list in order, and then stops
procedures and Procedure calls
-
It takes zero or more arguments
-
The procedures in college board are block statements
-
The return statemen may appear at any point inside the procedure and causes an immediate retrn from the procedure back to the calling statement
Robot
- It basically gives directions to move left or right. Or rotate it 90 degrees or something like that. Then I think it is kind of like the turtle function where you can actually see a figure moving to those commands
Listing Operations
-
there are several ways that you can list things:
-
[value1, value2, value3]
-
there are a few more ways to do a list but the one above is the most common way to do it.
Relational and Boolean Operators
-
It evaluates true if the condition is false; otherwise evaluates to false
-
Boolean are true and false statements
-
the AND and OR statements compare the two conditions and that is a boolean statement because it is a true or false comparison type of statement.
-
Iteration: a repeating portion of an algorithm, repeats a specified number of times or until a given condition is met
-
Iteration Statements: change the sequential flow of control by repeating a set of statements zero or more times, until a stopping condition is met
-
Repeat Until: if the condition evaluates to true initially, the loop body is not executed at all, due to the condition being checked before the loop
Lists examples:
append() - adding element to the end of the list
names = ['taiyo', 'ethan', 'parav', 'nikhil']
# add element to the end of the list
names.append('luna')
# start with empty list
names = []
names.append('taiyo')
names.append('ethan')
names.append('parav')
names. append('nikhil')
insert() - adding an element in a specific position
names = ['luna', 'taiyo', 'ethan', 'parav', 'nikhil']
# adding in a specific position
names.insert(0, 'taiyo')
names.insert(3, 'ethan')
remove() - remove an item from the list
names = ['luna', 'taiyo', 'ethan', 'parav', 'nikhil']
# deleting by value
names.remove('parav')
# by position
del names[-1]
pop() - takes the item off the top of the "stack" (by default it returns the last element of the list but you can also pop from any position)
names = ['luna', 'taiyo', 'ethan', 'parav', 'nikhil']
# pop last item of list
most_recent_name = names.pop()
print(most_recent_name)
# pop first item of list
first_name = names.pop(0)
print(first_name)
len() - returns the number of items in a list
names = ['luna', 'taiyo', 'ethan', 'parav', 'nikhil']
# find length of list
num_names = len(names)
print("There are " + str(num_names) + " names.")
sorted() - returns a copy of the list which leaves the original copy unchanged
names = ['luna', 'taiyo', 'ethan', 'parav', 'nikhil']
# sort permanently
names.sort()
# sort permanently in reverse alphabetical order
names.sort(reverse=True)
# temp. sort
print(sorted(names))
print(sorted(names, reverse=True))
# reversing the order of a list
names.reverse()
range() - use to work with number efficiently
for number in range(11):
print(number)
# making list of numbers 1 to 10
# using the list() value can generate a large list of numbers
numbers = list(range(1, 10))
min() - find the least value in list
nums = [10, 30, 40, 60, 70, 60]
least = min(nums)
print(min(nums))
max() - find the highest value in list
nums = [10, 30, 40, 60, 70, 60]
highest = max(nums)
print(max(nums))
sum() - sum of all in list
nums = [10, 30, 40, 60, 70, 60]
total = sum(nums)
print(sum(nums))