When ya code on paper and can't look at Stack Overflow... [PYTHON EDITION]

¡Supera tus tareas y exámenes ahora con Quizwiz!

Suppose we want to change every pixel in an image. What is the minimum number of for loops we would need to access every pixel item?

2 (for each row, access every column)

The maximum intensity of a color is represented by what number?

255

How many different colors can you represent with RGB?

256 * 256 * 256

A digital image is formed with...

pixels

How is black represented in RGB?

0, 0, 0

How many elements are in this tuple? myTuple = (34, )

1

can you use .find() with lists?

NO WAY! use .index()

can you use .append() with sets?

NO!!! use .add()

T/F: a list comp is always possible as a loop.

T

How does tuple assignment work?

The left side is a tuple of variables; the right side is a tuple of values. The values are assigned to the vars on the left. It's like doing (e.g.) 3 assignment statements at once. (a, b, c) = (1, 2, 3)

What is the output of the following code? sounds = {cat: meow, dog:bark, pig:oink} noises = sounds noises["pig"] = grunt print (noises == sounds), (noises is sounds)

True True changing noises changes sounds. one dict IS the other because of the nature of the assignment statement!! so changing one changes the other. dicts are mutable

Consider the following code. What is output? sounds = ["Fa", "La", "Ti"] sounds2 = [sounds] * 2 sounds[1] = "So" print sounds2

[ ["Fa", "So", "Ti"], ["Fa", "So", "Ti"] ] because you put brackets around sounds in the assignment to sounds2, val of sounds2 will change when sounds is updated. you have made a list of lists, both those lists are the original sounds, lists are mutable

Consider the following code. What is output? sounds = ["Fa", "La", "Ti"] sounds2 = sounds * 2 sounds[1] = "So" print sounds2

["Fa", "La", "Ti", "Fa", "La", "Ti"] just one big NEW list made from concatenating 2 old lists, so changing val of the original sounds doesn't matter

You have a dictionary d. You see the line "print sorted(d)." "Wat? You can't sort a dictionary!" you think. But then you realize that, rather than sorting the whole dictionary, sorted(d) gives you _______, and it's all good.

