Lesson 2 notes and hacks
- Notes:
- How I aided my understanding:
- Solution:
- Change the variables in the first section, so that each if statement resolves as True.
- solution:
- Hacks 1:
- Hacks 2:
- Hacks 3:
Notes:
Algorithms:
- set of instructions that are made to accomplish a task or produce a solution. Algorithms have three parts:
- Sequencing
- Selection
- Iteration
Sequencing:
- Order of how to do something to get a result. Ex: how a student would follow the instructions from a teacher
Selection:
- allows an algorithm to make a decision based on a condition. Often the decision is true or false
Iteration:
- is a loop. When you iterate, the code keeps repeating until a condition (set by the programmer) is met
Mathematical Expressions:
- sequential statements:
- set of statements that are executed one after another (in a sequence)
- Add: x+y
- Subtract: x-y
- Multiply: x*y
- Div: x/y
- Mod: x % y (x and y can be string or number)
Strings:
- concatenation: Joins two strings together
- substring: part of an already existing string
How I aided my understanding:
When the lesson was taught, it didn't really make sense to me. Because of this, I had only improved my understanding of the topics by a tiny amount. I went to learnpython.org and worked through their lessons on related material and then completed their exercise. Here are some of the online problems I used to practice.
s = "Hey there! what should this string be?"
# Length should be 20
print("Length of s = %d" % len(s))
# First occurrence of "a" should be at index 8
print("The first occurrence of the letter a = %d" % s.index("a"))
# Number of a's should be 2
print("a occurs %d times" % s.count("a"))
# Slicing the string into bits
print("The first five characters are '%s'" % s[:5]) # Start to 5
print("The next five characters are '%s'" % s[5:10]) # 5 to 10
print("The thirteenth character is '%s'" % s[12]) # Just number 12
print("The characters with odd index are '%s'" %s[1::2]) #(0-based indexing)
print("The last five characters are '%s'" % s[-5:]) # 5th-from-last to end
# Convert everything to uppercase
print("String in uppercase: %s" % s.upper())
s = "I like pancakes with"
# Length should be 20
print("Length of s = %d" % len(s))
# First occurrence of "a" should be at index 8
print("The first occurrence of the letter a = %d" % s.index("a"))
# Number of a's should be 2
print("a occurs %d times" % s.count("a"))
# Slicing the string into bits
print("The first five characters are '%s'" % s[:5]) # Start to 5
print("The next five characters are '%s'" % s[5:10]) # 5 to 10
print("The thirteenth character is '%s'" % s[12]) # Just number 12
print("The characters with odd index are '%s'" %s[1::2]) #(0-based indexing)
print("The last five characters are '%s'" % s[-5:]) # 5th-from-last to end
# Convert everything to uppercase
print("String in uppercase: %s" % s.upper())
number = 10
second_number = 10
first_array = []
second_array = [1,2,3]
if number > 15:
print("1")
if first_array:
print("2")
if len(second_array) == 2:
print("3")
if len(first_array) + len(second_array) == 5:
print("4")
if first_array and first_array[0] == 1:
print("5")
if not second_number:
print("6")
number = 16
second_number = 6
first_array = [1,2,3]
second_array = [3,2]
if number > 15:
print("1")
if first_array:
print("2")
if len(second_array) == 2:
print("3")
if len(first_array) + len(second_array) == 5:
print("4")
if first_array and first_array[0] == 1:
print("5")
if not second_number:
print("6")
Hacks 1:
- don't know what you're asking for
- sequence: There are no conditions or decisions to be made. Therefore, it has to be a sequence
- selection: The condition is if number = item and the decision is display or do nothing. Therefore it has to be a selection.
- iteration: the code scans for more numbers and if the code finds the command to be true, it loops until the code finds no more numbers. Therefore, this has to be an iteration.
- sequence: There are no conditions or decisions to be made. Therefore, it has to be a sequence
numlist = [25, 32, 42]
list_ave = sum(numlist) / len(numlist)
print(list_ave)
num1 = 5
num2 = num1 * 3
num3 = num2 / num1 * (9 % 2) * 4
result = (num3 % num1 + num2) % num3 * 3 / 5
print(result)