Transcript for:
Python Programming Tutorial

in this python tutorial you're going to learn everything you need to know to start programming in python if you want to learn python programming for data science machine learning or web development this python tutorial is the perfect place to learn python you don't need any prior knowledge in python or programming in general i'm going to teach you everything from scratch i'm mosh hamadani and i've taught millions of people how to code through this channel if you're new here make sure to subscribe as i upload new videos every week now let's jump in and get started all right before we get started let me give you some ideas about what you can do with python that's a very common question python is a multi-purpose programming language so you can use it for a variety of different tasks you can use python for machine learning and ai in fact python is the number one language for machine learning and data science projects python is also very popular in web development using python and a framework called django you can build amazing websites here are five websites powered with python and django youtube instagram spotify dropbox and pinterest you can also use python in automation with python you can save your time and increase your productivity by automating repetitive tasks so why are you learning python are you learning it for automation for data science or web development let me know in the comment section below all right the first thing i want you to do is to head over to python.org to download the latest version of python so you go to downloads and select the latest version of python here in your downloads folder you should see this package simply double click it you're going to see this python installer if you're on windows you will see this checkbox over here add python to path make sure to check it it's really important otherwise you're not going to be able to follow this tutorial simply click on continue again one more time i agree with the terms and install the latest version of python now here you need to enter the username password of your computer so let's do that real quick next you need to install a code editor we use a code editor to write our code and execute it the most popular code editor for python is pycharm you can get it from jetbrains.com pycharm so on this page click on download you should see two different editions one is the professional edition which is commercial and we also have this community edition which is free and open source so we're going to download the community edition now in your downloads folder you should have this package let's double click it if you're on windows you're going to see an installation wizard so simply click on the next button until you install pycharm if you're on a mac you need to drag this pycharm and drop it onto the applications folder now let's open it the first time you open pycharm you have to configure a few settings we don't want to spend time on this so over here we're going to click on skip remaining and set defaults now let's create a new project over here we can specify the location and the name of our python project so let's append hello world to this path this is where our python project is going to be saved so let's click on create in this window you can see the content of our project so here's our hello world project currently we have only one folder inside this project that is vn which is short for virtual environment we'll talk about virtual environments in the future so currently we don't have any python files inside this project a real application can consist of tens or hundreds or even thousands of python files so let's right click on the project name and go to new python file we're going to call this file up now we can collapse this project window by clicking on this icon so now we have more space let's write our first python code we're going to write print all in lowercase then add parentheses then add quotes either single quotes or double quotes and inside this code we're going to write hello world so this is what we call a string a string means a string or sequence of characters in simple words that means textual data so in python and in many other programming languages whenever we're dealing with textual data we should always surround our text with quotes in python we can use single or double quotes now this print you see here is a function built into python and we can use it to print a message on our application window so let me show you how to run this code on the top we go to the run menu and then select run note that there is a shortcut associated with this command i always use shortcuts because they increase my productivity so let's click on this now select app and over here you can see this little window this is what we call the terminal window and it shows the output of our program so here's the hello world message printed in the terminal window now as you learn more python you will learn how to build applications that have a graphical user interface that's an advanced topic so for now let's not worry about it alright now let's talk about variables we use variables to temporarily store data in a computer's memory for example we can store the price of a product or someone's name their email their age and so on let me show you so to declare a variable we start by typing a name for that variable let's say age then we add an equal sign and then we type a value let's say 20. so with this we're storing the number 20 somewhere in our computer's memory and we're attaching this age as a label for that memory location so now we can read the value at this memory location and print it on the terminal so instead of printing hello world we want to print the value of the age variable so i'm going to delete what we have inside parenthesis and type age note that i'm not adding quotes because if i run this program we'll see the text h on the terminal we don't want that we want the value of the age variable so let's remove the quote and print the value of the age variable now here on the toolbar you can click on this play icon to run your program or you can use the shortcut that i showed you in the last video so the shortcut is over here on a mac that's ctrl shift and r so there you go now you can see the value of the age variable now we can also change the value of a variable for example on line 2 we can set 8 to 30. now when we run our program we see 30. so as you can see our program gets executed from top to bottom so this is how we can declare and use a variable now let's look at a few more examples so i'm going to declare another variable called price and set it to 19.95 so in python we can use numbers with a decimal point or whole numbers we can also declare a variable and assign it a string value so let's say first underline name so if you want to use multiple words in the name of a variable we should separate them using an underscore this makes our code more readable see what would happen if i didn't use this underline this is not easily readable so we always separate multiple words by an underscore now we set this to a string so we can use single quotes or double quotes let's say march we also have a special type of value called a boolean value which can be true or false that is like yes or no in english let me show you so i'm going to declare another variable called is online and set it to true we could also set it to false what we have here is called a boolean value now note that python is a case sensitive language so it's sensitive to lowercase and uppercase letters in this case if i use a lowercase f we can see an error over here because this is not recognized in python so false with a capital f is a special keyword in python that represents the boolean false value so this is how we can declare and use variables in python all right now here's a little exercise for you imagine we want to write a program for a hospital so we're going to check in a patient named john smith he's 20 years old and is a new patient i want you to declare a few variables to store these values use the comment box below to share your code with others in this tutorial i'm going to show you how to receive input from the user so in python we have another built-in function called input we use this to read a value from the terminal window let me show you so we add parenthesis then we type in a string here we can type a message like what is your name we had a question mark followed by a space you will see why we need this space in a second so let's run this program we get this message now we have to enter a value so we click over here now you can see that the carrot is separated from the question mark this is because of the white space that we added over here so now we have to type a value let's say john when we press enter this function will return the value that we entered in the terminal window so we can get that value and store it in a variable so let's declare a variable called name and set it to the return value of the input function now we can print a greeting message for this user so we use the print function we say hello we had a space now after the string we want to add the value of the name variable so we use a plus sign and then type name what we are doing here is called string concatenation so we're combining this string with another string now let's run our program and see what happens so what is your name mosh now we get this message hello mosh so this is how we can use the input function in python you'll learn about the three types of data in python we have numbers strings and booleans now there are times you want to convert the value of a variable from one type to another let me show you so we're going to use our input function to read the user's birth year so enter your birth here now this input function is going to return a value so we can store it in a variable called birth underline year okay now let's write code to calculate the age of this user so we write an expression like this currently we are in the year 2020 so 2020 minus birth year this expression or piece of code is going to produce a value so once again we can store that value in a variable let's call that variable age now let's print age on the terminal let's run our program and see what happens so my birth year is 1982. enter oops our program crashed so whenever you see this red message that indicates an error so this error occurred in this file that is our app.pi on line two right below that you can see the piece of code that generated this error so that is this expression 2020 minus birth year now below that you can see the type of error so here we have unsupported types for subtraction we have int and stir what are these well this end is short for integer and that represents a whole number in programming so 2020 is an example of an integer now birth year is an example of a string because whenever we call the input function this function would return a value as a string even if we enter a number in other words when i entered 1982 this input function returned a string with these characters 1982 so this string is different from the number 1982 they're completely different types so in this case let me delete these lines the reason we got this error is that we try to subtract a string from an integer so our code looks like this 1982 now python doesn't know how to subtract a string from an integer so to solve this problem we need to convert this string to an integer now in python we have a bunch of built-in functions for converting the types of our variables so we have this end function we can pass our burst here to it and this will return the new numeric representation of the birth year so to solve this problem we need to replace the string with the end function so let's see what's going on here on the first line we call the input function this returns a string on the second line we pass the string to our end function the in function will return the numeric representation of the burst year then we subtract it from 2020 we get the age and store it in the age variable now let's run our program so 1982 and there you go i'm 38 years old so this is how the in function works now we also have another built-in function called float that is for converting a value to a floating point number a floating point number in python and other programming languages is a number with a decimal point so 10 is an integer and 10.1 is a float so we have int we have float and we also have bool for converting a value to a boolean and finally we have stir for converting a value to a string so these are the built-in functions for converting the type of our variables now here's a little exercise for you i want you to write a basic calculator program so here we have to enter two numbers we can type a whole number or a number with a decimal point and then our program will print the sum of these two numbers so pause the video spend two minutes on this exercise and then see my solution all right first we're going to call our input function to read the first number we get the result and store it in a variable called first now let's declare another variable called second and read the second number now we calculate the sum so that is first plus second now let's see what happens when we print sum on the terminal so i enter 10 and 20 but instead of 30 we get 10 20. this is because we're combining or concatenating two strings so as i told you before the input function returns a string so this line will be equivalent to first equals 10. we're dealing with a string not an integer similarly second is going to be 20 as a string so when we combine two strings 10 plus 20 will get 10 20 because we're dealing with textual data okay so to solve this problem we need to convert the values we read to their numeric representation so over here we're going to pass first to our int function and here as well now let's run our program so we enter 10 and 20 we get 30. what if we enter a floating point number so 10.1 and 20. we got an error so to solve this problem we need to treat both these values as floats so instead of the in function we're going to use the float function now let's run our program one more time we enter a whole number and a floating point number so the result is correct now let's add a label over here so sum is plus sum let's run our program one more time 10 and 20. once again we got an error the error is saying that python can only concatenate strings not floats to strings so on line four we have a string we're concatenating this with a float because the result of this expression is a floating point number we're adding two floats so the result is a float as well so python doesn't know how to evaluate code like this it doesn't know how to concatenate a float to a string to solve this problem we need to convert sum to your string so this is where we use the stir function now let's run the program again so 10 plus 20.1 and here's the result and one last thing in this example i'm calling the float function at the time we want to calculate the sum of these two numbers but this is not a requirement we can call the float function over here so this input function returns a string we can pass that string to our float function take a look so float parenthesis like this so the value that we're passing to the float function is the value that is returned from the input function similarly we call the float function over here now we can change this expression to first plus second that is another way to write this piece of code so type conversion is important in python and other programming languages there are times you need to convert the type of variable to a different type in this tutorial i'm going to show you a bunch of cool things you can do with strings in python so let's start by declaring a variable called course and set it to python for beginners now this string that we have over here is technically an object an object in python is like an object in the real world as a metaphor think of the remote control of your tv this remote control is an object and it has a bunch of capabilities it has a bunch of buttons for turning your tv on turning it off changing the volume and so on now in this program this course variable is storing a string object this string object has a bunch of capabilities so if we type course dot you can see all the capabilities available in a string object these are basically functions that you can call just like the print or input functions the difference is that the print and input functions are general purpose functions they don't belong to a particular object but the functions you see over here are specific to strings now more accurately we refer to these as methods so when a function is part of an object we refer to that function as a method so let's look at a few examples here we have a function or a method called upper and we use that to convert a string to uppercase so if we print course.upper and run this program they can see our course in uppercase pretty useful now what you need to understand here is that this upper method does not change our original string it will return a new string so right after this if we print course you can see that our course variable is not affected so the upper method returns a new string now similarly we have another method called lower for converting a string to lowercase we have a method called find to see if our string contains a character or a sequence of characters for example here we can pass y and this will return the index of the first occurrence of y in our string so in python the index of the first character in a string is 0. so here we have 0 1 2 3 4 and so on so when we run this program you're going to see one on the terminal because the index of y is 1. take a look first i'm going to delete this line we don't need it anymore also let's do this line let's run the program there you go now as i told you before python is sensitive to lowercase and uppercase letters so if i pass an uppercase y here this find method returns negative 1 because we don't have an uppercase y in this string we can also pass a sequence of characters for example 4. so this will return the index of the word 4. take a look so it's 7. now there are times we want to replace something in a string with something else to do that we use the replace method replace so we can replace 4 with a string containing the number 4. take a look so python for beginners obviously if you look for a character or a sequence of characters that don't exist in our string nothing is going to happen for example if we try to replace x with 4 obviously we don't have x here so nothing is going to happen also just like the upper method the replace method is not going to modify our original string so it's going to return a new string this is because strings in python and many other programming languages are immutable we cannot change them once we create them whenever we want to change your string we'll end up with a new string object in memory now one last thing i want to cover in this tutorial there are times you want to see if your string contains a character or a sequence of characters one way to do that is using the find method that we talked about so let's see if our string contains python now when we run this program that is the index of the first occurrence of the word python in our string now in python we can also use the in operator so we can write an expression like this we type a string python then we type in this is a special keyword in python this is what we call the in operator so after that we type the name of our variable so with this expression we're checking to see if we have python in course as you can see python code is very readable it's like plain english so when we run this program instead of seeing the index of the first occurrence of python we see a boolean value this is more desirable in a lot of cases next we're going to look at arithmetic operations in this tutorial i'm going to show you the arithmetic operators that we have in python these are the same arithmetic operators that we have in math for example we can add numbers we can subtract them multiply them and so on so let's print 10 plus 3. let me run this program we have 13. so this is the addition operator we also have subtraction we have multiplication and division now technically we have two different types of division operators we have a division with one slash and another with two slashes let's look at the differences if you use a single slash we get a floating point number that is a number with a decimal point but if we use double slashes we get an integer a whole number we also have the modulus operator that is indicated by a percent sign and this returns the remainder of the division of ten by three so that is one and finally we have the exponent operator that is indicated by two asterisks so this is 10 to the power of three so when we run this we get a thousand now for all these operators that you saw we have an augmented assignment operator let me explain what it means so let's say we have a variable called x and we set it to 10. now we want to increment the value of x by 3. so we have to write code like this x equals x plus 3. when python executes this code it's going to evaluate this expression or this piece of code the result of this expression is 10 plus 3 which is 13. then it will store 13 in the x now there is another way to achieve the same result using less code we can type x plus equal three what we have on line three is exactly identical to what we have on line two so what we have here is called the augmented assignment operator so we have this assignment operator but we have augmented or enhanced it now here we can also use subtraction to decrease the value of x by 3 we can use multiplication and so on so these are the arithmetic operators in python all right let me ask you a question i'm going to declare a variable called x and set it to 10 plus 3 times 2. what do you think is the result of this expression this is a basic math question that unfortunately a lot of people fail to answer the answer is 16. here's the reason in math we have this concept called operator precedence and that determines the order in which these operators are applied so multiplication and division have a higher order so this part of the expression gets evaluated first so 2 times 3 is 6 and then the result is added to 10. that is why the result of this expression is 16. now in python operator precedence is exactly like math but we can always change it using parenthesis for example in this expression if you want 10 plus 3 to be evaluated first we can wrap it in parenthesis so like this now when we execute this code we're going to see 26 because 10 plus 3 is 13 and that divided by 2 is 26. let's verify this so print x and we get 26. so you learn about the arithmetic operators in python now in python we have another set of operators called comparison operators we use these operators to compare values let me show you so i'm going to declare a variable called x and set it to an expression like this 3 is greater than 2. so what we have here this piece of code this expression is called a boolean expression because it produces a boolean value so in this case because 3 is greater than 2 the result of this expression is the boolean true so if we print x we get true on the terminal so here is the greater than operator we also have greater than or equal to we have less than we have less than or equal to here is the equality operator which is indicated by two equal signs do not confuse this with the assignment operator so here we're comparing three and two for equality so if we run our program we see false because 3 does not equal to 2. so here's the equality operator we also have the not equality operator that is indicated by an exclamation mark followed by an equal sign so let's quickly recap here are the comparison operators we have in python greater than greater than or equal to less than less than or equal to equal and not equal these operators are extremely important in real python programs because quite often we have to compare values to evaluate certain conditions you're going to see that soon in python we have another set of operators called logical operators we use these operators to build complex rules and conditions let me show you so i'm going to declare a variable called price and set it to 25. now let's print a boolean expression like this price is greater than 10. now let's say we want to check to see if the price is between 10 and 30. this is where we use the logical and operator so we type and and right after that we type another boolean expression surprise less than 30. so with this and operator if both these boolean expressions return true the result of this entire expression will be true take a look so in this case we get true because the price is between 10 and 30 dollars we also have the or operator with the or operator if at least one of these boolean expressions returns true then the result of this entire expression will be true to demonstrate this i'm going to change price to 5. let's see how python is going to execute this code so first it's going to look at this boolean expression is price greater than 10 no it's not so it will keep going then it will look at the second boolean expression is price less than 30 it sure is so the result of this entire expression will be true take a look there you go we also have the not operator which basically inverses any values that you give it let me show you so we're going to have one boolean expression price greater than 10. the result of this expression is false now if you apply the not operator this will inverse false to true so when we run the program we get true so let's quickly recap in python we have three logical operators we have logical and which returns true if both expressions return true we have logical or which returns true if at least one expression returns true and we have not which inverses any value that we give it in this tutorial we're going to talk about if statements in python we use if statements to make decisions in our programs for example we can declare a variable called temperature and depending on the value of this variable we can print different messages on the terminal let me show you so here's our temperature variable we set it to 35. now let's say if temperature is greater than 30 we want to print a message saying it's a hot day so we type if then we type a condition and this is where we use our comparison operators so we type temperature greater than 30. then so we add a colon and see what happens when i press enter now the character is indented and this represents a block of code so the code that we write over here will be executed if this condition is true otherwise it's not going to be executed let me show you so we're going to print it's a hot day and by the way note that here i've surrounded the string with double quotes because here we have a single quote as an apostrophe so i couldn't declare a string like this with single quotes if i typed it's a hot day look python gets confused because it thinks this single code represents the end of our string so it doesn't recognize the subsequent characters okay so that's why we use double quotes here so we can have an apostrophe in our string so it's a hot day now if i press enter again the carrot is indented so the code that right here will be part of our if block and it will get executed if this condition is true so here we can print a second message drink plenty of water now to terminate this block we press enter and then press shift and tab the carrot is no longer indented so the code that we write here will always get executed no matter what whether this condition is true or not now in c based programming languages like c plus c sharp java and javascript we present a block of code using curly braces so you start a block of code using a left brace and then end it using a right brace in python we don't have curly braces so we use indentation to represent a block of code okay so in this case these two lines are indented and that means they are part of this block of code now let's run the program and see what happens so we see these two messages because the temperature is greater than 30. now if i change the temperature to 25 and run the program again we don't see anything okay now after this block let's print done because this code is not indented it will always get executed it's not part of our if block okay so take a look here's the down message now let's add a second condition so if temperature is not greater than 30 that means it's less than or equal to 30. so i'm gonna add a second condition so if the temperature is between 20 and 30 i want to print it's a nice day so here we type l if that is short for else if and here we type a second condition so temperature greater than 20. we add a colon press enter now we have a new block so here we can print it's a nice day so if this condition is true that means the temperature is greater than 20 and less than or equal to 30. now what we have here is called a comment that is why it's grayed out it's not real code it's just some note that we add to our program python is not going to execute this so whenever we type a pound sign what we have after is treated as a comment okay so if this condition is true then we're going to see this message on the terminal let's run our program and verify this there you go the temperature is 25 that's why we see this message now we can have as many conditions as we want there are no limitations okay so let's add another condition l if temperature is greater than 10 then we're going to print it's a bit cold now in this case if this condition is true that means the temperature is greater than 10 and less than or equal to 20. now finally if the temperature is less than 10 let's print a message saying it's a cold day so here we type else then we add a colon and now we have a new block so this code will get executed if none of the above conditions are true so here we can print it's called so this is how we use if statements to make decisions in our programs here's a great exercise for you to practice what you have learned so far i want you to write a weight converter program like this so this program is asking me my weight i enter 170. next it's asking me if the weight is in kilograms or pounds so i can type k for kilograms or l for pounds i can type a lowercase l or an uppercase l it doesn't matter so let's go with a lowercase l now it tells me weight in kilogram is 76.5 so go ahead and spend 5 minutes on this exercise you can use the comment box below to share your code with others and then when you're done come back see my solution so first we call our input function to ask the first question wait we get the result and store it in a variable called weight next we call the input function one more time to ask the second question is this in kilogram or pounds we get the result and store it in a variable called unit this is where we're going to use an if statement so we want to check to see if unit equals k then we should convert the weight to pounds and print it on a terminal however with this code if i type a lowercase k this condition is not going to be true because earlier i told you that python is a case sensitive language so we need to convert this string to uppercase earlier we talked about string methods so if we type dot we can see all the functions or methods available in a string object so we use the upper method and this returns a new string in case now if this condition is true first we need to convert the weight to pounds so we declare a new variable called converted get the weight and divide it by 0.45 and then we can print this on a terminal so we say weight in pounds is then we append converted now to terminate this block we press shift and tab else colon so if this condition is not true that means the weight was entered in pounds so we need to convert it to kilograms once again we declare a variable converted and set it to weight times 0.45 and then we print weight in kilograms and here we concatenate this string with converter all right now we need to terminate this block so we press enter then shift and tab good now if we run this program we're going to see an error let me show you so let's run it here i'm going to enter 170 then i type a lowercase l okay here's an error can't multiply sequence by non-end of type float so this is where we got this error when we try to multiply the weight by 0.45 so that is line seven in our code now the reason this is happening is because the wait variable is storing a string object because earlier i told you the input function always returns a string so here we need to convert the weight to a number we can either use the end or the float function let's run the program one more time so 170 in pounds here's the second error can only concatenate string not float to string and that error occurred over here when we try to print the weight in kilograms so look at line seven in this case weight as an integer we are multiplying an integer by a float and the result of this operation is going to be a float now on line eight we are trying to concatenate or combine a string with a float and python doesn't know how to execute this code to solve this problem we need to convert this flow to a string so here we use the built-in stir function let's run the program and see what happens so 170 in pounds and here's my weight in kilograms now let's try entering a weight in kilograms so run it one more time let's say 76 kilos here we get a familiar error can only concatenate string to string not float so this error occurred on line five where we try to concatenate a string to a float so once again we need to convert this to a string object now let's run the program one more time 76 kilos is equal to 168 pounds hey guys i just wanted to let you know that i have an online coding school at codewoodmosh.com where you can find plenty of courses on web and mobile development in fact i have a comprehensive python course that teaches you everything about python from the basics to more advanced concepts so after you watch this tutorial if you want to learn more you may want to look at my python course it comes with a 30 day money back guarantee and a certificate of completion you can add to your resume in case you're interested the link is below this video in this tutorial we're going to talk about while loops in python we use while loops to repeat a block of code multiple times for example let's say we want to print the numbers one to five a poor way of doing this is writing code like this print one then print two print 3 print 4 and print 5. now why is this a bad approach well what if we wanted to print the numbers 1 to 1 million we don't want to write 1 million lines of code each line printing a number this is where we use while loops so we'll start off by declaring a variable like i and set it to our initial number let's say one then we type while and here we type a condition so once again we can use our comparison operators let's say i less than 5 less than or equal to 5. as long as this condition is true then the code that we write inside of the while block will get executed so here we type a colon then press enter now we have a block of code in this block we can print i and then we need to increment i y one so we set i to i plus one if you don't do this i will always be one and this block of code will get executed indefinitely it will never terminate basically our program will continue running until it runs out of memory so in the first iteration i is 1 is less than 5 so python is going to execute this block of code it will print 1 on the terminal and then i becomes 2. then the control moves back over here so python evaluates this condition 2 is less than or equal to 5 so the condition is true and once again python is going to execute this block one more time in the second iteration we're going to see two on the terminal and then i will become three so this will continue until this condition is no longer true let me show you so let's run this program now we see the numbers one to five so this is the beauty of while loops i can easily change five to one thousand and by the way i'm separating these three digits using an underscore this makes my number more readable we don't have to add this but it just makes our code more readable so let's run the program now we see the numbers 1 to 1000. it's much easier than 1000 lines of code each printing a number okay now let me show you something really cool so i'm going to change this to 10. now instead of printing i i want to print an expression so i'm going to multiply i by a string an asterisk now you might be confused here because earlier i told you that in python we cannot concatenate a number to a string but here i'm using the multiplication operator well this is different we can multiply a number by a string and this will repeat that string based on the value of that number so if i is one we're gonna see one asterisk if i is five we're going to see five asterisks let me show you so when we run this program we see this triangle shape because in the first iteration i is one so we see one asterisk in every iteration i is incremented by one so we see one extra asterisk and finally in the last iteration i is 10 and we see 10 asterisks on the terminal so you have learned about three types of data in python we have numbers which can be integers or floats we have booleans and strings these are what we refer to as primitive or basic types in python now in python we have a bunch of complex types as well these complex types are very useful in building real applications in this tutorial we're going to talk about lists we use lists whenever we want to represent a list of objects like a list of numbers or a list of names let me show you so i'm going to declare a variable called names and set it to a list of names so here we add square brackets to represent a list and now we can add one or more objects inside this list in this list we add a bunch of names like john bob marsh sam and mary so we separate this element these items using a comma okay now let's print our list so print names take a look it comes out exactly like how we wrote it now we can also get individual elements in this list for example if we want to get the first element in this list here we type a pair of square brackets and inside the square brackets we type an index the index of the first element in this list is zero so now when we run this program we're going to see john on the terminal there you go now in python we can also use a negative index this is a feature that i personally have not seen in other programming languages so if 0 represents the first element in this list what do you think negative 1 represents it represents the last element in this list let's verify it so let's run the program and here's mary what about negative 2 well that represents the second element from the end of the list so let's run the program and here's some then we can also change an object at a given index for example let's say here we made a mistake and this john should not be spelled with an h so we need to reset it we type names of zero now we treat this like a regular variable so we set it to a new value we set it to john without an h now let's print our list and here's our updated list beautiful we can also select a range of values for example let's say we're only interested in the first three names so over here we type square brackets and here we need to type two indexes a start index and an end index our start index is a zero because we want to start from here and our end index is going to be zero one two plus one that's going to be three so we add a colon three so python is going to return all the elements from the start index up to the end index but excluding the end index so it's going to return the elements at index 0 1 and 2. take a look here are the first three names and by the way this expression does not modify our original list it returns a new list so right after this print statement if we print our original list you can see that it's not changed so this is how we use lists in python earlier i told you that strengths in python are objects objects in programming are kind of like objects in the real world like your mobile phone your bicycle the remote control of your tv and so on they have certain capabilities so if we type a string here and then press dot we can see all the functions or methods available in a string object in python now lists are also objects so they have a bunch of methods for adding items or removing them and so on let me show you so i'm going to declare a list of numbers let's say 1 2 3 4 and 5. now to add a new element at the end of this list we can use the append method so we type numbers that append and here we type 6. now let's print our list so here's our updated list beautiful now what if you want to insert a number somewhere in the middle or at the beginning for that we use the insert method so we're going to call the insert method now on the top go to the view menu and look at parameter info look at the shortcut on a mac computer it's command and p on windows it's probably control and p if we use this shortcut we can see the values that this method expects so the first value that this method expects is an index value and the type of this value is an integer so if i want to insert a value at the beginning of this list i should pass 0 as the index of the first element right so let's pass 0. now the second value is highlighted so the second value is an object and the type of this is t that basically means this can be any type we can pass a number we can pass a boolean we can pass a string we can pass a list or any type of objects in python so i'm going to pass negative 1. now let's run our program you can see negative 1 appeared at the beginning of our list we also have a method for removing items so let's call remove three let's run the program three is gone we only have one two four and five now if you wanna remove all the items in the list we call the clear method so clear this method doesn't expect any values so let's run our program our list is empty now sometimes you want to know if a given item exists in our list or not to do that we use the in operator so let's remove this line instead of printing our numbers list i'm going to print an expression one in numbers so here we're using the in operator we're checking to see if one is in the numbers list so this is a boolean expression it returns a boolean value take a look so we get true obviously if we search for a value that doesn't exist in this list like 10 we get false okay now finally there are times you want to know how many items you have in the list to do that you can use the built-in lend function so let's print len of numbers so len is a built-in function just like the print function that is why it's highlighted as purple it returns the number of elements in a list take a look so we have five elements in this list when writing python programs there are times you want to iterate over a list and access each item individually let me show you so i'm going to declare a list of numbers one two three four five now if we print this list it comes out exactly like how we wrote it using the square bracket notation but what if we wanted to print each item on a separate line that is where we use the for loop let me show you so we're not going to print the entire list instead we're going to type 4 now we declare a variable which is called a loop variable let's call it item then we type in numbers next we add a colon to start a block of code so this is what we call a for loop with this for loop we can iterate over all the items in this list in each iteration this item variable is going to hold one value so in the first iteration item is going to be equal to 1 and the second iteration is going to be equal to 2 and so on so now if we print item and run our program we see each item on a new line so this is how we use a for loop now we could also achieve the same thing using a while loop but our code would be a little bit longer let me show you so we would have to start by declaring a loop variable outside of our while loop let's say i we set it to zero now we say while i is less than here we need to find out how many items we have in this list so we use the len function len of numbers as long as i is less than the length of the list print now we can use the square bracket notation to get the element at this index so numbers of i now we need to increment i by one so i equals i plus one let's run the program and see what we get so we get the numbers one to five these are coming out from our for loop and then we get the numbers one to five one more time these are coming out from our while loop now if you compare these two approaches you can definitely see that the implementation using the for loop is shorter and easier to understand we don't have to use the square bracket notation we don't have to call the len function we don't have to declare a variable a loop variable and then increment it explicitly so with the for loop in each iteration the item variable will automatically hold one value in this list in this tutorial we're going to talk about the range function in python we use the range function to generate a sequence of numbers let me show you so you type range this is a built-in function just like the print and input functions here we can pass a value like 5 and this will return a range object a range object is an object that can store a sequence of numbers let me show you so let's call that numbers so this is a range object in this object we're going to have the numbers 0 to 5 but excluding 5. now if we print numbers we're going to see this message range of zero to five not the actual numbers because this is the default representation of a range object to see the actual numbers we need to iterate over this range object using a for loop in the last video you learned how to iterate over a list using a for loop but we can also iterate over a range object using a for loop basically we can use the for loop with any object that represents a sequence of objects so instead of printing numbers we're going to use a for loop for number in numbers colon we're going to print number take a look now we see the numbers zero to four so range of five generates a sequence of numbers starting from zero up to the number we specify here now if we supply two values the first value is going to be considered the starting value and the second value is going to be considered the ending value and it's going to be excluded so range of 5 to 10 is going to generate the numbers 5 to 9. take a look there you go now we can also supply a third value and that will be used as a step so let's say instead of having a sequence of numbers like five six seven eight nine we wanna jump two numbers at a time so 5 7 9 and so on so here we pass 2 as the step take a look so we get these odd numbers 5 7 and 9. so this is the range function in python like quite often you see the range function used as part of a for loop because we don't really need to store the result in a separate variable we can call the range function right here where we are using the numbers variable so we can type range of five and this will return a range object holding the numbers zero to four take a look there you go so we don't really need to store the result in a separate variable like numbers there you go in this tutorial we're going to talk about tuples in python tuples are kind of like lists we use them to store a sequence of objects but tuples are immutable which means we cannot change them once we create them let me show you so i'm going to start by defining a list of numbers 1 2 3. now we use square brackets to define a list and parenthesis to define a tuple so now this numbers variable is storing a tuple if we try to reassign let's say the first element we're going to get an error topple object does not support item assignment so this is what i meant by tuples are immutable they're unchangeable also if you type numbers dot you don't see any methods like append insert remove and so on we only have count and index count returns the number of occurrences of an element for example if we have let's say two threes in this tuple and call count of three this will return 2. the other method we have here is index and this returns the index of the first occurrence of the given element now these other methods you see here that start with an underscore they're called magic methods it's an advanced topic and i've covered it in detail in my complete python programming course if you're interested the link is down below this video so tuples are immutable we cannot change them once we create them now practically speaking most of the time you would be using lists but there are times that once you create a list of objects you want to make sure that somewhere in your program you or someone else is not going to accidentally modify that list if that's the case then you should use a tuple hey guys i just wanted to let you know that i have an online coding school at codewoodmarch.com where you can find plenty of courses on web and mobile development in fact i have a comprehensive python course that teaches you everything about python from the basics to more advanced concepts so after you watch this tutorial if you want to learn more you may want to look at my python course it comes with a 30 day money back guarantee and a certificate of completion you can add to your resume in case you're interested the link is below this video hey