a list of all the keys, sorted (different than d.keys(), which gives a list of keys but doesn't sort the keys because GOTTA GO FAST GOTTA GO FAST GOTTA GO FASTER FASTER FASTERFASTERFASTER)

d.values() gives you... (can there be repeats?)

a list of values that correspond to every single key in the dict. yes, there can be repeats, since multiple keys can have the same value

Sets are really, really efficient for ________

add/search

lst = ["a", "b", "c"] x = lst.pop() print x print lst what does this code print?

c ['a', 'b']

You have a file already opened and ready for reading. [f = open(fname)] You want to make a list of every line in the file. WHAT DO YOU DO???

f = open(fname) answer = [] for line in f: answer.append(line.strip()) return answer

You have a file already opened and ready for reading. [f = open(fname)] You want a string consisting of every word in the file which could potentially be manipulated in various ways later. WHAT DO YOU DO???

f = open(fname) st = f.read() f.close() return st

You have a file already opened in writing mode. [f = open(fname), "w"] You want to write every element in words, a list of strings, to the file whose name is fname. WHAT DO YOU DO???

f = open(fname,"w") for word in words: f.write(word) f.close()

You're building a counting dictionary for a list of strings "data" (all the values are the number of times the keys occur). How do you make this happen?

freqdict = {} for item in data: if item not in freqdict: freqdict[item] = 1 else: freqdict[item] += 1

Are tuples mutable? that is, do they support object assignment? Can you say (e.g) myTuple[0] = 'X'?

hell no my dude

You're building a grouping dictionary for a list "musicdata", where each item in the list is in the format "artist:song". You want to make a dict where each artist is mapped to all their different songs. How do you make this happen?

mydict = {} for item in musicdata: itemlist = item.split(":") artist = itemlist[0] song = itemlist[1] if artist not in mydict: mydict[artist] = [song] else: mydict[artist].append(song)

lst = ["a", "b", "c"] lst.pop() does this code have any output?

nope!

lst = [1, 2, 3] lst.pop(3) does this code work?

nope. it's going to think you meant to pop the thing at index 3, not the 3 itself, and there is no index 3.

What is the output of the following code? sounds = {cat: meow, dog:bark, pig:oink} noises = sounds.copy() print sounds["pig"], noises["pig] noises["pig"] = grunt print sounds["pig"], noises["pig"]

oink oink oink grunt .copy() only creates a SHALLOW COPY, so changing one does not change the other

What is the output of the following code? sounds = {cat: meow, dog:bark, pig:oink} noises = sounds print sounds["pig"], noises["pig] noises["pig"] = grunt print sounds["pig"], noises["pig"]

oink oink grunt grunt AGAIN: changing noises changes sounds. one dict IS the other because of the nature of the assignment statement!! so changing one changes the other. dicts are mutable

symmetric difference of sets s and t is... (in code and english)

s ^ t (everything in either s or t, but not in both)

set difference of sets s and t is... (in code and english)

set with all elements in s that are not in t s - t

subset test in simple math symbols

set1 <= set2

First things first: how do you initialize a blank set???

set1 = set() (why can't I remember this...)

superset test in simple math symbols

set1 >= set2

How do you create a shallow copy of a set? Can this function be assigned to a var?

set1.copy(), yes

How do you remove an elt from a set without any chance of an error being thrown?

set1.discard(elt) (if no chance of error, use .remove())

What are subsets and supersets? How can you test if set1 is a subset or superset of set2?

subset = it is within the other set superset = it contains the other set s1.issubset(s2) s1.issuperset(s2)

Can you concatenate slices or indices of an old tuple with another tuple to create a BRAND NEW GIANT TUPLE? That is, can you be like... julia = ("Julia", "Roberts", 1967, "Duplicity", 2009, "Actress", "Atlanta, Georgia") julia = julia[:3] + ("Eat Pray Love", 2010) + julia[5:] ?

that is totally kosher. congratulations

You're reading the reference sheet and you see, when it talks about a built in function (like our friend .pop()), that it says "has side-effect of altering list and returns value". What does this mean? Does the function print a value on its own?

the function simultaneously alters the list, and while it doesn't automatically print a value, it is possible to assign the function to a variable without crashing the program. some functions, like .append(), cannot be assigned to variables because they don't return a value

what does enumerate do?

use in a for loop (it's an iterator I think), gives you (index,item) tuples for each item in a list for (ind,item) in enumerate(lst): (you can't say "print enumerate(lst)". but you CAN say this: for (ind,item) in enumerate(lst): print (ind, item). If lst = ["a", "b", "c"] then this would print (0, 'a') (1, 'b') (2, 'c') each on a new line )

You create a list and then want to reverse it. What are 2 ways you could do this? What is recommended?

way 1: put reverse = True with a comma after creating the list, for example: sortedByFriends = sorted([(len(v), k) for k, v in d.iteritems()], reverse = True) way 2: use .reverse(), but remember to return something for example: sortedByFriends = sorted([(len(v), k) for k, v in d.iteritems()]) sortedByFriends.reverse() return [person for numFriends, person in sortedByFriends] way 1 is recommended but way 2 makes more sense to me so IDK BRUH

can you assign .pop() to a variable?

yep!

you assign lst.pop() to a variable and that's all you do with .pop() in your program. Is lst changed?

yes

Can you have a tuple as a return value in a function?

yes you can! just make sure every var in the tuple being returned has a value. for example: c = 2 * 3.14159 * r a = 3.14159 * r * r return (c, a)

You can use .reverse() to reverse a list, but can it be assigned to a var (like can you say x = lst.reverse())? What must you remember to do?

you can't assign .reverse() to a var remember to return the final reversed list somehow because just saying .reverse() reverses the original list but doesn't return anything

how do you make .pop() have output?

you gotta say print lst.pop(), which prints the last thing in the list, or assign to a var and then print the var


Conjuntos de estudio relacionados

Econ Macro principles final practice

View Set

AP English Midterms (LOTF and Rhetorical Devices)

View Set

DATABASES: IS 3100 (chapter four)

View Set

¿Quiéne es?/¿Quienes son? ¿De dónde es?/¿De dónde son?

View Set

Deleon Chemistry- Chapter 7 test answers

View Set

Cognitive-Behavioral Family Therapy

View Set

Unit 5 Review: Industrialization & Global integration 1750-1900

View Set