1. Home
  2. Explore
  3. Computer Science
  4. Object-Oriented Programming in Python

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Object-Oriented Programming in Python

Transcript

[Music] all, right, this, is, cs50's, Introduction, to programming, with, python, my, name, is, David men, and, this, is, our, week, on, object oriented, programming, or, o, it, turns, out that, in, the, world, of, programming, there's different, paradigms, of, programming languages, there's, different, ways, of solving, problems, with, code, and, it's, a little, hard, to, see, this, at, first, if you've, only, learned, one, language, but over, time, if, and, when, you, learn, other

languages, besides, python, you'll, start, to notice, certain, patterns, and, certain capabilities, of, some, languages, but, not another, thus, far, within, the, world, of python, you, and, I, have, largely, been writing, code, that's, procedural, in, nature whereby, we're, writing, procedures, we're writing, functions, and, we're, sort, of doing, things, top, to, bottom, everything, is step, by, step, by, step, as, you, would, expect in, general, from, an, algorithm, but, along the, way, we've, actually, dabbled, in another, Paradigm, known, as, functional programming, with, python, whereby, we've been, able, to, pass, functions, around, we even, had, an, anonymous, function, some weeks, ago, and, that's, evidence, of features, of, a, functional, programming language, even, though, we've, just scratched, the, surface, thereof, today, we focus, on, another, Paradigm, and, this, one in, more, detail, namely, object-oriented programming, and, now, while, some, of, you

might, have, prior, programming, experience and, have, learned, languages, like, Java which, are, by, Design, fundamentally objectoriented, python, indeed, allows, you a, bit, of, flexibility, when, it, comes, to how, you, solve, problems, with, code, but, it turns, out, oop, object-oriented programming, is, a, pretty, compelling solution, to, problems, that, you, invariably encounter, as, your, programs, get, longer larger, and, more, complicated, so, indeed oop, for, our, purposes, is, going, to, be, a solution, to, a, problem, that, builds, on, so many, of, the, lessons, passed, so, let's, go ahead, and, do, this, let's, start, by, writing a, program, very, procedurally, by, opening up, vs, code, here, I'm, going, to, go, ahead and, create, a, program, called, student., py and, in, this, program, I, want, to, do something, relatively, simple, initially, uh as, we, might, have, done, some, weeks, ago, now

where, I, just, ask, a, user, for, their, name and, maybe, in, the, context, of, the, Harry Potter, Universe, their, house, and, just print, out, where, that, student, is, from, and let's, gradually, enhance, this, program, by adding, more, and, more, features, to, it, and see, if, we, don't, stumble, upon, problems that, up, until, now, we, might, not, have, had very, elegant, well-designed, solutions, to but, if, we, introduce, explicitly object-oriented, programming, as, a programming, technique, I, bet, we, can, clean up, our, code, and, set, the, stage, for writing, even, more, sophisticated, programs longer, programs, down, the, line, so, in student., piy, let, me, go, ahead, and, do, a name, variable, setting, it, equal, to, the return, value, of, input, and, just, prompt the, user, for, their, name, like, this, and then, let, me, go, ahead, and, do, the, same, for a, house, variable, and, and, prompt, the, user for, their, house, using, input, like, this

and, let's, do, something, super, simple, now let's, just, go, ahead, and, print, out, an, F string, that, says, something, like, name from, house, just, so, that, I, can, confirm that, the, contents, of, these, variables, are indeed, as, I, expect, I'm, not, going, to, do any, error, checking, or, trimming, or anything, like, that, for, now, I'm, really just, going, to, spit, back, out, whatever, the user, just, typed, in, all, right, let, me, go ahead, and, run, python, of, student, doy let's, use, our, go-to, like, Harry, as, in Harry, Potter, from, Gryffindor, and, when, I hit, ENT, now, let's, see, if, I, see, that Harry, from, Gryffindor, is, indeed, the, case all, right, so, I, think, we, have, a, working program, at, this, point, but, let's, now introduce, some, of, those, Lessons, Learned way, back, from, week, zero, where, we, started writing, our, own, functions, not necessarily, because, it, solves, the problem, more, correctly, I, dare, say, this

is, correct, as, is, but, it, begins, to, give us, building, blocks, that, we, can, extend, so as, to, solve, more, complicated, programs, so let, me, go, back, up, to, student., and, let's go, ahead, now, and, do, this, let's, put, the entire, log, IC, I, just, wrote, inside, of, our typical, method, called, Main, and, let, me indent, those, three, lines, so, that, at least, they're, now, combined, into, one, main method, but, instead, of, using, input, on line, two, and, input, on, line, three, why don't, we, go, ahead, and, assume, for, the moment, that, we've, got, some, function called, get, name, in, the, world, and, let's go, ahead, and, assume, we've, got, another function, like, get, house, in, the, world that, don't, take, parameters, but, their purpose, in, life, is, by, their, named, going to, be, to, get, the, user's, name, and, to, get their, user's, house, respectively, and, then I'm, going, to, print, out, the, exact, same, F

