Posted on 5 Comments

(FREE) Python Programming Course on Udemy!

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.

Enroll for FREE on Udemy

http://bit.ly/2lDvi2O

We will cover the following topics in this course:

  • Python installation
  • Running Python scripts in terminal
  • PyCharm IDE setup
  • Numbers, strings, Boolean operators, lists, dictionaries, and variables
  • Functions, arguments, return values, loops, and modules
  • Final project using the information covered in the course

We hope you enjoy the course and it our goal to give you the knowledge to begin writing your own programs in Python!

http://bit.ly/2lDvi2O

Posted on 7 Comments

The Complete Linux Course: Beginner to Power User!

Get The Complete Linux Administration Course Bundle!
https://josephdelgadillo.com/product/linux-course-bundle/

Enroll in the courses directly on Udemy!

A Beginner’s Guide to Linux System Administration!
http://bit.ly/2gHV6dD

CentOS and Red Hat Linux to Certified System Administrator!
http://bit.ly/2h7ONwE

BASH Programming Course: Master the Linux Command Line!
http://bit.ly/2y4GyY8

The Complete Wireshark Course: Go From Beginner to Advanced!
http://bit.ly/2yLpqIy

Together we’re going to take a journey through Linux and give you the knowledge you need to be a power user, but first we must ask the question, what is Linux? Well, confusingly it depends on who you ask. In order to get an idea of what Linux is we’ve gotta go back in time. In the early 1980’s Richard Stallman, then working in the AI lab at MIT, started the GNU project with the goal of creating an entirely free and open Unix-like operating system. This all started when the lab got a new printer, but the license restricted his ability to modify the code. He had hacked earlier printers to electronically send messages to users who printed items when the printing was complete, as well as notifying other users when the printer was free to use. By the early 1990’s there was almost enough GNU software to create an entire operating system, however their kernel, the GNU Hurd, was not yet complete. Meanwhile, in the early 1990’s Linus Torvalds set out on a hobby project to develop Unix-like colonel known as Linux, and used GNU software such as GNU’s “C” compiler to do it. While a kernel on its own was useless, he ended up including GNU software with the kernel tree to release an operating system. Later, Richard Stallman’s free software foundation sponsored the group Debian to release a GNU/Linux distribution that was completely open for people to use and contribute to. Debian over the years grew from a small group of Free Software Foundation hackers, to the enormous community that is today. Due to its popularity, Debian has become the base of countless Linux distributions. Because of how open the software is, anybody to read the source code, modify it, and then redistribute it. Because of this, this is what we have now, it’s kind of a mess. There are so many Linux distributions that a common problem for beginners is what Linux distribution should I use. While there are a few distros out there that actually include its own software, one of the biggest problems in Linux is how many distros there are and the fact that a lot of them are the same distribution with new wallpapers, and icons, and everything else is the same. Ubuntu was started in the early 2000’s and is owned and distributed by Canonical. The base of Ubuntu is Debian, and Ubuntu has become so popular that it has in turn been forked countless times. Forking is a process which the operating system is used as the base of a new distribution. Ubuntu includes it’s own desktop environment called Unity, and has recently started distributing phones running a version of Ubuntu. Canonical also contributes bug fixes and other contributions upstream, meaning that they send these changes back to Debian to include in future releases. While Debian releases new versions sporadically, Ubuntu’s aim was to capture the stability of Debian, but released new versions more frequently. As such, Canonical releases two distributions a year, one in April and one in October. The naming convention of Ubuntu is year & month. So, the version we’ll be working with was released in October of 2015 and it’s called Ubuntu 15.10. Every two years in April, a long-term support version is released called LTS, which is officially supported for five years. While releases in between LTS versions are supported for only 9 months, the next LTS release will be in April of 2016. So, to download Ubuntu we’re going to go to Ubuntu.com, and when the page loads we’re going to see in the top navigation that there’s an option that says desktop. Just click on that, this is the version of Ubuntu we’re gonna be working with, and then when you get on the overview page, just click download Ubuntu, the big orange button in the main area. And on this page it’s gonna give us a few versions, so it’s gonna prompt us to download the last LTS release which was released 2014, in April. If you’re gonna be running Ubuntu on a server it makes sense to you long-term support versions because you only need to install a new version like every five years. You can install more frequently because there’s new LTS version every two years, but with a non LTS version there’s only official support and bug fixes for nine months. So, we’re gonna go with one of the nine months cycles here and download the latest stable release which is Ubuntu 15.10, this was released maybe 10-15 days ago. We’re gonna download 64 bit because that’s the processor type we’re running. You can either click the download button to download it directly in the browser, or you can click alternative downloads & torrents to view what other type of other files you can download. Now, if you’re running on a really super fast internet connection it doesn’t really make a difference. The in browser download is probably going to download just as quickly as a torrent, however if you don’t have an incredible internet connection, a torrent download is going to make a lot of sense. It’s going to download a lot quicker than it would in the browser. Now, I’ve already got my version downloaded, it took about 10-15 minutes, because I have really bad at the moment. Next thing we’re gonna do, we’re not going to install to directly onto our hard drives yet. That’s an awful big commitment to make when you you’re not really familiar with the system. So, we’re gonna do is go to VirtualBox.org, and this is a piece of software that allows us to create virtualized machines, virtual machines are a virtual computer if you want to call it that. This allows us to create different virtual machines, set them up differently, as well as star different operating systems on one. So, when you get to VirtualBox.org there’s a gigantic button here that you cannot miss. Click on that and it’s gonna take you to the download page for VirtualBox. Now, it offers different packages dependent on different operating systems. This is going to be for the host machine, and to put that it clearer terms, the computer that I’m in right now that we can see is running Windows 10, that is the host. So, I need to download VirtualBox 5 for Windows hosts, this link right here, click that it’s gonna start the download. Now, I’ve already got mine downloaded. We’re going to need these in the next few videos, so thanks for watching!

