3.1-3.2

3.1.1 Data Types and Variables

  • variable: abstraction inside a program that holds a value (each variable has associated data storage that represents a single value at a time)
  • typically have meaningful names that helps with the overall organization/understanding of the code
  • different programming languages provide a variety of methods to represent data (variables, Booleans, numbers, lists, strings)
  • one form of a value is better suited for representation than antoher
  • Data Types
    • integer (numbers)
    • string (or text/letters)
    • boolean (True/False statements)
age = "25"
name = "Timmy"

print(name + " is " + age)
Timmy is 25
car = "Tesla" #example of a string
yearmake = "2020"
ownerage = 16 #example of an integer
haslicense = True #example of a Boolean

print("My car is a " + car + " made in " + yearmake)
print("age: " + str(ownerage))

if haslicense == True:
    print("can drive")
else:
    print("cannot drive")
My car is a Tesla made in 2020
age: 16
can drive

3.1.2 Variables

  • An assignment operator asigns the value on the right of it (in this case =) to the variable on the left.
  • Collegeboard pseudocode uses the arrow, <--, to assign values to variables.
  • Python uses the = as an assignment operator
  • to assign a variable you need 3 parts:
    1. variable name
    2. assignment operator
    3. variable value
  • A variable, x, is initially given a value of 15. Later on, the value for x is changed to 22. If you print x, the command would display 22, since it has been updated and assigned the new value.

3.2.1 List and Strings Using Variables

  • string: a series of characters (numbers, letters, etc)
  • list: sequences of elements with each element being a variable
  • An element a smaller part in a bigger system, such as an item in a list.
  • An easy way to reference the elements in a list/string is to index it (the 3rd element, 2nd element, etc).
  • example of a string: "cookie monster"
todolist = ["finish hacks", "math hw", "chores"]
print(todolist[2]) # indexing from begining
print(todolist[-3]) # indexing from the end
chores
finish hacks

3.2.2 Data Abstraction with Lists

  • Lists bundle together multiple elements and/or variables under one name are not defined with specified lengths.
  • The variables that are stored in a list do not have to be limited to one kind of variable.
  • The user does not need to know how the list stores data (this gives way for data abstraction).
num1=input("Input a number. ")
num2=input("Input a number. ")
num3=input("Input a number. ")
add=input("How much would you like to add? ")

# Add code in the space below

numlist = [int(num1), int(num2), int(num3)] # Here I defined numlist so that it could be used in the abstraction later

# The following is the code that adds the inputted addend to the other numbers. It is hidden from the user.

for i in numlist:
    numlist[i -1] += int(add)

print(numlist)
[2, 3, 4]

3.2.3 Managing Complexity with lists

  • assinging values to one variable: use square brackets "[]" to store the values of a certain variable, then you can print the variable to output your desired list (more efficient and less complex)
import getpass, sys

def question_with_response(prompt):
    print("Question: " + prompt)
    msg = input()
    return msg

questions = 4
correct = 0

print('Hello, ' + getpass.getuser() + " running " + sys.executable)
print("You will be asked " + str(questions) + " questions.")
question_with_response("Are you ready to take a test?")

rsp = question_with_response("The purpose of lists and dictionaries are to manage the ____ of a program")
if rsp == "complexity":
    print(rsp + " is correct!")
    correct += 1
else:
    print(rsp + " is incorrect!")

rsp = question_with_response("Lists are a form of data ______")
if rsp == "abstraction":
    print(rsp + " is correct!")
    correct += 1
else:
    print(rsp + " is incorrect!")

rsp = question_with_response("Which brackets are used to assign values to a variable to make a list?")
if rsp == "[]":
    print(rsp + " is correct!")
    correct += 1
else:
    print(rsp + " is incorrect!") 

print(getpass.getuser() + " you scored " + str(correct) +"/" + str(questions))
Hello, alexac54767 running /bin/python3
You will be asked 4 questions.
Question: Are you ready to take a test?
Question: The purpose of lists and dictionaries are to manage the ____ of a program
complexity is correct!
Question: Lists are a form of data ______
abstraction is correct!
Question: Which brackets are used to assign values to a variable to make a list?
[] is correct!
alexac54767 you scored 3/4
food1 = "pizza" 
food2 = "hot dog" 
food3 = "sushi"
food4 = "strawberry"
food5 = "sandwich"
print(food1, food2, food3, food4, food5)

food = [food1, food2, food3, food4, food5] #this is simplifying it (making food a list and then only need one word to print it)
print(food)
pizza hot dog sushi strawberry sandwich
['pizza', 'hot dog', 'sushi', 'strawberry', 'sandwich']
name1 = "Alexa"
name2 = "Ava"
name3 = "Lydia"
name4 = "Sri"
print(name1, name1, name3, name4)

# managing complexity
names = [name1, name2, name3, name4]
print(names)
Alexa Alexa Lydia Sri
['Alexa', 'Ava', 'Lydia', 'Sri']

