Grades

3.1 & 3.2 3.3 and 3.4 Total
0.95/1 1/1 1.95/2

3.1 and 3.2

Variables

  • Using meaningful variable names help with readability and help keep stuff organized
  • be concisive with variables and thier names

Integer

  • numbers

text/string

  • words phrases

Boolean

  • True or False

List of data can be stored in variable

  • easily remove/add/change items into the list
  • print/retrieve specific values in the list without creating a lot of variables
  • help reduce need for new variables, simplifying the complexity

Assignments

  • assignment operator allows a program to change the value represented by a variable.
  • assigns values to variables
  • Different operators lile =, +=, /=, *=, etc are used to change value of a variable

Data Abstraction

  • Method used in coding to represent data in a useful form, by taking away aspects of data that aren't being used in the situation
  • Variables and lists are primary tools in data abstraction
  • data abstractions help manage complexity in programs by giving a collection of data a name without refernecing the specific detailsof the representation.

Lists and Strings

  • list -> ordered sequence of elements
  • Element -> individual value in a list that is assigned to a unique index
  • Index -> a way to reference the elements in a list or string using natural numbers; each element of a string is referenced by an index
  • String -> ordered sequence of charachters (letters, numbers, special charachters.)

Lists

  • allow data abstraction
  • bundles variables togther
  • can keep adding elements
  • can store elemetns as a single variable by using a list
QuestionsandAnswers= {
    "#1 | What is the operator that displays only true or false?": "boolean", 
    "#2 | What lessons were taught by students this week?": "3.1-3.4", 
    "#3 | Who is the best teacher?": "mr.mort", 
    "#4 | Why are variables important?": "meaningful variable names help with readability and help keep stuff organized", 
    "#5 | What is a list in coding?": "ordered sequence of elements", 
}
def QuestionandResponse(Question): # display the question then return a response
    print(Question)
    Response = input()
    return Response
Correct = 0 

# Setting up the start of the quiz
print("Number of questions: " + str(len(QuestionsandAnswers)))

# Looks through code till desired answer
for key in QuestionsandAnswers:
    rsp = QuestionandResponse(key)  #answer input is saved to a variable
    rsp = rsp.lower() # answer is case sensitive, so match response to lowercase for answer key to work
    if rsp == QuestionsandAnswers[key]: # checks if answer is correct accoring to Questions and answers variale defined at the top.
        print(f"Correct! --> {rsp}")    
        correct += 1
    else:                           # if else statement to print either correct or incorrect
        print(f"{rsp} is super wrong") 

percent = str(round(correct/len(QandA), 2)*100) # calculate percentage

print("You scored " + str(correct) +"/" + str(len(QandA)))
print(f"This is {percent}%") # print score and percentage
QuestionsandAnswers= {
    "#1 | What is the operator that displays only true or false?": "boolean", 
    "#2 | What lessons were taught by students this week?": "3.1-3.4", 
    "#3 | Who is the best teacher?": "mr.mort", 
    "#4 | Why are variables important?": "meaningful variable names help with readability and help keep stuff organized", 
    "#5 | What is a list in coding?": "ordered sequence of elements", 
}
def QuestionandResponse(Question): # display the question then return a response
    print(Question)
    Response = input()
    return Response
Correct = 0 

# Setting up the start of the quiz
print("Number of questions: " + str(len(QuestionsandAnswers)))

# Looks through code till desired answer
for key in QuestionsandAnswers:
    rsp = QuestionandResponse(key)  #answer input is saved to a variable
    # answer is case sensitive, so match response to lowercase for answer key to work
    if rsp == QuestionsandAnswers[key]: # checks if answer is correct accoring to Questions and answers variale defined at the top.
        print(f"Correct! --> {rsp}")    
        correct += 1
    else:                           # if else statement to print either correct or incorrect
        print(f"{rsp} is super wrong") 

print("You scored " + str(correct) +"/" + str(len(QuestionsandAnswers)))
colorList=["green", "red", "Blue"]
colorlist2=[]
    

print(str(colorList))

3.3 and 3.4

Algorithms can be represeted in different ways

  • flow charts

strings

  • len() finds length
  • lower() converts to lowercase
  • concat() returns a string made up of the concatenated strings ex. concat("string1", "string2") would return string1string2
  • substring() returns the characters from the string beginning at the at the first position to the last so an example of this would be substring ("abcdefghijk", 2, 5) would print bcde (pseudocode starts at 1)

3.3 and 3.4 hacks

Variable hacks

  • Problem 1 : 23.91
  • Problem 2 : 1.25
  • Problem 3 : 17
  • Problem 4 : straightbrownshort
Num1 = 50
Num2 = Num1 % 9 + 15 # 50 / 9  +15 = 20.5
Num3 = Num2 / Num1 + ( Num2 * 2 ) #  20.5/50 + (20.5 x 2) = 44.41
Num4 = Num3 + Num1 / 5 - 10 # 44.41+50/5 -10
Result = Num4 - Num2

