Python Data Types for Beginners | Python tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome I'm Dave today we will review Python's basic data types and I'll provide links to all resources in the description below we are ready for Lesson Four and I have Visual Studio code open you can see I have a folder to the left that is named Lesson Four and that's what you'll find in the course resources now let's create a new file in this folder I'll click the little plus icon up here for a new file this is a good time to talk about naming conventions for files with python we use all lower case so I'm going to type data and then if we need a space we do not use dashes or hyphens if you will we use underscores and then we continue to type in lower case so I'm naming the file data underscore types dot pi and then press enter and there is the file that we'll work with now that we have the file you'll see the play button up here that we'll use to run our code and the associated menu that can show with with that so let's begin by typing inside the file and I'm going to just put a comment here that says string data type it's the first data type that we're going to cover and I'm going to talk about the literal assignment of value so I'll type literal assignment here in another comment and I'm going to type a variable named first and I'm going to assign the string Dave this is literal assignment so I literally assigned this value Dave that is a string value to the variable first I'm also going to create a variable named last and I'll put my last name in there these are both string values now we can check this in Python if we use a print statement and then we'll use the type function and I'll pass in first so that is the variable first that holds the string value Dave and we'll see that when we check it so I'm going to press Ctrl s to save the file you could also do that from the file menu and then find save here here you can see it's control s after that I'll come up here to the little menu by our play button and choose run python file now it went ahead and opened the terminal window for me when I did that and you can see the output says class string this Str stands for string okay after that I can close the terminal and it will reopen it when we run the file again there's another way that we can check for this value so I'll once again type print then I'm going to say type pass in first let's see if it is equal to and that is 2 equal and then Str for a string and besides that let's do one other one that we can also check now I'm going to do shift alt and the down arrow that will just copy this down and then I'm going to change what's inside of the print there is an is instance function that we can use and with this I'll pass in the variable first and then we're going to say it is an instance of what and in this case we're checking for string so now we'll see the output of all three of these print statements once again Ctrl s to save and then I'll just click the play button to run the code and I'll need to pull this up maybe to see all of the output and I'll scroll just a little bit there we go so we see string that we previously had here and then true true so the output of the following two statements is type first equal to string and the output is true and then is the first variable an instance of string with this is instance function and that is also true okay now that we have looked at literal assignment let's look at another way we can assign a value and that's with a Constructor function so I'll make a comment for that now I'm going to Define pizza and this variable is going to hold a string that says pepperoni but I'm going to use the string Constructor function so it's Str and then parentheses and inside of this I'm going to pass in the string pepperoni now after this I'll copy all three of these and I will paste below so I copied with Ctrl C after I highlighted this and I paste with control V the letter V is in Victoria okay then I need to change this word first to Pizza so I highlight the first one and then Ctrl D to select the second and the third instance of first now I can just start typing pizza and it will change all three so now we're going to check the output for pizza as well and I can highlight these also and press Ctrl and the Slash and it will comment these out so we only see the output for our pizza now after I save once again with Ctrl s now clicking the play button and we should get some output that we once again see in the terminal and I'm going to just leave that terminal as a little bit larger so we can always see that there we go so it is class string and true and true once again so now we checked pizza here and it said class string and then this said true where we checked the type if it was equal to string and then this said true where we checked if pizza was an instance of string so now we know two different ways to assign string data to variables and we can do this with other data as well so we have literal assignment and Constructor functions that can assign data now I'm going to comment this out as well again highlighting this and pressing Ctrl and the slash will comment these out now when you find these in the course resources you can do the same thing to uncomment so you could highlight the lines you want to uncomment and press control and the Slash and they'll once again be uncommented so you can toggle between the two by doing that now let's discuss concatenation and that means adding two strings together to form a larger string and that's what we will do so we will type concatenation here and after this let me Define full name as a variable and I'll use my values from above so I'm going to set full name equal to first and then I can use the plus operator and then I'm going to put an empty string here with one empty space and then plus again and type last and this concatenated my first name with the space in between to my last name so now we could print full name there we go and save once again press the play button you can see my full name here inside of the terminal now we can also add something to the end of full name if we want to so I will say full name and now I can use plus equals and this is going to take the value of full name and add to it and I'll just put an exclamation mark here at the end and one once again print full name and if I run the code with the play button we should see my name now with an exclamation mark here in the terminal you can also take a number and change it to a string and this is called casting so here I'll say casting a number to a string and let me assign a variable name decade with the value and I'll use the string Constructor here and I'm going to pass in 1980 so I had a number 1980 the value and I am now casting it to a string with the string Constructor after this we'll go ahead and check this value so I'll say print and type and decade and then after that let's go ahead and just print this out to see what it looks like after it has been casted as well and now let's check those values and you can see that yes that type is a string and there's the value but it's a string here it is not a number and that is 1980. and now this is important if I wanted to concatenate that value inside of a sentence I would need to cast that number to a string as we previously have here so now let me create a statement so this is a variable name statement and I'll just say I like rock music from the now I'm going to concatenate that value so the plus operator and then I'll just pass in that variable decade another plus operator and then the letter S and a period at the end so now let's print our statement and save with Ctrl s and run our code and we can see we've have the statement I like rock music from the 1980s but what if we wanted a statement with multiple lines we can do that as well so I'll scroll up for some more room and underneath this I will just leave another comment that says multiple lines and now we'll take a multi-line I'll just call this multi-line for the variable and we want to start with three quotation marks either double or single will work so I'll put three quotation marks and notice vs code wants to help me so it already automatically added the closing three after this I'm going to press return and leave a few lines between the opening and closing quotation marks again three at the beginning and three at the end here I'll say hey how are you with a question mark now let's add some spaces with the space bar press return again and again and then another statement I was just checking in and then another space or two or three and then return once again and now I'll tab over five or six times and say all good question mark and then notice we have a blank line after as well let's save that with control s and now after we do that let's look at our statement in the output when we print multi-line save once again and run now we have this big statement that is on multiple lines and of course it has white space before and after some of the things as well we'll come back to that white space in just a little bit now let's talk about escaping special characters also because sometimes we have special characters inside of our code we just started this multi-line statement with single quotes and what if we were to use single quotes around a sentence we created so here I'll leave a comment escaping special characters and now I'm going to create a sentence variable and I'm going to set this equal to a value that is inside of single quotes there we go single quotation marks and the word I'm so this would have a single quote in the word and notice vs code doesn't like this we need to escape this single quote if we want it to be in our statement because right now vs code just thinks the value ends right here with the letter i it would not include the letter M so let's put a slash here and it's a backward slash and that escapes that single quote so now it knows that it should be part of this phrase so now we can use I'm in our sentence and I'll say back at work exclamation mark and then I want a tab after back at work instead of a space we can indicate that with another backward slash in a lowercase T notice how vs code leaves this as red so that's a special character so now we have a tab in our sentence and I'll just put the word hey with an exclamation mark again and now another backward slash and a lowercase n that indicates a new line that is the new line character so that will make everything else that follows it be on another line let's put two of those just so we can see what really happens there then I'm going to use the word where's so it once again has a single quote that I need to escape then the letter s then this then I want at slash located and I want to put a slash between at and located but as we know already we're using slashes here for special characters so we also need to be able to escape that slash and so we do that by adding a second slash but only one will appear in our sentence and then I'll put a question mark at the end and let's go ahead and print this sentence out and see what we get Ctrl s to save and we run our code and here we have our full sentence I'm back at work Hey where's this at located once again it put in the tab it put in the line breaks and we were able to use the single quotes even though we had wrapped our full sentence inside of single quotes I'm going to scroll up for some more room and then I'm going to leave another comment because we're going to begin working with string Methods now methods are functions that are called on the string class and that's not too important right now but we do want to see what these methods will do for us so let's start out just by looking at the value that we once again have in the variable first but after that I'm going to put another print line and say first dot lower and we'll see what it does to the value that is in the first variable and we'll also note that it won't actually change the value in the first variable it will just return another value and then instead of typing that all again I'm going to use shift alt in the down arrow and do that twice and I'm going to change lower in this second one to Upper see what happens there and then I'm just going to remove lower from this last one so we can once again see the value of first and note that it did not change Ctrl s to save and click the play button and now here's our value of first which is my first name Dave and then after we called the lower method that we see here on line 49 we then had the lowercase value of my name and then first Dot Upper and we had the all uppercase value of my name then we once again just checked the value that was in the first variable and it is still left untouched as it was let's look at some more string Methods and instead of using my first name here in the first variable let's use that multi-line variable that we created so we'll now say print and then inside of this print I'm going to use multi-line and I'm going to use the title method at the end of this now this method will turn everything to proper case and capitalize using the first letter of every word in that multi-line phrase but I'm also going to print another version of multi-line so we should see it twice and in this version I'm going to use the replace method I'm going to replace the word good with the word okay and we should see the difference there and then finally I'm just going to print multi-line so we can see that the original value did not change so I will save that click play and of course we can't see all of that at once I'm going to make this terminal window take up the full screen for a moment we'll see three versions of this so here was the version that has the proper case where we called the title method so it says hey how are you I was just checking in all good the second version used the replace method and we changed good to OK and then finally we printed the original version that you can see did not use proper case with the title method and still contains the word good now I'll go ahead and close out this terminal window for a moment and we talked about all of that white space that is in our multi-line variable so let's work with that now I'm going to call Print once again and I'm going to call the length function which is just l e in and I'm going to pass multi-line into this so we can see what the original length of our multi-line string is so it will count every character after that we're going to change the multi-line variable and I'm going to use the plus equals to add to the end of it let's just add a lot of extra white space after that let's go ahead and once again change the multi-line variable and I'm going to add some space to the beginning of it and we'll do that in a different way instead of the plus equals I'll use the equal symbol I'll add some space and then at the end I'll concatenate the value that we have in multi-line currently and then after that let's once again check the length so we'll check the length function with Len and then we'll pass in multi-line so we're checking the original length we're adding to the end and the beginning and then we're checking that length again and if you want to you could even check in between where you'd get a value that only has it added to the end and not at the beginning so now let's run this and see the values that we get so we start out with 126 and after we add all of that extra white space at the beginning and at the end of the variable it's 184 characters now of course this white space is undesirable but I'm adding it as an example so now let's look at some string Methods that will remove that white space if you had some values that you needed to remove white space from so I'll say print once again and now we'll say multi-line dot strip and that should remove the white space if we wanted to remove it but this will of course print out the output it's not checking the length if we don't add length and we won't really be able to see that because you can't see white space so let's go ahead and just check the length here Len and we need to wrap that in parentheses and now I'm going to do something else I'll do shift alt in the down arrow twice there are two other methods we can check so let's check if we only remove the white space from the left side and that is with L strip let's also check if we only remove the white space from the right side and that is our strip so if we check all three of those then we'll see different values so let's run this code with the play button and we can see the last three here if we removed well all of the white space it took it down to 123 and remember our original it had 126 up here before we added the white space so this actually removes some extra unneeded characters as well and then when we only remove the white space from the left side we still had 165 characters when we only removed it from the right side then we had 142 and I think I'm saying that in the correct order yes we removed the right side last and there are many more string Methods and I'm going to leave a link in the course resources where you could go to the documentation and find all of the available string Methods that you can experiment with right now let's just use a few more string Methods to build something just so we can apply this in a small way for now let's build a menu I'm going to set a title variable equal to menu notice it's all lowercase value and we can call string Methods directly on the value itself it doesn't have to be called on a variable name as I have been showing you so here I'm going to say menu Dot Upper and call a string method directly there and that will be stored in the title variable after that I'm going to print the title and I'm going to call the center method I'll pass in 20 characters and I'm going to pass in a value that is an equal sign let's see what this does if we just return this much and I'm also going to leave a empty line here so we do not actually see everything that we've had above pressed right up against our menu so Ctrl s to save and now I'll run the code with the play button and you can see what we've got so we called a center method and it centered our word menu that was set to all uppercase and it used that character I passed into the center method to fill out the rest of the space so that's a very interesting method to call now let's call another one that is equally interesting so after this I'll say print and I'm going to use the string coffee and after this I'm going to use l just which means left justify I'm going to give this 16 characters I'm going to have it fill out the remaining space after coffee with periods and then I'm going to concatenate inside of our print statement and I'm going to say one dollar but I want it to be a string so the dollar sign and the number one and I'm going to call the right justify method on it I'm going to say four characters there is nothing that I want to fill out the rest of the space with so I'm just going to pass in one argument here or one value I should say to this right justify method so after I do that and save let's see what we get in our terminal once again when we click run now our menu has coffee and you can see it filled out the space that we gave it for 16 characters and then we have our dollar that is right Justified over here let's go ahead and complete our menu I'm going to scroll up and once again as I've explained I'm going to use shift alt and the down arrow to copy this down twice and now let's just change our values so I'll double click copy and change it to muffin and in the last line I'll change it to cheesecake and then here let's just change our values to two dollars and let's go with four dollars Ctrl s to save and we run our code and we have a nice little Coffee House menu let's close the terminal and let's look at just a few more things with strings and one thing to discuss is string index values so I'm going to say string index values here in a comment and just to keep our menu separate I'll once again print empty string here okay after the string index values comment let's look at string index values I'll come back to that first variable that we have and if we just want the value that is at index one do you think that will be the D in my name it would seem to be if we say we want the number one value right the very first value well that might not be what that indicates let's go ahead and press play you can see it actually Returns the second letter and that's because indexes start at zero so if we wanted the first letter we would pass in a 0 right here let's copy this down with shift alt and the down arrow and let's see what minus 1 gives us so if we save now and run our code we have the E which is the last letter in my first name so anytime we want the last value in the string we can reference the minus one index and that could be very useful if you didn't know how many letters were in the string you were working with but you needed to get that last value you can also use a range of values so here instead of just one or minus 1 you can start with the index say at the first position well it's actually the second position but at the one position and then we can say we want to end at the last position but this might not deliver what you think either when you provide a range let's go ahead and run this you see we just get the AV so the value you give at the end of the range will not be part of the output so you need to consider that when you create the range and if you want to go all the way to the end you just provide a range without providing the last value at all like this and now we should get the a through the end of my first name run this yes we get Ave when we don't provide that final value and now when we've previously checked what data type we had some of those checks were returning true and that is Boolean data let's look at a couple of methods that also return Boolean data and I'll just say some methods return Boolean data and if you're wondering what Boolean means it just means we're working with true or false data so if we print here and say first starts with now I could say does the value in the first variable does it start with the string D well that should be true because my name starts with a capital D let's copy this down so we can check two things at once now we can say does first ends with and now we'll check the letter Z so let's see what result we get oh it does not have two s's and notice Visual Studio code will help you with that how it this in my theme at least it turned purple here and if you're not using the same theme it may turn a different color but overall if I mistype something then it doesn't look right it doesn't have that color that starts with does but if I remove the S there you can see it identifies the method likewise and I'll just remove this for a second when we're working here in Visual Studio code you can just type the period and suddenly a list of all of these different methods can show up including the one I want which is ends with and you can search for them that way as well so now I'll just put in the letter Z again save and we'll run the code and of course we get true that yes first starts with the letter D at Capital D but false it does not end with the letter Z so now since we've brought up Boolean data let's quickly discuss that and I'll just leave another comment here that says Boolean data type and now after this I'm going to set my value equal to true now notice the true has a capital T if I remove that and just put true Visual Studio code tells us something is wrong here it needs to be proper case with a capital T or faults will not work either it needs to be capital F for false so either way you do it just needs to be proper case whether you choose true or false now after this I'll set another variable just the letter X I'll set it equal to Bool and I'll pass in false now this is the Constructor function once again so you can set this data type in either way and once again we can print say type and check the type of X and we could also say print and say is instance that we see right there and then we could pass in my value say is it a boole value we should get true for both of those let's go ahead and run the code and well we didn't get true it just said what type it was actually we didn't check that against a value like Bool so yes we get the output here what it is then yes true for the second one but just important to remember that Boolean means you are working with true or false data let's move on to the numeric data types and there is definitely more than one so we'll start here and I'll just say numeric data types and the first type we'll work with is integer and we have seen this in previous lessons I'm going to set price equal to 100 then I'll set best price and I'm going to set this equal to the Constructor function and pass in 80. so you can set an integer in either way and just like we did in the past as a matter of fact I could just copy these lines Ctrl C and then down here Ctrl V to paste I could change X to price and we should see that it Returns the type integer and then here I could change this to best price and we'll compare it not to Bool but to an integer type and we'll see what we get for the output and yes it is a type integer and yes it's true that is an instance of integer after that let's do some more with numbers and we'll look at the float type float types have decimals so a grade point average GPA set that equal to 3.28 that would be a float type and that is a literal assignment just like we did with price up here but we could also use a Constructor but it almost seems pointless because you still type the flow so I'll just say y equals float 1.14 and of course this is a float data as well we just had to type a little bit more so here we could say print check the type GPA and it should return float and of course we could do is instance as well if we wanted to but let's see what we get yes it is the type float now there is one more numeric type to quickly mention it is not a type we will use in these basic lessons but I should bring it up that's the complex type and it's often used in electrical engineering it uses J notation so here we'd say comp value equals five plus three J I know that looks a little strange especially if you're not into engineering we can print type and we'll check the type for our comp value but now a complex value has a couple of property values we can check so let's go ahead and do that just so you see how we can say comp value dot real we can also say comp underscore value dot image which stands for Imaginary this is based on a real number system and it has a real number value and an imaginary number value once again if you're not into engineering it may not make a whole lot of sense but python has so many applications it's pretty awesome that this is built in so let's go ahead and run this see what we get back yes it's a complex type and you can see when we return those individual property values they are float values but they are part of the complex data type okay I'm not going to go over all the different mathematical operations that you can do we have previously covered several of those in the past lessons so no need to rehash that we went over those especially with operators in the last lesson but there are some built-in functions for numbers so let me just highlight those with a comment this way built in functions for numbers and let's print one of those out let's print the absolute value of the GPA that we previously created and remember that was a float type I think we said 3.28 so let's see what the absolute value of that GPA value would be it is 3.28 so no change there but once again this is checking the absolute value now let's see if we did something else with that that we could check we could say print and we could say round pass in the GPA there and then let's go ahead and do one other let's print round and say GPA and we can specify the decimal so the difference here would be this would round to the nearest integer this would round to the nearest decimal place that we specify so we have two decimal points here with our absolute value and you know what I should check this as well by saying print and since that didn't change before we could say GPA multiplied by minus one so you would expect to have a negative 3.28 but the absolute value will probably not be negative so now we can check all of this let's run and yes we got 3.28 twice without the negative although that would have been expected now the round rounded down to the nearest integer which was three but then when we specified the decimal point it rounded up from 3.28 to 3.3 so those are just a couple of built-in functions that you can use with numbers without importing a module but there are some modules that provide many good mathematic helpers and one of those is simply the math module now if I type import math right here and when I save the file it's going to move this to the top of the file which is nice and that's because I have that python extension installed that we'd previously talked about Visual Studio code just wants to help us format this what you should typically do is type your Imports if you're bringing in a module which we'll get into more further in this series but you would type your Imports at the top of the file I'm going to type it here to just give the example of how it will be moved to the top now after that I want to use this math module just to get the value of pi so I'm going to say math dot pi and once again we can use dot notation to check all of the different things available in this module for us I just want pi right there okay when I control s to save notice that import statement was moved it should be at the top and it is so vs code moved it right up here to the top above our first actual code if I was typing this at the top where I would normally put it is at the absolute top of the file okay after that let's scroll back down to where we were and we should just run our code once we're back here to check the value of pi so we'll do that and there is pi from the math module let's do a few more things with the math module because we've imported it so let's use it I'll shift alt and the down arrow three more times and let's go ahead and use the square root function that we can use let's pass in like 64 and see what the square root of that is let's also use the seal function and here let's pass in our GPA and see what seal does and then let's use I want to go ahead and highlight that let's use floor and pass in GPA as well so we're going to see Pi once again then we'll see the square root of 64 then the ceiling of our GPA value and the floor of our GPA value so control s to save run the code so after Pi then we went ahead and found out the square root of 64 is 8 and that is a float value that was returned but then when we checked the ceiling value of our GPA which was 3.28 it's now 4 so that rounded up to the nearest integer likewise math floor rounded down to the nearest integer and one more note earlier we cast a number to a string but we could also let's say here casting a string to a number we could also cast a string to a number so for example if I had zip code which is my variable and inside of it I had this ZIP code which is a string I could check that value or turn that string into a numeric value using the integer Constructor function pass in zip code then I'll check the type of the zip value so now that I have that control s and run the code and yes it is an integer after we have cast that string to an integer with the Constructor now a quick note you can and I'll scroll just for some more room again you can have an error if you attempt to cast incorrect data so for example if I said ZIP underscore value equals and I said integer and then I passed in New York which I believe this is a zip code for New York but we pass in the actual words New York to our integer Constructor python isn't going to like this so let's take a quick look at the error you can see it says invalid literal and it doesn't like what we did it really highlights it there so let's close out of that I'll comment this out but I'm going to leave it in the code for you in the course resources just so you can see this reference that this will be an error remember to keep striving for Progress over Perfection and a little progress every day will go a very long way please give this video a like if it's helped you and thank you for watching and subscribing you're helping my channel grow have a great day and let's write more code together very soon
Info
Channel: Dave Gray
Views: 24,794
Rating: undefined out of 5
Keywords: python data types, python, python strings, python string data, python string data type, python numbers, python integers, python floats, python data, python numeric data, python string methods, python concatenation, concatenation, string methods, python math module, python math import, python vs code, python vscode, python boolean data, python booleans, python int, python str, python for beginners, python tutorial, python tutorial for beginners, python programming
Id: INGJh9DEaBM
Channel Id: undefined
Length: 37min 28sec (2248 seconds)
Published: Tue Mar 07 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.