3.3 Expressions

Vocabulary :)

  1. the symbol for exponent is **
  2. the symbol for addition is +
  3. the symbol for subtraction is -
  4. the symbol for multiplication is *
  5. the symbol for division is /
  6. the symbol for modulus is %
  • this gets you remainder
  1. an algorithm is sequence of steps that do a specific task

practicing sequencing

value1 = 5
value2 = value1 / 10 #step 1
value3 = value2 * 2 #step 2
value4 = value3 - 4  #step 3
print(value4)
-3.0

selection & iteration practice

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

incorporating binary

def convert(number):
    binary = ""
    i = 7

    while i >= 0:
        if number % (2**i) == number:
            binary = binary + "0"
            i -= 1
        else:
            binary = binary + "1"
            number -= 2**i
            i -= 1

    print(binary)

print("Here is your number in binary!")
convert(47)
Here is your number in binary!
00101111

Psuedocode

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

Can be thought of with ordinary things you do: Brushing your 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

  1. substring is a part of a string
#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(string[0:8])
len(string)
hello
bye
hellobye
8

Concatenation Practice

  1. concatenation is the combining of strings
string1 = "butter"
string2 = "fly"
string3 = string1 + string2
print(string3)
butterfly
names = ["cat","dog","racoon","rabbit"]

def length(names):
    for x in names:
        print(x, len(str(x)))

length(names)
cat 3
dog 3
racoon 6
rabbit 6