RESULT = 23.91
Num1 = 10
Num2 = Num1 % 3 * 4 #10/12 or 0.83
Num1 = Num2 # num 1 is now alos 0.83
Num3 = Num1 * 3 # num 3 is 2.5
Result = Num3 % 2 #num 3 (2.5) becomes 1.25

Result = 1.25
valueA = 4
valueB = 90
valueC = 17
valueB = valueC - valueA # 17 - 4
valueA = valueA * 10 # 4 x 10
if valueB > 10: # b is bigger than 10
    print(valueC) # c stays 17

Result = 17
type = "curly"
color = "brown"
length = "short"
type = "straight"
hair = type + color + length # straight + brown + short
print(hair)

String hacks

  • Problem 1: " Mr.Mortenson is very handsome.
  • Problem 2:
Noun = "Mr.Mortenson" 
Adjective = "handsome" 
Adjective2 = "Very" 
Verb = "is" 
abrev = subtring(Noun, 1, 7) 
yoda = concat(Adjective2, " ", Adjective, " ", abrev, " ",Verb, ".") 
display[yoda]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/vscode/ahadsblog/_notebooks/202-09-09-Notes.ipynb Cell 13 in <cell line: 5>()
      <a href='vscode-notebook-cell://wsl%2Bubuntu/vscode/ahadsblog/_notebooks/202-09-09-Notes.ipynb#X15sdnNjb2RlLXJlbW90ZQ%3D%3D?line=2'>3</a> Adjective2 = "Very" 
      <a href='vscode-notebook-cell://wsl%2Bubuntu/vscode/ahadsblog/_notebooks/202-09-09-Notes.ipynb#X15sdnNjb2RlLXJlbW90ZQ%3D%3D?line=3'>4</a> Verb = "is" 
----> <a href='vscode-notebook-cell://wsl%2Bubuntu/vscode/ahadsblog/_notebooks/202-09-09-Notes.ipynb#X15sdnNjb2RlLXJlbW90ZQ%3D%3D?line=4'>5</a> abrev = subtring(Noun, 1, 7) 
      <a href='vscode-notebook-cell://wsl%2Bubuntu/vscode/ahadsblog/_notebooks/202-09-09-Notes.ipynb#X15sdnNjb2RlLXJlbW90ZQ%3D%3D?line=5'>6</a> yoda = concat(Adjective2, " ", Adjective, " ", abrev, " ",Verb, ".") 
      <a href='vscode-notebook-cell://wsl%2Bubuntu/vscode/ahadsblog/_notebooks/202-09-09-Notes.ipynb#X15sdnNjb2RlLXJlbW90ZQ%3D%3D?line=6'>7</a> display[yoda]

NameError: name 'subtring' is not defined
cookie = len("choclate") # 8
cookie2 = len("rasin")  # 5
len1 = cookie / 2 # 8/2 = 4
len2 = cookie2 * 45  # 5 x 45 = 225
vote1 = (cookie, "vote", len2)  # 8, vote, 225
vote2 = (cookie2, "vote", len1) # 5, vote, 4
votes = concat(vote1, " ", vote2)  #8 vote 225 5 vote 4
display[votes] # 225 vote chocolate, 4 vote rasin
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/vscode/ahadsblog/_notebooks/202-09-09-Notes.ipynb Cell 14 in <cell line: 7>()
      <a href='vscode-notebook-cell://wsl%2Bubuntu/vscode/ahadsblog/_notebooks/202-09-09-Notes.ipynb#X16sdnNjb2RlLXJlbW90ZQ%3D%3D?line=4'>5</a> vote1 = (cookie, "vote", len2)  # 8, vote, 225
      <a href='vscode-notebook-cell://wsl%2Bubuntu/vscode/ahadsblog/_notebooks/202-09-09-Notes.ipynb#X16sdnNjb2RlLXJlbW90ZQ%3D%3D?line=5'>6</a> vote2 = (cookie2, "vote", len1) # 5, vote, 4
----> <a href='vscode-notebook-cell://wsl%2Bubuntu/vscode/ahadsblog/_notebooks/202-09-09-Notes.ipynb#X16sdnNjb2RlLXJlbW90ZQ%3D%3D?line=6'>7</a> votes = concat(vote1, " ", vote2)  #8 vote 225 5 vote 4
      <a href='vscode-notebook-cell://wsl%2Bubuntu/vscode/ahadsblog/_notebooks/202-09-09-Notes.ipynb#X16sdnNjb2RlLXJlbW90ZQ%3D%3D?line=7'>8</a> display[votes]

NameError: name 'concat' is not defined

3.5 , 3.6 , 3.7 Notes and Problems

  • Boolean: data type with two possible values (true or false)
  • Selection(conditionals): uses a condition that evaluates to true or false
    • Selection determines which part of an algorithm are executed based on a condition being true or false
  • Algorithm is a finite set of instructions that accomplish a specific task
  • Nested conditional statements consist of conditional statements within other conditional statements
