3.3 Expressions(Show video 1 and 3)

Vocab: fill in the blanks

the symbol for exponent is *
the symbol for addition is + the symbol for subtraction is - the symbol for multiplication is
the symbol for division is / the symbol for modulus is %
an algorithm is a set of instructions that can repeatedly produce a set of results with different inputs.

Sequencing Practice: the code below does not follow the intended steps below. change the code so that it does so.

  1. divide value1 by 10(value1 = 5)
  2. multiply 2 from the result of the step 1
  3. subtract 4 from the result of the step 2
  4. print the result of step 3
value1 = 5
value2 = value1 / 10 #step 1
value3 = value2 * 2 #step 2
value4 = value3 - 4 #step 3
print(value4)
-3.0

Selection/Iteration Practice: Create a function to print ONLY the numbers of numlist that are divisble by 3.
Hint: use the MOD operator (a % b) to find the remainder when a is divided by b.

numlist = ["3","4","9","76","891"]
for num in numlist:
    if int(num) % 3 == 0:
        print( num + " is divisible by 3")
        continue
    else:
        continue
            
3 is divisible by 3
9 is divisible by 3
891 is divisible by 3

Homework/Binary Adaptation: Create a python function that will convert a decimal number 1-255 to binary using mathematical operations and powers of 2. Challenge: add frontend with javascript or html.

def takeinput(ct):
    numtype = input(ct+": int or bin?")
    num = input(ct+": please enter your number")
    if numtype == "int":
        num = int(num)
    return num
def bintoint(s):
    ct = 0
    ans = 0
    for i in s[::-1]:
        if i == "1":
            ans += 2**ct
        ct += 1
    return ans
def inttobin(num):
    if num == 0:
        return 0
    ct = 0
    ans = ""
    while(2**ct <= num):
        ct += 1
    ct -= 1
    while(ct >= 0):
        if num >= 2**ct:
            num -= 2**ct
            ans += "1"
        else:
            ans += "0"
        ct -= 1
    return ans
num1 = takeinput("1")
num2 = takeinput("2")
if type(num1) == type("a"):
    num1 = bintoint(num1)
if type(num2) == type("a"):
    num2 = bintoint(num2)
print("decimal sum: "+str(num1+num2))
print("binary sum: "+str(inttobin(num1+num2)))
print("decimal difference: "+str(num1-num2))
if num1 >= num2:
    print("binary difference: "+str(inttobin(num1-num2)))
else:
    print("binary difference: -"+str(inttobin(num2-num1)))
print("decimal product: "+str(num1*num2))
print("binary product: "+str(inttobin(num1*num2)))
print("decimal quotient: "+str(num1//num2))
print("binary quotient: "+str(inttobin(num1//num2)))
print("decimal remainder: "+str(num1%num2))
print("binary remainder: "+str(inttobin(num1%num2)))
'10000'

3.4 Strings(Show video 1)

Vocab: fill in the blanks using the video

Index is a number representing a position, like a character's position in a string or a string's position in a list.
Concatenation is connecting two strings into one. Length is the number of characters or elements in a string or array, respectively.
A substring is a section of a string, containing some or all of the characters.

What is psuedocode?

Pseudocode is writing out a program in plain language with keywords that are used to refer to common coding concepts.

Can you think of some benefits of using pseudocode prior to writing out the actual code?

  1. Choose an everyday activity
  2. Imagine that you are providing instructions for this activity to a person who has never done it before
  3. Challenge someone to do the steps you wrote out

Ex. Brushing Teeth

  1. Pick up your toothbrush
  2. Rinse toothbrush
  3. Pick up toothpaste
  4. Place toothpaste on the toothbrush
  5. Rinse toothbrush again
  6. Brush teeth in a circular motion
  7. Spit
  8. Wash mouth
  9. Rinse toothbrush
  10. You have brushed your teeth!

Sleeping

  1. Finish all other work
  2. walk to the bathroom
  3. turn on the light
  4. pick up the brush
  5. apply paste onto the brush
  6. rub the brush throughly through your teeth
  7. wash your mouth and the brush
  8. walk to your bed
  9. spread out the blanket on the bedsheet
  10. If the temperature is cold, then spread a second blanket
  11. turn off the light
  12. lie down on the bed with your head on the pillow
  13. cover yourself with the blanket
  14. Sleep

Substring/Length Practice: change the print functions to print "hello", "bye", and the string length

#the substring will have the characters including the index "start" to the character BEFORE the index "end"
#len(string) will print the length of string

string = "hellobye"
print(string[0:5])
print(string[5:8])
print(len(string))
hello
bye
8

Concatenation Practice: combine string1 and string2 to make string3, then print string3.

string1 = "computer"
string2 = "science"
string3 = string1 + string2
print(string3)
computerscience

Homework/List Adaptation: create a function that prints the name of each string in the list and the string's length. Challenge: add frontend with javascript or html.

names = ["jaden","max","dylan","orlando"]

def length(list):
    for string in list:
        print(string, len(string))

length(names)
jaden 5
max 3
dylan 5
orlando 7

Stuck?

Have any questions?

  • Ask us if you have any questions!

Notes:

  • Using coding, we can do math and algebra as we would in math class with variables
  • We can use operators like +, - , *, /, **, and %
  • % represents the remainder after division, so 23 % 4 = 3
  • We can use conditionals, fuctions and loops to manipulate varuables in our program.
  • Strings and arrays can be acessed with index, which represents the position withing that list or array in which an element or character is stored.
  • While indexes in most pogramming languages begin with 0, indexes on the AP Exam and pseudocode begin with 1, as we would count.
  • Pseudocode is instructions to represent what a certain program does without useing code
  • Pseudocode can be helpful for others who are trying to read our code so they understand the gist of a program.
  • Substrings can be taken from strings to acess a range of incices in a string.
  • Strings can be concatonated together using the + symbol, "jkcd" + "sj" = "jkcdsj"