Hi everyone and welcome to applied calculus with Python. My name is Professor Catron, and I'll be your instructor for the course. This course is going to attempt to do two things. One is going to teach you the concepts of single-variable calculus, and it's going to introduce the numerical aspects of the course through the Python programming language. We're going to teach you a little bit of Python and a lot of calculus. If you are familiar with some Python, that's great. This first video is just going to introduce some of the basics. I expect that folks have had a little bit of programming before. If you've never seen Python before, if you've never programmed before, then this might not be the right course to start with. You might want to take an introduction to programming course in Python or any language of your choice. But we're going to go through the calculus concepts and then show you how to implement them in Python. Upon successful completion of this course, you will not only be better at calculus and be ready for more advanced math courses that rely on these concepts, you'll also be a stronger programmer and be able to manipulate functions and data and do better analysis and bigger and greater things in Python. With that all said, let's get started. As you probably know, python is a programming language that's unique in its own way and has gained some popularity recently, although it's been around for a while. But a Python program is really just a collection of statements and comments. Statements are an expression for Python to execute, with comments being a note to yourself and really for those reading your programs,. These are ignored by python. A good program is readable and able to be passed off to someone else for them to critique or test. Notice that the little program that I have here below, I'm using the pound sign or the hashtag sign. This is a comment, this is ignored by Python. x equals 10 is a statement. Python will sign the number 10 to x, y equals 12, and then hashtag, this part is ignored. You can have comments on the same line as your code. One of the strengths of Python is its readability and as you would just read a book from top to bottom, left to right, Python goes the exact same way. Now, Python does not care about spaces, so the two Python statements that you see are equivalent, x space equals space 10 or x equals 10. What you have to be careful though is it does care about capitalization. It is case sensitive. If you call a variable capital X, then you must call it capital X throughout. If you use lowercase x, then you must use lowercase x throughout. Here I have the command x equals 3.9. A variable stores a value in the computer's memory for use later in the program. The left side of the equals is the name you want to give the variable so we can retrieve it later, whereas the right side is the value you want to store. In programming, is we have to switch between your math brain and your computer brain. Programming equals doesn't actually mean equals, you can read it as a sign, so that first line x is assigned the value of 3.9. You can start to do arithmetic with your variables, for multiplication we use the asterisk. Here I have 3.9 times x times parentheses 1 minus x, so you can have as complicated expression as you want. You can also have a string, this is in text, so you can also say x equals "some value". Now that sentence, some value or those words is assigned to x, and you can have numbers, expressions, letters, whatever you want, and variables can be overwritten and update it as needed. Here we have x equals x plus 1. This is weird to say as a mathematician, if this bothers you, you're good mathematician, but as a programmer, this should be completely normal. What you're doing is you're taking whatever value is in x, you're adding one to it and then you're assigning it back to x, so if x is 2, after this command is executed, then it becomes three. Now here's the magic of Python or why we're choosing Python for this class and why it's so popular these days, especially with more advanced topics. Variables can hold any type of data, numbers, or text. For example, x equals "Python is fun". That's a string, that's text. Those are words. y equals 2. That's an integer, that's a number, z equals 3.5. A really nice feature Python is that you do not have to specify which type of data you are storing. Other languages don't have this feature. If you've programmed in C or Java, you have to declare what type the variable is and then proceed. This adds some more words and makes readability difficult. Python does that all for you. Another magic of Python, as long as you combine variables of the same type, there's not a problem, so you can say x equals 2, y equals 3, and z equals x plus y, z then we'll have the value of 5 and python knows exactly what to do. Same thing if you did strings, x equals Hello space, y equals World, then you can write z equals x plus y and it knows what to do with these strings. It'll write the sentence Hello space World, put it together for you and get a nice string. The problem comes in, you're going to get an error if you try to combine variables of different types, depending on the variables, so if you say x equals 2 and y equals Hello World, if you try to add them, you're going to throw an error. I really encourage you, as you go through these slides to try to compile these codes. I will share my screen and I will write code and we will compile it together. In this very introduction, these are things that I expect you to know how to compile to be able to type x equals 2 and y equals Hello World to go through that. Again, if you're not at this level of programming where you can't get it to work, go back a step and take a programming course, introduction to programming, and then come back to this. These are the foundational pieces that we're going to add calculus on top of, but we're just introducing the concepts here. Now, functions. Of course, there's functions in math. There's functions in computer science. A function is a rule for taking zero or more inputs, performing some operations and then returning an output. They are very useful when you have repetitive task. You see here the code to define a function, we're going to use def double, parentheses x: x equals x times 2, return x. Can you look at this and guess what this function is doing? See if you can pause the video and think for a second what it's doing. This function is going to double a number and multiply it by two. Notice that def is a very specific word in Python and is Python speak for define a new function. Double is the name of your function. You can call it whatever you want. It's helpful when the function name actually matches what the function does. It'd be weird to call this function triple if you multiplied by two. A good name would be multiplied by two or double, but a bad name would be square root. That doesn't really make sense. You want these things to be readable, again, imagine passing this off to somebody to approve. The parentheses x, that's your input. That's the thing that's being fed into the function that should look familiar from math notation. Parentheses x is what goes into the function. Of course, x equals x plus 2 is what the function is actually doing. This function is a single line, there can obviously be more steps involved. Then of course, if you return the answer, and this is what the function returns. This is your output of the function. To call the function, we can have our function declared and then we say a equals double 2, b equals double 3, and c equals double 4. We will have lots of functions in this course and I'll show you when we run these what they look like in action. Here's an exercise for you. If you want to pause for a second and just practice your programming, I don't know how rusty or how sharp you are but write a Python function that takes three inputs. So three things. So x, y, and z. The function should add these three numbers together and return the sum, come up with a good name for this function. Write a Python statement that will execute the function, and use the pound sign or hashtag to add a brief description of what your function is doing. All good code is commented. Try to put some comments as needed. Don't over-comment, just enough so that the user knows what's going on. Here's another one. You have a friend sitting abroad in a country that lists the temperature in Celsius. But your friend is more familiar with Fahrenheit. You'd like to write a program that will do the conversion. Then of course, here's the formula, F equals 9/5 times degrees Celsius plus 32. Write a program that converts between Fahrenheit and Celsius as another exercise for introduction to Python. You can test your program on some values, is a good practice for debugging. Make sure that the correct inputs give the correct outputs. Zero Celsius is 32 degrees Fahrenheit, and 100 Celsius is 212 degrees Fahrenheit. Now, when you use variable names, maybe you've come across this. There are a couple that Python reserves for very specific thing, you can't call variables these things. For example, true and false mean specific things when you're testing logical statements. If it's going to be a conditional statement. If you're getting certain words that don't work, like def for these things, you're probably using one of the reserved words in Python that have specific meetings, try to come up with something else. Simultaneous assignment. Some programmers like their programs to be as compact as possible. If this is you, you can assign multiple variable values at once by separating them with a comma. Instead of two lines and say x equals 10 and y equals 15, you can say x, y equals 10, 15, and Python will know exactly what you mean. This turns a very long, lengthy programming assignment into a shorter one, and it focuses on the important steps of the process and not just declaring the variables. Try again, use this thing to shorten your original assignment. What if you're given 500 temperatures? What if you're given the three temperatures? How do you do this? How does this scale up? Try a couple of small examples. You'll notice very quickly that it doesn't scale up to large values. A variable can hold only one value, for example, x equals 10. The new structure we're going to use called list is a special variable that can hold multiple values. Now use square, brackets, to denote a list and you separate the entries with commas. The spaces between, remember, spaces don't matter in Python. Those don't matter. But you square bracket, you must open and close your list and you can have as many values in list as you want. With a list, you tend to loop through them or do some repeated action. We'll say a definite loop executes a definite number of times i.e. at the same time Python starts to loop, it knows exactly how many iterations to do. For a variable in a sequence, then you'll have your body of commands. The beginning and end of the body are indicated by indentation or tabbing over or space. This is how Python knows what the body of the loop is by spacing. These are called for loops. Hopefully you've seen a little of these before, but here's how you implement these in Pythons. Let's declare a list called values. We'll give it the three entries, 54, 76, and 100. These are square brackets that denote that, for value in values. Now listen to that for a second. It sounds funny to read, but for the entries in the list values for each one. Take the value and assign it to its double value times two. What this for loop will do, we'll go through each member of the list, multiply it by two, and replace their entry in the values list. You can then print to show your answers by using the print command, and as an important note, Python starts counting at zero. The first entry in the list will start at zero. Return to our temperature conversion and use a loop to convert some list of temperatures practice and again, there's the formula for you between Fahrenheit and Celsius. One thing you could do if you want to manipulate this, what if I don't want to iterate over every value, so you want every other value or every third, the range function allows us to specify the step size. The range function which we will use a bunch will be start, stop and step. You can start at zero, stop at 100 and you can move in steps of 10. So that's a comment. How would you implement that if you just want to go zero, 10, 20, 30, 40, etc, you'd say for I in your range, we're declaring I to be our variable that loops through the range, we start at zero, we stop at 100 and you move or jump in step size of 10. Now, one thing you might notice is why does it stop at 90? A hundred is the stopping value. When it hits 100, then the loop ends. It doesn't execute at 100. This is your full stop. If you wanted to include 100, you got to go past the 100. Return to your temperature conversion one more time and try to modify the program so it converts all the temperatures from 0-100 in steps of five. Another loop, besides the for-loop, which is a definite loop, is what's called the while loop. This is known as an indefinite loop. An indefinite loop will execute until a condition is true. This is used when you don't know how many times the loop will be executed. For example, if you're asking someone if they want to play again, you don't know if they're going to say yes or no versus I want to go through a list of 10 elements, then I know it's going to go through 10 times. If you don't know in advance, the while loop is the loop to use, the command is of course "while" you put in your condition. Here, I have x equals 10 and while x is less than 100, we print x equals x plus 3. We add three to it and print. This will go on forever until x is greater than or equal to 100. Try running this program to see how it works and keep these two loops in mind as you go through the programming assignments. Here's a practice problem for you. We need a function that accepts grades as an input, combine the average and print the average to the screen. The challenging part is we don't know how many grades the user will want to enter. Try writing a function that accepts a list of grades of unknown size, compute the average and here's my hint for you. Use the length function len, L-E-N, is a built-in Python function for determining the length of a list and use the.sum function is a built-in Python function for summing up all the elements of a list. These are just practice problems. See if you can solve and remind yourselves some of the notations we go through. Let's look at another example. Here I have x equals 10 and print x and we get 10. If I say x equals 10 and I write an if statement, if x is greater than 50, then print greater than 50. Notice this won't print, there's an error here. Can you find the error? The quotation mark is not closed on the greater than 50. Now, a list can hold multiple values, but they can't be treated as vectors. For example, if you take a as a vector or a list 1, 2, 3, and b is 4, 5, 6, multiply them together, try this. You will generate an error. You'll get an error message. It'd really nice though if we can multiply them together as you go through and we have numpy. Numpy is a library of functions that are just built out. It's abbreviated as np and whenever you import a library, you need this import statement. So we're going to write this a bunch and import numpy as np. I also write that as is, and then a equals np.array and you enter your array, b is np.array, and then you can multiply and now it uses it as you expect. It converts them to numpy arrays and multiplication is defined on these numpy arrays. Here's another example, if x is just an empty list, we use square brackets with nothing in them. You say for i in the range of 10, then you can add things to the list. We add things to a list by using the append function and we do x.append. This says append the number i times 2. We're appending the values to the end of our list, it grows as we go. You can start with the list and add elements or remove elements as well. To add elements, the function is.append. What you'll notice whenever you want to try to do something is that you may not know the function offhand. It is okay to look up stuff, it is okay to check the help functions or look things up online as you go through. I will walk you through the common commands that we use in a calculus course, but you may want to use them for other classes. Don't be afraid to look stuff up and keep track. Obviously, as you use a function over and over again, you start to remember it, you don't need to look it up as much. But we're going to go through lots of them. There's so many functions out there that if you don't know them, that is okay. This brief introduction to concepts of programming are things that are covered in every single introduction to programming course. Hopefully, some of these concepts looks very familiar, if not pause here and maybe go back and review some introduction to programming. If all this look familiar and you feel pretty comfortable with this and you got your little programs to run, then we are ready to start doing some calculus. Let's move on and I'll see you in the next video.