grade1 = 90
grade2 = 65
grade3 = 60
grade4 = 75
grade5 = 95

GradeAverage = (90 + 65 + 75 + 60 + 95) / 5

if GradeAverage > 80 :
    print(GradeAverage)
else :
    print("no")
print("100 == 100:",100==100)
print("Hello == Adios:","greeting"=="farewell")
print("Hello != Adios:","greeting"!="farewell")
print("Hello == Hola:","greeting"=="greeting")
print("5>=4:", 5>=4)
print ('')

# Notice that relational operators can even work on lists!
# For lists, the relational operator compares each respective component until an answer is derived

print("['a','b','c'] > ['x','y','z']:", ['a','b','c'] > ['x','y','z'])
print("[1,2,3,5] > [1,2,3,4]:", [1,2,3,5] > [1,2,3,4])
print("[1,2,3,5] < [1,2,3,4]:", [1,2,3,5] < [1,2,3,4])
print("[1,2,3,5] == [1,2,3,4]:", [1,2,3,5] == [1,2,3,4])
print("1 > 2 or 5 < 12:",) 
# Output TRUE  using OR ^

# Output FALSE using NOT
print("24 > 8:",) 

# Output FALSE using AND
print("10 > 20:", 10 > 20 and false)
x = 20
y = 10
if x > y:
    print("x is greater than y")
else:
    print("x is not greater than y")
x = 2
y = 10
if x > y:
    print("x is greater than y")
else:
    print("x is not greater than y")
num1 = 100
num2 = 100
sum = num1 + num2

if sum >= 200:
    print("200")
else:
    print(sum)

Analyzing Code Walkthrough

score = 82
if (score >= 90)
{
    console.log("You got an A, congrats!")
}
else
{
    if (score >= 75)
    {
        console.log("Please come to retake up to a 90 next week at tutorial!")
    }
    else
    {
        console.log("You have detention!")
    }
} 
//will print 'Please come to retake up to a 90 next week at tutorial!' because score >= 75.

//javascript dont work on python base kernel
protein = 25
carbs = 36
sugar = 11
if (carbs >= 55 || protein <= 20 || sugar >= 15)
{
    console.log("Your lunch is too unhealthy, please pick a new one")
}
else
{
    if (carbs < 35 || protein < 25)
    {
    console.log ("This lunch is alright but try to add some more carbs or protein")
    }
    else 
    {
    if (sugar >= 11)
    {
    console.log ("Looks great but lets see if we can cut down on sugar, we don't want diabetes!")
    }
    else
    {
        console.log ("Amazing, you created a healthy lunch!!!")
    }
    }
}  // will print "Looks great but lets see if we can cut down on sugar, we don't want diabetes!"

//javascript dont work on python base kernel

Writing Nested Code Activity

hours = 25

if (hours <= 8)
{
    print("inexpierenced, 50k salary")
}
else
{
    if (hours >= 10)
    {
    print("expierenced, 150k salary")    
    }
    else
    {
    print("Expierenced, 90k salary")    
    }
} //javascript dont work on python base kernel

3.5 - 3.7 hacks

Write a program that fits these conditions using nested conditionals:

If the product is expired, print "this product is no good"
If the cost is above 50 dollars, and the product isn't expired, print "this product is too expensive"
If the cost is 25 dollars but under 50, and the product isn't expired, print "this is a regular product"
If the cost is under 25 dollars, print "this is a cheap product"
x = "expired"
product = x
cost = 70


if (product <= x, cost <= 50): 
    print("This product good and not expensive" ); 
else: 
    if (product >= x, cost >= 50):
        print("Product is not good and expensive"); 
    else:
        if (product <= x, cost <= 25):
            print("regular product")
        else:
            print("cheap product")
This product good and not expensive
x = 0
score = x

# Question One 
print("What is 1 + 1")
answer_1 = input("a)1 b)2 c)3 d)4:")
if answer_1.lower() == "b" or answer_1.lower() == "2":
    print("Correct")
    x = x + 1   
else:
    print("Incorrect, 1 + 1 is 2")

# Question Two
print("What is my name?")
answer_2 = input("a)ahad b)akshat c)xeem d)josh:")
if answer_2.lower() == "a" or answer_2.lower() == "ahad":
    print("Correct")
    x = x + 1
else:
    print("Incorrect, my name is Ahad")

# Question Three
print("True or False Ahad is 17")
answer_3 = input(":")
if answer_3.lower() == "true" or answer_3.lower() == "T":
    print("Correct")
    x = x + 1
else:
    print("Incorrect")  


#Total Score
score = float(x / 3) * 100
print(x,"out of 3, that is",score, "%")
What is 1 + 1
Correct
What is my name?
Correct
True or False Ahad is 17
Incorrect
2 out of 3, that is 66.66666666666666 %