Expressions

Sequencing: 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: 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(str(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.

def convert(dec):
    bin = ""
    i = 7

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

    print(bin)

convert(98)
01100010

Strings

Vocab:

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 __
Length is
A substring is ___

Substring/Length: 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(len(string))
print(string[0:5])
print(string[5:8])
8
hello
bye

Concatenation: 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.

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

def length(list):
    for name in names:
        print(name + ": " + str(len(name)))

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