string, as, before, I, of, course, need, to implement, these, functions, now, so, let, me go, lower, in, my, file, and, Define, a function, called, getor, name, nothing, in these, parentheses, because, it's, not, going to, take, a, parameter, and, I'm, going, to, go ahead, and, do, something, like, name, equals input, quote, unquote, name, just, like before, and, then, I'm, going, to, go, ahead and, return, name, so, it's, a, super, simple function, but, it's, a, an, abstraction, I, now have, a, function, called, get, name, whose implementation, details, I, don't, have, to care, about, anymore, I, just, know, that, the function, exists, and, I, can, tighten, this up, in, in, fact, I, don't, really, need, a, name variable, on, line, 8, if, I'm, immediately going, to, return, that, same, name, variable on, line, 9, so, let, me, just, tighten, this, up a, little, bit, even, though, it, doesn't change, the, functionality, and, just immediately, return, the, return, value, of

the, inputs, uh, function, call, here, let's do, something, very, similar, now, for, get house, which, will, similarly, take, no arguments, I'm, going, to, go, ahead, and return, the, return, value, of, input, this time, prompting, the, user, for, their, house and, I, need, one, final, detail, at, the, very bottom, let's, continue, our, habit, of, doing if, the, name, of, this, file, equals, equals quote, unquote, main, then, let's, go, ahead and, actually, call, Main, and, recall, that we, have, that, in, place, so, that, if, this eventually, becomes, part, of, a, module, a library, of, sorts, I, don't, accidentally call, Main, blindly, I, only, do, it, if, I, mean to, run, main, from, the, command, line, on this, file, all, right, so, if, I, didn't, make any, mistakes, here, let, me, go, ahead, and, in my, terminal, window, again, run, python, of student., piy, enter, let's, type, in, Harry enter, let's, type, in, Gryffindor, enter, and

we're, set, Harry, from, Gryffindor, seems, to still, be, working, so, we, haven't, really solved, the, problem, any, more, correctly but, I've, laid, the, foundation, to, maybe now, do, some, more, interesting, things because, I've, have, these, building, blocks in, place, all, right, but, let, me, propose that, we, could, be, doing, this, a, little, bit differently, get, name, get, house, is, fine but, at, the, end, of, the, day, I'm, really trying, to, get, a, student, from, the, user, I want, their, name, and, their, house, not, just one, or, the, other so, maybe, it, would, be, a, little, cleaner still, to, define, a, function, called, get student, and, let, get, student, do, all, of this, work, for, us, now, theoretically, get student, could, call, get, name, and, could call, get, house, but, because, these functions, are, so, short, I, think, I'm, okay with, just, defining, one, function, called get, student, that, similarly, won't, take

any, arguments, but, it's, going, to, do, two things, it's, going, to, get, the, student's name, by, prompting, them, with, input, as before, and, it's, going, to, get, the students, house, by, also, prompting, them, as before and, then, now, hm, I, want, to, return, the student, but, I, think, I, might, have, painted myself, into, a, corner, here, because, I, now have, two, variables, name, and, house, and yet, up, until, now, we've, pretty, much returned, one, or, the, other, we've, returned one, value, so, any, suggestions, for, how, we can, perhaps, solve, this, problem, that, I just, created, for, myself, whereby, I, want to, return, really, a, student, but, I currently, have, a, name, variable, and, a house, variable, I'd, minimally, like, to return, both, both, of, those, I, believe, that we, can, return, a, dictionary, includes, the name, and, the, house, yeah, so, we, absolutely could, return, a, dictionary, a, dict, object in, Python, whereby, maybe, one, key, is, name

one, key, is, house, and, the, values, thereof are, exactly, the, values, of, these variables, we, could, totally, do, that, uh, I worry, that, that, might, be, getting, a little, complicated, I, wonder, if, there's, a simpler, way, instead, any, other, instincts even, if, you're, not, sure, it, would, work return, both, name, and, house, return, both name, and, house, I, I, like, the, sound, of that, it, sounds, simple, I, don't, have, to figure, out, what, a, dictionary, is, going, to look, like, and, in, fact, this, too, would, be a, valid, approach, even, if, you've, not, seen this, before, it, turns, out, in, Python, that you, can, kind, of, return, multiple, values but, that's, a, bit, of, a, white, lie, or, we could, take, Muhammad's, approach, of actually, returning, a, dictionary, and putting, multiple, Keys, therein, so, here again, we, have, yet, another, example, of, how you, can, solve, the, same, problem, in, at least, two, ways, and, I, dare, say, we're

