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 sequence of steps that accomplishes a specific task

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; 1 #step 1
value3 = value2*2; 2 #step 2
value4 = value3-4; 6 #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 i in range(len(numlist)):
    xyz = int(numlist[i])
    if xyz % 3 == 0:
        print( numlist[i] + " 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 convert(decNo):
    divValue = decNo
    remainder = 1
    x = ''
    while divValue > 0:
        remainder = divValue % 2
        divValue = (int)(divValue / 2)
        x = str(remainder) + x
    print(x)
    

convert(4) # 100
convert(255) # 11111111
convert(15) # 1111
convert(39) # 100111
convert(1000) #1111101000
convert (5456) #1010101010000
100
11111111
1111
100111
1111101000
1010101010000

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 when two strings are combined with each other.
Length is the amount of items in a string. A substring is a part of a string.

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!

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"
x = string[0:5]
y = string[5:8]
print(x)
print(y)
hello
bye

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 getStringLength(namelist):
    print('Name List Length = '+ str(len(namelist)))
    for name in namelist:
        s = name + ' - ' + str(len(name)) + ' letters.'
        print(s)

getStringLength(names)
Name List Length = 4
Jaden - 5 letters.
Max - 3 letters.
Dylan - 5 letters.
Orlando - 7 letters.

Stuck?

Have any questions?

  • Ask us if you have any questions!

Unit 3.3 and 3.4 Notes

  • Not many notes here since most were on the document itself!
  • Will still be adding some notes here anyways, for convenience

  • Length - the number of characters/item in a string

  • Addition, subtraction, and division are all represented by their respective symbols
  • The only differences are multiplication and exponents (* and ^)
  • Concatenation (which I cannot spell) is essentially adding 2 strings together
  • Substrings are parts of strings
  • The length of a string is the amount of items or numbers in it
  • 'Str' can be used to initialize something for printing
  • 'len' tells the code to consider the entirety of the string's length