0:02 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 Hamedani, 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, 1:00 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 comments 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. 2:00 Now, here you need to enter the username and 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 3:01 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 app. 4:04 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 these quotes, 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, 5:05 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. 5:34 All right, 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 parentheses and type age. Note that I'm not adding quotes because if I run this program, we'll see the text age on the terminal. We don't want that. We want the value of the age variable. So, let's remove the quotes 6:35 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 control, 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 two we can set age 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 7:35 first_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 Mosh. 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. 8:38 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 you 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. 9:12 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 parentheses then we type a string. Here, we can type a message like what is your name? We add a question mark followed by a space. You'll 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, 10:14 we use the print function. We say, "Hello." We add a space. Now, after this string, we want to add the value of the name variable. So, we use the plus sign and then type name. What we're 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. 10:52 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 year." Now, this input function is going to return a value, so we can store it in a variable called birth_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. 11:52 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.py, 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 str. What are these? Well, this int 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 will 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 12:53 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 tried to subtract a string from an integer. So, our code looked 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 int function. We can pass our birth year to it, and this will return the numeric representation of the birth year. So, to solve this problem, we need to replace the string with the int 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 int function. The int function 13:54 will return the numeric representation of the birth 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 int 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 str 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. 14:55 You 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 1020. 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. 15:56 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, we'll get 1020 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 int 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 16:57 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 a string. So, this is where we use the str function. Now, let's run the program again. So, 10 plus 20.1, and here's the result. 17:58 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. You 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 parentheses 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 a variable to a different type. 18:54 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 you 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 a string. Now more accurately, we refer to these 19:54 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 upper case. So if we print course.upper and run this program, you can see our course in upper case. 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 lower case. 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, 20:57 in Python, the index of the first character in a string is zero. 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 one. Take a look. First I'm going to delete this line. We don't need it anymore. Also, let's delete this line. Let's run the program. There you go. Now as I told you before, Python is sensitive to lower case and upper case letters. So if I pass an upper case Y here, this find method returns -1 because we don't have an upper case Y in this string. We can also pass a sequence of characters. For example, four. So this will return the index of the word four. Take a look. So it's seven. 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 four with a string containing the number four. Take a look. 21:59 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 you try to replace X with four, 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 a 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 23:01 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. 23:46 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 + 3. When we run this program, we get 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 you 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 10 by 3. So that is one. And finally, we have the exponent operator that is indicated by two 24:46 asterisks. So this is 10 to the power of three. So when we run this, we get 1,000. 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 three. So we have to write code like this. X = X + 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 + 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 + = 3. 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 25:48 subtraction to decrease the value of X by three. We can use multiplication and so on. So these are the arithmetic operators in Python. 26:03 All right, let me ask you a question. I'm going to declare a variable called X and set it to 10 + 3 * 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 * 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 parentheses. For example, in this expression, if you want 10 + 3 to be evaluated first, we can wrap it in parentheses. So, like this. Now when we execute this code, we're going to see 26 because 10 + 3 is 13 and 27:03 that divided by 2 is 26. Let's verify this. So print X and we get 26. 27:15 So you learned 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's 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 three does not 28:17 equal to two. 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. 28:56 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. So price 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. 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 29:56 true. To demonstrate this, I'm going to change price to five. 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, let me 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 30:57 if at least one expression returns true. And we have not, which inverses any value that we give it. 31:10 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 caret 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 32:11 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 the single quote 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 caret is indented, so the code that we write 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 caret 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 33:13 like C++, C#, Java, and JavaScript, we represent 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 done message. Now, let's add a second condition. 34:14 So, if temperature is not greater than 30, that means it's less than or equal to 30. So, I'm going to 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 35:15 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 cold. So, this is how we use if statements to make decisions in our programs. 36:20 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 show your code with others. And then when you're done, come back and see my solution. So, first we call our input function to ask the first question. Weight. 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? 37:22 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 the 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 you 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 uppercase. 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 the terminal. So, we say weight 38:22 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 the string with converted. All right. Now, we need to terminate this block, so we press enter, then shift and tab. Good. Now, if you 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-int of type float. So, this is where we got this error. When we tried to multiply 39:23 the weight by 0.45. So, that is line seven in our code. Now, the reason this is happening is because the weight 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 int 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 is an integer. We're multiplying an integer by a float, and the result of this operation is going to be a float. Now, on line eight, we're trying to concatenate or combine a string with a float, and Python doesn't know how to 40:23 execute this code. To solve this problem, we need to convert this float to a string. So, here we use the built-in str 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, running 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 lbs. Hey guys, I just wanted to let you know that I have an online coding school at codewithmosh.com, where you can find plenty of courses on web and mobile development. In fact, I have a 41:24 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 you 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 three, print four, and print five. Now, why is this a bad approach? Well, what if you wanted to print the numbers one to 1 million? We don't want to write 1 million lines of code, each line printing a number. This is where you use while loops. So, we 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 42:25 condition. So, once again, we can use our comparison operators. Let's say I less than five. Less than or equal to five. 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 and press enter. Now, we have a block of code. In this block, we can print I, and then we need to increment I by one. So, we set I to I + 1. If we 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 one, is less than five, so Python is going to execute this block of code. It will print one on the terminal, and then I becomes two. Then the control moves back over here. So, Python evaluates this condition. Two is less than or equal to five, so the condition is true, and once again, 43:25 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 1 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 one to 1 thousand. It's much easier than 1 thousand 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 44:27 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 going to 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. 45:15 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 they 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, Mosh, Sam, and Mary. So, we separate these elements, these items, using a comma. Okay? Now, let's print our list. So, print names. Take a look. 46:16 It comes out exactly like how we wrote it. Now, we can also get individual elements in this list. For example, if you 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 zero represents the first element in this list, what do you think negative one 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 two? Well, that represents the second element from the end of the list. So, let's run the program, and here's Sam. Now, we can also change an object at a 47:17 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 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 48:19 index zero, one, and two. 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, we can see that it's not changed. So, this is how we use lists in Python. 48:51 Earlier, I told you that strings 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 you 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, for removing them, and so on. Let me show you. So, I'm going to declare a list of numbers. Let's say one, two, three, four, and five. Now, to add a new element at the end of this list, we can use the append method. So, we type numbers dot append, and here we type six. 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. 49:52 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 you want to insert a value at the beginning of this list, I should pass zero as the index of the first element, right? So, let's pass zero. 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 -1. Now, let's run our program. We can see -1 appeared at the beginning of our list. We also have a method for removing items. So, 50:54 let's call remove three. Let's run the program. Three is gone. We only have 1 2 4 and 5. Now, if you want to 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 a list. To do that, you can use the 51:55 built-in len 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. 52:20 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, 1 2 3 4 5. 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 for. 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 one, in the second iteration is going to be equal to two, and so on. 53:21 So, now if you 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. So, 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 + 1. Let's run the program and see what we get. So, we get the numbers 1 to 5. These are 54:21 coming out from our for loop. And then, we get the numbers 1 to 5 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. 54:58 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, we type range. This is a built-in function, just like the print and input functions. Here, we can pass a value like five, 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 zero to five, but excluding five. 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 56:00 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 five to 10 is going to generate the numbers five to nine. 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 5 6 7 8 9, we want to jump two numbers at a time. So, 5 7 9, and so on. 57:00 So, here we pass two 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. 57:47 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 parentheses 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. Tuple object does not support item assignment. So, this is what I meant by tuples are immutable. They're unchangeable. Also, if we 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, 58:47 and call count of three, this will return two. 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 codewithmosh.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, 59:47 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 Mosh, hi Mosh.