Posted on

Builtin functions in Python

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

Click here to subscribe for more videos like this!

Alright guys, we are going to talk about some built-in functions before we move on, and then we’re going to get onto defining our own functions, and the different parts of a function. So, let’s go ahead and open up a terminal here. I want you guys to loosely follow what I’m doing here so that you guys can get used to using these functions which I think we’re going to be using some of them quite a bit and you’re going to be using some of them quite a bit once you, once you’re done this course. So these are some basic, very basic, functions that we need to learn about, we’re not going to go through any modules like the math module or anything like that, we’re just going to go through a few basic functions. So let’s jump into a Python console here and we’ve already learned about a few so they’re just going to get a quick mention here. You can print out on the screen in the terminal, whatever you you want to basically use the print command, you can convert things to a string using the str function, and you can do this for both integers and floating points, and actually Booleans, and then for each of these types you can also convert them to their own types if they are a string. So I can use int here, and it’s going to convert that to an integer. I could use int that…oh sorry “flo(“5.6″)” I can use float to convert 5.6 to an actual number, and then you can perform mathematical operations on if you want. Now, let’s say we have a string like true, and we want to convert that to a Boolean to actually be able to test things with it. What we would do is type “bool(“True”)” and there we go that’s going to convert that to a Boolean operator. Now let’s also talk about a command called “len” Basically, what this is is it’s going to determine the length of something so it can work with arrays and strings. So let’s go ahead and try and figure out how long “len(“Hello”)” Hello is which means how many characters are in this, and there are 5. Now this command can also be used on an array. So “len([1, 2, 6, 3, 4])” just a few random numbers in there and it’s 5 digits long. Now also if you were put strings in here each with different a number of characters, you would see that this is still going to be equal to the number of items and not the number of characters within each item. So that’s how to calculate the length of an array. Now another thing you can do with an array that’s pretty neat is sort the array. So let’s go ahead and create an array which is let’s say we’re going to put some random numbers in here in you know no specific order there “[16, 3, 8, 6, 9, 133, 435, 21, 823, 45]” Now as you can see this is incredibly random but let’s say that we wanted to sort the array into lowest to highest, or you know first to last. What we would do is actually wrap it in a function called “sorted” and this will shift around the array until all numbers are in ascending order. Now let’s do the same with an array with strings instead. So let’s create some letters here, “sorted([“B”, “R”, “a”, “N”])” couple issues with that line of code there. So it sorts it but as you’ll notice the “a” is at the end. Well, why did that happen? If we were to capitalize it the “A” would that be at the beginning. So, again if we turn some of these letters into lower case which obviously come before other letters here, and one more, you can kind of see how it’s working. The capital letters are parsed first because that’s just the way it works, and then lower case letters, what if we threw a number in there? You’ll see that it can’t do that, so what you would have to do is wrapping quotes and let’s throw another number right here, and a floating-point wrapped in quotations, and you’re going to see that the way this sorted is it put numbers first regardless of if it’s a floating-point or integer type. They have to be wrapped in quotations in order to be sorted with other strings in an array, and then it parses the capital alphabet and the lowercase alphabet, so that’s kind of a priority order there. Just so you guys are aware of that. So what we’re going to do next is actually explore functions, this was just a few basic building commands in Python that we are going to use through the rest of the course, and we’re probably going to, well we definitely are going to, find other built-in functions as we move along, as we need them. So let’s get into functions.