Fundamentals of Programming: Python Basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello hello hello welcome to the fundamentals of programming today we're going to be covering the basics of Python so we're gonna cover a lot of things today actually the basics sound pretty basic but there's usually a lot that comes along with it so today we're gonna be talking about how you can actually print things out for an output on your actual module that we'll be using how you can input data how you can input your variables and values and things like that how you name and identify variables so that you can you know use them later as well as things like the data types that your values might have which is gonna be very important for when we try to do some operations that we will do at the end of this video again so we're gonna be covering a couple things here the basics of inputs and outputs datatypes and variable identifiers x' and the basics of operations as well as a little bit on data type conversion so 5 big topics today again these are the Python basics if you you can watch this video multiple times but they're gonna try to cover a lot of things today ok so let's go ahead and go to our Python little programming IDE here alright I'm going to be using the repple it's on line IDE their little compiler they have it's pretty nice I can type over here type my code over on the right here and then I can go ahead and run it on the output over here and I can see everything again this is kind of a really nice online tool if you want to use it I recommend it there's also things you can download I though like to use some of these online IDs just because I for what we're doing right now it's pretty much all I need so let's go ahead and try to write a first little script here let's the classic hello world except for let's put a little spin on it so there's a function in python called print now with this function it's just going to print that whatever we tell it to prints over on the right here so if i hit run it's gonna print hello so recommend maybe you code along a little bit here we can kind of print we can print a couple things print hello how are you doing and see those are on two separate lines because they're two separate prints if I were to instead take how are you doing and put it here it's going to be on the same line see it's on the same line I did one print so if I can do multiple prints I can count if I want to and notice I'm putting things in quotations now the reason I'm doing that let's see what happens when I do one without quotations so the prints the same thing the difference here is what we'll call the data type right so things inside of quotations are going to be known as strings things outside this one would actually be an integer and we'll cover those datatypes here in a minute one thing I want to show you that's kind of interesting is if we print let's say we want to print two million and I'm printing it as a string this time but let's see what happens if I print it as with the commas as a number see the difference here so notice right here two million is printed out and it's two million but when you go down here it's two zero zero now in Python variables are separated by commas so as you can see I have two which is printed out then comma 0 0 0 which python is like okay well you don't need all those extra zeros I'm just gonna print 1 0 because it's the second variable and then 3 is a nother 3 zeros the third variable and then that's this 0 here notice there's spaces in between so Python thinks that those are three individual variables so that's kind of an interesting little quirk and we'll cover some of these little things about that so when I put things inside of quotes it is connecting them they are a connected string and it'll make a little bit more sense when we talk about strings some more so I can go ahead and type a full sentence hello welcome to the basics of programming enjoy run and notice it doesn't care about the commas or the periods it just runs it all together as if it were one giant variable that's different than if we do not put it in quotations where the commas make it separate variables that's kind of the first point I want to talk about well this is kind of cool we're printing things out we're getting the computer to talk to us but let's go ahead and talk about variables because and inputs and outputs cuz we can actually do some cool stuff so that we can get the computer to actually talk to us in a much cooler way so in Python when we name a variable it is known as an identifier so it's like a keyword that we can use to store a value now there are certain rules when we give an identifier to a variable and that is it must start with a lower upper case letter from A to Z or an underscore you cannot start with a number or any of these characters like this that will cause some issues so again lower or upper case letter or an underscore it also can contain no punctuation characters like the @ symbol ampersand percentage or the hashtag and it also cannot have any spaces in it because if you put a space in there Python will probably think it's two separate variables and it's going to get really confused also if you try to name something with one of the reserved Python words like f4 or list or you know there's a there's a big long list of all of the words that you can't use that are reserved for various functions and control structures that will also be invalid so let's go ahead and take a look at some of the what's the name some variables and let's get some let's get some inputs going so we can actually store some interesting data and actually have some fun all right let's take a look at some good and bad variable names here so X is a great variable name I can assign X's 10 I could assign Y also another good variable name as 20 and then I can go ahead you know and print X plus y very easily let's do that and see what it works and it prints out 30 perfect those let's see what happens if I try to store let's say I started it with let's say my first variable I need the first variable you know something maybe this is my first variable and I wanted it to be one if I hit run let's see what happens so it says first variable invalid syntax now the reason is is because I put the number one as the first character in my identifier that is never gonna work if I wanted to name this first variable I could put an underscore before this and now it will run so notice it still prints out thirty and I still have this first variable then I could go ahead and do second variable and add those two things together and I don't know if I really want to type out first seconds hit run and then it prints out both of them so notice I can use numbers in my variable identifier but they cannot be the first characters also if I were to you know want to store my first name here and I put a space in there let's see what you guys think is gonna happen it's probably not gonna work right so let's say my first name was Bob hit run and I get another invalid syntax error right here just saying that there can't be a space so I could put an underscore there again and still saying Bob because that's not a that's a string name Bob is not defined which means if you can't store Bob as an integer or a floating point obviously so I'm gonna do it as Bob and we're good then I can print out first name run and it works so really underscores are a big help in a lot of these situations get used to using underscores they will be your good friends let's go ahead and play around with a new little function that we're gonna talk about and we're gonna call it we'll call it input so name equals inputs it's a really nice function so what you can do with the input function is Python allows you to input data is as a variable for the value of that identify so when I say name equals inputs whatever I inputs if I hit run it's gonna prompt me to give an input if I say that now name is Devon so if I were to prints name and hit run and then I were to say Billy Bob it's going to print out Billy Bob right so this name variable is stored as this input so we can go ahead and input anything we want and it'll be printed out so that's a really handy feature and we're gonna be using that here shortly so just wanted to kind of show you guys that you don't always have to when you give a variable it doesn't always have to have a hard value like x equals 10 you can have x equals or y equals inputs and then say what put this in quotations what is the height and then run and it'll ask name right so Devon what is the height in let's say 20 and then I could do stuff with that later so if you're looking for feedback from a user on your program you want to get inputs from that user right that's how you would do that so I've been mentioning things like strings and integers and floats and all these things and I know that hopefully you'll watch the video before this and you know what I'm talking about data types but I just want to go over the basic Python data types that are used and there are five of them so if we look at it there's the integer what you guys know about it's the real valued whole number the floating points which is all is a number with a decimal point in it so it's not a whole number right so that's the difference between floating point and an integer and be careful with those that can come and bite you later also there was a string and we've been using strings a lot it identifies a set of connected characters inside of quotation marks so anytime that was using those inputs and I was asking questions or I was trying to store names or you know words I had to store it as a string inside of quotation marks and that's really the key is inside of quotation marks otherwise it's not going to work so other than that there's the list which is a compound data-type that is made up of multiple items and we'll take a look at that in a second there is also things like the tuple or the tupple and it's like a list but you store it inside parentheses and once you set the tupple it cannot change so it's basically a static thing at that point then finally there's the dictionary and it's not like the dictionary you use to look up words it's like a list but each entry is made up of key value pairs which is like a dictionary you have the key would be the word any value would be the description of that word we'll be dealing with dictionaries and tuples later on in this course however for right now go ahead and put those on the side and we'll cover those later you may be wondering how you can determine what your data type is now I made this little you know flow diagram here for you that you can look at and refer to it's also going to be in your class notes so you can take a look at it later but basically you can just ask yourself a couple questions like is the variable number word character or does it store more than one value if it's a number you can then ask is it a whole number or a decimal points and that will tell you whether it's a floater an integer if it's a word or character just store it as a string don't worry about anything else it is a string and that's it's more than one individual sentence right which that would mean it's more than one value right if they had more than one set of quotes is what I mean by more than one sentence if there were multiple sets of quotes it would be more than one value and then you would have to go down this kind of more complicated branch of this flow diagram where it says can the values be changed no it's a tupple yes and then you ask do the values come in pairs if it does it's a dictionary if it does not it is a list so that's kind of a really quick easy way to figure out what your data type is shouldn't take too long and once you get more practice with it it becomes pretty easy now the next thing I want to talk about which is really really important and really convenient is just data type conversion so we are going to want to be able to convert between floats and integers and lists and strings and things like that because when you actually get an it one noticed before when we were doing the inputs we asked for an input from the user that is actually stored as a string now what's gonna happen is if you wanted to perform like let's say you are asking for the number like the height of a building it'd be stored as a string which you can't perform mathematical operations on that so you would have to convert that to an integer or float using these functions and we're gonna take a look at that an example of that in a second here so these are all just they're pretty simple you just X is whatever you want to convert and then the function is what you want to convert it to so int X would be converting X to an integer float X would be converting X to a floats STR X will be completely converting X to a string and so on you guys get the picture I did this little graphic here showing you know York you can convert between all of these and you can cycle through depending on your needs before I go ahead and go back to the programming part I want to just show you really quickly some of the arithmetic operations we can use I already used addition but you can also use like subtraction multiplication division modulus exponent and floor division and we're gonna look at an examples of each of these in just a minute here when I start programming again but the reason I'm talking about data types is because data types determine what operators you can use on your actual variable so if you are using a string you can't actually divide a string right how could you divide a word by two you can't really divide a word by two it doesn't make sense you can't the same way you can't take an exponent to a list well you can't actually take an exponent to list but certain things don't make a lot of sense so we're gonna experiment around with some of these and I'll show you guys some cool stuff that you can do with this and why data types are important alright so let's go ahead and go back to our IDE and go back to our compiler here really quick alright so we've got a number of different identifiers here in variables with various different types so x equals 10 that's an obvious an integer and notice I used the pound or hashtag sign to comment on each line and I really recommend that you get in the habit of just commenting you know maybe not every line but every couple lines just telling you what your code is doing like I could have at the top of here instead of you know commenting each line I could said declare all variables and values right because that's what I'm doing but instead I for this vid purposes the video I did it every line so I got a floating point here 1.07 again that's what the decimal points that's a different integer and floating point I have a name here which is a string and that is skip-bo and it's inside of quotations and that's really really important that you put all strings inside of quotations if I were to try to say name equals skip-bo without the quotations it's gonna give me an error because you know it's it has to be a string you can't store words without having it inside of quotations so remember that then I said Fibonacci equals this list which notice it is inside of the square brackets and it's each of the values is separated by commas so that's a list then a little bit below I had two inputs length and width and they're asking for the length and width of an object and both of these will be actually stored as strings so the length will be stored as a string so let's go ahead and see what happens if I try to add a couple of these variables together let's see what happens if I add X plus y let's first see there so X plus y oh it's gonna ask me for the length of say 10 10 and it says 11 point zero seven so what's gonna happen is that whatever I print here is going to be actually stored as a floating point so if I were to say a equals or let's say Z equals X plus y and then print prints Z in here it's gonna print 11 point zero seven so Z will then be converted to a floating points because two of the variables that were added together one was an integer and one of the floating points Python will automatically make Z a floating points because one of the variables was a floating point so if they were both integers obviously it will stay an integer but if you're adding an integer and floating point the result will be a floating point so the same thing will happen if I do Z X times y it's going to give me another floating point right so when you're multiplying or doing any kind of operation between an integer and a floating-point it's going to give you a floating-point okay that's something to keep in mind so let's go ahead and take a look let's see if I tried to I don't know let's say I wanted double and I'll say name times two let's see what happens here so and then I'll print out that double so let's see if we can multiply strings here I'm trying to multiply skip-bo by two and then print it out let's see what happens so just prints out Skippy Oh Skippy oh so you can double a or multiply strings it just repeats itself right so I could say name times four and it's gonna print it out four times so if you want to really repeat something or drive somebody crazy you can you know multiply words by a lot of words and then get craziness so that does work let's see what happens if we did divide by four let's see what happens so we can't divide strings it says unsupported operand type for division string and int so you cannot divide a string by an integer it's just not gonna work I could maybe define name by name and see what happens and see it still you can't divide strings by strings so you can't divide things in a string right that doesn't make sense how could you divide a word that doesn't really work so there's other things you can do and we'll talk about that a little bit later to grab specific you know letters from a string and those are just little handy dandy functions that we'll talk about later let's go ahead and play around with the list here so let's say test I'm just gonna name it Fibonacci times two now go ahead and take a guess for a second what you guys think is gonna happen is it gonna be like a list or is it gonna be doubling the list or like W the values or maybe it will you know multiply it might have two of these same values like it did with the string so let's see here prints test hit run and just doubles it and adds it inside of that list so as you can see it goes 1 1 2 3 5 8 13 and then it repeats that once again so we could do that 5 times 5 and it's just gonna repeat it 5 times at that point so that's kind of nice what happens if we did plus five do you think that's gonna work no it's not because the reason for that is each of these has their own index and we can't actually you know add five to each of these without using some special syntax that we'll cover in a future video if we wanted to add five to one of them we could say x equals Fibonacci and then we could say angle bracket x equals Fibonacci and we own one that it's the first one who would say x equals four Bonacci zero and we can print X which is just gonna be a single value in this case hit run and it's gonna print out zero so if I wanted to say plus four now it's gonna print out five because I added four to fit on G zero and we'll cover that a little bit more when we get further along in these videos but I just want to kind of show you guys how operators work and what we're doing here so let's go ahead and uncomment uncomment these inputs here so I'm going to say the area equals length times width right so I'm gonna get inputs here hit run what is the length it's gonna be ten well as the width it's gonna be 20 and you can't multiply sequence by a string right so again these are gonna be stored as a string so what I could do is use one of those data type conversions right I could say I want to store it as they floats right so I put all of this in put inside the float conversion right so you see this is my float data type conversion function put that the input inside of it now the input will be converted to a float so let's go ahead and do that for both of them and see if this works hit run ten twenty five five and oh I need to print out the area with my bad let's do that again ten twenty five point five and it prints out two hundred fifty five point zero so that works we just made ourselves a little area calculator so let's go ahead and review what we covered today so the first off we covered the print function which we can print out either a number or print out you know hello world and then we can protect strings like that Soviet run and it's gonna print out those things second we talked about variable identifier so those are like your names for your variables now remember we can name things with they have to start with a letter or an underscore so if I were to try to say first variable again equals ten this is going to give me a big fat red error and errors are okay by the way I just want to make sure that's clear if you get an error don't be frustrated it's just an error it's just a friendly reminder from the computer that you you can communicate with it right you just uh it didn't quite understand and that's okay sometimes you know there's a communication breakdown so don't be frustrated the if you wanted to use a number in that early on in there you just put an underscore there and that allows you to do that so you won't get an error then also if you wanted to do two words you need to not use a space you need to use an underscore instead of spaces so don't use spaces don't start with numbers always start with letters or an underscore and that will save you a lot of headaches so let's go ahead and go over some of the datatypes again so obviously there is an integer which is just a whole number and that's real valued obviously and then we also have straight or inflow ting point sorry floating points which are numbers that have decimal points in them so that's the key point difference between an injure and a floating point is the decimal point so that allows you to have greater precision then there's also a string and let's say Bob so that is a string nope automatically correcting the bass string which I do not want okay so that's a string and then we also talked about lists today and we talked about tuples and dictionaries but we didn't really cover them in this video that will be enough again that's a suit that's a video later on and those are really really cool but I didn't want to you know spend 20 minutes on this video or I'm already close to 20 minutes but you know 30 minutes on this video so and again a list you use square brackets so each item is separated by a comma so whatever you want to put in here you could and then you can go ahead and if you wanted to access those you would have to say expose list you would have to use this index and we'll talk about lists again later they are kind of they're not easy but they are once you get the hang of them they're pretty easy this is the first number in the list so again don't get can don't get frustrated if you're a little confused with what this means all I'm at that's the zeroeth end index of this list which would be one in this case so X would equal one and and in computers things always start on zero so it goes zero one two three four five six not one two three four five six when you're counting so we also talked about some of the some data type conversion so remember when you ask for an input like the height you would ask for it you'd say inputs and say what is your height and this is going to be stored as a string so if you wanted to multiply let's say you wanted to double your height say height two times two that's not gonna work because if we hit run now and I say ten did work there oh well that's because height you can multiply strings right so if I say print height hit run again 20 it's gonna say 20 if I say print double it's gonna say 2020 which is not what we want so we'd have to convert this using data type conversion so I'd convert this to a float probably so that I could maybe you know do 20.5 if I wanted to say 20 and now it doubles it mathematically rather than like a string so remember when you you can double strings you can multiply strings but it doesn't work the way you want to usually you're not gonna be you know doubling the content so that's about it we cut again we talked about variable identifiers we talked about data types we talked about inputs and outputs we talked about printing and we talked about data type conversion as well as some operations so we covered a lot of stuff today again if you are confused at all that is okay we will be covering this throughout the course and I will be repeating a lot of the things I talked about today I just wanted to give you a big broad overview of everything that you kind of need to know to get started so the best way to learn this again is just to play around on your own and have some fun with it so all right enjoy your programming
Info
Channel: Devin Saywers
Views: 8,906
Rating: undefined out of 5
Keywords: #python, #programming
Id: 5DQHlGcbIMs
Channel Id: undefined
Length: 26min 26sec (1586 seconds)
Published: Wed Apr 12 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.