Variables, Assignments, Data Abstraction

A variable is an abstraction that can hold inside a program and hold the value. Changex Contains three parts:

  • Name
  • Value
  • Type

Name variables are short and not as specific. Do not make your code for variables too complicated because it can become messy. However, do not make your variables too vague.

Types of data:

  • Integer
  • Text/string
  • Boolean

Strings consist of a key word, the second variables would be considered as integers as it would display a number. The third variable would be a boolean because it shows if anything is true or false.

A list of data can also be stored in variables. This is useful as it can print/retrieve specific values n the list without creating lots of variables.

Assignments

Operators that allow the program to change the value represented by a variable.

Some operators:

  • = (assigns both right and left) (a=b)
  • += (a+b=ab)
  • -= (a-b=a-b)
  • and so on

The value stored in a variable will be the most recent value assigned.

Changing Values: Change any value, but the most recent value will be the output.

Data Abstraction

Data abstraction is a method used in coding to represent data in useful forms in applicable scenarios.

Data abstraction has tools likes variables and lists which are very important.

Lists and Strings

A list is an ordered sequence of elements, an element is a specific value in that list to a unique index. An index is a reference to the elements in the string.

A string is a ordered sequence of characters.

Example of a list: EXAMPLE - WE USE COLORS:

colorsList=["pink", "yellow", "green", "blue, "orange", "purple"]

print(colorsList)

^ Now it will print the colors and the list you defined along with the specific colors, which are the elements.

NOTE: Index always starts at ONE on AP Exam

Managing Complexity

  • Makes code more clear
  • Updates data easily

There is only one other way for interchanging data between lists, and that is to completely overwrite previous data in the list.

Lesson Activities

colorsList=["pink", "yellow", "green", "blue", "orange"]

print(colorsList)
['pink', 'yellow', 'green', 'blue', 'orange']
team1="USA"
team2="England"
team3="France"
team4="Italy"
team5="Portugal"
team6="Argentina"
team7 = "Germany"
team8 = "Japan"

print(team1)
print(team2)
print(team3)
print(team4)
print(team5)
print(team6)
print(team7)
print(team8)
USA
England
France
Italy
Portugal
Argentina
Germany
Japan
colorList=["green", "red", "pink", "purple", "blue", "brown"]

print(str(colorList))
['green', 'red', 'pink', 'purple', 'blue', 'brown']

HOMEWORK

import getpass, sys

questions = 7
correct = 0   

print('Hello, ' + getpass.getuser() + '!')
print("\t", "You will be asked " + str(questions) + " questions on cars.")

def question_and_answer(prompt, answer):
    print("Question: " + prompt) 
    rsp = input() 
    
    if rsp == answer :
        print("\t", rsp + " is correct!")
        global correct
        correct += 1
    else:
        print ("\t", rsp + " is incorrect!")
    return rsp

Question_1 = question_and_answer("Whats the most expensive car in the world?", "Mercedes Benz 300 SLR - $142,000,000")
Question_2 = question_and_answer("Which company owns Rolls Royce?", "BMW")
Question_3 = question_and_answer("What is the biggest electric car company?", "Tesla")
Question_4 = question_and_answer("What company owns Jaguar?", "TATA Motors")
Question_5 = question_and_answer("Whose the best F1 Racer?", "Max Verstappen")
Question_6 = question_and_answer("Who is the F1 G.O.A.T?", "Lewis Hamilton")
Question_7 = question_and_answer("What company owns Corvette?", "GM")

print(getpass.getuser() + " you scored " + str(correct) +"/" + str(questions) + "!!")

Quiz = []

Quiz.append({
    "Q_1": Question_1,
    "Q_2": Question_2,
    "Q_3": Question_3,
    "Q_4": Question_4,
    "Q_5": Question_5,
    "Q_6": Question_6,
    "Q_7": Question_7

})

#List Implementation 
def print_data(d_rec):
    print("Question 1:", d_rec["Q_1"]) 
    print("Question 2:", d_rec["Q_2"]) 
    print("Question 3:", d_rec["Q_3"])
    print("Question 4:", d_rec["Q_4"])
    print("Question 5:", d_rec["Q_5"], end="")  
    print()

print("Here is a record of your quiz:")
def for_loop():
    print("For loop output\n")
    for record in Quiz:
        print_data(record)

for_loop()
Hello, akshat1228!
	 You will be asked 7 questions on cars.
Question: Whats the most expensive car in the world?
	 Mercedes Benz 300 SLR - $142,000,000 is correct!
Question: Which company owns Rolls Royce?
	 BMW is correct!
Question: What is the biggest electric car company?
	 Tesla is correct!
Question: What company owns Jaguar?
	 TATA Motors is correct!
Question: Whose the best F1 Racer?
	 Max Verstappen is correct!
Question: Who is the F1 G.O.A.T?
	 Lewis Hamilton is correct!
Question: What company owns Corvette?
	 GM is correct!
akshat1228 you scored 7/7!!
Here is a record of your quiz:
For loop output

Question 1: Mercedes Benz 300 SLR - $142,000,000
Question 2: BMW
Question 3: Tesla
Question 4: TATA Motors
Question 5: Max Verstappen