Posted on 1 Comment

User defined functions 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 get into functions. Now every programming language basically has functions, I mean they’re going to be different depending on which programming language we’re discussing, even COBOL has functions but they’re radically different because that’s just an entirely different language. However, basically if you have any experience with PHP, JavaScript, functions are going to look a lot, very well very similar. So, we are now past the need to use an interpreter and what we’re going to be doing from here on out is using our IDE that we have installed. Again I want you guys to follow along maybe even do some extra repetitions of this just to get used to it. So, the first lesson here in the subsection of functions is going to be building a function, so pretty simple, we’re going to build a function, we’re going to call it, and we’re going to run the script. So let’s go ahead and drop down a couple lines, and also we’re gonna go over some pep guidelines throughout this course. We’re probably going to have a module specifically for pep, but as I go along and I see or think of anything that’s addressed in the pep guidelines, I’ll let you know. So basically what pep is is Python’s style guide, so it’s like how to write certain things and certain you know we always drop down two lines between any texts or at the top of the script. So, we would have our imports up there, and then drop down two lines, and then start coding. So we drop down two lines we’re gonna instead of typing function we’re gonna “def” and what this is telling Python is that it’s going to define a function. So we write def and then the name of the function. Now regarding naming functions there’s a specific way to do it according to the pep guidelines. This isn’t new, this specific instance, it’s using snake case for function names. So if you know camel case that’s basically like this that’s that looks like, the first letter of each word is capitalized, that’s used in Python for class names but it’s not used for functions. When you define a function in Python you’re encouraged to use snake case which is separating words with underscores, so that’s what we’re going to do. So I’m going to create a function here called my function. So basically so far we’ve got “def” which says we’re about to define a function, we’ve got the name of our function, and then we’ve got brackets right here and these brackets are going to be used for parameters which we’re going to be going over in one of the next videos, they’re also called called arguments. Now after we do this here’s the point when in most programming languages you’d open your curly braces and start to code, but instead with Python where we don’t use those curly braces we just put a colon here. Now this is going to be smart, I mean Python’s smart because it’s going to automatically determine when this function is complete based on indentation, so based on that you can probably assume how we’re supposed to use this, but we can’t enter you’ll notice that it automatically indents four spaces in. This is because anything four spaces in from the left hand side as of right now is going to be the top level of this function and then you can further intense stuff, and we’re gonna explore that as needed as we go on. So this is going to be just a very basic function that prints something out, so let’s go ahead and print out this is my function, and then we come down here, and we removed the indentation telling the function that we’re done. So now anything we write here is not considered inside that function. Now we could, we could additionally put more in this function simply by doing that and writing more indented to the fourth space in, and now also with Python this isn’t something that I’ve mentioned yet i don’t think but you don’t need to end statements with a semicolon liking in a lot of other programming languages, so just keep that in mind. Now we can drop down out of that, out of that function and now we can call that function. So to call a function you’re just gonna type the name of the function, and then any parameters that you’re going to pass in which would go in these brackets. In this case we’re not going to put anything in these brackets because we’re not passing in arguments, we’re going to be over that in the next video. Now actually you can see PEP 8: no newline at end of file which means we need a new line at the end file. You’ll see that the issue has been resolved. So PyCharm will kind of let you know if you’re ignoring or just not using any pep guidelines in your code. So let’s go ahead and save this and let’s go ahead and run, and as you’ll see down here in the console it first printed out this is my function, and then it printed out a second string. So if we look up here at the code again we’re going to go over the flow of this one more time. We’re defining a function, we’re function my function so that we can call it by that name, and we’re putting brackets here which will contain arguments at some point, you don’t have to, If your function doesn’t take arguments, it doesn’t need outside information, just don’t put anything in there, and then a colon which starts the indented block of code on the next lines which is indented four spaces. Now, I mentioned further indentation like in a function like eight spaces, 16 spaces, that’s for each block of code, and that’s not really something I can explain further, you guys are going to see it as we go on. So anyway this is the function because they’re both invented to four lines of Python knows that’s inside the function, and then down here outside the function we’re calling the function making it printed out. So, in the next video we’re going to talk about arguments and how to use those inside your functions.

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.

Posted on

How to use variables in Python

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

Click here to subscribe for more videos like this!

