3.1: Variables and Assignments

What is a variable?

  • A variable is an abstracttion inside a program that can hold a value.

  • It is important to know that using meaningful variable names helps with teh readability of program code and the understanding of what values are being represented by the variables

Examples of Variables:

alphabet = "abcdefghijklmnopqrstuvwxyz"

alphabetList = []

for i in alphabet:
    alphabetList.append(i)

print(alphabetList)

For the first code block the variable is "alphabet" because it is being assigned to a string that contains the alphabet (and also has a meaningful name whichc makes it easy for the user to know exactly what that variable means). And in the second code block the variable is "n" because it is being assigned to an integer.

What is an assignment? What does it mean to assign something?

  • An assignment is essentially setting a value or giving a value to a variable.

Consider the code segment below:

num1 = 25
num2 = 15
num3 = 55
num2 = num3
num3 = num1
num1 = num2

print(num1)
print(num2)
print(num3)

3.2: Data Abstraction

What is Data Abstraction?

  • Data abstractions manage complexity in programs by giving a collection of data a name without referencing the specific details of the representation.

  • Some examples of Data Abstraction are lists and strings

Strings

  • What are strings?
    • Strings are an ordered sequence of characters that may contain letters, numbers, and all other special characters

Some examples of strings:

  • Words
  • Phrases
  • Sentences
  • ID numbers

  • Why do you think it wouldn't make sense to consider a phone number, for example, as an integer instead of a string?

    • Because usually integers are something we can add and subtract and average, whereas it would make sense to average a phone number because don't really consider them as just a bunch of numbers.

Lists:

  • What are Lists?
    • Lists are an ordered sequence of elements, where each element is a variable

Some examples of lists:

  • Playlist of songs
  • names of students in a class
  • contacts on your phone

  • Each element of a string is referenced by an index (which is a number) and they generally start 0 but for the AP Exam it starts at 1.

    • AP Exam: 1,2,3,4 etc.
    • Python: 0,1,2,3 etc.

How do lists Manage Complexity of a program?

  • We may not need as many variables. For example:
    • One Variable that holds all students would be better than having a variable for EACH student
    • There can also be a list of test scores so if they need to be curved then the same calculation can be applied to the list (that has all the students) instead of doing the calculations one at a time
# variable of type string
name = "Sri Kotturi"
print("name", name, type(name))

# variable of type integer
age = 16
print("age", age, type(age))

# variable of type float
score = 90.0
print("score", score, type(score))

print()

# variable of type list (many values in one variable)
langs = ["Python", "JavaScript", "Java", "Bash", "html"]
print("langs", langs, type(langs))
print("- langs[2]", langs[2], type(langs[2]))

print()

# variable of type dictionary (a group of keys and values)
person = {
    "name": name,
    "age": age,
    "score": score,
    "langs": langs
}
print("person", person, type(person))
print('- person["name"]', person["name"], type(person["name"]))

3.3: Mathematical Expression

What is an algorithm?

  • it is a finite set of instructions that accomplishes a specific task

Sequencing

  • means that there is an order in which to do things

Selection

  • Helps to choose two different outcomes based off of a decision that the programmer wants to make

Iteration

  • Repeat something until the condition is met. (also refered to as repetition)

What is the output of the cell below? What Mathematical Expressions do you see being used?

grade1 = 10
grade2 =  grade1 

average_grade = (grade1 + grade2)/ 2

print(average_grade)
  • The problem above is being done in a specific order.

What is the value of num1, num2, and num3? Explain the result for at least one of the values?

num1 = 2
num2 = 4
num3 = 6
num1 = num2 + num3
num3 = num1 + 5
num2 = (num1 + num3)/5

print(num1)
print(num2)
print(num3)

Arithmetic Operators:

  • Addition:

    • a + b Ex. grade + 10
  • Subtraction:

    • a - b Ex. 100 - pointsDeducted
  • Multiplication

    • a b Ex. base height
  • Division:

    • a / b Ex. sum / 28
  • Modulus:

    • a MOD b Ex. 17 MOD 2
    • (Used with a % and it is the remainder after division)
  • Just like in math, order of operations apply here :)

3.4: Strings

What is a string?

  • ordered sequence of characters

Some Examples of string procedures:

  • len(str)

    • which gives the length of the string (how many letters there are)

    • len("happy") returns 5

  • concat(str1, str2)

    • which combines str1 and str2 into one word

    • concat("key", "board") returns "keyboard"

  • substring(str1, start, length)

    • starts with a letter within a string and then the length starts with the starting letter and prints each consecutive letter afterwards for however long the length is

    • substring("APCSPrinciples", 3, 6) returns "CSPrin"

3.5: Boolean Expressions

  • A Boolean Value is either true or false.

Relational Operators:

  • Equal to:

    • a = b Ex. num_students = 30
  • Not Equal to:

    • a ≠ b Ex. count ≠ 10
  • Greater Than:

    • a > b Ex. grade > 70
  • Less Than:

    • a < b Ex. high_score < current_score
  • Greater Than or Equal to:

    • a ≥ b Ex. num_pets ≥ 0
  • Less Than or Equal to:

    • a ≤ b Ex. tacos ≤ 3
  • Write a Boolean expression to determine if num1 odd:

    • num1 MOD 2 = 1
  • Write a Boolean expression to check if the temperature outside is less than 90 degrees.

    • temp < 90

These examples use the relational operators above to display boolean expressions.

NOT:

isRaining = False

result = NOT(isRaining)

isRaining currently is false but when you use the NOT operator, the value outputted is true.

AND:

grade = 85

result = grade > 70 AND grade ≤ 100

if the grade is above 70 and grade is less than or equal to 100 aare both true, then the entire expression is true. Since 85 is greater than 70 that part of that condition is true, and since 85 is less than 100 that part is true so since both of those values are true, the overall value will output TRUE.

grade = 45

result = grade > 70 AND grade ≤ 100

For this one, 45 is less than 100 so that condition is true, but 45 is not greater than 70 so that condition is false. since both conditions are not true, the output of this expression is FALSE.

BOTH CONDITIONS HAVE TO BE TRUE IN ORDER TO OUTPUT TRUE. IF ONE OR BOTH OR FALSE, THEN THE OUTPUT IS FALSE

OR:

score = 175

high_score = 150

lives = 2

result = score > high_score OR lives = 0

Since one of these values equals true, the value that is outputted by this expression is true because it either the first value OR the second value that has to be true.

score = 100

high_score = 150

lives = 1

result = score > high_score OR lives = 0

Since both expressions are false, the entire expression ends up being false.

3%2
1