Posted on

Python 3 Final Project

Enroll in The Complete Python 3 Course: Beginner to Advanced!

Click here to subscribe for more videos like this!

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.

Posted on

Modules Explained in Python

Enroll in The Complete Python 3 Course: Beginner to Advanced!

Click here to subscribe for more videos like 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.

Posted on

Loops Explained in Python

Enroll in The Complete Python 3 Course: Beginner to Advanced!

Click here to subscribe for more videos like this!

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.

Posted on

If Else Statements in Python

Enroll in The Complete Python 3 Course: Beginner to Advanced!

Click here to subscribe for more videos like this!

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.

Posted on

Return Values in Python

Enroll in The Complete Python 3 Course: Beginner to Advanced!

Click here to subscribe for more videos like this!

Alright guys, this is the last video in the subsection for functions and we’re going to be talking about return values from functions. So, again this is not new to some of you, to some of you it is, so if I mean it’s a pretty basic concept basically in the past we’ve created functions that do something in which case each time the function was called it printed something to the screen because inside that function that’s what we’ve done, we’ve told it to print. But, what if we want to return a value from the function and don’t want to print it out and do something else with it? So let’s go ahead and define a function, call it “do_math” and define two variables “(num1, num2)” It’s going to be a very simple function here, and we’re going to “return num1 + num2” Now, if we call “do_math” and pass in “(5, 7)” it’s not going to do anything because we’re not printing anything out, right. So what we could do is actually let’s create a variable called “sum1” or rather “math1” and this variable is going to include the sum of the two numbers that we pass in. So we’re going to call it a second time as well, “math2 = do_math(11, 34)” and we’re going to save it. Now when I run this function, or when I run this script, again nothing happened because we haven’t done anything with it yet. So what we’re going to do now is print out both results in one print statements. So we are going to say “print(“First sum is”, math1 “and the second sum is”, math2)” So we save this and hit the run button you’ll see that the first sun is 12 and the second sum is 45. So let’s go over this again so you guys, I’m not sure if I’ve explained it well enough so I’m going to do that again. We’re defining a function, we’re calling it do math, and we’re passing in number 1 and number 2, so there’s two arguments going into this function. Inside this block of code we are only returning the sum of num1 and num2, so in the case of the first sum it’s going to be 12. So we are returning to whatever part in our code we’re at, so this is going to become 12. So math1 is equal to 12 and then math2 we’re going to call that function again we’re going to pass 11 and 34 in and again it’s going to return to the spot right here with the value that we tell it to return with. So this line of code right here is going to become 45, so math2 equals 45. Then what we do we just print it out and math1 knows that it’s 45 because that’s what the result of that function is, and so this is going to be useful for a lot of reasons later on in programming. So in the next video we’re going to create a calculator using Python and it’s going to be a command-line tool, so we’ll be able to run it and perform different math operations.

Posted on

Infinite Arguments in Python

Enroll in The Complete Python 3 Course: Beginner to Advanced!

Click here to subscribe for more videos like this!