Welcome back everyone, let’s talk about variables now. Now you can probably guess by the name a variable is something that has variable meaning that the name the value of it is not always going to be constant. So by explicitly stating strings they’re not reusable as well. If I wanted to you know say let’s jump into a Python console, alright. If I wanted to create a string not using variables, “This is a string” I could create that. However, there’s no way now for me to reuse it to do something else so I’d have to do “This is a string” + “Hello” and then there’s no way for me to reuse that one and see you can see the problem. Now that’s a one reason why we use variables is so we can reuse the variables, now another reason is because when you pass values into functions it’s going to take on the form of variable through the parameter list of the function, we will get more functions later. Now the difference between Python and some other programming languages is some programming languages are typed, and you might say well hey aren’t all programming languages typed? Yes, you type them with your fingers on the keyboard, however a typed language basically means that you need to define the type of variable. So as we learned in the previous videos the different types are integers, strings, Booleans and stuff like that. So if you’re setting a string variable you would have to explicitly tell the program that it’s a string but with Python you don’t need to do that. All you need to do is give your variable a name and a value. So, let’s go ahead and let’s create a variable called greeting and it’s going to equal a string that’s going to say “greeting = “hello world”” Now you’ll notice when I hit enter it didn’t print anything on the screen and it didn’t do anything noticeable and it’s because setting a variable doesn’t have any type of response, so it just creates variable and it’s there. Now we can use it by using greeting. So I could type “print(greeting)” and it’s going to print “hello world” Now also now that I’ve got it in a variable we can manipulate that variable in a number of ways. So I’m going to type “greeting = greeting.split(” “)[0]” So basically what we’re doing here, and I can’t use double quotation or double equal sign there, we’re setting greeting to a new value which is going to be greeting which we’re gonna split at the space, so we’re going to split it right here, and we’re going to get the first item in there. So it’s going to turn greeting into just Hello. So let’s hit enter here. We’ve assigned it a new value so if we now go ahead and “print(greeting)” you’re going to see that it says Hello. So now what we can do is now concatenate this with something else. So, we can do that in a number of ways we can reuse the previous example where we just let’s just print it out first, we can “print(greeting + “Nick”” and it’s going to say Hello Nick. Now if we wanted to store that in a variable we could do the same thing that we did before, or you can continue using this variable for different people. So I could say “print(greeting + “someone else”” and so that’s how that works. Now if i did want to make the greeting equal to concatenate a string all you have to do is redefine it as “greeting = greeting + “Nick” Now when we print out greeting you’ll see that it says Hello Nick. So that’s a variable and the useful things that you can do with it, now you can also assign values of other types. So we can say number is going to equal 1 and then second number is going to equal 2. So now we’ve got number and second number stored in variables. Let’s do something with that. So, what we’re just going to do is add these two numbers together in a print statement. So, “print(number * secondnumber + secondnumber * number)” and this is going to print out 4. So essentially what the print statement is doing is before it prints it out it’s calculating this because we’ve stored these as integers, and so it’s going 1*2+2*1 So it’s essentially saying 1*2 + 1*2 and it’s coming up with this, so. And as well you can set “check = True” So, that’s how to set variables, it’s pretty simple and we’re going to be doing a lot of this so this is a concept that you’re going to have to be very familiar with, and get used to using it. In the next video, we’re going to be talking about some built-in functions, and we’ve already actually talked about some of them.

Posted on

How to use dictionaries in Python

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

Click here to subscribe for more videos like this!

So let’s get into dictionaries and that’s gonna end the sub-series I guess of types, this is going to be the last type that we’re going to talk about. So let’s go ahead and open up a terminal here and jump into Python. Now the previous video when we created a list we used square brackets and we entered comma-separated values in there which became our list, however in order to create a dictionary we’re going to open up sand close curly brackets here. Now in a previous video I told you guys that we don’t use curly brackets in Python and that was a lie, and just so you know there’s going to be a few of those throughout this course, so keep your eyes open for them. What I meant to say, and just to clarify, you don’t wrap code blocks in curly brackets like you do in PHP and JavaScript. Instead the only use that Python has for curly brackets is to define a dictionary. So, rather than entering comma-separated values we’re going to enter comma for separated values but each one of those is going to be a key and a value, so let’s go ahead and create one. Let’s create a person so the name is going to be {“name”: “Nick”, “age”: is 27, “hobby”: “code”} and that becomes our dictionary. Now, again, you can…you can access, I had tried to do that the JavaScript way here which you put a dot and then the name of the key that you’re looking for, in Python you do it like this kind of like when you define an index number here. If you were to do this it wouldn’t be able to find it because it’s not a list but with an array we would have done that, in here we just type the name of the value we want. So name is going to print out “Nick” if we put in age that’s going to print out “27” and if we enter hobby that’s going to print out “code” and so that’s how to define a dictionary and how to retrieve certain values from the dictionary. This is also going to become a very useful later on once we get to, well once we get JSON itself because we are going to do a video on JSON, but also once we start once we start working with a lot of data some of it’s, some of it we’re going to explicitly convert into JSON data just so we can parse it like this because this is one of the easiest ways to parse data is using this form. So thanks for watching this video and I hope you guys enjoyed it. This ends the sub section of types and in the next video we’re going to be learning about variables.

Posted on

How to use lists in Python

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

Click here to subscribe for more videos like this!

Alright, so let’s discuss lists and what they are in Python. So, let’s open up terminal again and launch a Python 3 console. So, if you do have experience with programming in other languages, let’s say PHP, you know how to create an array and what an array is. For anybody else it’s a way to to keep data organized and within one construct. So we’re going to go ahead and define an array and how we do that Python is just open up and close square brackets, and then in them we’re going to have some items and those items are going to each have their own index number within the array. So, let’s say we’re making a list of things that I like. I’m going to type “[“Movies”, “Games”, “Python”]” Now what this becomes is a list that has three indexes and we saw earlier when we split a string apart that created one of these and so you guys already know how to access items within this array, however if you forgot we’re going to go ahead and do that. So, basically I can put an index of 0 “[0]” and it’s going to print out movies. Now if we put that in a print statement, we’re going to concatenate it, “print(“I Like” +” Alright, we’re gonna get there so…let’s go ahead and print this out it says “I like movies” if we change that index number [1] it’s going to say “I like games” and if I change that to [2], oh not 3, so “I like Python” as well. Now what happens if one of these are a number? Let’s say I replace this with the number 16, let’s say “I like 16” go ahead and change this index to “[1]” then hit enter. It can’t convert the integer to string. So what we would do is if we were trying to print, and this specifically printing out an array and then choosing an index that time of recreation or list, that’s impractical and we’re never going to do things that way. So, what we just saw this error message doesn’t really matter. Basically again if you’re trying to concatenate strings with an integer it’s gonna freak out because it doesn’t know how to do that. So that is what a list is, it’s just a way to to create a collection under one variable name and when we discuss variables we will revisit this so you guys can see what I mean. In the next video we’re going to be talking about dictionaries, and basically these are like in JavaScript you can create JSON objects and that’s basically what they are. So, let’s get into that.

Posted on

Boolean operators in Python

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

Click here to subscribe for more videos like this!

Welcome back people, in this video we’re going to be talking about Boolean operators. So, what is a Boolean? Well, basically is not just a Python specific concept it’s a programming concept in general and it consists of two items, one is true, one is false. Now in a lot of programming languages you typed true as “true” and false as, well you know how to spell those, but you’d spell them all lowercase, Python’s different. So let’s explore that a bit and open up terminal here, and launch a Python3 console, and we’re going to just type in true and it’s going to return true, this is not in quotations, make sure when you’re typing true and false for truth checking that you’re typing true and false and not like this, because that will return a string which is true, yeah. So let’s talk about what these operators do. Basically, and we’re going to get into comparison operators a bit later which is like checking if something is equal to something, or more or less than, or if it even exists, or if it’s a certain type, and so you can basically perform different actions on different types of data and that’s why it’s necessary to do some fact checking with that. So, we need the true and false in programming, however we can explicitly declare true-and-false like we’ve just done by just typing true or false, or we can generate it in other ways. So let’s have a look at some of the ways we can generate a true or false. So let’s type “5 = 5” that’s true. Now when you’re comparing two objects and you’re checking if they’re equal to each other you can’t type that because the single quote, the single equal sign there, is used for a different purpose and programming it’s used to assign a value to a variable, and we’ll go over that here soon as well. So when we’re comparing to check if two numbers or two strings are equal to each other we need to put two equal signs, so you’ll see that returns true. So let’s go ahead and “5 == 4” that’s going to return false because of course we’ve all been through grade 2 math and we all know that 5 is a little larger than 4. Now you can also do this with strings and also without using equal signs at all, you can actually type “5 is 5” and that will return true, and you can type “5 is not 5” and that will equal false. So basically you’re saying 5 is not 5 and Python is saying well that’s false. So you could type “5 is not 6” and that will return true. Again you can do this with strings as well so you can type “This” is “This” and this is going to compare both of these strings to see if they’re equal to each other and it’s going to return true because yes, it is. Now we can also do this “True is True” will return true because of course true is true. Now that last one is a bit of a you know obvious result, so let’s go ahead and compare this with this “True” is True. If you think this will return true because we’re comparing true and true the results will not be as obvious as you expect because one of them is a string and the other one is a Boolean operator. So, not only when you compare two items is it checking if the value is equal, it’s also checking the type of data that it is. So, if we wanted to make the above statement return true we would have to convert true into a string. So just like you would convert a number into a string, you can just wrap the Boolean operator in the str function and it will convert it to a string prior to the comparison, and then it will return the result of true is true which equals true. Now, true-and-false again they’re going to come up in in the if-else statements because that’s where we’re really going to do some error checking and you know make sure things are set and that you know they’re not just not equal to each other, and you’re going to see more practical uses of Boolean operators later on, but this was just to show you guys what true and false is and again you should already know this without a programming background. So in the next video we’re going to be looking at Pythons version of arrays which are called lists.

Posted on

String manipulation in Python

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

Click here to subscribe for more videos like this!

Alright, so what are some of the fun things and useful things that we can do with strings in Python? Well there’s a few concepts that I want to talk about here, so what we’re going to do is go ahead and open up a Python interpreter, you can do this in pycharm by going to view tool windows, and then selecting python console, or you can open up terminal and type Python3 and hit enter. However you enter this if you want to follow along that’s probably helpful because I find if you’re told how to do something you know you kind of know how to do it, but if you actually do it yourself you have a better handle on what happens, and you’re more likely to remember how it works. So from here on out basically what I want you guys to do is to follow along with your code and do everything that I do. So, with that being said one of the first things that we’re going to do is in a previous video when we were talking about numbers I showed you how to convert strings into numbers if the only thing in that string were numbers. So, this is going to be kind of the opposite. Let’s say you have a few values, let’s say you have the number 6, right, and you want to print out this costs six dollars. You can’t, just well actually first we’re going to talk about concatenating, because you can’t do that in Python. I’m going to show you how, but first let’s learn how to concatenate a string which means had a glue multiple strings together. So we saw this actually in the numbers video as well when we tried to add numbers that had been identified as the string or yeah the string type and what it did was concatenate the numbers and made 5 and 6, when you add them together, it made it equal 56, and so your gluing strings together. So I want to say “Hello, ” then pop out of that string put a “+” and then in this string I’m going to type “Nick” Now what’s going to happen is it’s going to print out “Hello Nick” because I’m concatenating these two strings. You can also do this with however many instances of strings you want, so if I really wanted to get a little bit convoluted I could do this and this would print out hello using five strings all concatenated together. Now what happens if I want to say this costs six bucks? “This costs” + 6 + “bucks” Well, because part of this line is an integer it’s going to try and use those plus symbols to perform a mathematical operation rather than printing out a string, actually it’s the opposite it can’t convert the integer to a string so it knows that we want to concatenate some strings together but it’s like well hey hold up this is not a string I don’t know what to do with this. So what we have to do is use the string function which is similar to the int function. So how we do that is “this costs + str(six) + “dollars” and what this is gonna do is it’s going to convert the number 6 to a string so it can be used. Now you can also perform mathematical operations within the string parameter, I believe so. Let’s test this out. “This costs” + let’s go str(6 + 5) + “dollars” and hit enter. Yeah, so it can perform mathematical operations within that parameter. It’s going to convert the sum or the result of whatever mathematical operation you are performing on it, it’s going to convert that to a string and then use that string to be concatenated with the other strings that you’re working with. So that’s how to use the string function and maybe this doesn’t seem like it’s going to be important, or maybe it does but you’re like well how often do you use this, you know. We’re going to see because when we get into defining some functions we’re going to be using both strings and integers to return certain information and so this is going to be important at that point. So, that’s how to convert integers to strings, it will also work for any other type. So if you have true, we’re going to get into that after actually, but we’re going to be using this string function again at a later point. Now what happens when you want to do the opposite of concatenating strings? So, think of concatenation, I’m not sure if that’s the appropriate term, think about contaminating two strings as actually let’s look that up. Now it’s gonna open up xcode just go in to Google here, because if I’m saying that wrong I want to know. Yeah, okay, concatenate, good stuff. Not sure if you guys heard that but I’m using that word correctly, good stuff. Alright, so anyway back to the code that’s how you concatenate two strings together. What happens when you want to do the opposite, you want to split strings apart? So, let’s say we have this string like this so we’re going to say “Hello:Nick” we’re going to separate that word from the other word with a colon so that we actually have a character that we can use to split. So what we’re going to do is after the string we’re going to put a “.split(“:”)” going to call a function split on it, and the split function or method takes one parameter, well for the sake of this video takes one parameter, and that’s where you want the string to be split. So we’re going to split it at the colon there and hit enter. What it’s going to do is it’s going to create an array with all elements that had been split from that point, so for instance it split it into two items in the array but let’s say I did this it’s going to have an array with three values in it, and then we can reference those values by its index number, so this is going to be a bit more complicated. Let’s say we’re splitting it at this so let’s concatenate that lets say “My name is” + so we want this to print out my name, and not hello world, and not anything else, so what we’re going to do is put 2 square brackets and the number one “[1]” and it’s going to say “My name is Nick” I’m not sure if that’s a bit too complicated, so I’m going to try and explain it again from the beginning. So, we have a string, we have it say my name is and then a space, and then we can concatenate it with the result of this right here. So this can be broken down into a few parts, here’s the string that we’re performing the split function on okay, then we’re calling split on it and this is going to turn this string into an array with three values each with an index. It’s gonna split it into hello, Nick, and world and stuff those values into its own array, and then we can access the result of this by using an index number here in square brackets so we’re calling the index number one, so hello would be 0, Nick would be 1, and world would be 2. So, what’s happening is it’s printing out my name is Nick. Now you might be thinking shouldn’t it be printing out my name is hello if we’re using the number one here? This isn’t so much Python but it’s a concept in programming in general, no. So, 0 equals 1. If you have an array the first item in it you think would equal 1 but it actually it equals 0 and we’ll talk more about arrays, which are actually called lists in Python, we will talk more about those coming up in you know I think not the next video but the video after the next one. So that’s how you can manipulate strings in a very basic way, we might expand on this in the future, we actually probably will. So, next we’re going to be talking about Booleans.

Posted on

Complete BASH Linux Programming Tutorial

https://www.youtube.com/watch?v=6hAeBJvAM2A

Enroll in the complete course on Udemy!

https://www.udemy.com/bash-programming/

Welcome! Here you can learn you how to master Linux command line (BASH). What do I mean by this? Ever seen a system admin in a movie or some devious hacker using a terminal? Do you want the same knowledge? Well you are in the right place!

This course is designed to take you from beginner to advanced in the Linux command line using bash (Bourne-again shell), a Unix shell and command language. You get 10+ hours of HD videos full of useful tutorials founded on motivation and excitement for learning bash programming! You can count on an answer to every question you ask in the course from a Linux command line expert which gives you peace of mind that you can get help with every challenge you have as you learn!

Posted on

How to use strings in Python

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

Click here to subscribe for more videos like this!

Alright, so what is a string? Well, a string is any text that you want to be treated as text within a program. So again we need to talk about a few other things that we’re going to be talking about actually a few videos from now, but for right now just trying to follow along. So, this is a string because it’s wrapped in quotations, and if we hit enter Python is going to say hello string. Now, you can also use single quotations here to do the same thing. So basically you have two options either using double quotations or single quotations, and let’s say you want to have a word in your string that you well let’s say you need to have a word in that string of text which contains an apostrophe. So let’s say we wrapped in single quotes and we say ‘don’t do that’ Python is going to freak out about that because what it thinks is happening is that we’re starting a string here, we’re typing three letters, and then we’re ending the string, and then it doesn’t know what to do about all this. It’s saying well t do that I don’t know what that means, it’s no variable, and there’s no function that I can perform on this, so I’m just going to freak out and give you a syntax error. So what you would need to do in order to print out don’t do that is you can just wrap it in double quotations and then just type don’t do that, and as you can see what I print it’s going to keep that intact. Now alternatively let’s say you want to quote somebody. So, let’s wrap in double quotes and say, “she said “I want this” and then close it, close it. Again, it’s gonna freak out because what it thinks that we’re trying to do is start a string, say she said, then end the string and then it’s going to freak out over all this because it doesn’t realize that this is part of that string because we have explicitly told it to end the string here. So what you would do in this instance is just wrap the entire string in single quotations. So, ‘she said “I want this”‘ and as you can see it’s going to print it out correctly. Now alternatively what if she said something that had an apostrophe in it. So, let’s try and cope with that so ‘she said “don’t put that there”‘ again it’s going to freak out because we’re starting the string here, we’re adding a single quotation which is not interpreting as the other string, in fact right here where this apostrophe is it thinks that’s the end of the string, so it’s treating this like a string and then this as constants, or variables, or whatever and it doesn’t know what to do with them because we’re not telling it to do anything and then these just random quotations here. So what we need to do in this instance is we need to learn about escaping certain characters. So, let’s write it out first then I’ll explain it. So what we’re going to do is wrap it in single quotes and we’re going to say ‘she said “don/ ‘t do that”‘ and what’s happening here, let’s hit enter. That’s not what…let me that out. It’s just returning what we wrote there. There. So we print that out and what this does is the backslash here tells the next character to be interpreted as text regardless of what it would normally be interpreted as, so rather than this trying to end the string Python knows at this point that we want this to be printed out with the string and not end the string but just printed out as a single character, and so that’s what it’s going to do. So there’s times when you’re going to need to do each of these in your Python code and so that’s how to handle strings and what they are. Again, a string is just a series of characters which is interpreted as text and you can print it, you can manipulate it, we’re actually going to get into string manipulation in the next video.

Posted on

Learn Python Episode #6: Numbers

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

Click here to subscribe for more videos like this!

Alright, so before we actually get into functional programming, or object oriented programming for that matter, with Python we need to discuss some basic types, and then variables, and some other stuff. So in this video we’re going to be covering numbers. So, what is a number? Well, I’m sure you guys know what a number is and I’m sure we’ve all been through grade primary and one so we’re all familiar with that. Now there’s two different types of numbers, one is called an integer, and then a floating point. The floating point is basically any number that has a decimal included in it. So let’s go ahead and write out some numbers on the screen. Now I’m going to be using the Python console down here for this video so watch down there, that’s where stuff is going to happen. So, for an instance of a number we can just write a number, it’s going to appear in the console, or the ID, or whatever you know has syntax highlighting that you’re using it’s going to show as blue or it should you know, actually that really depends on your color scheme that you’ve got going on, it’s going to show a specific color. Now, we can just use numbers in Python just by writing the numbers and if we hit enter it’s going to print out 5. Now with numbers we can perform mathematical operations on these numbers as you saw in some of the previous videos that we’ve done so far. So I could write 5 + 6 and we’re going to get a response of the sum of 5 + 6. Now what would happen if I wrapped these numbers in quotations? So let’s go ahead and write “5 + 6” if you think that this will produce 11, you’re wrong, It’s going to produce 56, and you might be thinking, hold up, 5 + 6 does not equal 56. You’re absolutely right. Not in math, however when you’re concatenated strings it does. So basically when you wrap something in quotations in Python, Python is going to treat it as a string, not a number. So if you’re trying to perform mathematical operations just remember don’t wrap your numbers in quotations. Now additionally we can use floating points to perform math operations, and you can see it returns a floating-point. Now if we ran 5.5 + 5.5 it’s going to still return a floating point, however you know it’s 11.0 because we’ve passed it, you know, decimal floating-point numbers and so it’s going to return with one because that’s just how it works. Now, we’re going to talk about how to convert different types of types, I guess. These are called types. A number is a type, a string is a type, and right here we’ve got, we’ve shown, both numbers and strings and we’re going to cover strings in the next video, but for right now let’s say well I have some numbers but they’re wrapped in quotations and I still want to perform mathematical operations on them, how can I do that? So, basically in Python there’s a built-in function called int which will convert a string to a number if that string contains only numbers. So let’s look at a correct usage of int and then an incorrect usage and see the output for both. So let’s say I have the number “5” wrapped in quotations, what I’m going to do is wrap that in the int function and this is also how you write a call to a function in Python, and then I’m going to perform a mathematical operation on it with another number wrapped in quotations. Now this is not going to produce 57, in fact it’s going to convert both of those into integers and then perform the math operations on them. So that’s how to add up different numbers that are contained within quotations, let’s have a look at trying to convert text to an integer. So let’s write “int” and let’s write “(hello)” and hit enter. As you can guess it’s printing out an error because there’s an invalid literal for int with base 10. Basically, what that means is we’ve passed it something that is not an integer. So think of this function basically as it takes whatever you put into the function, it removes the quotations, and then returns that. Same if I just type this, Python is going to freak out because it doesn’t know what hello is because this is going to be interpreted not as a string, but it’s going to be interpreted as computer code, and we’re going to get more into strings in the next video.