 So, like I said in class, I really have two goals this week. One is that you learn more of the Python language, but two is that you learn more about programming languages in general. So I have this picture that I showed you from O'Reilly that has a timeline of all of the different programming languages or some of the more popular ones. There are a lot of them out there, and hopefully you'll learn several of them throughout your years as a computer science major. But regardless, all of these languages boil down to the same sorts of features. They can do loops, they can do decisions, they can do procedures and variables and so forth. And so I'd like to show you these concepts today in the context of Python. But again, we're just looking for the general concepts as opposed to learning a lot about programming. I need you to do something to prepare for Wednesday's class. So here I have a list of terms from chapter six that you should be familiar with. Things like assembler, compiler, interpreter, the difference between natural language, formal language, the difference between a declarative and an imperative statement, variables, constants, literals, procedure function, method, parameter, and argument. That's a lot of jargon and a lot of technical terminology. But what it does for us is allows us to talk about specific features of a language in a way that we don't have to keep explaining which thing we're talking about. So make sure as you're reading through section 6.2 and 6.3 of the book, and then also 6.1 from earlier this week, that you pay attention to these terms as they're bolded. And maybe what you want to do is just write down a list of definitions next to this on your own sheet. So go ahead and do that and bring that with you to class on Wednesday. I'll ask you a few questions about these to make sure we're all on the same page. So without further ado, I'm going to go ahead and start idle. Remember that when you're in the lab, you want to make sure you're running idle for version 2 of Python or 2.7. So I've got one of those already open here. And also, typically what I like to do is say file a new file or open an existing file so that on the left I have my Python shell or in the interpreter. And on the right I have my editor so I can just write code. I'm just going to go through section 6.2 of the textbook and demonstrate in Python some of the concepts. So for example, the very first section is variables and data types. That's on page 269. And I'll make this file available. You should definitely download a copy from the video page and follow along in your own Python shell, maybe try out some of these same commands hands on rather than just watch the video passively. So let me create some variables. Limit equals 5. Pi is 3.14159. Name may feel happy. Why not? Let's just make happy true. And let me go ahead and save this file. Now typically in the Linux lab when you go to save a file it shows you all your hidden folders too. So you have to scroll over to your desktop or wherever you want to save these. I already have a couple of those there. I'll just call this one happy save. Alright, so I've got my happy Python file here. I've got four variables. And if I run this program you'll see that nothing happens on the right because all I've done was just define some variables. But you know I can do a whole bunch of things with these variables. If I just type the name of a variable it will automatically print out its current value. So I could demonstrate some of the operations you can do. If I add 5 to limit I get 10. Maybe I'll do pi times 2. And that's twice as much as 3.14159. You can also add text. So I might say doctor space name and that's doctor Mayfield. Notice too how it changed my double quotes to single quotes. Again the quote marks are interchangeable in Python as long as you use the same ones on either end of the text. And finally I can say not happy which is false because happy is true. Now the thing I wanted to really demonstrate here with variables is that each of them have three things. They have a name like limit pi happy and so forth. And they have a value which is like 5 or 3 or Mayfield are true. And then they also have a type. So I could say what is the type of my limit variable and it tells me the type is an int or integer. What's the type of my pi variable? That's a float or floating point number. Type of name is a string. Type of happy is a bool or boolean. And the type of data is actually really important so that the computer knows how to display or operate on that data. So remember going back to chapter one of the book the binary representation for numbers is different from that of text and images and sound and so forth. So just because I give a memory address a name I also have to know how to interpret that memory address and that's what data types are for. Data types also prevent me from doing things that don't make sense. So for example if I wanted to say limit plus name it will say wait a minute you can't add a number to text unless you first convert the number to text or the text to number like you can only add things that make sense. Let's move on now to the next section in 6.2. This one is about data structures and we're not going to spend a lot of time this week on data structures because there's a whole chapter about that coming up in a couple weeks. But one thing you should know in Python is it's really easy to make a list of things. So let's say for example I want to make a variable called scores and these might be the scores I got on my exams let's say right so I've got a hundred percent on one of them, 92, 89, I'm just making up these numbers. But the nice thing is here is this bracket syntax in Python is a way that you can store multiple values into a single variable. So rather than having score one, score two, score three, and so forth I just have a variable called scores. Let me go ahead and run this code so I have it down here. And this variable now I can, well let me just print it out, scores. Notice how it uses the same bracket notation to print them all out. If I just wanted a single score I can use brackets to ask which one I want. So if I want the first one I say score sub one and notice how it actually printed not the first number but the second element in the list. These numbers are actually indexed at zero. So if I say score sub zero I get 87. This is the zeroth element, the first element, the second, the third, and the fourth. Depending on how you count. So actually index zero is the first and the last one is the fifth element. But if I were to say score sub five and my editor here is going to say wait a minute five is out of range that's past the end of your list. The last one is actually score sub four. We're going to talk in a couple weeks about why indexes are based at zero in most programming languages. But the point here is again a data structure is a way that you can take lots of data put it into one variable and that might be organized as a big long list of things. It might be a dictionary of things or a set of things and we'll talk about these different data structures later in the course. So let's take a step back and talk about constants and literals now. That's actually the next section but by taking a step back I'm just going to pull out these variables I had previously in the video. Variables are the names of memory addresses but literals on the other hand are literal values. So five is an integer literal may filled as a string literal and so forth true and false are literal boolean values. And literals are used if you need to hard code a value in your code. So let me go ahead and run this again. You know I might say something like print pi right that's 3.14159 but notice the difference if I put pi in quotes right pi in quotes is a string literal that's different from pi without quotes which is a variable in fact this is not even a string variable it's a floating point variable and notice how python will put a string literal in green font whereas just normal words are interpreted as variable names. So a common mistake to make as a programmer is did you mean to say something literally or did you mean to recall a variable name. There's some interesting things that you can do as well so I already showed you how you can add where was it earlier on you can you can add two strings together which just concatenates them into one big string. You can also multiply strings I always thought this was an interesting feature of pythons so if I say pi times 3 I get pi pi pi right it's three copies basically of the same string so that's that's kind of fun. Notice how though if I said pi times 3 I actually get the number pi multiplied by 3 this is called overloading in terms of a language and you'll see the term overloading later in the chapter basically what it means is you can use the same symbol or the same procedure in a language to mean different things in different context right here multiply means make copies of a string here multiply means what multiply means of math right take the number and add it to itself that number of times. Now not every operation is overloaded so for example I know that pi plus 2 is 2 plus 3.14159 but if I try to do the same thing to a string oops sorry that's not valid right so just because you can overload something doesn't mean that it's always overloaded for every symbol in the language and the language reference you know if I take you to Python's website and you were to go to the documentation let me just pull up the Python 2 docs you know there's this language reference you can look at which is this huge library of like what every possible thing is allowed and all that it's not something that you would read it's like reading a dictionary or an encyclopedia but it's nice to refer to to know you know what the limits are of a programming language. Now one thing I failed to mention is that constants are used in some language to make a variable unchangeable like you can create a variable that will never be changed you can't accidentally change it Python actually doesn't support constants you can't like all variables you can always change at any moment and but other languages like Java allow you to mark a variable as final I'm never going to change this again so don't worry too much about that detail as described in the book I'm more interested this week that you understand the difference between variables and literals there's another constant that you should understand basically comes from algebra and that's called operator precedence you can read about operator precedence on page 275 but it's just like it works in algebra so let me just give you an example here if I were to do the amount 1 plus 5 times 2 minus 3 the question is what order do I do this operation notice how just like in math you usually multiply before you can add or subtract you may have seen the term PEMDAS which is basically parentheses then exponentiation then multiply divide add subtract that's an acronym that some algebra course some algebra teachers teach you for learning the order of operations but note that this is 5 times 2 is 10 plus 1 is 11 minus 3 is 8 right and if I wanted to force a certain order of operations I can do the same thing and put parentheses around one of them right so this would be 5 plus 1 is 6 times 2 is 12 minus 3 is 9 so notice just by placing those parentheses I get a different answer just to drive the point home I'll do it this way right 1 plus 5 times negative 1 is negative 4 so again just like the rules of algebra in most programming languages there's a precedence to operators and the computer's going to evaluate everything from left to right but give it a higher priority to divide and multiply than it would for adding and subtracting so the next topic in section 6.2 is control statements and just for a bit of trivia the reason why it's called control for things like if and while is these statements control the program counter so remember thinking back to how machine execute code you set the program counter of the next instruction to execute and that's exactly what an if statement does so here's an example from python let's say I have my happy equals true variable and if I'm happy I'm going to print you know it otherwise I'll print why not and this type of structure in a programming language is basically controlling the flow of execution let me go ahead and run this program it should be obvious why it prints the word you know it and of course I can change that condition and say if I'm not happy print you know it so now I print exactly the opposite out and we talked today in the previous quiz about how an if statement and a while statement are different they're basically the same thing it's just that a while statement will continue to evaluate that condition forever until this condition changes so if I make a loop this says while happy print great and basically going to get it to print great great great great forever and there's really no way to stop it unless you hit control C which is the symbol on a computer to say stop right or interrupt so if I hit the control C character it basically raises this error called keyboard interrupt and that's how I sort of gain control back over my python shell I wanted you to see how to break an infinite loop since some of you have experienced that already and it's just a good skill to know on any terminal program so in this last example I'm going to skip forward to section 6.3 and talk about procedural units and python these are all called functions but generically speaking you have procedures that do something and you have functions that return values the book refers to this type of procedure as a fruitful function and you can tell the fruit because it has a return statement right so let me go ahead and run this code and show you a little bit about how it works right so all I've done is run this file you can download a copy of this from the video page on the website and basically I have two procedures here so one is greet I can just go ahead and type greet let me go ahead and put in my name here so I'll just say mayfield right and we'll say hello mayfield how are you today and it goes ahead and does that three times and maybe I'll do another example like greet you know Alice and notice how it's the same procedure except it took the word that I gave it and filled in the blank there right and that's exactly what procedures do they take some kind of parameter or option and it will go ahead and use that in the procedure now I've got multiple things going on here let me step you through the code so this first line here of course is just a comment when you define a procedure or a function you give it a header now and this is going back to what the textbook is going to be talking about in 6.3 the header will have the name of the procedure parameters, parameters are variables that basically get assigned whatever values you specify when you call the procedure so here I have the name mayfield this value gets assigned to that variable name so notice here I'm going to print out the name notice I have a couple other variables here greeting and question these actually got assigned way up here so I'm using a mixture of global variables which were assigned at the level of the whole file and local variables which were assigned local to this procedure here so for example I can cheat a little bit I can say name equals ha ha ha ha and notice if I try to do that greeting again it says hello Alice hello Alice because even though this variable here name is the same name as my global variable name within the scope of this function name had a different meaning so you'll read a little bit about in the book about scope and what scope means is if you have two variables with the same name whichever one was defined latest is the one that you're referring to so here if I do this print statement print greeting print name print question it will look for the closest version of that variable so name is defined here in this function I can go ahead and just use it to be found so I'm going to go up a level ah there it is at the top of the module and that's basically what it means to be a global variable versus a local variable is where did you define that variable now let me go ahead and focus on this second example here the one that computes the area of the circle so remember the area of the circle is pi r squared so radius squared is just radius times radius radius is the value I'm going to give as a parameter for this function area and then we just go ahead and make up a couple examples here so maybe I want my program to print you know a medium pizza is area let's see a medium pizza is 12 inches right square sqa re inches and I'll go ahead and do the exact same thing but change the size to 14 and say a large pizza and notice what's going on here I'm printing out a message in the middle of the message I'm computing a value right so area is my function and the parameter is going to be 12 and let's see what this program does here if I run it so I'll run go ahead and hit save you know a medium pizza is 452 square inches and a large pizza is 615 square inches so the point here isn't about pizzas of course right it's how am I calling this function giving it a value and it's returning a value back to where this goes right so basically the program language itself is going to replace this code which is in black with the actual value that's returned from that function and it all gets printed out as one big message. Just a couple minor things about terminology here so when you refer to a variable in the header of a function it's called the parameter when you refer to the value that you're assigning that parameter this is called the argument and we've seen the word argument before in the course we had command line arguments things that you type so for example if I just pull up in a terminal here and type ls-l dash l is an argument to the ls command in the terminal right and so there's all my files but it's the same thing in the programming language the argument is the value that you're assigning to that parameter so I just wanted to make sure that you were a little familiar with those terms. Procedures and functions you've seen in this video you know functions return a value procedures don't method is another term that you might hear applied to this concept but that's in object oriented languages so we'll talk more about object orientation for the lab this week but method basically means the same thing it's the procedure or function for an object variables constants literals we've seen we've talked about earlier this week in class so I think we've seen most of the terminology either in class or in this video and again just as a reminder please bring in a sheet of paper with you to class that has your own words defining each of these terms that way we can make sure that everyone's on the same page and we're ready to start talking about the real meat of things in class so I hope you've enjoyed this little demonstration make sure you take a look at those source files on the website and we'll see you next time.