about, to, see, even, more, so, one, way, you could, solve, this, problem, whereby, you want, to, return, multiple, values, would, be to, do, something, like, this, I, could, go ahead, and, literally, return, not, just, name but, I, could, put, a, comma, and, also, return house, this, is, not, necessarily, something you, can, do, in, other, languages, if, you have, programmed, in, other, languages before, it, depends, on, the, language, but, it looks, like, thanks, to, this, comma, maybe, I can, in, fact, return, two, values, as actually, proposed, well, if, I'm, returning two, values, in, this, way, on, line, 10, how, do I, get, both, values, at, the, same, time, well there's, a, couple, of, ways, let, me, go, up, to my, main, function, I, know, minimally, I'm going, to, have, to, change, the, get, name, and get, house, to, get, student, but, what, am, I going, to, store, the, return, value, in, I think, I, could, actually, do, this, and, we

have, seen, this, technique, before, where you, can, unpack, so, to, speak, sequences, of values, that, are, coming, back, and, indeed consider, this, to, be, exactly, that, name comma, house, is, some, kind, of, sequence that, I'm, returning, of, values, name, comma house, so, if, I, want, to, unpack, those, and store, the, return, values, in, two, separate variables, I, can, in, fact, use, the, commas on, the, left, hand, side, of, my, assignment operator, the, equal, sign, to, do, just, that now, to, be, clear, I, don't, need, to, call these, variables, name, and, house, here, I could, simplify, this, and, use, just, n, here and, H, here, and, then, I, could, return, just n, and, H, but, I, would, argue, that's, not very, uh, clear, to, the, reader, as, to, what's going, on, so, I, think, in, this, case, even though, it's, a, coincidence, that, I've, used the, same, variable, names, in, get, student and, get, Main, and, in, main, it's, a, little

more, readable, to, someone, like, me, so, I'm going, to, leave, it, as, is, well, let's, go ahead, and, see, now, if, this, works, let, me clear, my, screen, down, here, and, run, python of, student., enter, let's, again, type, in Harry, let's, again, type, in, Gryffindor enter, and, voila, we, still, see, that, Harry is, from, Gryffindor, but, what, are, we actually, doing, here, what, are, we, actually doing, by, returning, this, value, well, it turns, out, that, what, we've, just, done, is used, a, tupple, a, tupple, is, another, type of, data, in, Python, that's, a, collection, of values, X, comma, y, or, X, comma, y, comma, Z it's, similar, in, spirit, to, a, list, in, that sense, but, it's, immutable, it's, not mutable, now, what, does, that, mean, a, list as, we've, seen, it, before, is, a, data structure, in, Python, that, you, can, change the, values, of, you, can, go, into, bracket zero, for, the, first, location, and, change the, value, there, you, can, go, to, bracket

one, bracket, two, bracket, three, and actually, change, the, values, in, lists, but if, you, have, no, intention, of, changing, the values, of, variables, and, you, want, to return, effectively, multiple, values, you don't, have, to, even, return, it, as, as, a list, you, can, return, it, as, a, tupple instead, just, by, using, a, comma, and, it turns, out, we, can, make, explicit, that here's, the, white, lie, I'm, not, actually returning, two, values, per, se, whenever, you use, a, comma, in, this, way, on, line, nine you're, actually, returning, one, value which, is, a, tupple, inside, of, that, tupple now, are, two, values, so, it's, similar, in spirit, to, returning, one, list, with, two things, here, I'm, returning, one, Tuple, with two, things, and, the, mere, fact, that, I've used, a, comma, and, nothing, else, tells python, that, I, indeed, want, to, return, a tuple, but, there's, more, explicit, syntax that, we, can, use, instead, I, can, actually

more, verbosely, put, explicit, parentheses around, the, values, of, this, tupple, just, to make, more, clear, to, me, to, the, reader, that this, isn't, two, values, per, se, this, is, one value, with, two, things, inside, of, it, and what, I, can, actually, do, then, too, is, I don't, have, to, unpack, this, up, here, so, to speak, I, can, actually, go, up, here, and maybe, give, a, more, apt, name, like, student and, I, can, name, the, value, or, rather, name the, variable, in, which, I'm, storing, the return, value, of, get, student, as, quote unquote, student, so, maybe, this, is, a little, better, design, now, because, I'm sort, of, abstracting, away, what, a, student is, it's, implemented, at, the, moment, as, a tupple, with, two, values, but, at, least, now I, have, a, variable, called, what, I, mean, a student, but, there's, going, to, be, a, catch on, line, three, I, still, want, to, print, out that, Student's, name, and, their, house, but

