1.2 Run your first Python code - Video Tutorials & Practice Problems
Video duration:
14m
Play a video:
<v ->Okay, so now let's get to running our first python code.</v> So in Pycharm, remember that we can go to the Python Console on the bottom here. Other ways of accessing it are- view, tool windows and Python Console. So here is where we can run some Python code in interactive mode. So this is just like a quick, fast way of evaluating some Python code, but most of the work we are going to be doing later on will be running Python from files. So I can put some code in here beside the three arrows. The three arrows indicate it's ready to accept some code. So I can say 2+2 and then the next line is going to print out the results that it just evaluated. So you can see already that we can use Python as a calculator. We also have to go through our obligatory first program which is printing hello world. So in Python, it would just be using a function called print that will output some text. And then these parentheses to say that we are going to call this function and inside of that we'll put in some quotes and say hello world and then press enter at the very end. So now it prints hello world. I can also do hello world with single quotes. And in Python, that will work just that same as double quotes. So for people who already know some programming, this is called the string type. Don't worry, we will look at strings and other types in lesson 2.1. For now, all you need to know is this is how you put a string of characters together. It can include numbers and spaces and even new line characters that are um, this /n. So let's go back to using Python just as a calculator. So many of us in high school used calculators and maybe even graphing calculators and that was kind of our first foray into programming. So let's go backwards. We've got, we can do addition. We can do subtraction. I am just pressing the up arrow to go to previous commands. So that is a little shortcut you can use. We can do multiplication. Uh, We can do division. And you don't, it doesn't really matter how many spaces you have in between these characters they will be treated the same way. Um, so 4 divided by 8 is 0.5. If you are getting 0 for this then you are running Python Version 2 and we will talk about that later about some of the differences between Python Version 2 or Version 3. We will be doing this course in Version 3 by the way. So if you are running into this problem, please update so that you are running Python Version 3 in Pycharm. So we can also do something called modulo and that is like getting the remainder after division. So 3 divided by 2 is 1.5. If we wanted to get 3 divided by 2 as a whole number it would be 1 and the remainder would be gotten through modulo which is this % sign. So 3 is equal to 2x1+1. We can also do more like longer math lines by adding parentheses and it will use the same order of operations that we learned in math class. So BEDMAS. So I can do like (2+10) x8+1. So it will evaluate these ones first, then this, then add 1 at the end. You can also do exponents. So 3 squared would be 3 star star 2. 3 cubed would be 3 star star 3. And then we can also save things to variables. So say X=20. I can say X times 5 is going to be 100. X divided by 5 is 4. And there is also decimal places. So X times 3.3 is going to be 66.0. If we wanted to do more complicated math, we can actually import a math library. This is a special, this is one of the cool things about Python. It is easy to import extra bits of code that other people wrote in the form of libraries. So you might not need it all the time, but if you do need it, you can import it. So I can import math. And now I can do things like get the sin of 0.5. And then it will give me this. You can do logs. It actually includes some constants. So math.pi is getting, is saved as pi 3.141 blah blah blah. And there is also math.e. In addition to these numbers like whole numbers and ones with decimals, we can also use ones with 10 to the power of by using the letter e. So for example, we can do 3.4e10 would be 3.4x10 to the power of 10. You can see that here. You can say 0.4e-3 and it equals .4x10 to the -3 So that will come in handy when we work on our first problem. And so that problem is going to be using Python as a calculator. So we are going to work on this gravitational force calculator. If you remember from physics class, G is the gravitational constant, m1 and m2 are two different bodies of mass and r is the distance between. So we can find the amount of gravity being exerted between 2 bodies of mass. So I chose this because it's like somewhat complicated. You maybe don't want to remember it every time, maybe you just want to have a quick thing that you can change any time you want or run this calculation. So we're going to open up problems- and then problem 1-equation calculator. And let's run this code and see what happens. So to run the file in Python in Pycharm. We would right click, and then go run problem 1 equation. And you can see the only thing that is printing out is none. So there is a lot of code here, but not much is being printed out. What is being printed is just what's being passed into this print function. Currently, that is a variable called force and that is currently set to none. We want to, as this comment says, replace this with your code. So if you're new to programming, you're probably also new to comments in code. Comments are things that you can write in your code file that the interpreter doesn't interpret as actual code. It's just for humans to read. So this is code. G is the gravitational constant and it is set to 6.67e-11. This is not code. This is just something for you to read. And Pycharm makes this clear because it's in gray. To write a comment, you just have to use hash, or pound symbol and then everything after it is not run as code. In our files, I have also used this to write some multi line comments. So this at the top will kind of explain what the purpose of this code is. So now looking back at our force calculation, we have all the information here plus the equation to finish this problem and calculate the force. So I will delete this cause this won't be useful anymore and (force) = (G x mass_1 x mass_2) And you'll notice there is a suggestion feature in Pycharm /(distance to the power of 2). So these parentheses are not super necessary, but it helps break our code up a little bit and then I can run this here. See this is related to this problem. And you should get an answer that looks like this. 1.2 x 10 to the 18. So that would be the gravitational force between Earth and Phoenix. So that would be how you run a file in Python. To get back to the Python Console, you can click on the Python Console down here and we are back in interpreted mode. There are a few other ways you could run Python outside of Pycharm. One way is in terminal like we saw before. Where I can just say Python 3, 2+2 and then my code will run there. And I can exit by calling the exit function. If I had a Python file here, I could run python and then the file name. Like test.py and that would run a file called test.py. Don't worry about this command line stuff if you are not familiar with it yet. Lastly, Python comes pre-installed with a program called IDLE. This is actually how I started learning Python. And this is the shell. This is the console looks like this. You can run your code here and then if you wanted to run a file or open a file, Create a new file, print hello world, and then run this file. Run module, save as test.py. And then it runs hello world here. So each of these methods for running python are totally fine. It just depends on what you find the most comfortable. So you can now use Python as a scientific calculator, you can save equations in a file, rerun them with different values, and send your calculations over to coworkers using .py files which is the Python file name extension. So if you're into more advanced math, there are also libraries for calculus, differential equations. But trust me, you can also use Python for much more than just math. You can create websites, visualize data, manage servers, and create games and much more. We'll talk about some of these usages in lesson 2 and also go over data visualizations and creating web apps in lessons 6 and 7.