Alright guys let’s go ahead and talk about an infinite number of arguments being passed into a function. So what we’re going to do is define a function called “print_people” and here’s where we’re going to pass in the arguments for a function but we don’t know how many were going to get, we may get 3, we may get a 100. So what we’re going to do is start off we’re going to only pass one argument into this function and that’s going to begin with an “asterisk” and this “*” tells this argument that it’s going to be an array of all of the arguments that are passed into the function, so you’re going to see more how this works in a moment. Now with an array we need some way to loop over it and so we’re going to be using a for statement and we’re going to go more into this in the future, but for right now all you need to know is you’re going to write “for person in people:” and you’re going to notice that when i drop down to the new line after putting the colon here it further indented my code which means what I’m writing here is going to be a different block, it’s going to be one level deeper than right here so, and that’s not sure why that’s underlying red maybe because it’s expecting me to type something here. So what I’m going to do is “for person in people:” I’m going to “print(“This person is” , person)” there we go. So that’s our function, we’re done with that. Basically how a for loop works is this is a list or an array as we’re defining right here. This is going to take in all of the values we’re passing to this function and create a list called people. Now you can iterate over the people list by using a for statement, so for person in people and this person can be any you could write this for item in people, but where we are using the variable name person that is going to be available in this block of code as the next person in the list and so you guys are going to see how this works. Again if you’re familiar with other programming languages this is not a new concept, it’s just a slightly different way of doing it Let’s go ahead and call the function “print people” and let’s pass it some names so “(“Nick” , “Dan” , “Jack” , “King” , “Smiley”)” I’m not even sure if this is a real persons name but we’re going to pass it into this function anyway. So what we’re doing is we’re passing in 1, 2, 3, 4, 5 arguments. Now if we knew we were always going to expect five arguments we could accept each individual argument as its own variable, however we don’t so we’re capturing all these, we’re going to create an array out of them, and the array is going to be stored in the variable called people. So we’re going to run this and you’re going to see that for each person it prints out this person is and then the name of the person. So that’s how to include an infinite number, or a flexible, it’s not really infinite eventually if you were going to pass in an infinite amount of parameters you would never be done with your program, you could pass it on to your descendants they would never be done, and so on. This is truly a flexible number of arguments. So, with that being said we’re all done with that. In the next video we’re going to be discussing return values and then we’re going to build a calculator.

Posted on

Keyword Arguments in Python

Enroll in The Complete Python 3 Course: Beginner to Advanced!

Click here to subscribe for more videos like this!

Alright guys so let’s talk about keyword arguments. In the last video we found out how we can pass only one parameter into a function if we have the other one set up with the default value. So, this will allow us to pass the first argument only there’s going to be no way to omit this and pass the age, so let’s do this. Let’s run it and it’s going to say my name is 27 and my age unknown. Now there is another keyword in Python called “none” just like this, this would be a Boolean I guess or just a keyword in general, basically this is the equivalent to null in other languages. So we’re going to save this and run this again, and it’s going to say my name is none and my age 27. There’s literally no way to only pass in an argument that is not the first argument unless you use keyword arguments, and that is defining what variable this is supposed to be. So if we do this and hit run we’re going to see that my name is someone, because we didn’t define a name, and my age is 27. Now we can also use this capacity and parameters in different orders. So I can say name “name=Nick” there let’s save that and run it and you’ll see that the order we’re passing it in it should if we weren’t using keyword arguments would say my name is 27, and my age is Nick. So, by using keyword arguments we can specify which value is supposed to go into which variable, when it goes into the function. So that was actually pretty quick. That’s keyword arguments and how to use them. I kind of like the sister concept of default arguments. So in the next video we’re going to be talking about an infinite number of arguments.

Posted on 1 Comment

Arguments explained in Python

Enroll in The Complete Python 3 Course: Beginner to Advanced!

Click here to subscribe for more videos like this!

Alright guys, welcome back. In this video we’re going to expand on this function here and we’re actually going to implement arguments into the function. So what this means that this is very static right now, nothing would change this even if we wanted to change it. I could call this function let’s say 10 times and each time it’s going to print out very static strings. So how we can change this to make it more dynamic is by using arguments and the arguments again go in the brackets here at the end of the function name, and what you’re doing is creating a variable so we don’t even need to know the value that’s being passed in we just let’s decide to use two arguments here in this function, and each one is going to be used in its own print statement. So we’re going to name it “str1, and str2” there and this basically we’ve abbreviated string 1 and string 2. It also tells us right now it’s not being used. You’re going to find PyCharm pretty smart. So what we’re going to do is change this line of code to print out string 1, we’re going to use this print statement to print out string 2. Let’s go ahead and save it and run it. We’re going to get an error and the reason is because we’re calling this function without passing anything into it, and it actually tells us right here parameter string 1 unfilled. So it knows that this function is looking for two values here separated by a comma, one of them is string one and one of them is string two, we haven’t passed anything into it so it’s calling this function and immediately the function is freaking out saying I can’t find this information. So what we need to do is pass in two strings here and how we do that is similar to how it looks in here except each one is going to be the value and not the variable name. So, the first one is going to be called “This is argument 1”, “This is the second argument which is also a string.”) Now if we save, go-ahead run, you’re going to see that this is now printing out string 1 as defined right here, and string 2 as defined right here. So, why would we need to do this? Well, let’s say we’re always going to want to print out two things. Let’s go ahead and call the function again, but this time we’re going to pass in different strings. Let’s fall back to the good ole hello world. Now we’re going to save this and what’s gonna happen is it’s going to print out four lines. It’s going to print out “This is argument 1”, “This is the second argument which is also a string.” being called from the first call of that function, and then “Stringy” , “Hello World” from the second call that function and so that’s how to use arguments. Now in the next video we’re going to be discussing keyword arguments and what those are.