I, don't, have, a, name, variable, anymore, and I, don't, have, a, house, and, I, also, don't have, a, dictionary, as, was, proposed earlier, so, I, can't, even, go, at, those, keys by, name, but, what, a, tupple, is, it's, very similar, in, spirit, to, a, list, but, it, is indeed, just, immutable, and, what, I, mean, by that, is, I, can, still, index, into, it numerically, by, saying, student, square bracket, Z, for, the, item, in, the, first location, in, that, tupple, and, then, over here, instead, of, house, I, can, say, student bracket, one, student, bracket, one, is, going to, give, me, the, second, location, in, that tupple, let, me, go, ahead, and, clear, my terminal, window, again, run, python, of student., let's, type, in, Harry, let's, type in, Gryffindor, enter, and, we, still, have some, working, code, let, me, pause, here, now and, see, if, there, are, any, questions, on this, technique, of, returning, a, tupple, and indexing, into, it, in, this, way, I, guess

what's, a, like, a, actual, use, case, where you, would, use, a, tupple, versus, you, know, a list, or, something, else, that's, similar it's, a, really, good, question, when, would you, use, a, tuple, versus, a, list, when, you want, to, program, defensively, or, in general, when, you, know, the, values, in, this variable, shouldn't, change, so, why, would you, use, a, data, type, that, allows, them, to be, changed, it, just, invites, mistakes, bugs down, the, line, either, by, you, or colleagues, who, are, interacting, with, your code, so, tupple, is, just, another, way, where you, can, increase, the, probability, of correctness, by, just, not, letting, anyone yourself, included, change, the, contents therein, so, it's, just, another, tool, in your, toolkit, but, let's, make, clear, then what, I, mean, by, immutable, again, I, claim that, immutable, means, that, you, cannot change, the, value, well, let's, go, ahead, and try, to, do, this, let, me, go, ahead, and, run

this, program, once, more, as, is, python, of student., let, me, go, ahead, and, type, in, for instance, uh, how, about, uh, padma's, name and, I'm, going, to, go, ahead, and, say, that padma's, in, Gryffindor, as, in, the, movies and, we, see, Padma, from, Gryffindor, but technically, I, went, down, this, rabbit, hole and, looking, at, Harry, Potter, more, closely technically, in, the, books, Padma, I, believe was, from, Ravenclaw, so, this, is, actually, a mistake, or, an, inconsistency, between, the movies, and, the, books, let's, see, if, we can't, fix, this, inconsistency, in, our, code so, how, about, we, do, this, if, the, student's name, that's, inputed, equals, Padma, why don't, we, override, whatever, the, uh whatever, the, house, is, and, change, it, to be, properly, Gryffindor, let, me, go, ahead and, do, if, student, now, if, I, want, to, get at, padma's, name, I'm, going, to, have, to, do student, bracket, zero, I, have, to, know, what

location, the, name, is, in, in, this, tupple but, if, that, in, value, equals, equals, Padma let's, go, ahead, with, this, if, statement and, make, a, change, let's, change, the students, F, uh, bracket, one, value, so, the second, value, if, we're, zero, indexing let's, change, it, to, be, another, house, in the, World, of, Harry, Potter, called Ravenclaw, so, I'm, just, fixing, maybe, the user's, input, they, watch, the, movie, so they, type, in, Padma, Gryffindor, but, in, the books, it, was, Padma, from, Ravenclaw, all right, let, me, go, ahead, and, go, down, to, my terminal, window, clear, my, uh, terminal, and do, python, of, student., enter, I'm, going, to do, Harry, as, well, as, Gryffindor, just, to demonstrate, that, that, is, still, working as, intended, Let, Me, Clear, My, screen, again though, and, run, python, of, studenty, on Padma, and, I'll, put, her, too, in, Gryffindor as, in, the, movies, and, hit, enter, and, now, I

just, see, a, big, mess, of, errors, on, the screen, some, kind, of, exception, has, been thrown, and, indeed, a, type, error, has happened, I'm, using, a, data, type, wherein there's, an, error, and, what, is, that, error well, tupple, object, does, not, support, item assignment, it's, a, little, arcany, Express that, is, that's, not, really, very, user friendly, but, if, you, think, about, what those, words, mean, Tuple, object, does, not support, item, assignment, so, assignment, is copying, from, right, to, left, so, somehow that's, invalid, and, here, is, a manifestation, of, the immutability, of, topples, you, cannot change, location, zero, or, one, or, anything inside, that, is, a, feature, that, is, the design, of, a, tupple, so, if, I, want, to override, that, I, think, I'm, going, to, have to, use, a, different, type, of, data, that we've, used, before, namely, a, list, and that's, fine, if, you, want, to, enable yourself, and, colleagues, using, your, code

to, change, the, contents, of, that, container well, we, can, go, ahead, and, return, not, a tupple, using, explicit, parentheses, or, no parentheses, just, the, comma, but, I, can, use square, brackets, and, if, I'm, using, square brackets, on, the, left, and, the, right, this is, indeed, explicitly, a, list, same, idea but, it's, mutable, that, is, to, say, you, can change, the, contents, of, a, list, so, making no, other, changes, just, returning, a, list with, square, brackets, instead, of, a, tuple with, parentheses, or, just, the, comma, let me, go, ahead, now, and, run, python, of studenty, enter, let, me, type, in, Harry, and Gryffindor, again, that's, still, working good, to, see, let, me, run, this, once, more and, type, in, Padma, and, Gryffindor, as, in the, movies, but, no, now, we've, corrected, it to, be, Padma, from, Ravenclaw, as, in, the books instead, any, questions, now, on, tuples versus, lists, or, this, idea, of

