 So we're entering into the programming unit of the course and not only do you get to learn how to program in Python But we're also going to use it to explore concepts like algorithms programming languages data structures and so forth And so I'd like to tell you a little bit about Python the language before we dive in to the concepts One so what is Python and there's lots of places where you can learn about that Wikipedia? For example talks about it being a general purpose language It's very high level and by high level we're talking about levels of abstractions So remember early in the course when we learned about machine instructions when you write Python code that translates One line of Python code translates into many of these load store jump instructions at the CPU level From a language design point of view will look at different philosophies next week when we talk about programming languages Python's deal was really to emphasize readability I think someone says that you read code about 85% of the time and you write code about 15% of the time in the Software industry, so readability is crucial and there's a minimal syntax indenting is required for the language to be correct So there's a lot of interesting philosophies there It also supports multiple programming paradigms which again, we'll talk about next week So you can program imperative object-oriented procedural functional Whatever mood that you're in it's supported by the language There's a bunch of other features to learn and you can go to Python org For example, if you want to learn more about you know the up in the latest features of the language But you know the main thing is that it's batteries included There's a whole bunch you can do with very little effort and very little learning That are useful and just to sort of look ahead to the end of the semester The second performance task will be creating a program of your choice And there's actually quite a bit that you'll be able to do with that with only several weeks of programming experience in this class So start thinking about what kind of programs or apps you would like to build for your final project So who uses Python one thing I want to emphasize is that Python is not a toy language. It is used by pretty much everyone I mean everyone big. It's also the number one language for doing scientific research So there's a lot of chemists biologists physicists Other researchers that use Python Just because it's a great language for getting work done You may not be shipping software necessarily you might just be writing code to do some other kind of analysis So, you know NASA stock exchange things like that If you'd like to install Python One thing to note is we'll be using version 2.7 the latest version of Python is version 3 If cast going back to the Python's website, you know, you can go to the downloads link And it will ask you do on 3.4 or 2.7 The reason why we're using 2.7 is that still the default version of Python on Mac and Linux And until that community makes the jump to version 3 I guess I won't either in my class I mean, they're pretty much the same in terms of what we're doing in this class So if you have Linux or Mac just run it from the terminal already if you have a Windows computer you can go to this link and Download the installer for Python 2.7 and then you'll just be able to run idle directly from the start menu So let me tell you in this video what you need to know in terms of the Python language in this course we've been talking about syntax and semantics this week and What that means with respect to primitives now if you go back and look at section 5.2 of the book There's really only four Syntactical primitives that you need to know Probably for the whole course right so one of them is assignment to notice I'm using an arrow here instead of the equal sign because I want you to visually remember that what's on the right hand side of The assignment is stored into the left hand sign So when you see an equal sign in Python code, just think left arrow in terms of a mental model There's also decisions. So we've learned how to write if statements in class and in the coding bat Exercises, but when you read an if statement, there's this implicit then there's no then word in Python But that's what you should be thinking if this then that and there's also an if then else structure The else is optional in most programming languages The third thing is loops and the only type of loop you really need to know is the while loop So while some condition do some activity, right? So do corresponds to then in the same way the while corresponds to if You know, and we'll look at how this in the next slide how this actually looks in Python And finally we have functions functions is when you give a bunch of code a name and you can give that That function as well some optional parameters or or data that's required to run the function just like an algebra Usually have f of x and the way you say the parenthesis is the word of right? So f of x of course in programming We don't like to have single variable single letter variable names So you might have an entire name and parameters So here's an example in Python that you should be able to understand at this point after going through the code Academy tutorials and in yesterday's class that uses all four of these Primitives discussed in 5.2. So what I'd like to actually do is show you this example in action I'm going to run idle here In a separate window and remember you can always say file new file or file open to to have code here and Notice what we have first of all is a definition statement So I'm going to define a function called greeting and this function requires a parameter hour Right, and I'm actually going to fast forward past the body of this function and show you what my program does All what my program does is it runs the greeting function or procedure with the parameter value 9 and then it does the same thing with 15 so let me go ahead and run this module so you can see what it does Again an idle to run you go to the run menu click run module f5 is the shortcut key And when I run that I actually see the output here on the left in the terminal window, right? So you can see it says good morning. It is now 9 o'clock. It's 10 o'clock all the way up to 17 then it says time to eat and Let me go ahead and add an extra print statement here so you can see Where the two different test cases run so if you were to say run module after changing the code You're actually going to get this little Message that says you have to save the source code in order to run again So I'm just going to hit hit the enter key so it go ahead and click the okay for me And notice how I've got now two blocks of output right good morning blah blah blah good afternoon and so forth So let's dive into the code and figure out why it's doing this behavior So you can see the very first statement of the greeting Function is an if statement if the hour is less than 12 print. Good morning Otherwise print good afternoon and you can see the first time I ran greeting I ran it with the nine the second time I ran it with the 15 and that's why I have good morning the first time and good afternoon the second time So now it says I'm going to count up to 5 p.m. Which is actually 17 o'clock In military time. I'm just using a simple integer value to represent the hour So I have this Variable now which is going to be the hour and as long as now is less than 18. I'm going to print It's now blank o'clock right you can see how that variable Now turns into 9 10 11 12 and so forth in the program's output Each time I'm incrementing by 1 and then at the end of the program. It says time to eat now. Remember this code is just defining This procedure and I can still call this procedure in this other window I can come down here to the cursor and say greeting Let's say 20 right and you'll see all that this code does is say good afternoon time to eat Now, of course, I probably should add something else that says good evening or I don't know But it completely skipped the while loop because 20 right here is not less than 18 So anyway, it's kind of nice to be able to interactively You know try out your code in one window while you're editing it in the other window and and and you never have to sort of stop To run your program again It's just a way to play around with it and get familiar with it So let me minimize idle and again as a reminder you may want to rewind and rewatch sections of this video I am going pretty quickly But I also want you know people to be able to consume this material at their own pace So one thing to note again from this whole example is that in Python Files are executed just from the top to the bottom and there's no sort of starting point like in Java There's a main method. There's nothing like that in Python It just starts running all the code from top to bottom and the very first instruction of this file is to define a Function these other two lines greeting 9 greeting 15 call that function So define and call or that is the terminology for the two different sides of functions So while I'm talking about you know other students I Recognize that some of you are already taking a programming class or have had a programming class before It's not assumed that you've had any prior programming experience and those students actually are probably at an advantage Because I don't have to sort of change the way they think about things But I would like to just briefly compare Python and Java since that's the the number one language for You know CS intro courses that aren't like this one I guess that's all I need to say so Python is a dynamically typed language That means the type of variables happens on the fly You don't have to in advance say okay, you know this variable right here now is going to be an integer The language figures that out as it goes And so the way to think about this is I'm taking a value in the program and associating a name with that value as opposed to Declaring a variable and the other main difference between Python and Java is the Python is very concise So it takes very few words to express an idea and by the way these Definitions are just from a dictionary word net whereas Java is verbose It's a bounding and extra stuff. You always have to type which is kind of annoying So here's one of my favorite examples if in Python if I just want to open a file I say my file equals open and then the file name of course this green line right here's a comment in Java if you want to open a file first you have to import the iLibrary Then you have to create a buffered reader Well first you have to declare the variable then you have to create a file reader object and wrap that in a buffered Reader object and I mean you can just see on the right how much more code you have to type to do something simple like open a text file So anywhere there's this nice blog Python conquers the universe And they have a whole series of posts there about you know the differences between the two languages a few tips I might give you if you if this is not your first programming language And there's some things you need to unlearn so you know I'll just compare these two examples here on the left and on the right The first thing is forget about declaring variables you should never have to do that So you don't need it a line like int x you just simply start using x And you'll notice too in this earlier example our right here the parameter I didn't have to declare that either I just put our and the parentheses and the language will figure out what type it is when it needs to Forget about semicolons So it's actually okay if you want to write a semicolon at the end of every line like in Java Python will just ignore it But if someone else looks at your Python code with semicolons, they're going to know that you're sort of a Java programmer But do remember colons, okay? So in Python you always need a colon before you change the indentation level So for example when I define a greeting notice, there's a colon here at the end of the line That means that the following lines are about to be indented and you can see the same thing with else if while You always have a colon there because the indentation level is about to change What else did I need you to remember forget about parentheses, right? So an if statement, for example doesn't need parentheses like it does in Java forget about braces So indentation automatically captures the idea of blocks, right? So all of this code is under the if statement this block of code print because it's indented If you forget to indent your code will not work, right? It's absolutely a forced requirement of the language that you indent it correctly Which is actually a very good teaching tool because students if they're not forced to do something won't do it So that's why a lot of schools are switching from Java to Python for their intro courses including JMU So there's this also this nifty little comic that you should go read at some point If you're not already subscribed to xkcd you should it's it's it's an awesome comic But that's the obligatory Python comic that everyone should read All right. So what's the point of all this? I talked about how we're not only learning Python in terms of programming But we also want to explore Concepts like algorithms and programming languages and data structures And I would say that the biggest concept for this week is going back to that big idea I introduced to the beginning of the semester that computer science is a creative activity. This is a great quote by Jeanette Wing She's the department head over at CMU and she wrote this great article In 2006 called computational thinking and I would actually encourage you this is not required for the course or anything It's just a nice three-page article that summarizes what Computer science is and it has a call at the end of the article that professors ought to create classes about ways to think like a computer Scientist well, blah, here we are eight years later and and you guys are taking this kind of a course, right? So anyway, go back and read that article sometime. It has some great Insights about how computer science or computational thinking as she calls it is a way to solve problems It's a way that humans think not the way that computers think and and anyway I'll just let you read that on your own time, but going back into creativity. That's what section 5.3 is all about So please please read this section of the book It's a great read about how to solve problems, right? How to you don't just follow a set of steps like the way math is taught in junior high and high school It's it's more about taking the initiative and really understand what's going on Let your subconscious work for you, you know You're going to be solving problems not only in this class But other computer science courses that you've never seen before and and you can never say the teacher never showed me how to Solve this problem because that's exactly what the industry is like You're you're constantly be given problems to solve with that technology that nobody's done before and you have to just figure out How to do that so one of the techniques is is trying to get your foot in the door, right? Getting started on a new problem is often the hardest part. You might have to reverse engineer things But please resist the temptation to look for a formula or to memorize some kind of solution It's more about just getting insight And the more problems that you learn how to solve Using computation the more creative that you become so I would say the creativity in computer science is not so much about making art You know like in a graphics program like Photoshop. It's more about solving algorithmic puzzles Because you have experience doing lots of those challenges before and that's the type of creativity That I'd like you to learn not only in this class, but the whole computer science major One more thing about the book before we close in section 5.4 There's a lot of stuff in there and it's actually more than we're going to be able to cover this week in class So I just want you to focus on the section about loop control. So don't worry about Sequential search don't worry about insertion sort Those are great algorithms and we will take a look at those in CS 240 and maybe a little bit in the data structures chapter a couple weeks From now, but for this week just focus on loop control. It's just these four pages in particular section 5 Sorry figure 5.7 talks about how every loop in any programming language This is an algorithm structure. It always has three components, right? So let me just show you this while loop From the book this greetings example right count equals 3 While count is greater than zero print. Hello, and then count equals count minus one Here's the three parts of the loop this discussed in figure 5.7. We got the initialization step We've got the test where we're testing the loop variable to see if we've reached the termination point and Finally the modify step where you're modifying the variable that you're looping on You can actually find these three elements in any type of loop So there's another loop in Python called the for loop Or some people call it the for each loop, right? So for I in range 3 basically is doing the same code in this while loop but in fewer lines And the reason why is the for loop is designed to do all three loop components in one line of code, right? So the for I in range 3 is doing initialized test and modify all in one concise little package And now the only thing I have to have inside the loop is what the loop does in this case The loop is just printing the word hello One thing you'll notice you read the book is Python does not have a post test loop There's no sort of do while or repeat until structure in the Python language And that kind of goes back to the language philosophy that there should be fewer ways of doing something So you don't always have to figure out which way you have to do it It's easier to read code if there's fewer options And so pretty much in Python you have a while loop and you have a for loop and those are your two choices for iteration So that's it for now. Um, if you need to review again, please rewind to watch parts of this video Take a look at these slides on the website Feel free to continue to go through Codecademy in the Python track You're welcome to go and do additional modules You only have to do the first one required for this class And you might also want to get started on the lab for tomorrow. Let me go ahead and pull up the website for that So going to the schedule page click on Python and idle There's going to be two main parts to the lab one is just making sure you're familiar with idle Which is this program for running Python code And the other one is to write a little program that sings the bottles of pop song I'll give you a little warning if you watch this youtube video Be careful because you might get the song stuck in your head for the rest of the day We're also going to do some coding back problems in the lab So you might want to take a look at that beforehand And please come to the lab ready to ask questions That's just the best use of time and then you can all finish the lab before you leave for the weekend And not have to come back on saturday or sunday. So with that i'll sign out and see you later