3.3-3.4

3.3.1 algorithm that uses sequencing without using programming language

  • algorithms: a finite set of instruction that accomplish a task (it can be expressed by natural language, diagrams, + more)
  • 3 parts to an algorithm: sequencing, selection, and iteration.
  • every algorithm can be created by a mixture of sequencing, selection, and iteration

    • sequence: the order of how to do something to achieve a result
    • selection: allows an algorithm to make a decision based on if a condition is met
    • iteration: a loop and doing something again until a condition is met

Describe the given algorithm

  • sequencing:
    • The sequencing in this algorithm provided is how it has the steps (1-5) that must be performed in order. To go onto step 2, you miust do step 1 first.
  • Selection:
    • Selection is used in this algorithm when it decides what to do when it evaluates if the numer equals the item. If it does, it must decide to display "item foud". However, if it doesn't, then the algorithm decides to display "item not found".
  • Iteration:
    • Iteration is used in this algorithm when it must cycle through the list, checking if the condition is met. Once it has gone through all of the number, it returns to step 2 to repeat.

3.3.2 Mathematical Expressions

  • Question: How do we represent a step-by-step algorithmic process using sequential code statements?

  • Sequential statements are used in processes to specify how signals are assigned

  • the process is executed in order as a whole
  • after all the sequential statements in the process are executed the signals are assigned new values.
  • execute in the order in which they appear in the process (sequencing)
  • numerical values can be within variables
  • variable values can be inside other variables

3.3.3 Evaluate expression using arithmetic operator

  • arithmetic operator: language that use addition, subtraction, multiplication, division, and modulus operators
  • order of arithmetic operation = same with mathematic operation(PEMDAS but Subtraction first)
num1 = 5
num2 = num1 * 3
num3 = num2 / num1 * (9 % 2) * 4
result = (num3 % num1 + num2) % num3 * 3 / 5

print(result)
3.0

crossword puzzle hack

  • 1 down - Iteration (creates a loop in the algorithm)
  • 2 down - Selection (allows an algorithm to create a decision)
  • 3 across - Sequence (an order of events)

3.4 Evaluate expression that manipulates strings

  • string concatenation: joins two or more strings end-to-end to make a new string
  • substring: part of an existing string
    • substring (str1, str2, length): returns a substring of consecutive character from str1, starting the character at position 'start' and containing 'length' character
    • ^ Website that explains this
  • strings: ordered sequences of characters
    • substring (str1, str2, length): returns a substring of consecutive character from str1, starting the character at position 'start' and containing 'length' character
len("Hi Mr. Mortensen")
16
string1 = "Hi "
string2 = "Mr.Mortensen"
print(string1 + string2)
Hi Mr.Mortensen
string = "Hi Mr. Mortensen"
print(string[3:5])
Mr
string1 = "degree"
string2 = " passenger"
FinalString = string1 + string2
print(FinalString[2:9])
print(len(FinalString))
print(len(FinalString[2:9]))
gree pa
16
7

How this helps me

These topics are very common in everyday code and in collegeboard. They wil appear on the AP test, either for me to interpret for for me to correct or write. This lesson has helped me to:

  • recognize when a variable is assigned
  • recognize when other data types (such as a boolean, integer, string) are present
  • understand what each data type does and which is best for a situation < showed up on the collegebaord MC
  • how to assign a variable
  • how to create/interpret a list

  • Data Abstraction

    • I rememebr first seeing data abstraciton on the college board requirements and not understanding what it meant. I feel that this lesson has helped me to understand and hopefully implement Data Abstraction in my future projects (including AP test.)
  • Managing Complexity

    • I used to approach code the very inefficient way, where you type everything out multiple times and take forever to accomplish something. Now, I recognize ways to shorten the preocess, and make it easier to keep track of the algorithm. This lesson has helped me to recognize those opportunities.
  • Sequencing, Selection, and Iteration

    • To be completely honest, I had no idea how to approach algorithms and try to describe them with these words before this lesson. That was pretty bad, especially since I need to create a project that implements all of these aspects for the AP test. Now, I can more confidently describe each of these words and how an algorithm uses them, which will be extremely helpful for the AP test.
  • Mathematical Expressions

    • I remember struggling with the mathematical expressions on our collegebaord MC. I had to research what MOD did. Despite understanding enough to answer the questions, I still didn't have a solid understanding. After this lesson, I feel more more confident in the operator and what it does. I think I will remember it much better now as well, which will really come in handy when it shows up on the AP test third trimester.
  • Evaluating Expression that Minipulates Strings

    • One thing that definintely confused me on the collegeboard MC was manipulating strings. There was a specific question asking how you could result in a certian string, by using these munipulations on two previous strings. After researching, I understood what concat did, and how I could use it. However, my skills and understanding were still very shaky. Now, I feel more confident in my ability to both recognize when these manipulations are used, and to be able to write them. Once again, this will be extremely helpful on the AP exam.