From the course: Introduction to Python: Learn How to Program Today with Python by Pearson

Run Python code

Okay, so let's get started with what you came here for, writing some Python code. So in PyCharm, I'm going to open up the Python console. You can use whatever Python console you've got. And I've got the three lines here. I'm just going to refresh this actually. You can press this rerun button to clear everything out. Let's do the classic first code in a new language, which is printing hello world. That prints out here. We can print. What's happening here, print is a built-in function, which means that I don't have to import anything. I can just use it, print, and then parentheses. The parentheses are how you call a function, and then you put in some inputs, and our input is gonna be some text. That text is gonna be, it's called a string, and we use quotations, either single quotes or double quotes as long as the starting quote type is the same as the ending quote type. So I can use double quotes here, and that also works, okay? Technically, when I'm working in the Python console and I want to work with some text, I don't have to put print around it. But when it prints out on the next line, it is going to have those quotes. And certain things aren't going to work. When you are working in files, though, you are going to need to use the print function in order to print anything to the console. So, let's just keep using the print function. And, okay, so I've got a single quoted string here. If I wanted to say, like, I'm learning Python, and I wanted to include a single quote inside of a single quoted string, well, first of all, PyCharm is going to complain and give me a bunch of red errors here. If I try to run it, I'll get a syntax error. saying unterminated string literal. And it also gives me a little arrow for where the errors happened, if it's a syntax error. And I'll talk more about the different kinds of errors in lesson 2.3. So I can press the up arrow and keep going back in my history. So let's go back to that line that failed. In order to get this working, I can put a backslash in front of the quote to escape it and tell Python this is just a character inside of a string and not a closing quote, okay? But another thing you can do is just use the opposite quote type when you want to. So if I use double quotes, then I can put a single quote in just fine. And if I'm using single quotes, I can put a double quote in without escaping. Another escape character you probably want to know about is the new line character. So I can write stuff on new lines like that, and it'll print out 1, new line, 2, new line, 3. Another reason to use the print, though, is because if I didn't have this in print, it would just treat this as, you know, it's just printing out the literal string here with the backslash N, but once you put it inside of the print function, that's when it will print in with new lines. Okay, there are more escape characters that you might care about, but these are the main ones that you should know. Other things we can do with strings, we can add them together. So if I said hi plus bye, like that, this is doing what's called string concatenation, where it's smushing two strings together, but it's very literal again. So you could put a space there if you wanna actually see that space in the result. And this on its own is not super helpful, but then if we start to use variables, like I can use name equals Arianne, and you can put your name if you wanted. Then I can print and say, hi space plus name. It says now printing out hi, Arianne, and that name can be dynamic. I can change the value of it. So maybe I'll have name is equal to Pete. And then now I'll just click up to go back to a previous command and do the exact same thing. And now it prints out Hi Pete instead of Hi Ariane. So in addition to using the plus operator for strings on two different strings, We can also use star to multiply strings with integers or whole numbers. And we can even use parentheses. So if I said, hello plus name, all multiplied by 3. Oops. Let's add another one in here. This isn't the ideal way to do it, but it is one way you could work with strings and have expressions like this. All right, so that's enough of strings for now. We'll learn more about how to work with strings and format them and other ways to manipulate them in lesson 2. But for now, let's look at using Python as a calculator and working with numbers. So we've seen Python using whole numbers already and addition. Whole numbers in Python, or numbers in general, can be as big as you want them to be, which not every language supports that. We can do multiplication here. We can do subtraction. we can do decimal places. And yeah, if you wanted to model numbers using scientific notation, either very large numbers or very small numbers, you can do that as well. So 1.2e3 will be 1.2 times 10 to the power of 3. And then 1.2 times 10 to the power of negative three would look like that. Oh, and a straight up negative number will just have a minus in front of it. Makes sense. Okay, when we're doing division, okay, let's look at eight divided by two. These are two whole numbers, but the way that Python handles division, at least in Python three, is it returns a number with a decimal place. These are called floats, and we will talk more about them in lesson two. And so 8 divided by 2 is 4.0. If we said 8 divided by 3, or let's do 11 divided by 3, as a good example, that is going to give us 3.6 repeated. And note with decimal places or floats in Python, you might have a little bit of a rounding error at the very end. And that's common with other languages as well based on how numbers are stored in binary in memory. So 11 divided by 3 is 3.6 repeated. If we wanted to do integer division instead and get 3 remainder 2 as our result, we can do that seeing 11 slash slash 3. This is integer division, so that gives us 3. And to get the remainder, we use modulo, which is the percent sign. Okay, so that's the remainder now. To do to the power of or exponents would be like 2 to the power of 4 is 16, 3 to the power of 2 is 9. Okay so and then 3 to the power of negative 2 would look like that. And then we can and combine everything using parentheses. And the order of operations is like it normally is when you're using a calculator or what you learn in high school math. And that's bed mass or bod mass. So we can evaluate brackets first, say 1 minus 2, and then exponents to the power of 3. And then it does multiplication and division, and then subtraction and addition. And that gives us some weird number. But yeah, parentheses first, then exponents, et cetera. And then if you wanted to do some more complex math, like maybe square root, well, these are kind of the built-in things that you can do. built-in operators. But you can also import a module called math. And what is inside there? I can say math dot. And so this is a nice feature of PyCharm. It's showing me what is inside of a module. And I'll talk more about modules at the end of lesson two. And there's a bunch of functions in here. There's also a few variables. So we've got a variable of pi, okay, and that gives me pi. So modules are just Python files that you can import somewhere else. And basically any Python file can be imported. So any Python file can be a module. And this is just one that comes built in when we download Python. So we also have math dot let's see square root and square root of 25 is in fact 5 and again it always returns a float it looks like. Yeah if you were trying to use one of these and let's let's refresh this console okay if you Let's try to say math.squareRoot, and you get an error like nameError, name math is not defined. Now Python gives us this nice helpful error message, but yeah, it's because math has to be imported in a certain environment before it can be used. So it has to be imported to what's called its namespace. So let's import math, and then now we can say math.squareRoot. You can get a very, the name error is going to be one that you're going to see a lot. So say I set, if I set x to 10, so I can use variables, I can do them in calculations, them in calculations. Okay but variables are case sensitive so for example if I try to use it in a different case that's going to give me a name error or if I try to use a variable that I haven't set yet like Y that's also going to give me a name error. When we're working in the Python console you can see the different variables that have been set on the right here. Okay and inside of special variables, that's where we can see that the math module has been imported and it's got a bunch of other stuff that you can ignore as well. Okay, lastly another question I often get is do you need the spaces around your operators? No, you don't. It's still valid Python if you don't have that. It's just good practice and it's easier to read if you do have spaces and that is stated in in the style guide for Python, which I will also mention later on. That is a brief overview of the kinds of things you can do with Python just out of the box. Next, we'll look at running Python in files.

Contents