immutability, versus mutability, uh, can, be, used, a, nest, cou, in Python, like, a, a, nested, list, absolutely you, can, have, not, only, nested, lists, in Python, where, one, of, the, elements, in, a list, could, be, another, list, so, you, have some, square, brackets, out, here, you, might have, some, other, square, brackets, inside you, can, absolutely, do, the, same, with, a tuple, as, well, there's, no, constraint, on the, types, of, values, you, can, put, in, there we've, not, had, occasion, to, do, that, in this, case, I'm, just, returning, a, simple, uh simple, tupple, with, two, elements, but, yes you, could, absolutely, do, that, too, other questions, on, tupples, versus, lists okay, for, example, um, when, I, see, the square, bracket, um, is, it, mainly, used, for the, list, oh, a, really, good, question, uh sort, of, so, when, you, create, a, value, like a, list, you, use, square, brackets, and, that would, indeed, be, a, visual, indicator, that

this, is, definitely, a, list, uh, if, you instead, see, parentheses, that's, a, visual indicator, when, creating, a, value, that it's, definitely, a, tuple, however, somewhat confusingly, both, lists, and, topples, use square, brackets, when, you, access, the contents, of, them, when, you, index, into them, at, location, zero, or, location, one you, always, use, square, brackets, so, that's the, distinction, there, good, question allow, me, to, propose, now, if, I, may, that, we solve, this, problem, yet, another, way, and let's, see, if, we're, either, making, things better, or, for, worse, than, us, recall, that dictionaries, or, dict, objects, also, exist in, python, and, a, dictionary, is, this collection, of, keys, and, values, and, the upside, in, particular, of, a, dictionary, is that, they, have, better, semantics, right you, don't, just, have, to, assume, that, that a, name, is, always, going, to, be, at, location zero, house, is, always, going, to, be, at

location, one, I, mean, that's, the, kind, of thing, especially, if, you, had, three, four or, more, values, eventually, you, or, someone is, going, to, get, confused, and, forget, what the, order, is, and, you're, going, to, write buggy, code, so, a, dictionary, is, a, little more, powerful, and, that, you, can semantically, associate, Keys, like, little descriptions, with, the, values, those, keys and, those, values, respectively, so, let, me go, ahead, and, do, this, and, we, can, do, this in, a, few, different, ways, but, let, me propose, that, we, focus, on, get, student here and, let's, go, ahead, and, do, this, let, me, go ahead, and, uh, in, delete, the implementation, of, get, student, Asis, let me, create, a, student, variable, and initialize, it, to, an, empty, dictionary, and I, can, do, that, with, just, two, curly, braces here, and, then, let, me, go, ahead, and, set two, keys, inside, of, that, dictionary inside, of, the, student, there, will, be

quote, unquote, a, name, key, and, the, value of, that, is, going, to, be, whatever, the return, value, of, input, is, when, I, prompt the, user, for, their, name, and, then, the house, key, inside, of, that, that, same student, dictionary, is, going, to, be, the return, value, of, whatever, the, user, types in, for, their, house, and, lastly, I'm, going to, go, ahead, and, return, student, so, now, I am, literally, returning, one, thing, still but, this, time, it's, a, dict, rather, than, a tupple, rather, than, a, list, but, there's still, two, things, in, it, technically, four things, if, you, count, the, keys, and, the values, but, there's, two, key, value, pairs now, my, code, up, here, is, going, to, have, to change, a, little, bit, and, let's, simplify this, and, remove, for, instance, now, the, the pad, if, statement, just, to, focus, on, what's changing, at, hand, and, let, me, go, ahead, now and, leave, line, two, alone, I'm, still, going

to, have, a, student, variable, that, gets assigned, the, return, value, of, get, student but, what, I, want, to, do, here, now, is actually, access, the, keys, inside, of, that dictionary, not, by, numeric, index, which was, for, tuples, and, lists, zero, and, one but, by, way, of, the, keys, now, normally, I might, be, in, the, habit, as, I, personally, am of, using, double, quotes, quote, unquote name, inside, of, there, and, quote, unquote house, inside, of, there, but, before, I, even run, this, code, and, show, you, a, mistake, see an, error, on, the, screen, does, anyone, want to, call, out, what, I, have, done, wrong, here this, is, just, an, F, string, I, just, want, to print, out, the, value, of, the, name, key, the value, of, the, house, key, in, this dictionary

