3.3 Mathematical Expressions

Vocabulary: 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 function with a set of rules that tells a program what to do.

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 number in numlist:
    if (int(number) % 3) == 0:
        print(number + " is divisible by 3")
        continue
    else:
        continue
            
3 is divisible by 3
9 is divisible by 3
891 is divisible by 3

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 combining strings.

Length is the amount of characters in a string.

A substring is individual characters 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"
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

This section is for my completion of the homework assignments.

3.3 - Binary Calculator

Below is my code for the binary assignment from the 3.3 lesson.

import random

def convert(input):
    if 0 <= input <= 255:
        pass
    else:
        print("Invalid input.")
        return
    i = 7
    binary = ""
    while i >= 0:
        if input % (2**i) == input:
            binary += "0"
            i -= 1
        else:
            binary += "1"
            input -= 2**i
            i -= 1
    print(binary)

convert(-3)
convert(284)
convert(random.randrange(256))
Invalid input.
Invalid input.
11101001

3.4 - Name Length Loop + Frontend Challenge

Below is my code for the name length assignment of 3.4. Below it is my attempt at the challenge to convert the data into frontend, as well as a block showing the code behind the table.

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

def length(list):
    for name in names:
        print(name.capitalize() + " is", str(len(name)), "letters long.")

length(names)
Jaden is 5 letters long.
Max is 3 letters long.
Dylan is 5 letters long.
Orlando is 7 letters long.
<table id="nametable">
  <!--SCRIPT DATA GOES HERE-->
</table>

<script>
    table = document.getElementById("nametable");
    newhtml = "";
    defaulthtml = "<tr><th>Name</th><th>Length</th></tr>";
    var namelist = ["Jaden", "Max", "Dylan", "Orlando"];
    arrayLength = namelist.length;
    for (var i = 0; i < arrayLength; i++) {
        console.log(namelist[i], namelist[i].length);
        newhtml = newhtml + "<tr><td>" + namelist[i] + "</td><td>" + String(namelist[i].length) + "</td></tr>";
        table.innerHTML = defaulthtml + newhtml
    };
</script>