In this video we are going to talk about the difference between an interpreted programming language and a compiled programming language. Now, this may be a bit adept for the novice programmer, but just stick with us. So, first, with a compiled programming language you write your code, you save it into a file, and it is not yet executable. For example, let’s say you are writing C++ code, you write a script, and if you try to open that file with the .cpp extension (which is for C++ files), it is just going to open that in a text editor or code editor. What you need to do with a compiled programming language, is once you save your file you need to compile it into a language that the computer can read, so binary (zeros and ones), and by compiling this filing into and executable file, then you can double click it and it will run. This is where you will get the .exe file for Windows.
So, with Python you can write a script and you can instantaneously run that script without having to compile it into binary. When you run Python scripts you’re going to run it with the Python command, and then the name of the file, and what happens is you’re running the program Python which is interpreting your code in real-time. So, it does compile your code into binary, but it does what is called just-in-time compilation. The interpreter is great to use if you want to test something quickly, or if you want to debug a few lines of code.
So, we have learned about installing and using Python within MacOS and Linux, and those two operating systems have a similar process because they’re both derived from the UNIX operating system. However, setting up Python on Windows is a bit different, so in this video we will walk-through the process. First, go to https://www.python.org/downloads/ and select the latest Python 3 version. If you need to check whether you have a 32 or 64 bit system, right-click on the Windows icon on the bottom left of your desktop, and select system. Once you have downloaded the file, open up the installer and make you check the box to add Python to path, and then click install now. This procedure should work for Windows 7, 8, and 10.
I go into great detail covering how to verify that the PATH is set to the correct version of Python, and installing new packages using pip in the command prompt, but I think it is much easier to show the steps in the video 🙂
The first thing that we need to do to get into programming with Python is installing it. In this video we will discuss the three different methods for installing Python on your system. First, we will discuss MacOS because that is the system I will be using. So, what we’re going to do is open up the terminal and run a Ruby command to download and install Homebrew:
Homebrew is a package manager that enables you to install various programs without having to search for a source on the internet. If you have used Linux, using a package manage should be very familiar to you. You can find more information on Homebrew at https://brew.sh/. Next, we will run the following command in terminal:
brew install python3
MacOS does ship with a version of Python 2 pre-installed, but we will be using Python 3 in this tutorial series. To verify that Python 3 was installed, run the follow command:
python3 --version
If you are using Ubuntu, or some other Debian based Linux distribution, you are going to open up a terminal window and run the following command:
sudo apt-get install python3
Similar to Mac, you can type python3 –version to verify that it was installed correctly. Fedora now comes with Python 3 as a system dependency, and if you are using RHEL/CentOS you will type the following command in terminal to install Python 3.6.1:
sudo yum install python36u
If nothing seems to happen or if you receive an error message, check to see if Python 3 is already installed on your system. Lastly, to install Python 3 on Windows you will need to go to https://www.python.org/downloads/. You will download either the 32 or 64 bit version depending on your operating system, and proceed through the installation. I will cover how to install Python 3 on Windows 10 in the next video.
Hello everyone and welcome to the first video of this Python tutorial series. This tutorial series is going to be aimed at somebody who may have basic knowledge about what a programming language is, but this would be your first programming language. Python is a great programming language to start with! It does have it’s limitations, and sometimes it is not the right tool for the job, but Python offers people who have no experience programming a great way of understanding what a programming language is and how to use one. Throughout this series of videos we will discuss what Python is, how to get it installed, how to run a Python script, the basic syntax, and then get into the language itself. We will finish by writing a fully functional program. I hope everyone enjoys this Python tutorial series and finds it useful!
If you want to learn how to program, you will LOVE this course! This course was designed for complete beginners with little to no understanding of programming, and will give you the knowledge to get started coding using Python 3.
If you want to learn how to program, you will LOVE this Udemy course! This course was designed for complete beginners with little to no understanding of programming, and will give you the knowledge to get started coding using Python 3.
Alright guys, welcome back. We are at the end of the second section for this course which means you guys have a basic understanding of Python. You actually have enough right now to build some basic programs. Now, you might be thinking well, hold on, we didn’t even do that much. That might seem like it’s true but we’ve learned some core principles and concepts here as well as the language syntax, so far. So, we currently know how to create loops like while and if, or while and for loops, we know the if else statement, we know how to create strings and all the different types like Booleans, and numbers, and we also know how to set variables, among some other stuff. So, what we’re going to do right now is for the project for this section we’re going to put most of what we’ve learned in this section and we’re going to build a calculator program. So, I’m going to open up Chrome here and we’re just going to search Python calculator, just to see you know basically how we might write a calculator, so this looks good. So as you can see in the top of the script their defining for functions, one for addition, subtraction, etc, and each one of them is going to take a limit of two numbers and it’s going to return the result of the chosen mathematical equation between the two numbers. So that’s going to be the same for multiply, divide, everything and here’s where the actual program is going to start to output/input. So basically it’s going to say select an operation and it’s going to tell you what each number is equal to, and then it’s going to create a variable called “choice” and it’s going to use an input function which basically allows the user to type something in that hit enter and whatever the types can be stored choice. So this is going to be the prompt, so this is going to print out before the input so basically it’s gonna say enter choice 1, 2, 3, 4 and then whatever the user types after that it’s going to be stored in the variable choice, and then they’re creating two other variables, the first number and the second number, and for each is going to ask you to enter the number. So, first you enter the type of math you’re gonna be doing, and then you enter the first number, and then you enter the second number, and once you enter here what it’s going to do it’s got everything it needs, so it’s going to use an if else statement to determine what your choice was so that it knows what function to call with the two numbers that you’ve put in, which is awesome, and you guys might be really excited to build this. But, we’re actually going to go a few steps further, and the reason is if we open up a calculator you’re probably going to know that I wasn’t asked right off the bat what mathematical operation I want to perform, it just loads up with the number 0, and then I can type a number so let’s say 50, and the mathematical operator which we are going to use multiply, then two, we’re going to enter and we’re going to get a 100. Now with the ending of the math equation here you’ll notice two differences between this program and the Python script in the background there, and that’s that it didn’t quit, and actually we can continue to operate on the result of that first equation. If I want to add 75 I’m going to hit + 75 and hit enter and it’s going to add it to the result. If i want to multiply that by 2 I just hit x 2 and it’s going to multiply by that. So this is a continuous mathematical operation where the Python script allows you to perform one operation and is limited to two numbers, and it’s not very efficient, and I guess very simple. So, that’s good but we’re going to go a little more advanced. So, go ahead and open your IDE. What we’re going to do here is we are going to be using the regex library, so we’re going to import regex, and we’re going to print out the name of our program and it’s going to be called “print(“Our Magical Calculator”)” Now right off the bat we’re going to create two variables, so “previous = 0” and what this is gonna do is the previous variable is going to hold the result of the previously calculated equation, so we’re going to set it to 0 because we haven’t done any math yet, and then we’re going to create a variable called “run = True” You guys can probably guess what this is gonna do. Now, we need to create a loop for our program so we’re going to type “while run”: and we’re just going to call a function called “performMath” Let’s go up here and create a function called “def performMath” So the first thing we need to do is be able to accept input from the user to type in something, so let’s go ahead and type “equation = input(“Enter equation:”” and for now we’re just going to print out whatever they type so “print(“You typed”, equation)” hit save, now let’s run this. So I’m just going to print “Hello World” and it says you’ve typed Hello World, and then it drops back into the prompt. So going to type “Again typing something” and this is going to loop forever because we haven’t created a way to stop it other than, you know, closing the terminal window, or hitting stop right here. So, what we’re going to do now is create a way to end or quit out of this application. So let’s go ahead and “print(“Type ‘quit’ to exit\n”)” so let’s save that, Now what we need to be able to do is if they type “quit” we want to be able to you know actually quit out of the program. So what we need to do is we need access to the run variable in here, so for example let me just do this first. “if equation == ‘quit’:” let’s go ahead and set “run = false” “else:” print out whatever we typed. So go ahead and save and we’re going to run this. So we can type “Hello” and what it’s doing is this we didn’t type in quit so it’s not doing this instead it’s doing this. So let’s type in “quit” it’s not doing anything. So, basically what’s happening here is this is variable scope. Basically, to explain this this is a variable we created the top level of our program, so not defined in any functions or anything. We are in a function here and we’re in an if statement so we’re further into the code and we’ve stated run equals false. Now this is not going to have any effect on this very well it doesn’t have access to it. So, what we need to do first is actually get that global variable into this function. The way we do that is just at the top of the function type “global run” and then the name of the global variable you’re trying to get access to. Let’s go ahead and save and restart. So you’ll see we can still type in stuff but soon as I type “quit” it actually exits. So that’s what we wanted to do. Now, let’s go ahead and restart this. We’re expecting equations to look like this. So, how can we do that? Well, let’s actually how can we do that without creating a limiting set of functions that will perform operations, what if we want it to do this, you know, or what if we wanted to do this? In the script that we just looked at this wouldn’t be possible because we can only use two numbers and they’re each collected in their own variable, So what we’re going to do is we are going to use a built-in function. So actually what I’m going to do is grab access to the previous global variable as well, going to set previous here “previous = equation” then if we go ahead and rerun this, well we see it still says you’ve typed 80+2, or you’ve typed you know whatever the equation was. So we want this to perform math and what we’re going to do is we’re going to use a built-in function called “eval” So let’s go ahead and type that here “previous = eval(equation)” and then let’s save, let’s restart, and you’re going to see that before it prints out the result, or whatever we’ve put in, it’s going to evaluate it. So this is going to be able to perform complex mathematical operations from strings. So we can literally type in “80*100+42-10+78*142” hit enter and it’s going to calculate that up, and it does it in the correct order. So we all know that multiplication happens before addition and the evaluate statement our function actually is aware of that and it does that for us. You might be thinking, well that’s awesome, why don’t you know in those basic calculator examples why don’t they just use the eval function? The reason is because the eval function is actually supposed to be avoided because it can be dangerous. So, let me show you why. I’m going to print out here “print(“Hello World”)” you’ll see what happens is when it evaluates actually if we type in Python code here it’s going to execute that. So, let me go ahead and set “global run” see so we can actually crash it as well. Now this is going to work as long as we assume that our users are going to be performing math equations, but what if they are not? Well, because of this we are actually going to do this. So go ahead and “equation = eval(equation)” is going to be equal to the evaluation of equation. Actually, hold on “previous” Okay, so what we need to do first is perform regex on it, so we want them to only be entering mathematical symbols or numbers, we don’t want them to be able to type anything or to issue commands. So the way we do that is basically let’s create a new variable, actually let’s assign here we’re going to be entering our regex so “equation = re.sub( ‘ [a-zA-Z,.()” “] ‘ , ‘ ‘, equation)” “equation = re.sub( ‘ [a-zA-Z,.()” “] ‘ , ‘ ‘, equation)” So we’re going to remove everything except you know well everything important. Let’s actually go ahead and also remove the colons, we’re going to replace it with nothing, we’re going to do this equation, so we go ahead and show you what I mean. We’re going to rerun this and I’m going to say, “Hello 6 World” and what happens is going to strip everything out of that before it evaluates it, so it’s only going to accept numbers or as you can see you know the plus symbol, sorry, plus right there, let me remove that, alright. There you go. So “5+11×2” is 27 because 11 x 2 is 22 + 5 = 7. So, we’ve already got an awesome calculator but it doesn’t look quite right. So what we’re going to do is we’re basically just going to put an if statement, it’s only going to ask for you to enter equation if there’s no previous. So what we’re going to do is actually “equation = ” “” here, we’re gonna go ahead and say “if previous == 0:” it’s gonna put that otherwise equation is going to be “else:” “equation = input(str(previous))” So let me go ahead and the first time it’s going to ask for an equation, so “4+3” and then it’s is going to drop down and we can go “-1” and you’ll see that it doesn’t quite work. So, what we need to do is we need to tell it if it is, let me separate this here, there. We want to evaluate it separately if there’s a previous result or not. So what we need to do is let’s go ahead and “if previous == 0:” we’re actually going to do this “previous = eval(equation)” which is going to only evaluate whatever we type in. “else: previous = eval((str(previous) + equation)” “else: previous = eval((str(previous) + equation” Go ahead and save, restart, we’re going to go “4+4” equals 8, “-2” is 6, “*10” is going to be 60, and you can see that we’re actually using different types of math on it. So “+5-2+3” is 66. So this is what we want, but let’s just remove where it says you typed because we no longer need that. “3-2” is 1, “+56” and you can see that now we can really get into the math here. You can evem type, let me start that again, so you can perform basically any mathematical operation here, and when you’re done just hit “quit” Now we wanted to give a specific message when it quits, so let’s just drop down here and have it print out “print(“Goodbye, human.”)” Go ahead run it and we type “quit” you’ll see it now says “Goodbye, human.” and then it closes. So this is actually a completely valid program, and it looks a lot different from the other example that we looked at. Now the eval function can be dangerous OK, it very well can be, and that’s why what we’re doing before we evaluate anything is we’re making sure there’s no letters and there’s none of these characters in it, because if there were you know somebody could…let me open up this. I can’t do that, okay. Somebody could you know if they import the system and the OS modules that are included in Python they could end up damaging their system, and so by using regex to remove all that before its evaluated we’re keeping things safe. So thank you guys for joining me and congratulations on finishing the second section of this course. In the next section, in the next bunch of videos, we’re going to be learning more advanced concepts, and we’re going to be developing more complicated programs than this.
Hey guys, what we’re going to do in this video is learn how to import different modules to a Python script and we’re going to learn about regex. So, what’s a module? Basically, it’s an external library that you can include and use in your project providing additional functionality without you having to write this additional functionality. So, for example, we are going to go ahead and “import re” which is the regex library, and that’s how you import something, just type import and the name of it. Now, re is included with Python and so there’s nothing that we need to install in order to use it. So, now the basic usage, but well let’s get into regex. So I’m going to go ahead and create a “string = “‘I AM NOT YELLING’, she said. Though we knew it not to be true.” Now basically regex is not part of Python. It’s kind of like a mini programming language that you can use in basically any programming language. So I mean you can use regex in Python, in PHP, in JavaScript, you know, Java, C++, so regex is basically a way to match certain characters and then do something based on that. Now as you can see on Wikipedia here, there’s standard libraries for .net, java, python, c++, and there’s some built-in for Pearl, JavaScript, etc. So this is not Python specific and we’re only going to cover it to a limited degree as we need it. So, in this instance we’re going to be learning a few things for in the next video. So let’s go ahead and start figuring out how to use regex. So, the first thing we’re going to do is you know if I “print(string)” you’re going to see it says ‘I AM NOT YELLING’, she said. Though we knew it to not be true.” So we’ve got capitals, lower case, we’ve got a period, comma, and quotations. So, let’s go ahead and play around with this a bit. I’m going to create a new variable called “new = re.sub ” and what we’re doing is we’re instantiating the re object that we imported at the top of the script, and we’re calling the sub or substitute function on the re object, which the sub is built-in to the re object. So just like calling any other function when you call a function on an object you need to put the parameters into the parameter list. Now, how we haven’t discussed classes and objects yet, and we’re going to get to that, but this is you know we need to know this for the sake of this video and the next one now. The three parameters that this substitute function takes is the matches that we want to make, what we want to replace them with, and then the string that we’re going to manipulate by doing this. So we’ve already got the strings stored in the variable named string, let’s go ahead and cover some matches. So let’s say we want to remove all the capital letters. So what we’re going to do is basically open and close square brackets “[ ]” now rules in regex are contained within square brackets. So if we want to remove all the capital letters I mean we could go “[ABCDEFGHI]” you know we could do it that way and list out the entire alphabet, but regex actually provides a way to not have to do that by allowing us to choose a range of letters so this is going to say any capital letter from “[A-Z]” we want to replace with nothing, and we wanted to do this on the string, string. Let’s go ahead and hit enter. Now, if I “print(new)” you’re going to see that it removed all the capital letters and left everything else intact just the way it is. So we can also do the same for lower case letters, so instead of removing all the capital letters, it’s leaving everything except the lowercase letters. Well let’s say we want to remove all the special characters. So what we do is actually we could put multiple rules inside of the square brackets. So let’s go ahead and put “new = re.sub(.,\’]’, ‘ ‘, string)” let’s hit enter, print it out, you’ll see that it removed all the punctuation from the string. Now let’s go ahead and combine this with the lowercase letters, and then additionally uppercase letters. We’re left with nothing except the spaces, can’t see them here but there are spaces here. Let’s go ahead and leave the lowercase letters just so you guys can see. There are spaces. So there’s about four right there, you know, and so we’ve got spaces. So let’s remove the spaces as well. How we’re going to do that is within here “new = re.sub(‘[.,\’A-Z+” “]’, ‘ ‘ , string)” “new = re.sub(‘[.,\’A-Z+” “]’, ‘ ‘ , string)” Now let’s go ahead and, let’s add actually, so let’s go “string = string + “6 298 – 345″ ” then let’s “print(string)”, and what you can do as well you can actually create “new = re.sub( ‘ [^0-9] ‘ , ‘ ‘ ,)” what we’re going to do here is we’re just going to tell it to remove anything except numbers, we actually need to put that in quotations, we’re going to replace anything that’s not numbers with nothing, and we’re going to perform that on the string. So if I “print(new)” you’re going to see that all that remains is the numbers, so that’s the extent to which we need to learn regex in order to do what we’re doing in the next video which is building an awesome calculator.
Alright guys, so we are about to create a calculator program in Python and before we do that actually we need to learn a few more things. So we’re going to go over a couple of the loop types in Python, so there’s two. One of them is a for loop and one of them is a while loop. So basically a for loop is good for if you want to iterate over an array or a list in Python. You can do something for each item in the list, so let’s go ahead and create a list. So let’s just call it “numbers =” and we’re going to create array “[1, 2, 3, 4, 5]” So let’s type “for item in numbers:” and we’re going to just “print(item)” going to save and let’s go ahead and run this, and you’re going to see that on each line it prints out it’s number. So, we would be able to do this if we wanted to have different names in here. So, again, “[“Nick”, “Someone”, “Another Person”]” save this. Let’s go ahead and run the script again and it’s going to print out “Nick Someone Another Person” So what we can do here actually is we can “print(‘This persons name is”, item)” So let’s go ahead and you’ll see that for each one it does print out that. So that is a for loop and basically the second parameter here in the for loop is the array or the list, and then the first one here is what you want each item in the list to be called while inside it’s little block of code. So in this case we’re calling it item. So that is a for loop, now we’re going to learn about a while loop. So let’s go ahead and create two variables, one is going to be called “run = True” and the other is going to be called “current = 1” So let’s go ahead and what we’re going to do is type “while run:” and then we write what we want to happen you know if it’s currently running. So what we’re going to do is we are going to put an if statement here, so we’re going to go “if current == 100:” actually don’t need those brackets. Alright so if “if current == 100:” we are going to set “run = False” but if it’s not “else:” we’re going to “print(current)” then after we print current, “current += 1” Let’s go ahead and save this and what this is going to do is basically we’re setting run to true initially because we want it to run at least once, so while run which in this case the first time it goes over it’s definitely going to be true. It’s going to check is current equal to a 100. Well, on the first time no it’s not it’s equal to 1, so this block of code won’t run. If it’s not equal to a hundred what it’s going to do is it’s going to print the current number, and then add one to the current number, and then run is still true so it’s going to go over it again. So what we should see here is it’s going to print the numbers 1 to 99. So let’s go ahead and run the script, and that’s exactly what we see here. Now these are two useful concepts that we’re going to be using, so hold onto those, and if you didn’t quite understand what I’m doing here, let me know in the discussion section.
Alright guys, now we’re going to be learning about some conditional statements in Python. So we’re going to be learning about the if-else statement, and this basically provides a way to evaluate if something is true or false, or if something is something, or it’s not something and can do something different based on which condition is true. So let’s go ahead and let’s set “check = False” and what we’re going to do with that is we’re gonna say “if check == false:” “print(“It is false”)” and I’m going to save this and this is the minimal you need for an if statement. We’re going to go ahead and run this and it’s going to say it is false because it is false. Now if we were to change this to true, and run the same code, it’s not going to do anything, and that’s because we haven’t provided and else. So if it’s equal to false it will do this otherwise it’s just going to do nothing. So let’s go ahead and drop back four spaces to where the if statement began, I guess, and that type “else:” and we’re going to “print(“It is actually equal to True”)” So let’s go ahead and let’s run the script and you’ll see that now where this is not the case, check is not equal to false, so then it’s going to do this instead. Now, what we’re going to do is this is good if you want two conditions, but what if you want more the two conditions? Between the if and else you can actually use something called “elif:” and here we actually need to supply a condition to the elif. So think of it like another level to the if statement, and think of the else as a catch-all like if none of the above are true than just do this. So if check equals false it’s gonna do that. “elif_check == “Hamburger”:” we are going to “print (“Yummm, hamburgers”)” and let’s throw another “elif_check == “Yo”:” and say “print(“Hello”)” So let’s go ahead and save this, we’re going to run it again, and as you can see check is equal to true. So it says is this true? Nope, move on. Is this true? Nope, move on. Is this true? No, move on. Okay, well the else is going to say well if none of the above is true them we’re just going to do this. So what we’re going to do right now is we’re going to set this to “check = Hamburger” and we’re gonna run the script, and you’ll see what’s happening is it’s checking this one first, that’s not true, so it moves on its like okay this one’s true so let’s just do this and then let’s get out of the if statement, so it doesn’t check any further. So that’s what an if statement is in Python and that’s necessary for what we are about to do.