but, you're, your, audio, was, a, little garbled, for, us, but, I, think, I, heard double, quotes, and, single, quotes, so, I'm going, to, assume, that, indeed, you've identif, precisely, the, issue, I'm, just going, to, confuse, python, right, now, even though, this, is, an, F, string, inside, of Double, quotes, prefixed, with, an, F, I, can't actually, use, my, double, quotes, inside, my double, quotes, because, that's, going, to potentially, confuse, python, indeed, if, I run, this, program, now, python, of, student. py, and, hit, enter, I, get, a, syntax, error, so the, program, didn't, even, run, fully, it just, couldn't, be, understood, because, it got, confused, by, those, double, quotes, So the, simplest, fix, here, would, indeed, just be, to, use, not, double, quotes, but, single quotes, around, the, keys, or, conversely flip, the, double, quotes, on, the, outside, to single, quotes, then, use, double, quotes, on the, inside, you, just, want, to, be

consistent, so, a, subtle, detail, but, again this, is, now, specific, to, dictionary syntax, this, isn't, fundamental, to, how we're, solving, this, current, problem, at hand, all, right, well, let's, go, ahead, and try, this, let, me, go, ahead, now, and, run python, of, student., let's, go, ahead, and type, in, Harry, let's, type, in, Gryffindor and, hopefully, Harry, is, back, from Gryffindor, no, syntax, errors, no, other errors, I, think, I'm, back, in, business, here and, what, I, do, like, to, be, clear, about using, a, dictionary, is, that, it, is allowing, me, just, better, semantics, again I, don't, have, to, remember, memorize document, that, zero, is, name, one, is, house instead, name, is, name, and, house, is, house it's, just, a, little, clear, a, little, more expressive, so, that's, generally, a, good thing, especially, if, we, stored, more, data about, students, than, just, their, name, in their, house, if, you, had, three, Fields, four

five, 10, different, fields, no, one's, going to, want, to, remember, or, be, able, to remember, forever, which, is, zero, which, is one, which, is, two, and, so, forth, better, to introduce, uh, names, like, name, and, house in, this, case, but, let, me, tighten, this, up further, in, indeed, I'm, typically, in, the habit, of, not, introducing, variables unnecessarily, unless, they, make, the, code more, readable, and, an, alternative, way, to format, the, same, code, would, be, this strictly, speaking, I, don't, need, to, create an, empty, dictionary, then, add, one, key, to it, then, add, a, second, key, to, it, and, then return, that, dictionary, I, can, actually consolidate, this, all, into, one, statement if, you, will, let, me, go, ahead, and, do, this let, me, go, ahead, and, say, name, equals inputs, return, value, house, equals, inputs return, value, and, then, instead, of returning, any, variable, name, student which, I'm, going, to, propose, doesn't, need

to, exist, anymore, let, me, just, create, and return, the, dictionary, all, at, once, let, me do, quote, unquote, name, in, lowercase, here and, then, the, variable, it's, storing, the user's, name, then, quote, unquote, house, as my, second, key, the, value, of, which, is going, to, be, house, the, variable, now, is this, better, maybe, maybe, not, maybe, the first, way, was, a, little, more, readable, and that's, totally, fine, to, create, variables if, they, improve, the, readability, of, your code, but, just, know, that, you, can, also create, and, return, a, dictionary, on, the fly, like, this, so, to, speak, all, in, one line, and, I, think, it's, arguably, pretty reasonable, in, this, case, why, it's, just pretty, short, I, probably, wouldn't, do, this if, it, got, longer, and, longer, and, longer, I might, minimally, then, start, moving, my, key value, pairs, to, separate, lines, but, this would, just, be, a, slightly, more, compact

way, of, doing, this, as, well, but, let, me propose, we, do, one, more, change, let's, go ahead, and, introduce, that, same, special casing, of, Padma, to, fix, her, house, from Gryffindor, for, instance, to, Raven, claw how, do, we, do, this, with, dictionaries, well dictionaries, like, lists, are, mutable, you can, change, what, is, in, them, just, like, you can, lists, and, how, do, you, do, that, is, just a, little, different, syntactically, so let's, go, back, into, Main, and, do, this, fix if, the, student, variable, has, a, name, key that, equals, equals, Padma, then, indented go, ahead, and, change, the, value, of, the house, key, inside, of, that, student dictionary, to, be, quote, unquote, Ravenclaw instead, so, very, similar, in, spirit, to what, we, did, with, a, list, but, instead, of using, location, zero, and, one, we're, much more, clearly, explicitly, semantically using, quote, unquote, name, and, quote

