Variables and Assignment Python/JS

  • a variable represents a value
  • Can be: strings, lists, booleans, numbers
  • variables must be assigned the above

Defining Variables in Python

  • Type the variable name (no spaces) and use = to assign a value to it

Mathematical Expressions

  • variable defined using a numerical value
  • that variable can be used in expressions in the place of a regular number

Functions on Lists

  • append function adds elements to list

Dictionaries

  • Dictionary can be defined using curly brackets

Interchanging Variables

  • you can interchange variables by setting them equal to each other with a temporary variable

Floats

  • used in the same was as int types
  • are decimals

JS Variables

  • one way to assign a variable is using var var x = 2;
  • console.log(x) to print variable
  • const q = 5 sets q as a constant
  • Boolean(w > x) prints true or false
  • const groupnames = [ string, string, string] to make a list

Unit 3.2 Data Abstraction Python/JS

  • manage complexity in programs by giving a collection of data a name without refrencing the specific elements of the representation, makes it easier to implement, develop and maintain code, AP exam index start at 1
  • Lists, dictionaries are a type of data abstraction
  • spilt() : splits string into a list and join(): joins list into a string
  • Js: declare function using function function_name(parameters)
  • can input and store data then display it

Challenge

data = [104, 101, 4, 105, 308, 103, 5, 107,
        100, 306, 106, 102, 108]    # list of the different numerical values
min_valid = 100  # minimum value
max_valid = 200  # maximum value

for num in data: 
        if num>200 or num<100 : 
                print (str(num) + " " + "index: " + str(data.index(num)))
4 index: 2
308 index: 4
5 index: 6
306 index: 9

HW/ Hacks

The list given contains 4 album names - Welcome to my Nightmare, Bad Company, Nightflight, More Mayhem - and each album contains at least 4 songs within another list. Given this, write a block of code that enables users to input in integer values that correspond to the albums and songs - Welcome to my Nightmare is 1, Bad Company is 2, etc. - Then, a sentence is outputted that says Playing ___ based on which song was chosen using the numbers inputted by the user that corresponds to each song.

albums = [
    ("Welcome to my Nightmare", "Alice Cooper", 1975,   # First album list
     [
         (1, "Welcome to my Nightmare"),
         (2, "Devil's Food"),
         (3, "The Black Widow"),
         (4, "Some Folks"),
         (5, "Only Women Bleed"),
     ]
     ),
    ("Bad Company", "Bad Company", 1974,   # Second album list
     [
         (1, "Can't Get Enough"),
         (2, "Rock Steady"),
         (3, "Ready for Love"),
         (4, "Don't Let Me Down"),
         (5, "Bad Company"),
         (6, "The Way I Choose"),
         (7, "Movin' On"),
         (8, "Seagull"),
     ]
     ),
    ("Nightflight", "Budgie", 1981,
     [
         (1, "I Turned to Stone"),
         (2, "Keeping a Rendezvous"),
         (3, "Reaper of the Glory"),
         (4, "She Used Me Up"),
     ]
     ),
    ("More Mayhem", "Imelda May", 2011,
     [
         (1, "Pulling the Rug"),
         (2, "Psycho"),
         (3, "Mayhem"),
         (4, "Kentish Town Waltz"),
     ]
     ),
]

albumid = int(input("What is your preferred album 1-4?"))
songid = int(input("What is your preferred song 1-?"))
       
print("Playing: " + str(albums[albumid-1][3][songid-1][1]) + " from Album: " + str(albums[albumid-1][0]) + " by Artist: " + str(albums[albumid-1][1]))
Playing: She Used Me Up from Album: Nightflight by Artist: Budgie