Learn Python Tutorial [For Absolute Beginners] - Full Course
Skills:
Python for Data80%
Key Takeaways
Builds a solid understanding of Python basics using Scrimba's interactive platform
Full Transcript
hey there welcome to this python 101 course my name is olaf paulsen and i'm really excited to get to guide you through the basics of the python language here on scrimba now not only will you be learning the most popular but also likely the most versatile programming language right now python but you will be learning on the scrimba platform which will take the tutorial experience to a whole new level i love it and i'm hoping that you will too so why should you learn python well python is a really versatile and really easy to learn language part of the versatility comes from it being a bit like a smartphone where you can access and use any of the 150 000 or so apps in the app store and those modules will help you fix more or less anything you need solved without much effort on your part at all python is also very easy to write and forgiving as programming languages go and to be frank it's probably the easiest language to get started with as it's even more like regular english than most other programming languages if you want to create a script to back up files or check something on a web page every 10 minutes or create a face recognition implementation using machine learning or land your first job in development then python is for you easy access to data on the web sometimes people refer to python as being made for the web and this mostly means there are lots of tools that you can use to get data from the web and websites or interact with other applications on the web again with very little coding effort on your part python is very often used as the programming language when working with ai and machine learning by data scientists and researchers and there are many reasons for this but on one level you need to remember that if you want to work within this area python is almost a requirement and if you ask me it's probably the easiest language to get started with so in this course we're going to work together to take you from a beginner to an intermediate python developer so you can start experimenting and using all the available tools or maybe to prepare you for a more comprehensive programming bootcamp or a course that you want to take we're going to take you from zero to good and if you want to get to great you just need to practice more the course consists of interactive tutorials where i walk you through various concepts and then exercises where you can practice these concepts but don't for a second think practice is only in the exercises you can and should practice and play around with the code as much as possible learning is supposed to be fun so just have fun with this and that is one of the really great things about scrimba you can pause any tutorial whenever you want and start playing with the code you can add remove run or fork the code and save it anything you see on screen you can modify and all this experimenting is what's going to make your python programming muscles strong and really cement the knowledge that you get so be curious and interrupt me as much as you want i won't mind at all as long as you're learning i'm happy in the end how much you learn is really going to be up to you and you'll get out of this course what you put into it if you just watch the tutorials without doing any of the coding exercises or trying out the code that you see you're not going to get anywhere near the full learning experience that i think you expect so commit to your own learning and work through all the exercises and play around with the code in every tutorial that is going to make you a great programmer so as a bonus to this course we've created a facebook group for you it's called python 101 and you see the link right here that you can click on otherwise search for python 101 and you'll find the course on facebook it's a social learning group where you can learn with others who are doing the course just like you and you can do more exercises and learn even more i'm hoping to see you there and interact with you directly so what are we going to learn in this course we're going to learn basic syntax and outputting data and program flow we're going to work with text using strings and store data and variables we're going to learn how to do math in python with arithmetic operations then we'll look at lists tuples sets and dictionaries for storing different types of data we'll learn how to control how your code runs with conditionals and if and lift statements we'll teach you how to use loops both while and for loops you'll learn how and why to use functions return statements why and how to comment your code and what to do when you encounter errors you'll learn about objects and classes and even about inheritance you'll even learn about lists and dictionary comprehensions and lambda functions sometimes called anonymous functions to prepare for your next step and open up the python world of opportunities you're going to learn about modules and how to use them in your code you will also get plenty of exercises and some projects and other bits and pieces so it's time to meet your instructor that's me my name is olaf paulsen i'm swedish and i have a passion for education when i'm not doing this i'm the advocate for khan academy in swedish that means i do as much as i can to make sure that we have a khan academy in swedish that's a not-for-profit project i've been working on for a long time and i will probably continue quite a while longer my background is mainly from the finance industry in investment banking portfolio management and also running development teams and the programming i've done is mainly been around writing algorithms if you want to reach out and tell me how you found the course you can do so on twitter or in the facebook group now with that there's really only one question left to ask are you ready then let's get started and see just how deep this rabbit hole goes i'll see in the next tutorial alright so i'll welcome you to the python 101 course once more and i thought i'd tell you how we get a back-end language like python to run on a front-end platform like scrimba and as usual it's an add-in or a module that comes to save us and in this case we'll find it in the index.html file so here in the html file we have the bryton plugin now normally we run this python minimum js and what it allows us to do is to write python code that then gets recompiled into javascript and can then run on the scrimba platform or i guess many other platforms and this is the one we normally use when we want to do more advanced stuff or if you want to experiment on your own edit out and enable this script the one that has the python standard lib that has more functionality and is something we're going to use when we import modules for instance if you take a look at the versions of course by the time you're looking at this course there might be newer versions and with really bad luck maybe some of the code we write isn't going to work on later versions but anyway you can do this you don't have to but you can there are also a few oddities that i thought i'd go through that you're going to encounter through the course and they are not many but there are at least two so the first one is the input box that we're going to use when we get user input is in this case really the javascript prompt and for some reason the screen capture doesn't capture that so you can't see it if you run your own code you can see it and what it looks like for me when i talk about it and i say hey i'm getting an input box and you can't see it is this and you'll see it as well when you run the code locally but you just won't see it when i'm running through the code in the tutorial so bear that in mind another thing is that the scrimba mini browser sometimes hovers in the corner in some tutorials you don't have to worry about it i'll show you what i mean we're talking about this little browser this guy right here so he's going to be hanging out down here in the corner just to be out of the way and in some tutorials you won't even see it but again don't worry about it so that's how we get python to run in javascript and in scrimba and are able to do this awesome course for you so with that let's get cracking with the first tutorial okay welcome it's time to get started i hope you're as excited as i am so let's start talking about programming languages very briefly so when we program we're giving the computer a bunch of instructions and the programming language we're using has different special words that mean different things luckily most languages have somewhat similar words and ways of writing so they're often more like dialects than totally different languages which is good if you want to learn multiple languages so in python is one of the more simple languages to learn so that's great right so let's get started so when we write our python code we will often want to see if it's working if we're getting the numbers that we're expecting and so on and the way we often do that is to output data down into the console down here now there isn't any data down in there right now but there will be soon and to do that we use the print command which looks like this and then inside the print command we put some quotation marks either single or double and then we type whatever string we want to output so in this case we'll say welcome to python 101 that means you okay and if we run this we'll see that it outputs the text without the quotation marks and without the print around it so the print statement is a way to tell the computer please print the stuff that i put inside the quotation marks okay and when python reads our code let's copy this it does it in the same way that you read at least if you read western languages so it reads top to bottom and left to right so if we add a two here and run it you'll see that we have two rows we have welcome to python 101 and then the second row is the second row that i typed now if i move these so that i put this row above the first one you'll see that it turns around and that's important to remember because when you're writing code you want to make sure that you're writing in the same direction that you're reading or and also that the computer is reading because otherwise you might be trying to use something that doesn't exist yet so for instance if we create a hammer create hammer copy it and then afterwards we use the hammer that will look quite good so first we create the hammer and then we use the hammer now it would get really weird if we tried to do this first we use the hammer and then recreate the hammer use hammer create hammer that's kind of impossible right because you can't use the hammer until you've created it so let's get rid of that and then when you're executing you can either press this green button over here to run your code or you can use the ctrl s shortcut or on a mac the command s shortcut as your very first assignment i want you to write a print statement that create some nails in the right place and then modify the use statement that we have here so that we also use the nails with the hammer so give that a dot and i'll be back with the solution shortly great so did you try that i hope you did and that goes for all the exercises in this course when there's an exercise try to do the exercise don't just wait for me to come up with the solution because you're going to learn so much more and it's going to stick for so much longer and it'll be so much more fun so here's a way to do this so let's copy this and always in programming there are many ways to solve the same problem the way that i show is just going to be one of many ways of solving a problem and that's something to keep in mind so just because i write it doesn't mean that it's right if you solve the problem in a different way kudos to you you've solved the problem all right so let's create some nails and then let's use hammer and nails let's run that and there we go we created nails create hammer and then we use hammer and nails awesome i'll see you in the next tutorial when we're programming we often work with a lot of data sometimes we want to save this data or maybe we calculate some stuff that we want to save and use later for that we use something called variables and a variable you can view a bit like a box with a label on it imagine that you have all your shoes and you put them inside a brown box and you put a label outside on the box that says shoes and every time you ask the computer for your shoes it gives you the box with all your shoes in it that's basically how a variable works so here's a story we print about eric so his mother is getting this note from school and eric had a plan to go to a party this weekend and he's failing six subjects eric will need to redo six courses eric is doing well in geography well that's nice but i guess he's not going to get to go to this party so what can we do as his slightly unethical friend to help this out well we could rewrite the note that he's getting home we could for instance go in and change these things like you could change the six to a two we can change this six to a two and we could change these things but let's say this was a really really long letter or maybe it was a whole book and we want to change the name of a character or something like that so how would we go about doing that well this is where variables come in handy and concatenating strings and i will show you how to do this so let's start by creating a variable and when we create a variable in python we used small caps so oops failed subjects we'll call the variable and we'll set that equals to and when we assign variables the equal sign is not the kind of equal sign that you use in math it's actually an assignment operator so a better way to see it instead of saying failed subjects equals six you should see it as failed subjects gets the number six so we put the number six inside our failed subjects box okay so that's how that works and then we are going to put that inside the story how do we do that well we start by closing off this string here using a plus sign which tells the computer to add on whatever we put after that we remove the subjects and then we'll copy this because we're lazy we don't want to make mistakes failed subjects and then we will finish off with another plus sign and then the end of the sentence which should have a space and then we'll copy this part here and we'll paste that into the next one where we have six as well add a space there we go so now we have changed the strings to contain the variable that we're using so let's try and run this and see what happens so we still get the same result because we haven't changed the variable but let's change it when we see here that the failing six we should have had a space there so let's change it to two that's a lot better isn't it run it again and now we see that eric is only failing two subjects but maybe it's still not good enough so we should maybe change this further so it turns out eric has a brother and his brother's name is john now unfortunately for john we're this unethical friend so and we're now gonna put the blame on john so let's take this copy it and replace this eric with failed subjects and you're saying why failed subjects just we're going to change it to name there we go do the same thing with this one actually we don't need those and then we place it here as well there so what we've done is replace these strings with the variables that we just created so eric should now have turned into john let's see if that works so now it turns out that mrs badger has the message that your son john is failing to subject john will need to redo two courses and john is doing well in geography so this is looking good i think we're going to be able to get eric to come to that party however we're not quite happy yet our evil friend isn't done yet so now what we can do is variables can be reassigned at any time during your program so we can reassign the value of the name down here to be eric and what do you think is going to happen now let's see exactly so things are looking a lot better for eric and it looks like he might be going to that party so i'll see you next time let's talk about the basic data types of python so in our previous program where we learned about variables we ended up with this code and all the blue parts here are a data type called strings and the way you tell a string is by the fact that it's surrounded by two quotation marks at the end it doesn't matter if you use double quotes or single quotes they are equal in value so this is a string and even the space here is part of the string basically that's alphanumeric characters you can get from the keyboard and they are treated as characters they're not treated as numbers if you want a number you input a number and you just don't use the quotes so that is now something called an integer so an integer is a whole number like 1 2 3 4 5 6 7 8 9 10 and of course sometimes we want to have or most of the time we might want to have decimal numbers and those are called floats so now i've turned this integer into a float a floating point number another type of data that we have is something called a boolean boolean and a boolean is a data type that can take on the value true or false only those two values and we use those a lot in programming when we want to control how our program runs or when we want to check for different things we don't use them that often in in real life but we use them a lot in programming so an example in this case might be is going to party spelled correctly there we go is going to party equals now in the beginning of our story that probably would have been false at the end of our story it probably would have been true which was better for eric not so good for john because he probably had false until monday at least when his mother found out what actually happened and eric would be in real trouble but that's a different story so when we name variables we obviously shouldn't do what i did there it needs to be one word and we use underscores between the words and don't use capitalization at least in python and you can also use camel casing which would be this type of writing where you capitalize every new word but the standard in python is to use underscores between the words so there we go now i said that it doesn't matter if you use double quotes or single quotes and that's correct and then there are some cases when you get into trouble and don't know exactly what you're going to do and how to handle it and here is an example of that so let's say you have a variable where you want to store the word it's so you write it and if you take a look at the code now only the i t has turned blue the s is white and it shouldn't be because we want all of it to be the string but python is not understanding us and that's because this thing is being interpreted as the end of the string so we need somehow to tell python that it's not the end of string that it needs to use the quotes at the end as the actual end of the string so the way we can do it here is to change this to double quotes and at the end double quotes and now it's seen as a string and it will be interpreted that way there's another way to do it which is called escaping and there's an escape character in most programming languages and it's the backslash so if we were to do the same thing so here copy that this time we want to use the single quotes we don't want to change the double to double quotes so it's now broken again and by inserting a backslash tells python the character i have after this i want to actually use the literal character nothing else so this tells python that after the backslash i want to use the character that you see don't interpret it as something smart just do what i tell you so that's how escaping characters works so when you're working with this you sometimes want to know what data type you're dealing with and there's a command for that called type and you input whatever variable you have into it and you get an answer so i'll show you an example right here so here are four statements and we'll get rid of these for the moment now if we run these we'll find out what type we're dealing with so we have a string hello we have a number one we have another number 1.64 and we have true so how are those interpreted well the first one is a string the second one is an int or an integer next one is a float and the last one is a boolean so now we'll go back to our previous program paste that back in remember i said that you could use the numbers well you can't mix numbers or different data types any way you want there are some rules around how you can do that so for instance you can't put numbers so if i make this a number you can't put that inside the string or you can't mix it with the strings so the way to get around that is something called typecasting or casting or changing a type basically you're telling the type so in this case it's an integer i'm telling the integer hey integer i don't want you to be an integer anymore i want you to be a string and the way you do that is by using the shorthand str and you encapsulate this like that and we do the same thing down here now it will use the actual number so let's run that now if i don't have this casting if we remove this it fails it actually says it can't convert into string implicitly so i have to do it explicitly string to change the integer to a string and the same thing would happen if i put this like that so that was a run through of data types and type casting and i'll leave you with a few examples so here if we cast 1 into an integer we will get 1. if we cast 2.5 into an integer we'll get 2 so it removes the decimal if we cast the string 3 into a decimal we'll get 3. and here's a special case if we cast 3.4 the decimal in string format into an int it's actually going to return an error so we'll come back to that a bit later if we cast one into a float we'll get a decimal number so 1.0 2.5 into a float 2.5 3 as a string into a float becomes 3.0 and 4.23 into a float becomes 4.23 so that's the case we had up here but here it works then if we cast 80s the string into a string we of course get the same thing 22 into string we get 22 as a string so it's no longer a number 3.01 becomes the string 3.01 again it's no longer a number let's try and run these and see if the results work so now let's go back to the one we had up here so it turns out that you can't actually do this if we run it error and the reason is this is a decimal string and we're turning it into an integer and that's not an allowed operation you can do it with the float as we did down here so what you can do to handle this is to first test it into a float and then cast it into an integer and then we add the variable c1 and we see that we get another three so i hope you enjoyed that and i'll see you next time hey and welcome to this exercise on variables and data types so what we're going to do this time is try to create an app and the app is say for a hardware store and the hardware store needs to keep track of a few things so i'm going to paste that in here as comments and that will give you a chance to see what comments look like so here's the text just to show you they are comments because they start with a hashtag so now it's a normal text now it's a comment so create appropriate variables for item name the name of whatever the item is the price and how many you have in stock so that's your job so create three variables and choose the right data type for them okay i'm hoping you tried that exercise so now i'm going to give you one alternative to how you could do this so let's start with the item name so why don't we just call that item name and that will get we'll give it a name which should probably be a string so let's say widget that's the fantastic name of our item so next step the price why don't we call that price don't have to make it harder than it is and the price could be maybe not always an integer maybe it's going to be some decimal number so let's make that a float and the way we make it a float is just simply to put a comma in it that automatically makes it a float and the next step how many do we have in stock or our inventory let's call that inventory and in this case we can't have half ones or whole ones because they're widgets so let's say that's an integer and let's just say we have a hundred of them now as a next step why don't we try and print this as well so let's print all of them let's say print item name comma price comma inventory what do you think that's going to print let's find out we get widget 23.5 and 100. now another thing we could have done is to use a boolean so we could have said is in inventory and set that to true and then we could have had a flag that told us whether or not we had any inventory at all but of course we wouldn't know how many we had an inventory so that would be sort of useful but not entirely so that covers that exercise i hope you enjoyed it and i'll see you in the next tutorial doing basic arithmetic operations in python and for that matter any other programming language is very very common so having a firm grasp on how to do it is definitely a plus plus just not in python after this short tutorial there's a good chance you will be very awesome at this are you ready and if you just heard me say plus plus before and have absolutely no idea why i said that or perhaps racking your brain don't worry eventually that penny will drop google might be your friend you'll realize just what a lame pun that was if you did well i guess that's just time you'll never get back sorry anywho here we go so let's paste in a few examples so here we have two variables a gets six and b gets two and then we're going to print out the results of various things so the first one is addition right here and you see in python we write addition as a plus b and we would expect the result to be eight right subtraction we write as we would expect a minus b multiplication a star b division float division this is what we would normally call division so if you divide 3 by 2 you get one and a half a divided by b then we have something called floor division here now floor division is sort of an artifact from older versions of python where it used to be that if you divided three by two you would get the answer one not one and a half so floor division actually does that old type of division so if you divide three by two you will get one not one and a half then we have modulus and modulus isn't one of the transformers it's actually an operation we use in computer science and in math that gives you the remainder of the division so for instance if we divide six by two we get three and the modulus will be zero if we divide seven by two will get 1 because if you divide 7 by 2 you'll get 2 groups of 3 and then you have 1 left over so it's that left over remainder that's shown in the modulus then we have exponent which is a to the power of b all right so let's run this first and then we'll change the numbers and show you some interesting stuff so here we go the addition is eight the subtraction is four the multiplication is 12 division 3.0 division floor same three the modulus is 0 and the exponent is 36 so that's to be expected right but it's not very interesting because the division floor and the modulus are both showing the same result or nothing so let's change the numbers let's say we have 10 and divide that by 3. now we'll see some of the more interesting bits so now the addition is 13 subtraction is 7. multiplication is 30. the division is 3.3333333 and then weirdly the last number is a 5 don't ask me why it's like that lower division turns out to be 3 the modulus is 1 so we're dividing 10 by 3 and the remainder will be 1 right because you have 3 groups of 3. and the exponent when you do 10 to the power of 3 that's 10 times 10 times 10 is a thousand and those are basically the arithmetic operators that you need to understand to be able to become a master of python you might need to learn some other stuff as well but we'll get to that talk to you later okay time to talk about strings so we use strings a lot in programming we take them apart we put them together we create new ones we delete old ones just lots of things and i'll introduce you to some of the basic concepts when it comes to using strings so as you see we've already printed we've already run this statement we have a message welcome to python 101 strings then we've run the print message and you see the output down in the console so one of the things we can do is print a message multiple times and you could do that by concatenating the message so just by typing a plus sign and then the message again so if we run that we'll get the string just the two strings put together with no space in between them so that's one way of doing that you can achieve exactly the same thing by multiplying the string which seems a bit weird but if we multiply that by two we'll get exactly the same result so no difference the strings are just put together so there's another way we can do this and that is to separate these by a comma and say message message so what's going to happen then now you see a space has been inserted after the strings so the comma does that and this this is actually a statement you can use inside print and i think you use it in input as well but i'm not sure if you can put together strings in a variable by doing this but at least now you know three different ways of multiplying strings so there we go so another thing we might want to do is change the casing of the string so there are some different ways we can do that we can say upper which will and then parentheses nothing inside the parentheses if we run that the whole string will become uppercase and you might guess that we can do the same thing by saying lower like that and that is going to give us the whole string in lowercase but we can do more we can say capitalize it will actually make sure that only the first word in the string is capitalized so then we get that result so as you see it even changed the capitalization of the word python it's now in lower case but the first letter which was previously in lowercase is now an uppercase now if you wanted all the different words to have uppercase there's actually a function for that as well and that function is called title that will give us this result so each of the words are capitalized now it actually fails sometimes for instance when we have a an apostrophe in what we're writing so i'll introduce one up here so let's say we have it's now i've messed up the string as well so i need to introduce an escape character to make it easy there we go so if i run it now and you take a look at the result you'll see that welcome to it's and then there's an apostrophe and it interprets the s as a new word so the s also gets capitalized which is not optimal but it sort of works except when you have that situation okay so those are some basic things that we can do with uh changing the aspects of the string so let's remove those let's try some other stuff let's find out some information about the string so for instance let's say we want to find out how long the string is and we'll also get rid of this it's just going to be annoying there we go so print message and what i want now is to find out the length of the message there's a command called len which is short for length which will give us the length of the string let's run that it's 30 characters long great so another thing we can find out is we can count the number of instances of a certain word or a letter and we do that by the command count and that looks like this so let's say message dot count and what do we want to call it count we want to count the instances of python we'll run that how many instances are there there are zero and that's because it's case sensitive so it's now only looking for lowercase python so if we change it to uppercase it will find one instance now of course we could search for something else we could search for just the letter o and see how many we find there are three o's in python 101 or in the whole string great so that's how you measure the length of a string and then how you count how many instances of something you have in the string but we can do more stuff we're going to go into something called slicing strings slicing so slicing strings is getting parts of a string and returning them so let me give you an example so the way strings in python are numbered is in the following way the numbering starts at zero so the first character is zero or the first position then it's two well sorry one two three four five six seven eight and so on and it's the same thing for lists in python so to access this first we'll need to comment this out so that it doesn't get run so the way to access this is to use square brackets i'll input square brackets number zero and that will give us the letter w if i use number five it will give us m because that's the sixth position which is number five now you can also use negative uh indexes on this so for instance is if for instance if i take minus one this will give me the last character that gives me s i take minus three it will give me the n so that's second from the last as it starts at minus one is the last one there's another way to use this so if i say two colon the colon will tell python to grab everything after the number two string so in this case if i say if i do this i will get almost the whole thing i'll come to python 101 strings if i specify an end point now it gets a bit more interesting so let's say 15 see where that winds up welcome to pyth so that's let's do another one that we can count so there so now i've asked for two to seven and two that specifies the second position seven is the seventh one but it's not inclusive so it takes up to that but it doesn't actually takes the seventh one as you'll see from the outcome that we have we have position two three four five and six but not number seven that's kind of important to keep in mind and then lastly if i turn it around remove the two python will now assume that i want everything starting from zero so this will give me the whole string up until the sixth one so welcome all right let's start with that and continue with some more string information in the next tutorial see you then okay great welcome to your first exercise so here's the exercise from the string welcome to python 101 strings extract text and then create print a new string that says one welcome ring to tyler every first letter in a word should be capitalized so it should be in title format and then the second exercise which we haven't gone through print the same string backwards google is your friend so give that a shot and see how it turns out i'll come back with the solution in a second great so did you try that out let's see what we can do let's start by creating a variable for our new string so we'll call that message one and then we're going to print message one as we work along so that we can see that we're doing the right thing so let's start concatenating a string so we'll start with message i think we're going to be using that a lot so message what's the first thing we need we need the number one so that's at position 17 maybe let's see well print that was empty so that means it's probably 18. so there we go great one and then we need to add to that a space since we want to have one space next up we want welcome and we actually have the word welcome right so we can use a slicing method here so message we want to slice from the first letter to the eighth the reason we want eight is because we also want the space so that we don't have to add a space here before the next one so then we grab another slice now we want ring and ring actually exists as a word inside the word strings right so we can grab that so that's if number one is 18 it's 19 20 21 22 23 24 25 it starts at 25 to 29. let's check what we have so far one welcome ring we add some more now we need to add a space because there wasn't one there and then we add the word to which we find at position eight to eleven eight to eleven and actually if we print that we'll see it works but do we really need this space we could do start at seven and we get the space before too so there we go then then we have to pick the different letters to create the name tyler so let's do that so we've got message t is in position 13. we've got we've got y in position 12. then we have l in position two e in position one and the r we can grab from the fifth position from the end so plus minus five let's try out our string one welcome ring to tyler awesome and then we need to capitalize it so let's do that how do you do that well you say title one welcome ring to tyler fantastic and then as our second exercise we're going to print this string backwards so how do you do that well there's a slicing trick and it's really simple just haven't gone through it yet so let's try it it's print message one and here comes the slicing trick colon colon minus one that prints a string backwards let's do that and let's also make it in title format so everybody's happy and there you go now you've learned a new trick on slicing strings talk to you later okay let's continue working with strings so the first thing i thought i'd show you is how to do multi-line strings now unfortunately this is one of those things that scrimba is not going to support or rather maybe it's the brython plug-in that doesn't support it but what you do to get a multi-line string is something along these slides you use triple quotes to start the string and then you just use return to break the lines and then you use triple quotes at the end and if i now execute this it's actually just going to print on one line which is not at all what we want but it's what we get in this version of scrimba so you'll just have to trust me on this and maybe go to replit or some other place and try it in a real python environment and then you'll see what it looks like so let's go back to where we were there as our next step we're going to try some find and replace so we'll start with find we'll go in here and we'll say print find and now we're looking for the h character so we find it the first h at position 14 which is the 15th position so that's one way we can then later reference it or if we look for for instance python it starts at position 11. okay so the other thing we can do is actually replace so let's go in back here change the find to replace and now we'll replace python with the syntax now it takes two parameters first one is what we're looking for the second one is what we want to replace it with so we'll replace it with java we'll run that so we now have a string that says welcome to java 101 or we could say c so that's how you use replace now one thing to remember about this is that strings are what are called immutable so that means that you can't change them when you've graded them so you need to save them into a new instance so if i want to save this result i can't save it in the existing message one i can create a new one or i can just set it to a new variable so i could do this message one equals message replace python with c then print message one so if i run this we now get the same result we need to save it into a new variable we could have also saved it into the same variable like this that would also work the next thing we're going to try out is something called membership so let's look at the following so now we're going to print and we'll say python in message now what this is going to do is check if python exists in message let's try that it prints true so it does exist in there if we want to check if it doesn't exist then we can say not in message it will print false and lastly we'll cover something called string formatting so previously you remember we concatenated strings now i'm going to paste in an example here so we have a variable that has the value terry we have a color red and then we've concatenated a string and then we'll run a print statement as well so we'll run print message so what does this give us it says terry loves color red now when we get longer strings that we concatenate it can get very messy to keep track of what's where so there's a function in python that allows us to format strings in a nicer format and make it easier for us to read and not make as many mistakes so i'll show you that so we can achieve the same result with a different formatting that looks like this so we'll create a different one called message1 and it starts with an f and then we start a string in this case we start with the square brackets and then what we do is we take our variables and insert them in curly brackets and this makes it a lot easier to read and we don't have the plus signs and the extra quotation marks floating around the string so here we have name loves the color and then new curly brackets color lower because we have those in capital letters or maybe we don't but we don't know so we want these to be in lower let's make the same change up here there we go and then we'll run another print statement print message one as you see this gives us exactly the same result and now we just have the terry bit to change so we've got name here and what do we want to change that to so here's your first assignment on this make the name terry capitalized so did you try that out so how do you do that well here's one way run it and there we go so those are some further things you can do with strings i'll talk to you later how do you capture user input well stick around and i'll show you so now we're gonna try some user input and actually scrimba doesn't support the input box properly so you won't see it in the screencast i mean i see it on my computer when i run this and you'll see it if you test the code on your computer but you can't see it in the screencast so you'll see me writing something and then something will pop up in the console at the bottom but you won't actually see the data that i input into the box the way you can see it is just to pause or stop and try the code yourself just after or before i do and then you'll get the same result and you'll see the pop-up box fortunately not my fault i'm sure scrimba will handle it at some point when they start supporting python properly but they don't so we can't really complain either so here we go so the first thing we're going to do is we're going to ask our user for their name so we create a variable name name gets and then we type input then we'll type something let's try what is your name question mark here we go maybe a column as well now when i run this i'm going to get an input box where i can input my name which you you won't see and then it will print name let's see how that works so i now see a box and i'm typing a name let's pretend that my name is eric there we go eric is now typed out but that's not fun enough so let's do something with that name let's add something to it like hello [Music] so now we've added hello and the name and let's add exclamation points at the end there we go and we'll run that so again type the name hello eric awesome so now what else can we do well let's add some more information let's ask for their age so age gets input what is your age and they give us some kind of answer and then we will add that here so hello eric and then we'll write some more you are plus and then we'll add our age age and then we'll add more text years old period and we want a space here as well so let's try that i'm running it i get a box ask for my name eric and it asks my age what's your age let's say i'm 37 not but let's pretend so it says hello eric you are 37 years old awesome right so this is one way to get some input now let's try and build something more interesting let's try and build a calculator see how that works so in a calculator we want to ask for a number so we have number one and we'll ask the user for a number so input and we will say enter a digit there we go and then we want another number so number two gets input enter a second number and then we'll do something with those numbers we'll add them together so we'll say answer gets num1 plus num 2 and then we will print the answer print answer what happens if we run this do you think well actually it's not going to work and do you know why well we're entering a digit up here and it's going to be a string and we're entering a digit here which is going to be another string so we need to convert these to some kind of number so let's typecast them into a float that looks a lot better so let's try and run that so it's asking me for a digit let's say 4 and then another one 12 the answer is 16.0 wonderful so that's a way to get some user input and uh see you next time hey welcome to the exercise now we're going to do a user input exercise and flex our programming muscles so here on the slide we have the exercise we're going to create a distance converter converting kilometers to miles so we're going to take two inputs from the user the first name and the distance in kilometers then we're going to print a greeting where we greet the user by name and we show the kilometer and mile values and one mile is equal to 1.609 kilometers and then as a hint make sure you we use the correct types for calculating in print and then remember to capitalize the name so let's remove this and go up here and i'll paste this as some help why don't you give that a shot and i'll be back in a second to show you one way of solving this problem all right i'm sure you did awesomely at this task so let's take a look at this and what we need to do so the first thing we need to do is take two inputs from user so that's two inputs let's take the first one the name let's just call that name so name gets input enter your name then as the second input we're going to get the distance in kilometers so we'll call that distance underscore kilometers and that's another input we'll say enter distance in kilometers and then we're going to calculate the distance in miles so we'll create a variable out of that distance miles which is equal to a float so we need to convert the number that we get in the input box because when we create an input and capture it it's always a string so we could convert that in the input line when we get distance kilometers or we can do it now in this case we're doing it right here so we'll convert to a float the distance in kilometers and then we'll divide that by 1.609 and the next step we need to do is write a print statement where we greet the user by name so let's do a print and we'll use an f string so we'll say f and then we'll say i and the name and we'll capitalize that we'll say title and the curly brackets exclamation point space open a new curly bracket for the variable that we're going to put here so we'll use distance kilometers close the curly bracket the text km for kilometer is equivalent to then we have the next variable distance miles close the variable and the word miles period so that should work let's see what happens if we run it we get an input box enter the name we'll say john and we'll say 10 kilometers so it answers hi john 10 kilometers is equivalent to 6.215 and a lot of decimals so probably we might want to round that number don't you think so the way to round it is to go in here to distance in miles and round that number we simply say round parenthesis and then after the variable put a comma and then how many decimals we want to see let's say one then we close the parentheses there we go and let's run it again so we'll say john and let's say 14 kilometers hi john 14 kilometers is equivalent to 8.7 miles and now we've solved that issue awesome so i'll see you in the next cast now we'll take a look at lists so lists are used very often in programming and they're very similar to variables so a variable can hold a number or a string or a boolean or something else and a list can hold multiple instances of these so you might have a list of 500 names or 500 numbers or maybe even 500 000 numbers and though so they're very practical and the way you create a list is again very similar to how you create a variable give you an example let's say i have some friends friends and friends get and then we use square brackets to hold the friends so we'll have our first friend john then we insert a comma next one michael terry eric and graham we finish with another square bracket and we're done so that is the list friends and now if we print that we see that we print the list now very similar to a variable the way we access the different parts of a list is by using an index and it's the same way that we access content in a string so the first one is position zero the second one is position one third one is position two and so on so that's how you access values in a list very similar to values in a string so one way you can access parts of the string is to actually print the index that you want if i input one i'm going to get the michael and we printed michael if i want two i can say friends where bracket and then maybe number four so now it gets michael and graham there's another way similar to what we did with strings and slicing where you can ask for different parts of the list so let's say print friends square bracket one and again it will print michael we can then use negative indices like minus 1 which will give us the last number we can use the slicing syntax using the colon so we can say from number 2 to number 4 but not by printing the actual number 4 so we get terry and eric and again exactly as we did with strings if we don't have a first number it will assume that we mean from the start of the string so we get john michael terry and eric if we don't input anything we get
Original Description
🎓 View our courses: https://scrimba.com/links/all-courses
Learn Python with Olof Paulson
💻 Full interactive course on Scrimba: https://rebrand.ly/gpython
The course consists of 4-8 minute tutorials covering a topic, and separate exercises where the topic is covered as a hands-on coding challenge. It aims to give you a solid understanding of the basic concepts in Python programming.
As with all Scrimba courses, you can stop, test, modify or spin-off the code at any time. This makes for a more immersive experience and is one of the greatest benefits with the Scrimba platform.
BUT! It only works if you do, so use the opportunity. Don't just watch the talkthroughs, work through them, play, experiment, fail and learn! That is how you become great. You got this!
Extend the learning:
To discuss learning Python in this course, share and meet others in a similar situation to yourself you are invited to join the Facebook group: Python 101 (http://khan.co/2PiuHlq) started for this purpose. The instructor Olof Paulson will be active and answer your questions.
🐦 Follow Olof on Twitter:
https://twitter.com/olofpaulson
⚡ Contents ⚡
00:00:00 - Course Introduction
00:05:17 - Running Python on Scrimba with Brython
00:07:26 - Print Statement and Programflow
00:11:20 - Variables
00:16:04 - Datatypes & Typecasting
00:23:20 - Variables & Datatypes - Exercise
00:25:35 - Arithmetic operations
00:28:58 - Strings - Basics / Slicing
00:35:36 - Exercise - Strings - Basics / Slicing
00:39:08 - Strings-2 Find/replace, string formatting
00:43:59 - User Input
00:47:57 - User Input - Exercise
00:51:12 - Lists- Basics
00:54:29 - Lists- continued
01:01:45 - Lists - Exercise
01:07:20 - Split and Join
01:11:40 - Split and Join - Exercise
01:15:47 - Tuples
01:18:02 - Sets
01:21:26 - Sets-Exercises
01:26:05 - Comments
01:29:49 - Functions - Calling ,parameters, arguments, defaults
01:37:13 - Functions - Exercise
01:41:04 - Functions - Named Notation
01:44:17 - Return statements
01:48:38 - Com
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Scrimba · Scrimba · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
CSS Grid Course: Learn the Basics in 3 Minutes
Scrimba
CSS Grid Course: Positioning Items
Scrimba
CSS Grid Course: Why Learn It And How It Compares To Bootstrap
Scrimba
CSS Grid Course: auto-fit & minmax
Scrimba
CSS Grid Course: Implicit Rows
Scrimba
CSS Grid Course: Fraction Units And Repeat
Scrimba
CSS Grid Course: Justify Items and Align Items
Scrimba
CSS Grid Course: An Awesome Image Grid
Scrimba
CSS Grid Course: Named Lines
Scrimba
CSS Grid Course: auto-fit vs auto-fill
Scrimba
CSS Grid Course: Justify Content and Align Content
Scrimba
CSS Grid Course: Template areas
Scrimba
27. Setting up the structure - Responsive CSS Tutorial
Scrimba
25. Making the navigation responsive - Responsive CSS Tutorial
Scrimba
36. Playing with the title's position and negative margins - Responsive CSS Tutorial
Scrimba
31. Starting the CSS for our page - Responsive CSS Tutorial
Scrimba
26. Taking a look at the rest of the project - Responsive CSS Tutorial
Scrimba
15. Spacing out the columns - Responsive CSS Tutorial
Scrimba
33. Starting to think mobile first - Responsive CSS Tutorial
Scrimba
22. Making our navigation look good - Responsive CSS Tutorial
Scrimba
37. Changing image size with object-fit - Responsive CSS Tutorial
Scrimba
44. Module Wrap up - Responsive CSS Tutorial
Scrimba
16. Controlling the vertical position of flex items - Responsive CSS Tutorial
Scrimba
39. Setting up the widgets and talking breakpoints - Responsive CSS Tutorial
Scrimba
42. Setting up the About Me page - Responsive CSS Tutorial
Scrimba
35. Changing the visual order with flexbox - Responsive CSS Tutorial
Scrimba
23. Adding the underline - Responsive CSS Tutorial
Scrimba
21. Using flexbox to start styling our navigation - Responsive CSS Tutorial
Scrimba
20. Creating a navigation - Responsive CSS Tutorial
Scrimba
40. Using a new pseudo class to wrap up the homepage - Responsive CSS Tutorial
Scrimba
43. Fixing up some loose ends - Responsive CSS Tutorial
Scrimba
32. Starting the layout. Looking at the big picture - Responsive CSS Tutorial
Scrimba
24. A more complicated navigation - Responsive CSS Tutorial
Scrimba
28. Feature article structure - Responsive CSS Tutorial
Scrimba
34. Styling the featured article - Responsive CSS Tutorial
Scrimba
18. Making layout responsive with flex direction - Responsive CSS Tutorial
Scrimba
19. flex direction explained - Responsive CSS Tutorial
Scrimba
41. Creating the recent posts page - Responsive CSS Tutorial
Scrimba
17. Media Query basics - Responsive CSS Tutorial
Scrimba
30. Home Page. HTML for the aside - Responsive CSS Tutorial
Scrimba
38. Styling recent articles for large screens - Responsive CSS Tutorial
Scrimba
29. The home page. HTML for the recent articles - Responsive CSS Tutorial
Scrimba
10. ems and rems an example - Responsive CSS Tutorial
Scrimba
1. Starting to think responsively - Responsive CSS Tutorial
Scrimba
4. Controlling the width of images - Responsive CSS Tutorial
Scrimba
5. min width and max width - Responsive CSS Tutorial
Scrimba
3 CSS Units. Percentage - Responsive CSS Tutorial
Scrimba
11. Flexbox refresher and setting up some HTML - Responsive CSS Tutorial
Scrimba
12. Basic Styles and setting up the columns - Responsive CSS Tutorial
Scrimba
8. The Solution Rems - Responsive CSS Tutorial
Scrimba
14. Setting the columns widths - Responsive CSS Tutorial
Scrimba
2 CSS Units - Responsive CSS Tutorial
Scrimba
7. The problem with ems - Responsive CSS Tutorial
Scrimba
6. CSS Units. The em unit - Responsive CSS Tutorial
Scrimba
13. Adding the background color - Responsive CSS Tutorial
Scrimba
9. Picking which unit to use - Responsive CSS Tutorial
Scrimba
Tutorial to Learn Alpine JS - Full Course for Beginners
Scrimba
Guide To Algorithms in Javascript [Binary Search] - Full Course / Tutorial
Scrimba
Learn UI Design [7 Fundamentals Tutorial] - Full Course for Beginners
Scrimba
Javascript Tutorial for Beginners [From 0 to ES6+] - Full Course
Scrimba
More on: Python for Data
View skill →Related AI Lessons
Chapters (26)
Course Introduction
5:17
Running Python on Scrimba with Brython
7:26
Print Statement and Programflow
11:20
Variables
16:04
Datatypes & Typecasting
23:20
Variables & Datatypes - Exercise
25:35
Arithmetic operations
28:58
Strings - Basics / Slicing
35:36
Exercise - Strings - Basics / Slicing
39:08
Strings-2 Find/replace, string formatting
43:59
User Input
47:57
User Input - Exercise
51:12
Lists- Basics
54:29
Lists- continued
1:01:45
Lists - Exercise
1:07:20
Split and Join
1:11:40
Split and Join - Exercise
1:15:47
Tuples
1:18:02
Sets
1:21:26
Sets-Exercises
1:26:05
Comments
1:29:49
Functions - Calling ,parameters, arguments, defaults
1:37:13
Functions - Exercise
1:41:04
Functions - Named Notation
1:44:17
Return statements
1:48:38
Com
🎓
Tutor Explanation
DeepCamp AI