unquote, house, because, you, index, into lists, and, topples, using, numbers, but, you index, into, dictionaries, using, strings, as I've, done, here, all, right, let, me, go, ahead and, run, python, of, studenty, we'll, again do, Harry, from, Gryffindor, and, I, think, all as, well, let, me, run, it, one, more, time, this time, with, Padma, who, in, the, movies, is from, Gryffindor, but, should, really, be from Ravenclaw, are, any, questions, then, on, this progression, from, tuples, to, lists, to dictionaries, we, haven't, necessarily introduced, anything, new, other, than, those tuples, which, have, been, available, to, us all, this, time, but, the, goal, at, the, moment is, just, just, to, demonstrate, this distinction, among, these, different, data types, and, how, they, each, work, a, little bit, differently, what, if, a, a, combination of, lists, is, there, in, a, tuple, so, is, the list, like, we, can, change, the, list, because

tupple, are, immutable, but, lists, are mutable, correct, you, can, change, the contents, of, lists, and, you, can, put, most anything, you, want, in, them, other, lists, or strings, as, I've, done, integers, or anything, else, tles, you, can, do, the, exact same, thing, but, you, cannot, change, them once, you've, created, them, a, dictionary, is more, like, a, list, in, that, it, is, mutable you, can, change, it, but, the, way, you, index into, a, dictionary, is, by, way, of, these Keys, these, strings, as, we, keep, seeing rather, than, by, numbers, those, numeric indices, all, right, well, let, me, propose that, there, is, yet, another, way, of, solving this, problem, and, indeed, I, would, argue that, there's, now, an, opportunity, to, hand even, though, this, program, isn't particularly, complicated, all, I'm, doing is, collecting, a, name, from, the, user, and, a uh, house, from, the, user, you, could, imagine wanting, longer, term, to, collect, even, more

information, like, the, student's, Patronis or, magical, spell, or, a, whole, bunch, of other, information, that, might, belong, in, a student, and, right, now, we're, just, kind, of using, these, very, general, purpose, data types, in, Python, a, tuple, to, combine, some values, together, a, list, to, do, the, same but, let, us, change, it, later, a, dictionary which, is, more, powerful, because, it's, a little, more, structured, it, does, have, keys and, it, has, values, not, just, values, but you, know, what, this, we, wouldn't, have, to be, having, this, conversation, if, the authors, of, python, had, just, given, us, a data, type, called, student, right, wouldn't it, have, been, nice, if, there, were, just, a type, of, variable, I, could, create, in, my code, called, student, then, we, wouldn't have, to, figure, out, well, do, we, use, a tuple, or, a, list, or, a, dictionary, but that's, pretty, reasonable, right, you, could imagine, just, a, how, slippery, of, a, slope

that, is, so, to, speak, if, the, creators, of, a language, had, to, anticipate, all, the possible, types, of, data, that, Pro programmers, like, you, and, me, want, to store, in, your, programs, so, they, just, gave us, these, general, purpose, tools, but, they gave, us, another, general, purpose, tool that's, going, to, allow, us, to, create, our own, data, types, as, well, and, actually, give them, names, and, that, uh, terminology, is, a class, A, Class, is, kind, of, like, a blueprint, for, pieces, of, data, object, so to, speak, a, class, is, kind, of, like, a, mold that, you, can, define, and, give, a, name, and when, you, use, that, mold, or, you, use, that blueprint, you, get, types, of, data, that, are designed, exactly, as, you, want, so, in, short classes, allow, you, to, invent, your, own data, types, in, Python, and, give, them, a name, and, this, is, a, primary, feature, uh, of object-oriented, programming, to, be, able

to, create, your, own, objects, in, this, way and, in, the, case, of, python, and, classes even, give, them, some, custom, names, so, what does, this, mean, in, real, terms, well, let, me go, ahead, and, come, back, to, VSS, code, here and, let, me, propose, that, we, introduce, a little, bit, of, new, syntax, I'm, going, to, go ahead, and, clear, my, terminal, window, first I'm, going, to, go, to, the, top, of, my, file and, I'm, just, going, to, start, a, thought but, not, finish, it, yet, I'm, going, to, use this, new, keyword, for, classes, called literally, class, so, indeed, the, new keyword, we're, going, to, have, here, and, if I, go, back, to, our, slides, here, this, would be, the, official, URL, where, you, can, read up, more, on, this, particular, feature, of python, in, the, official, tutorial, class, is a, new, keyword, we, can, use, now, this, is coincidentally, related, to, students because, students, take, classes, but, it, has

nothing, to, do, with, the, fact, that, dealing with, students, class, is, a, general, purpose term, in, a, lot, of, languages, python, among them, that, allow, you, to, Define, these custom, containers, with, custom, names, for pieces, of, data, so, let's, go, back, to, vs code, let's, use, this, new, keyword, and, let me, propose, that, we, create, a, class, called student, and, by, convention, I'm, going, to use, a, capital, S, here, and, I'm, going, to, go ahead, and, with, a, colon, get, to, later, the implementation, of, this, class, so, I'm, just going, to, use, dot, dot, dot, which, is, a valid, placeholder, for, now, that, just indicates, to, me, that, I'm, going, to, come back, back, to, implementing, this, later, but as, of, now, it, does, in, fact, exist, I, now have, a, student, uh, class, defined, for, me that, I, can, now, use, in, my, code, here, how am, I, going, to, use, it, well, first, of, all

let, me, go, down, to, get, student, and, let, me change, this, code, to, no, longer, use, a dictionary, but, to, use, this, class, I'm going, to, do, this, I'm, going, to, give myself, a, variable, called, student, as, I've done, before, but, I'm, going, to, set, it equal, to, Capital, student, open parenthesis, close, parenthesis, so, I'm going, to, do, what, appears, to, be, calling, a function, and, that, function, student, with a, capital, S, notice, matches, the, name, that I, gave, this, class, at, the, top, of, my, file all, right, what, do, I, next, want, to, do, I'm going, to, go, ahead, and, give, this, student a, name, now, if, I, were, still, using, a dictionary, I, would, say, student, quote unquote, name, using, square, brackets, but this, is, not, a, dictionary, it, turns, out classes, have, what, for, now, we'll, call attributes, properties, of, sorts, that allow, you, to, specify, values, inside, of them, and, the, Syntax, for, that, happens, to

be, a, DOT, we've, seen, dots, before, we've used, it, in, the, context, of, modules, and libraries, more, generally, this, is, another similar, in, spirit, use, of, a, DOT, that allows, you, to, get, at, something, inside, of something, else, so, student., name, is, going to, be, the, syntax, I, use, for, giving, this student, a, name, and, that, name, is, going, to be, whatever, the, return, value, of, name, is and, then, I'm, going, to, go, ahead, and, say student.h, house, to, give, another attribute, called, house, and, give, that, the return, value, of, input, here, prompting, the user, for, house, and, then, as, before, I'm just, going, to, return, student, but, now what's, really, powerful, about, class, and object-oriented, programming, more generally, is, that, I've, created, this custom, data, type, called, literally student, capital, S, I've, stored, one, such student, in, a, variable, like, I, can, always do, in, a, variable, called, student

lowercase, s, but, I, could, call, it, anything I, want, I, just, make, sense, to, call, it student, as, well, but, lowercase, for clarity, and, then, I'm, returning, that variable, and, because, of, my, syntax, in lines, 14, and, 15, that, has, the, result, of putting, inside, of, that, class, a, name attribute, and, a, house, attribute, I, just need, to, make, one, more, change, up, here, I'm going, to, go, ahead, and, remove, our, Padma code, just, so, we, can, focus, only, on, what's new, rather, than, fixing, her, house, and, I'm going, to, go, in, here, and, change, the syntax, that, previously, was, for dictionaries, again, dictionaries, use square, brackets, and, then, strings, in quotes, either, single, quotes, or, double quotes, depending, on, the, context, here though, I'm, going, to, change, this, to, be student., name, and, over, here, I'm, going, to change, it, to, be, student., housee, and that's, just, going, to, be, my, new, syntax

for, getting, the, contents, of, what, appears to, be, a, class, called, student, let, me, go ahead, and, rerun, python, of, student., by enter, let's, type, in, Harry's, name, as before, let's, put, him, in, Gryffindor crossing, our, fingers, as, we, often, do, and Harry, is, indeed, from Gryffindor, what, the, though, have, I, done let's, introduce, one, other, bit, of terminology, here, it, turns, out, that, I, can create, a, class, using, that, class, keyword but, anytime, you, use, a, class, you're creating, what, are, called, objects, and here, is, the, word, objects, as, an objectoriented, programming, or, oop, let, me go, back, to, my, code, here, and, even, though I, haven't, really, implemented, much, of, it at, all, I, literally, just, left, it, with, a dot, dot, dot, that's, enough, code, lines, one and, two, to, just, invent, a, new, data, type called, student, capital, s, that, may, or, may not, have, some, future, functionality, as

well, that's, enough, to, create, a, class what, though, am, I, doing, on, line, 11, on line, 11, what, I'm, technically, doing, is creating, an, object, of, that, class, so, this too, is, another, term, of, art, you, create objects, from, classes, so, if, we, go, back, to that, metaphor, that, a, class, is, like, a blueprint, for, a, house, or, a, class, is, like a, mold, an, object, is, when, you, use, that blueprint, to, build, a, specific, house, or something, that, comes, out, of, in, plaster the, mold, when, you, actually, use, that, mold to, create, such, an, object, so, a, class, is again, the, definition, of, a, new, data, type the, object, is, the, Incarnation, of, or technically, instantiation, of, and, another term, for, objects, would, actually, be, an instance, you, have, instances, of, classes as, well, so, that's, a, lot, of, vocabulary but, at, the, end, of, the, day, it, just, boils down, to, this, you, can, Define, your, own

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent