Python for Beginners - Learn Coding with Python in 1 Hour — Transcript
Full transcript
- 0:02In this Python tutorial, you're going to
- 0:03learn everything you need to know to
- 0:05start programming in Python. If you want
- 0:07to learn Python programming for data
- 0:08science, machine learning, or web
- 0:10development, this Python tutorial is the
- 0:12perfect place to learn Python. You don't
- 0:14need any prior knowledge in Python or
- 0:16programming in general. I'm going to
- 0:18teach you everything from scratch. I'm
- 0:20Mosh Hamedani, and I've taught millions
- 0:22of people how to code through this
- 0:23channel. If you're new here, make sure
- 0:25to subscribe as I upload new videos
- 0:26every week. Now, let's jump in and get
- 0:28started.
- 0:30All right, before we get started, let me
- 0:31give you some ideas about what you can
- 0:32do with Python. That's a very common
- 0:34question. Python is a multi-purpose
- 0:36programming language, so you can use it
- 0:38for a variety of different tasks. You
- 0:40can use Python for machine learning and
- 0:42AI. In fact, Python is the number one
- 0:44language for machine learning and data
- 0:45science projects. Python is also very
- 0:47popular in web development. Using Python
- 0:50and a framework called Django, you can
- 0:52build amazing websites. Here are five
- 0:54websites powered with Python and Django:
- 0:56YouTube, Instagram, Spotify, Dropbox,
- 1:00and Pinterest. You can also use Python
- 1:02in automation. With Python, you can save
- 1:04your time and increase your productivity
- 1:06by automating repetitive tasks. So, why
- 1:09are you learning Python? Are you
- 1:10learning it for automation, for data
- 1:12science, or web development? Let me know
- 1:14in the comments section below.
- 1:19All right, the first thing I want you to
- 1:20do is to head over to python.org to
- 1:23download the latest version of Python.
- 1:26So, you go to downloads and select the
- 1:28latest version of Python.
- 1:33Here in your downloads folder, you
- 1:34should see this package. Simply
- 1:36double-click it.
- 1:38You're going to see this Python
- 1:38installer.
- 1:40If you're on Windows, you will see this
- 1:41checkbox over here, add Python to path.
- 1:45Make sure to check it. It's really
- 1:46important. Otherwise, you're not going
- 1:48to be able to follow this tutorial.
- 1:51Simply click on continue
- 1:52again
- 1:53one more time
- 1:55I agree with the terms
- 1:57and install the latest version of
- 1:59Python.
- 2:00Now, here you need to enter the username
- 2:02and password of your computer. So,
- 2:05let's do that real quick.
- 2:09Next, you need to install a code editor.
- 2:11We use a code editor to write our code
- 2:13and execute it. The most popular code
- 2:15editor for Python is PyCharm. You can
- 2:17get it from jetbrains.com/pycharm.
- 2:21So, on this page
- 2:23click on download.
- 2:26You should see two different editions.
- 2:27One is the professional edition, which
- 2:29is commercial. And we also have this
- 2:31community edition, which is free and
- 2:33open source. So, we're going to download
- 2:35the community edition.
- 2:39Now, in your downloads folder, you
- 2:41should have this package. Let's
- 2:42double-click it.
- 2:45If you're on Windows, you're going to
- 2:46see an installation wizard. So, simply
- 2:48click on the next button until you
- 2:50install PyCharm. If you're on a Mac, you
- 2:53need to drag this PyCharm and drop it
- 2:55onto the applications folder.
- 3:00Now
- 3:01let's open it.
- 3:03The first time you open PyCharm, you
- 3:04have to configure a few settings. We
- 3:06don't want to spend time on this. So,
- 3:08over here, we're going to click on skip
- 3:10remaining and set defaults.
- 3:14Now, let's create a new project.
- 3:16Over here, we can specify the location
- 3:18and the name of our Python project. So,
- 3:22let's append hello world to this path.
- 3:25This is where our Python project is
- 3:26going to be saved. So, let's click on
- 3:29create.
- 3:32In this window, you can see the content
- 3:34of our project. So, here's our hello
- 3:36world project. Currently, we have only
- 3:38one folder inside this project, that is
- 3:40VN, which is short for virtual
- 3:42environment. We'll talk about virtual
- 3:43environments in the future. So,
- 3:45currently, we don't have any Python
- 3:47files inside this project. A real
- 3:49application can consist of tens or
- 3:51hundreds or even thousands of Python
- 3:53files. So, let's right-click on the
- 3:55project name
- 3:57and go to new Python file.
- 4:00We're going to call this file app.
- 4:04Now, we can collapse this project window
- 4:05by clicking on this icon.
- 4:07So, now we have more space. Let's write
- 4:09our first Python code.
- 4:11We're going to write print, all in
- 4:12lowercase
- 4:14then add parentheses
- 4:15then add quotes, either single quotes or
- 4:18double quotes.
- 4:20And inside these quotes, we're going to
- 4:21write hello world.
- 4:23So, this is what we call a string. A
- 4:25string means a string or sequence of
- 4:28characters. In simple words, that means
- 4:30textual data. So, in Python and in many
- 4:33other programming languages, whenever
- 4:35we're dealing with textual data, we
- 4:37should always surround our text with
- 4:39quotes.
- 4:40In Python, we can use single or double
- 4:42quotes.
- 4:44Now, this print you see here is a
- 4:45function built into Python, and we can
- 4:48use it to print a message on our
- 4:49application window.
- 4:51So, let me show you how to run this
- 4:52code.
- 4:54On the top we go to the run menu and
- 4:56then select run. Note that there is a
- 4:59shortcut associated with this command. I
- 5:01always use shortcuts because they
- 5:02increase my productivity. So,
- 5:05let's click on this. Now, select app
- 5:09and over here
- 5:10you can see this little window. This is
- 5:12what we call the terminal window, and it
- 5:14shows the output of our program.
- 5:16So, here's the hello world message
- 5:18printed in the terminal window. Now, as
- 5:20you learn more Python, you will learn
- 5:22how to build applications that have a
- 5:23graphical user interface. That's an
- 5:25advanced topic, so for now, let's not
- 5:27worry about it.
- 5:34All right, now let's talk about
- 5:35variables. We use variables to
- 5:37temporarily store data in a computer's
- 5:40memory. For example, we can store the
- 5:42price of a product or someone's name,
- 5:44their email, their age, and so on. Let
- 5:47me show you. So,
- 5:48to declare a variable, we start by
- 5:50typing a name for that variable. Let's
- 5:52say age.
- 5:54Then we add an equal sign
- 5:56and then we type a value. Let's say 20.
- 5:59So, with this, we're storing the number
- 6:0020 somewhere in our computer's memory
- 6:03and we're attaching this age as a label
- 6:05for that memory location. So, now we can
- 6:08read the value at this memory location
- 6:10and print it on the terminal. So,
- 6:12instead of printing hello world, we want
- 6:14to print the value of the age variable.
- 6:17So, I'm going to delete what we have
- 6:19inside parentheses
- 6:21and type age. Note that I'm not adding
- 6:25quotes because if I run this program,
- 6:27we'll see the text age on the terminal.
- 6:29We don't want that. We want the value of
- 6:31the age variable. So,
- 6:34let's remove the quotes
- 6:35and print the value of the age variable.
- 6:38Now, here on the toolbar, you can click
- 6:41on this play icon to run your program,
- 6:43or you can use the shortcut that I
- 6:44showed you in the last video. So, the
- 6:47shortcut is over here. On a Mac, that's
- 6:49control, shift, and R.
- 6:52So,
- 6:53there you go.
- 6:55Now, you can see the value of the age
- 6:56variable.
- 6:58Now, we can also change the value of a
- 6:59variable. For example, on line two
- 7:02we can set age to 30.
- 7:05Now, when we run our program
- 7:07we see 30. So, as you can see, our
- 7:09program gets executed from top to
- 7:11bottom.
- 7:13So, this is how we can declare and use a
- 7:14variable. Now, let's look at a few more
- 7:16examples.
- 7:17So, I'm going to declare another
- 7:19variable called price and set it to
- 7:2219.95.
- 7:25So, in Python, we can use numbers with a
- 7:26decimal point or whole numbers.
- 7:30We can also declare a variable and
- 7:32assign it a string value. So, let's say
- 7:35first_name.
- 7:38So, if you want to use multiple words in
- 7:40the name of a variable, we should
- 7:41separate them using an underscore. This
- 7:44makes our code more readable. See what
- 7:46would happen if I didn't use this
- 7:48underline.
- 7:50This is not easily readable. So, we
- 7:52always separate multiple words by an
- 7:54underscore.
- 7:56Now, we set this to a string. So, we can
- 7:58use single quotes or
- 8:00double quotes.
- 8:01Let's say Mosh.
- 8:02We also have a special type of value
- 8:04called a boolean value, which can be
- 8:06true or false. That is like yes or no in
- 8:09English. Let me show you. So,
- 8:12I'm going to declare another variable
- 8:14called is_online
- 8:16and set it to true.
- 8:18We could also set it to false. What we
- 8:21have here is called a boolean value.
- 8:23Now, note that Python is a
- 8:25case-sensitive language, so it's
- 8:27sensitive to lowercase and uppercase
- 8:29letters. In this case, if I use a
- 8:31lowercase f
- 8:34we can see an error over here because
- 8:35this is not recognized in Python.
- 8:38So, false with a capital F is a special
- 8:40keyword in Python that represents the
- 8:43boolean false value.
- 8:45So, this is how we can declare and use
- 8:47variables in Python.
- 8:48All right, now here's a little exercise
- 8:50for you. Imagine you want to write a
- 8:52program for a hospital. So, we're going
- 8:54to check in a patient named John Smith.
- 8:56He's 20 years old and is a new patient.
- 8:59I want you to declare a few variables to
- 9:01store these values. Use the comment box
- 9:03below to share your code with others.
- 9:12In this tutorial, I'm going to show you
- 9:13how to receive input from the user.
- 9:16So, in Python, we have another built-in
- 9:18function called input. We use this to
- 9:20read a value from the terminal window.
- 9:22Let me show you. So,
- 9:24we add parentheses
- 9:26then we type a string. Here, we can type
- 9:28a message like what is your name? We add
- 9:31a question mark followed by a space.
- 9:34You'll see why we need this space in a
- 9:35second. So, let's run this program.
- 9:39We get this message. Now, we have to
- 9:41enter a value. So, we click over here.
- 9:44Now, you can see that the carrot is
- 9:45separated from the question mark. This
- 9:47is because of the white space that we
- 9:49added over here.
- 9:51So, now we have to type a value. Let's
- 9:53say John. When we press enter
- 9:56this function will return the value that
- 9:58we entered in the terminal window. So,
- 10:01we can get that value and store it in a
- 10:02variable.
- 10:04So, let's declare a variable called name
- 10:06and set it to the return value of the
- 10:09input function.
- 10:10Now, we can print a greeting message for
- 10:12this user. So,
- 10:14we use the print function.
- 10:16We say, "Hello."
- 10:18We add a space.
- 10:20Now, after this string, we want to add
- 10:22the value of the name variable. So, we
- 10:24use the plus sign and then type name.
- 10:27What we're doing here is called string
- 10:30concatenation. So, we're combining this
- 10:32string with another string.
- 10:35Now, let's run our program and see what
- 10:36happens. So, what is your name? Mosh.
- 10:40Now,
- 10:41we get this message, "Hello Mosh."
- 10:43So, this is how we can use the input
- 10:44function in Python.
- 10:52You'll learn about the three types of
- 10:53data in Python. We have numbers,
- 10:56strings, and booleans.
- 10:59Now, there are times you want to convert
- 11:00the value of a variable from one type to
- 11:02another. Let me show you. So, we're
- 11:05going to use our input function
- 11:07to read the user's birth year. So,
- 11:10"Enter your birth year."
- 11:13Now, this input function is going to
- 11:15return a value, so we can store it in a
- 11:17variable called
- 11:19birth_year.
- 11:21Okay?
- 11:22Now, let's write code to calculate the
- 11:24age of this user. So, we write an
- 11:26expression like this. Currently, we are
- 11:28in the year 2020. So, 2020 minus
- 11:32birth year.
- 11:34This expression or piece of code is
- 11:35going to produce a value. So, once
- 11:37again, we can store that value in a
- 11:39variable. Let's call that variable age.
- 11:43Now, let's print age
- 11:45on the terminal.
- 11:46Let's run our program and see what
- 11:47happens.
- 11:49So, my birth year is 1982.
- 11:52Enter. Oops, our program crashed. So,
- 11:55whenever you see this red message, that
- 11:57indicates an error.
- 11:59So, this error occurred in this file,
- 12:02that is our app.py, on line two.
- 12:05Right below that, you can see the piece
- 12:07of code that generated this error.
- 12:09So, that is this expression, 2020 minus
- 12:12birth year. Now, below that, you can see
- 12:14the type of error. So, here we have
- 12:17unsupported types for subtraction.
- 12:21We have int and str.
- 12:24What are these? Well, this int is short
- 12:26for integer, and that represents a whole
- 12:28number in programming. So,
- 12:312020 is an example of an integer.
- 12:34Now, birth year
- 12:36is an example of a string because
- 12:38whenever we call the input function,
- 12:40this function will return a value as a
- 12:42string, even if we enter a number. In
- 12:45other words, when I entered 1982, this
- 12:48input function returned a string with
- 12:50these characters, 1982. So, this string
- 12:53is different from the number 1982.
- 12:57They're completely different types. So,
- 12:59in this case,
- 13:00let me delete these lines.
- 13:02The reason we got this error is that we
- 13:04tried to subtract a string from an
- 13:07integer. So, our code looked like this,
- 13:101982.
- 13:12Now, Python doesn't know how to subtract
- 13:14a string from an integer.
- 13:16So, to solve this problem,
- 13:18we need to convert this string to an
- 13:19integer.
- 13:21Now, in Python, we have a bunch of
- 13:22built-in functions for converting the
- 13:24types of our variables. So, we have this
- 13:27int function.
- 13:29We can pass our birth year to it,
- 13:31and this will return the numeric
- 13:33representation of the birth year. So, to
- 13:36solve this problem,
- 13:38we need to replace
- 13:40the string
- 13:42with the int function.
- 13:43So, let's see what's going on here.
- 13:45On the first line, we call the input
- 13:47function. This returns a string.
- 13:49On the second line, we pass the string
- 13:52to our int function. The int function
- 13:54will return the numeric representation
- 13:56of the birth year.
- 13:58Then,
- 13:59we subtract it from 2020, we get the
- 14:01age, and store it in the age variable.
- 14:04Now, let's run our program.
- 14:06So, 1982,
- 14:08and there you go. I'm 38 years old.
- 14:11So, this is how the int function works.
- 14:14Now, we also have another built-in
- 14:16function called float. That is for
- 14:18converting a value to a floating-point
- 14:20number.
- 14:21A floating-point number in Python and
- 14:23other programming languages is a number
- 14:25with a decimal point. So, 10 is an
- 14:29integer, and 10.1 is a float.
- 14:32So, we have int, we have float, and we
- 14:35also have bool
- 14:36for converting a value to a boolean.
- 14:39And finally, we have str
- 14:41for converting a value to a string.
- 14:44So, these are the built-in functions for
- 14:46converting the type of our variables.
- 14:48Now, here's a little exercise for you. I
- 14:50want you to write a basic calculator
- 14:52program.
- 14:53So, here we have to enter two numbers.
- 14:55You can type a whole number or a number
- 14:57with a decimal point.
- 15:00And then our program will print the sum
- 15:02of these two numbers.
- 15:03So, pause the video, spend two minutes
- 15:05on this exercise, and then see my
- 15:07solution.
- 15:11All right, first we're going to call our
- 15:12input function to read the first number.
- 15:15We get the result and store it in a
- 15:16variable called first.
- 15:19Now, let's declare
- 15:21another variable called second and read
- 15:24the second number.
- 15:26Now,
- 15:27we calculate the sum.
- 15:29So, that is first plus second.
- 15:32Now, let's see what happens when we
- 15:33print sum on the terminal.
- 15:36So, I enter 10 and 20,
- 15:39but instead of 30, we get 1020. This is
- 15:42because we're combining or concatenating
- 15:44two strings. So,
- 15:47as I told you before, the input function
- 15:49returns a string. So,
- 15:52this line will be equivalent to first
- 15:55equals 10.
- 15:56We're dealing with a string, not an
- 15:58integer.
- 15:59Similarly,
- 16:00second is going to be
- 16:0220 as a string. So, when we combine two
- 16:05strings, 10 plus 20, we'll get 1020
- 16:10because we're dealing with textual data,
- 16:12okay? So, to solve this problem,
- 16:16we need to convert the values we read to
- 16:18their numeric representation. So, over
- 16:20here,
- 16:23we're going to pass first to our int
- 16:25function,
- 16:27and here as well.
- 16:28Now, let's run our program.
- 16:30So, we enter 10 and 20, we get 30.
- 16:34What if we enter a floating-point
- 16:35number? So, 10.1 and 20.
- 16:40We got an error.
- 16:41So, to solve this problem,
- 16:44we need to treat both these values as
- 16:46floats. So, instead of the int function,
- 16:49we're going to use the float function.
- 16:52Now, let's run our program one more
- 16:54time.
- 16:55We enter a whole number and a
- 16:57floating-point number.
- 16:59So, the result is correct.
- 17:01Now, let's add a label over here. So,
- 17:04sum
- 17:05is
- 17:07plus sum.
- 17:08Let's run our program
- 17:10one more time. 10 and 20.
- 17:12Once again, we got an error. The error
- 17:14is saying that Python can only
- 17:16concatenate strings, not floats to
- 17:18strings.
- 17:21So, on line four, we have a string.
- 17:23We're concatenating this with a float
- 17:26because the result of this expression is
- 17:28a floating-point number. We're adding
- 17:30two floats, so the result is a float as
- 17:32well. So, Python doesn't know how to
- 17:35evaluate code like this.
- 17:38It doesn't know how to concatenate a
- 17:40float
- 17:42to a string. To solve this problem,
- 17:45we need to convert sum to a string. So,
- 17:47this is where
- 17:48we use the str function.
- 17:51Now, let's run the program again. So, 10
- 17:54plus 20.1, and here's the result.
- 17:58And one last thing.
- 17:59In this example, I'm calling the float
- 18:01function at the time we want to
- 18:03calculate the sum of these two numbers,
- 18:05but this is not a requirement. You can
- 18:07call the float function
- 18:09over here. So, this input function
- 18:11returns a string. We can pass that
- 18:14string to our float function. Take a
- 18:16look. So, float parentheses like this.
- 18:20So, the value that we're passing to the
- 18:22float function is the value that is
- 18:24returned from the input function.
- 18:26Similarly,
- 18:29we call the float function over here.
- 18:32Now,
- 18:33we can change this expression to first
- 18:35plus second.
- 18:37That is another way to write this piece
- 18:39of code.
- 18:40So, type conversion is important in
- 18:41Python and other programming languages.
- 18:44There are times you need to convert the
- 18:45type of a variable to a different type.
- 18:54In this tutorial, I'm going to show you
- 18:55a bunch of cool things you can do with
- 18:56strings in Python.
- 18:58So, let's start by declaring a variable
- 19:00called course
- 19:01and set it to Python for
- 19:04beginners.
- 19:06Now, this string that we have over here
- 19:07is technically an object. An object in
- 19:10Python is like an object in the real
- 19:12world. As a metaphor, think of the
- 19:14remote control of your TV.
- 19:16This remote control is an object, and it
- 19:18has a bunch of capabilities. It has a
- 19:19bunch of buttons for turning your TV on,
- 19:22turning it off, changing the volume, and
- 19:24so on.
- 19:24Now, in this program, this course
- 19:26variable is storing a string object.
- 19:29This string object has a bunch of
- 19:30capabilities. So, if you type course.
- 19:33dot, you can see all the capabilities
- 19:36available in a string object. These are
- 19:38basically functions that you can call,
- 19:40just like the print or input functions.
- 19:42The difference is that the print and
- 19:44input functions are general-purpose
- 19:46functions. They don't belong to a
- 19:47particular object, but the functions you
- 19:50see over here are specific to a string.
- 19:52Now more accurately, we refer to these
- 19:54as methods. So when a function is part
- 19:57of an object, we refer to that function
- 19:59as a method. So,
- 20:01let's look at a few examples. Here we
- 20:03have a function or a method called upper
- 20:06and we use that to convert a string to
- 20:07upper case.
- 20:09So if we print
- 20:10course.upper
- 20:12and run this program, you can see our
- 20:14course in upper case. Pretty useful.
- 20:17Now what you need to understand here is
- 20:19that this upper method does not change
- 20:21our original string. It will return a
- 20:23new string. So right after this, if we
- 20:26print course,
- 20:29you can see that our course variable is
- 20:31not affected. So the upper method
- 20:33returns a new string.
- 20:36Now similarly, we have another method
- 20:38called lower for converting a string to
- 20:40lower case.
- 20:41We have a method called find to see if
- 20:44our string contains a character or a
- 20:46sequence of characters.
- 20:47For example, here we can pass Y and this
- 20:51will return the index of the first
- 20:53occurrence of Y in our string. So,
- 20:57in Python, the index of the first
- 20:59character in a string is zero. So here
- 21:01we have 0 1 2 3 4 and so on.
- 21:04So when we run this program, you're
- 21:05going to see one on the terminal because
- 21:07the index of Y is one. Take a look.
- 21:11First I'm going to delete this line. We
- 21:12don't need it anymore. Also, let's
- 21:14delete this line.
- 21:16Let's run the program. There you go.
- 21:20Now as I told you before, Python is
- 21:21sensitive to lower case and upper case
- 21:24letters. So if I pass an upper case Y
- 21:27here,
- 21:28this find method returns -1 because we
- 21:31don't have an upper case Y in this
- 21:33string.
- 21:34We can also pass a sequence of
- 21:35characters. For example, four.
- 21:38So this will return the index of the
- 21:39word four. Take a look.
- 21:43So it's seven.
- 21:45Now there are times we want to replace
- 21:46something in a string with something
- 21:47else.
- 21:48To do that, we use the replace method.
- 21:51Replace.
- 21:52So we can replace four with a string
- 21:56containing the number four. Take a look.
- 21:59So Python for beginners.
- 22:02Obviously,
- 22:03if you look for a character or a
- 22:04sequence of characters that don't exist
- 22:06in our string, nothing is going to
- 22:08happen. For example, if you try to
- 22:10replace X with four, obviously, we don't
- 22:13have X here, so nothing is going to
- 22:15happen.
- 22:16Also, just like the upper method, the
- 22:19replace method is not going to modify
- 22:21our original string. So it's going to
- 22:23return a new string. This is because
- 22:25strings in Python and many other
- 22:27programming languages are immutable. We
- 22:29cannot change them once we create them.
- 22:32Whenever we want to change a string,
- 22:33we'll end up with a new string object in
- 22:35memory.
- 22:36Now one last thing I want to cover in
- 22:37this tutorial. There are times you want
- 22:39to see if your string contains a
- 22:41character or a sequence of characters.
- 22:43One way to do that is using the find
- 22:45method that we talked about. So let's
- 22:48see if our string contains Python.
- 22:52Now when we run this program,
- 22:54that is the index of the first
- 22:56occurrence of the word Python in our
- 22:58string.
- 22:59Now in Python, we can also use the in
- 23:01operator.
- 23:02So we can write an expression like this.
- 23:05We type a string,
- 23:07Python,
- 23:08then we type in. This is a special
- 23:10keyword in Python. This is what we call
- 23:13the in operator.
- 23:15So after that,
- 23:16we type the name of our variable.
- 23:18So with this expression, we're checking
- 23:20to see if we have Python in course. As
- 23:23you can see, Python code is very
- 23:25readable. It's like plain English. So
- 23:27when we run this program,
- 23:29instead of seeing the index of the first
- 23:31occurrence of Python, we see a boolean
- 23:34value. This is more desirable in a lot
- 23:36of cases.
- 23:37Next we're going to look at arithmetic
- 23:38operations.
- 23:46In this tutorial, I'm going to show you
- 23:47the arithmetic operators that we have in
- 23:49Python. These are the same arithmetic
- 23:51operators that we have in math. For
- 23:53example, we can add numbers, we can
- 23:55subtract them, multiply them and so on.
- 23:57So let's print 10 + 3.
- 24:01When we run this program, we get 13.
- 24:03So this is the addition operator. We
- 24:06also have subtraction.
- 24:07We have multiplication
- 24:09and division. Now technically, we have
- 24:12two different types of division
- 24:13operators. We have a division with one
- 24:15slash and another with two slashes.
- 24:18Let's look at the differences. If you
- 24:20use a single slash,
- 24:22we get a floating point number that is a
- 24:24number with a decimal point. But if you
- 24:26use double slashes,
- 24:28we get
- 24:29an integer, a whole number.
- 24:32We also have the modulus operator that
- 24:34is indicated by a percent sign and this
- 24:37returns the remainder of the division of
- 24:3910 by 3. So that is one.
- 24:43And finally, we have the exponent
- 24:44operator that is indicated by two
- 24:46asterisks. So this is 10 to the power of
- 24:50three. So when we run this, we get
- 24:521,000.
- 24:54Now,
- 24:54for all these operators that you saw, we
- 24:56have an augmented assignment operator.
- 24:59Let me explain what it means. So let's
- 25:01say we have a variable called X
- 25:03and we set it to 10.
- 25:05Now we want to increment the value of X
- 25:07by three. So we have to write code like
- 25:09this. X
- 25:10= X + 3. When Python executes this code,
- 25:14it's going to evaluate this expression
- 25:16or this piece of code. The result of
- 25:18this expression is 10 + 3 which is 13.
- 25:21Then it will store 13 in the X.
- 25:24Now there is another way to achieve the
- 25:26same result using less code.
- 25:28We can type X + = 3.
- 25:32What we have on line three is exactly
- 25:34identical to what we have on line two.
- 25:36So what we have here is called the
- 25:38augmented assignment operator. So we
- 25:41have this assignment operator, but we
- 25:43have augmented or enhanced it.
- 25:46Now here we can also use
- 25:48subtraction to decrease the value of X
- 25:50by three. We can use multiplication and
- 25:53so on.
- 25:54So these are the arithmetic operators in
- 25:56Python.
- 26:03All right, let me ask you a question.
- 26:04I'm going to declare a variable called X
- 26:07and set it to 10 + 3 * 2.
- 26:10What do you think is the result of this
- 26:11expression? This is a basic math
- 26:13question that unfortunately, a lot of
- 26:15people fail to answer.
- 26:17The answer is 16.
- 26:19Here's the reason.
- 26:20In math, we have this concept called
- 26:22operator precedence and that determines
- 26:24the order in which these operators are
- 26:26applied. So multiplication and division
- 26:30have a higher order. So this part of the
- 26:32expression gets evaluated first. So 2 *
- 26:343 is 6 and then the result is added to
- 26:3810. That is why the result of this
- 26:40expression is 16.
- 26:42Now in Python, operator precedence is
- 26:44exactly like math, but we can always
- 26:46change it using parentheses. For
- 26:48example, in this expression, if you want
- 26:5010 + 3 to be evaluated first, we can
- 26:53wrap it in parentheses.
- 26:55So,
- 26:56like this.
- 26:57Now when we execute this code, we're
- 26:59going to see 26 because 10 + 3 is 13 and
- 27:03that divided by 2 is 26. Let's verify
- 27:05this. So print X
- 27:08and we get 26.
- 27:15So you learned about the arithmetic
- 27:17operators in Python. Now in Python, we
- 27:19have another set of operators called
- 27:21comparison operators. We use these
- 27:23operators to compare values. Let me show
- 27:26you. So I'm going to declare a variable
- 27:28called X
- 27:29and set it to an expression like this. 3
- 27:32is greater than 2. So what we have here,
- 27:35this piece of code, this expression is
- 27:37called a boolean expression because it
- 27:39produces a boolean value.
- 27:41So in this case, because 3 is greater
- 27:43than 2, the result of this expression is
- 27:46the boolean true.
- 27:47So if we print X,
- 27:51we get true on the terminal.
- 27:53So here is the greater than operator. We
- 27:55also have greater than or equal to. We
- 27:58have less than. We have less than or
- 28:01equal to.
- 28:02Here's the equality operator which is
- 28:04indicated by two equal signs. Do not
- 28:07confuse this with the assignment
- 28:08operator. So here we're comparing three
- 28:11and two for equality. So if we run our
- 28:13program,
- 28:15we see false because three does not
- 28:17equal to two.
- 28:19So here's the equality operator. We also
- 28:21have
- 28:22the not equality operator that is
- 28:24indicated by an exclamation mark
- 28:26followed by an equal sign.
- 28:29So let's quickly recap.
- 28:31Here are the comparison operators we
- 28:32have in Python.
- 28:34Greater than, greater than or equal to,
- 28:36less than, less than or equal to,
- 28:39equal and not equal.
- 28:41These operators are extremely important
- 28:43in real Python programs because quite
- 28:45often we have to compare values to
- 28:47evaluate certain conditions. You're
- 28:49going to see that soon.
- 28:56In Python, we have another set of
- 28:58operators called logical operators.
- 29:00We use these operators to build complex
- 29:02rules and conditions. Let me show you.
- 29:05So I'm going to declare a variable
- 29:06called price and set it to 25.
- 29:09Now let's print
- 29:10a boolean expression like this.
- 29:12Price is greater than 10.
- 29:15Now let's say we want to check to see if
- 29:17the price is between 10 and 30.
- 29:20This is where we use the logical and
- 29:22operator. So we type and and right after
- 29:25that, we type another boolean
- 29:27expression. So price less than 30.
- 29:31So with this and operator, if both these
- 29:33boolean expressions return true, the
- 29:36result of this entire expression will be
- 29:37true. Take a look. So,
- 29:41in this case we get true
- 29:43because the price is between 10 and $30.
- 29:46We also have
- 29:48the or operator. With the or operator,
- 29:51if at least one of these boolean
- 29:52expressions returns true, then the
- 29:55result of this entire expression will be
- 29:56true.
- 29:57To demonstrate this, I'm going to change
- 29:59price to five. Let's see how Python is
- 30:02going to execute this code. So, first
- 30:04it's going to look at this boolean
- 30:05expression. Is price greater than 10?
- 30:08No, it's not. So, it will keep going.
- 30:11Then it will look at the second boolean
- 30:13expression. Is price less than 30? It
- 30:16sure is. So, the result of this entire
- 30:18expression will be true. Take a look.
- 30:22There you go.
- 30:23We also have the not operator, which
- 30:25basically inverses any values that you
- 30:27give it. Let me show you. So,
- 30:30we're going to have one boolean
- 30:32expression, price greater than 10.
- 30:34The result of this expression is false.
- 30:37Now, if you apply the not operator, this
- 30:40will inverse false to true. So, let me
- 30:43run the program, we get true.
- 30:46So, let's quickly recap. In Python, we
- 30:48have three logical operators. We have
- 30:50logical and, which returns true if both
- 30:53expressions return true.
- 30:55We have logical or, which returns true
- 30:57if at least one expression returns true.
- 31:00And we have not, which inverses any
- 31:02value that we give it.
- 31:10In this tutorial, we're going to talk
- 31:11about if statements in Python. We use if
- 31:14statements to make decisions in our
- 31:15programs. For example, we can declare a
- 31:18variable called temperature, and
- 31:19depending on the value of this variable,
- 31:21we can print different messages on the
- 31:23terminal. Let me show you.
- 31:25So, here's our temperature variable.
- 31:27We set it to 35.
- 31:29Now, let's say if temperature is greater
- 31:32than 30, we want to print a message
- 31:34saying it's a hot day.
- 31:35So, we type if,
- 31:37then we type a condition, and this is
- 31:39where we use our comparison operators.
- 31:42So, we type temperature greater than 30.
- 31:46Then, so we add a colon, and see what
- 31:49happens when I press enter now.
- 31:51The caret is indented, and this
- 31:53represents a block of code. So, the code
- 31:56that we write over here will be executed
- 31:58if this condition is true. Otherwise,
- 32:00it's not going to be executed. Let me
- 32:02show you. So,
- 32:04we're going to print
- 32:06it's a hot day.
- 32:08And by the way, note that here I've
- 32:09surrounded the string with double
- 32:11quotes, because here we have a single
- 32:14quote as an apostrophe. So, I couldn't
- 32:17declare a string like this with single
- 32:19quotes. If I typed it's a hot day, look,
- 32:23Python gets confused. Because it thinks
- 32:26the single quote represents the end of
- 32:28our string, so it doesn't recognize the
- 32:30subsequent characters, okay? So, that's
- 32:32why we use
- 32:34double quotes here,
- 32:36so we can have an apostrophe in our
- 32:38string.
- 32:39So, it's a hot day.
- 32:41Now, if I press enter again,
- 32:43the caret is indented, so the code that
- 32:46we write here will be part of our if
- 32:48block, and it will get executed if this
- 32:50condition is true. So, here we can print
- 32:53a second message, drink plenty of water.
- 32:57Now, to terminate this block, we press
- 32:59enter, and then press shift and tab.
- 33:02The caret is no longer indented, so the
- 33:05code that we write here will always get
- 33:07executed, no matter what, whether this
- 33:09condition is true or not.
- 33:11Now, in C-based programming languages
- 33:13like C++, C#, Java, and JavaScript, we
- 33:17represent a block of code using curly
- 33:19braces. So,
- 33:21you start a block of code using a left
- 33:23brace, and then end it using a right
- 33:26brace. In Python, we don't have curly
- 33:28braces. So, we use indentation to
- 33:30represent a block of code, okay?
- 33:33So, in this case,
- 33:35these two lines are indented, and that
- 33:37means they are part of this block of
- 33:40code.
- 33:41Now, let's run the program and see what
- 33:42happens.
- 33:44So, we see these two messages because
- 33:46the temperature is greater than 30.
- 33:48Now,
- 33:49if I change the temperature to 25,
- 33:52and run the program again, we don't see
- 33:54anything, okay?
- 33:57Now, after this block, let's print done.
- 34:01Because this code is not indented, it
- 34:03will always get executed. It's not part
- 34:05of our if block, okay? So, take a look.
- 34:09Here's the done message.
- 34:11Now, let's add a second condition.
- 34:14So, if temperature is not greater than
- 34:1630, that means it's less than or equal
- 34:18to 30.
- 34:20So, I'm going to add a second condition.
- 34:23So, if the temperature is between 20 and
- 34:2530, I want to print it's a nice day.
- 34:28So, here we type L if, that is short for
- 34:31else if.
- 34:33And here we type a second condition.
- 34:35So, temperature greater than 20.
- 34:39We add a colon, press enter. Now, we
- 34:42have a new block.
- 34:43So, here we can print
- 34:45it's a nice day.
- 34:48So, if this condition is true, that
- 34:50means the temperature
- 34:52is greater than 20, and less than or
- 34:54equal to 30.
- 34:56Now, what we have here is called a
- 34:57comment. That is why it's grayed out.
- 35:00It's not real code, it's just some note
- 35:02that we add to our program. Python is
- 35:04not going to execute this. So, whenever
- 35:06we type a pound sign, what we have after
- 35:08is treated as a comment, okay?
- 35:11So, if this condition is true, then
- 35:13we're going to see this message on the
- 35:15terminal. Let's run our program and
- 35:17verify this.
- 35:19There you go. The temperature is 25,
- 35:21that's why we see this message.
- 35:23Now, we can have as many conditions as
- 35:25we want. There are no limitations, okay?
- 35:28So, let's add another condition. L if,
- 35:31temperature is greater than 10, then
- 35:35we're going to print
- 35:37it's a bit cold.
- 35:39Now, in this case, if this condition is
- 35:41true, that means the temperature is
- 35:43greater than 10,
- 35:45and less than or equal to 20.
- 35:48Now, finally, if the temperature is less
- 35:49than 10, let's print a message saying
- 35:51it's a cold day. So,
- 35:54here we type else, then we add a colon,
- 35:57and now we have a new block. So, this
- 36:00code will get executed if none of the
- 36:02above conditions are true. So, here we
- 36:05can print
- 36:07it's cold.
- 36:10So, this is how we use if statements to
- 36:12make decisions in our programs.
- 36:20Here's a great exercise for you to
- 36:21practice what you have learned so far. I
- 36:23want you to write a weight converter
- 36:25program like this.
- 36:27So, this program is asking me my weight.
- 36:29I enter 170.
- 36:31Next, it's asking me if the weight is in
- 36:34kilograms or pounds. So, I can type K
- 36:36for kilograms, or L for pounds. I can
- 36:39type a lowercase L, or an uppercase L.
- 36:42It doesn't matter. So, let's go with a
- 36:44lowercase L.
- 36:46Now, it tells me weight in kilogram is
- 36:4976.5.
- 36:51So, go ahead and spend 5 minutes on this
- 36:52exercise. You can use the comment box
- 36:54below to show your code with others. And
- 36:57then when you're done, come back and see
- 36:58my solution.
- 37:02So, first we call our input function to
- 37:05ask the first question. Weight.
- 37:08We get the result and store it in a
- 37:09variable called weight.
- 37:12Next, we call the input function one
- 37:13more time to ask the second question.
- 37:16Is this in kilogram
- 37:18or
- 37:19pounds?
- 37:22We get the result and store it in a
- 37:24variable called unit.
- 37:25This is where we're going to use an if
- 37:27statement.
- 37:28So,
- 37:29we want to check to see if unit equals
- 37:32K.
- 37:34Then, we should convert the weight to
- 37:36pounds and print it on the terminal.
- 37:38However,
- 37:39with this code, if I type a lowercase K,
- 37:41this condition is not going to be true,
- 37:43because earlier I told you that Python
- 37:45is a case-sensitive language. So,
- 37:48we need to convert this string to
- 37:50uppercase. Earlier we talked about
- 37:52string methods. So, if you type dot, we
- 37:54can see all the functions or methods
- 37:56available in a string object. So, we use
- 37:59the upper method,
- 38:01and this returns a new string in
- 38:02uppercase.
- 38:04Now, if this condition is true, first we
- 38:06need to convert the weight to pounds.
- 38:08So, we declare a new variable called
- 38:10converted,
- 38:12get the weight, and divide it by 0.45.
- 38:17And then we can print this on the
- 38:18terminal. So,
- 38:21we say weight
- 38:22in pounds is
- 38:24then we append converted.
- 38:27Now, to terminate this block, we press
- 38:29shift and tab.
- 38:31Else, colon.
- 38:33So, if this condition is not true, that
- 38:35means the weight was entered in pounds,
- 38:37so we need to convert it to kilograms.
- 38:39Once again, we declare a variable
- 38:41converted, and set it to weight times
- 38:450.45.
- 38:47And then we print weight in kilograms,
- 38:52and here we concatenate
- 38:54the string with converted.
- 38:56All right. Now, we need to terminate
- 38:58this block, so we press enter, then
- 39:00shift and tab. Good.
- 39:02Now, if you run this program, we're
- 39:03going to see an error. Let me show you.
- 39:05So, let's run it.
- 39:07Here I'm going to enter 170.
- 39:10Then I type a lowercase L. Okay. Here's
- 39:14an error.
- 39:15Can't multiply sequence by non-int of
- 39:17type float. So, this is where we got
- 39:20this error. When we tried to multiply
- 39:23the weight by 0.45.
- 39:26So,
- 39:27that is line seven in our code. Now, the
- 39:29reason this is happening is because the
- 39:31weight variable is storing a string
- 39:33object. Because earlier I told you the
- 39:36input function always returns a string.
- 39:39So, here we need to convert the weight
- 39:41to a number.
- 39:42We can either use the int or the float
- 39:45function.
- 39:46Let's run the program one more time.
- 39:48So,
- 39:49170
- 39:52in pounds.
- 39:53Here's the second error. Can only
- 39:55concatenate string, not float to string.
- 39:59And that error occurred
- 40:01over here.
- 40:02When we try to print the weight in
- 40:04kilograms. So,
- 40:06look at line seven.
- 40:08In this case, weight is an integer.
- 40:10We're multiplying an integer by a float,
- 40:12and the result of this operation is
- 40:14going to be a float.
- 40:16Now, on line eight, we're trying to
- 40:18concatenate or combine a string with a
- 40:21float, and Python doesn't know how to
- 40:23execute this code. To solve this
- 40:25problem, we need to convert this float
- 40:27to a string. So, here we use the
- 40:29built-in str function.
- 40:32Let's run the program and see what
- 40:33happens.
- 40:34So,
- 40:35170
- 40:37in pounds, and here's my weight in
- 40:40kilograms. Now, let's try entering a
- 40:42weight in kilograms. So, running one
- 40:44more time.
- 40:45Let's say 76
- 40:47kilos.
- 40:49Here we get a familiar error. Can only
- 40:52concatenate string to string, not float.
- 40:55So, this error occurred
- 40:56on line five, where we try to
- 40:58concatenate a string
- 41:01to a float. So, once again, we need to
- 41:03convert this to a string object.
- 41:06Now, let's run the program one more
- 41:07time.
- 41:0976
- 41:10kilos
- 41:11is equal to 168 lbs.
- 41:16Hey guys, I just wanted to let you know
- 41:17that I have an online coding school at
- 41:19codewithmosh.com, where you can find
- 41:21plenty of courses on web and mobile
- 41:22development. In fact, I have a
- 41:24comprehensive Python course that teaches
- 41:26you everything about Python from the
- 41:28basics to more advanced concepts. So,
- 41:30after you watch this tutorial, if you
- 41:32want to learn more, you may want to look
- 41:33at my Python course. It comes with a
- 41:3530-day money-back guarantee and a
- 41:36certificate of completion you can add to
- 41:38your resume. In case you're interested,
- 41:40the link is below this video.
- 41:46In this tutorial, we're going to talk
- 41:47about while loops in Python. We use
- 41:50while loops to repeat a block of code
- 41:52multiple times. For example, let's say
- 41:54you want to print the numbers one to
- 41:55five.
- 41:56A poor way of doing this is writing code
- 41:58like this. Print one, then print two,
- 42:01print three,
- 42:02print four, and print five.
- 42:05Now, why is this a bad approach? Well,
- 42:07what if you wanted to print the numbers
- 42:09one to 1 million? We don't want to write
- 42:111 million lines of code, each line
- 42:13printing a number. This is where you use
- 42:15while loops. So,
- 42:17we start off by declaring a variable
- 42:19like I, and set it to our initial
- 42:21number. Let's say one.
- 42:23Then we type while, and here we type a
- 42:25condition. So, once again, we can use
- 42:27our comparison operators.
- 42:29Let's say I less than five. Less than or
- 42:32equal to five.
- 42:34As long as this condition is true, then
- 42:36the code that we write inside of the
- 42:38while block will get executed. So, here
- 42:40we type a colon and press enter. Now, we
- 42:43have a block of code.
- 42:45In this block, we can print I,
- 42:48and then we need to increment I by one.
- 42:50So, we set I to I + 1. If we don't do
- 42:53this, I will always be one, and this
- 42:56block of code
- 42:57will get executed indefinitely. It will
- 43:00never terminate. Basically, our program
- 43:02will continue running until it runs out
- 43:04of memory.
- 43:05So, in the first iteration, I is one, is
- 43:08less than five, so Python is going to
- 43:10execute this block of code. It will
- 43:13print one on the terminal, and then I
- 43:15becomes two.
- 43:16Then the control moves back over here.
- 43:18So, Python evaluates this condition. Two
- 43:21is less than or equal to five, so the
- 43:23condition is true, and once again,
- 43:25Python is going to execute this block
- 43:27one more time.
- 43:28In the second iteration, we're going to
- 43:30see two on the terminal, and then I will
- 43:32become three.
- 43:33So, this will continue until this
- 43:35condition is no longer true. Let me show
- 43:37you. So,
- 43:39let's run this program.
- 43:41Now, we see the numbers one to five. So,
- 43:43this is the beauty of while loops.
- 43:45I can easily change five
- 43:48to 1
- 43:49thousand. And by the way, I'm separating
- 43:51these three digits using an underscore.
- 43:53This makes my number more readable. We
- 43:55don't have to add this, but it just
- 43:56makes our code more readable.
- 43:58So, let's run the program.
- 44:01Now, we see the numbers one to 1
- 44:02thousand. It's much easier than 1
- 44:05thousand lines of code, each printing a
- 44:07number. Okay?
- 44:08Now, let me show you something really
- 44:09cool.
- 44:10So, I'm going to change this
- 44:12to 10.
- 44:14Now, instead of printing I, I want to
- 44:17print an expression. So, I'm going to
- 44:19multiply I
- 44:21by a string,
- 44:22an asterisk.
- 44:23Now, you might be confused here, because
- 44:25earlier I told you that in Python, we
- 44:27cannot concatenate a number to a string.
- 44:30But here I'm using the multiplication
- 44:32operator. Well, this is different. We
- 44:34can multiply a number by a string, and
- 44:37this will repeat that string based on
- 44:39the value of that number. So, if I is
- 44:42one, we're going to see one asterisk. If
- 44:45I is five, we're going to see five
- 44:46asterisks. Let me show you. So, when we
- 44:49run this program,
- 44:51we see this triangle shape, because in
- 44:53the first iteration, I is one, so we see
- 44:56one asterisk. In every iteration, I is
- 44:59incremented by one, so we see one extra
- 45:02asterisk. And finally, in the last
- 45:04iteration, I is 10, and we see 10
- 45:07asterisks on the terminal.
- 45:15So, you have learned about three types
- 45:16of data in Python.
- 45:18We have numbers, which can be integers
- 45:20or floats. We have booleans and strings.
- 45:24These are what they refer to as
- 45:26primitive or basic types in Python.
- 45:29Now, in Python, we have a bunch of
- 45:30complex types as well. These complex
- 45:32types are very useful in building real
- 45:34applications.
- 45:36In this tutorial, we're going to talk
- 45:37about lists. We use lists whenever we
- 45:40want to represent a list of objects,
- 45:42like a list of numbers or a list of
- 45:44names. Let me show you.
- 45:45So, I'm going to declare a variable
- 45:47called names,
- 45:49and set it to a list of names.
- 45:51So, here we add square brackets to
- 45:53represent a list.
- 45:54And now we can add one or more objects
- 45:56inside this list. In this list, we add a
- 45:58bunch of names,
- 46:00like John,
- 46:01Bob,
- 46:03Mosh,
- 46:04Sam, and Mary.
- 46:06So, we separate these elements, these
- 46:08items, using a comma. Okay?
- 46:10Now,
- 46:11let's print our list. So, print names.
- 46:15Take a look.
- 46:16It comes out exactly like how we wrote
- 46:18it.
- 46:19Now, we can also get individual elements
- 46:21in this list. For example, if you want
- 46:23to get the first element in this list,
- 46:26here we type a pair of square brackets,
- 46:28and inside the square brackets, we type
- 46:30an index.
- 46:32The index of the first element in this
- 46:33list is zero. So, now, when we run this
- 46:36program, we're going to see John on the
- 46:38terminal.
- 46:39There you go.
- 46:40Now, in Python, we can also use a
- 46:43negative index. This is a feature that I
- 46:45personally have not seen in other
- 46:47programming languages.
- 46:48So, if zero represents the first element
- 46:52in this list, what do you think negative
- 46:54one represents?
- 46:56It represents the last element in this
- 46:58list. Let's verify it. So, let's run the
- 47:01program, and here's Mary.
- 47:03What about
- 47:06negative two? Well, that represents the
- 47:09second element
- 47:10from the end of the list.
- 47:12So, let's run the program, and here's
- 47:14Sam.
- 47:15Now, we can also change an object at a
- 47:17given index. For example, let's say here
- 47:20we made a mistake, and this John should
- 47:22not be spelled with an H. So, we need to
- 47:25reset it. We type names of zero.
- 47:29Now, we treat this like a regular
- 47:30variable. So, we set it to a new value.
- 47:33We set it to John without an H. Now,
- 47:36let's print our list.
- 47:38And here's our updated list. Beautiful.
- 47:41We can also select a range of values.
- 47:44For example, let's say we're only
- 47:45interested in the first three names.
- 47:49So, over here, we type square brackets,
- 47:51and here we need to type two indexes, a
- 47:54start index and an end index.
- 47:56Our start index is zero, because we want
- 47:58to start from here,
- 48:00and our end index is going to be zero,
- 48:03one, two, plus one, that's going to be
- 48:06three. So, we add a colon, three.
- 48:10So, Python is going to return all the
- 48:11elements from the start index up to the
- 48:14end index, but excluding the end index.
- 48:17So, it's going to return the elements at
- 48:19index zero, one, and two. Take a look.
- 48:24Here are the first three names.
- 48:27And by the way,
- 48:29this expression does not modify our
- 48:31original list. It returns a new list.
- 48:34So, right after this print statement, if
- 48:37we print our original list,
- 48:40we can see that it's not changed.
- 48:42So, this is how we use lists in Python.
- 48:51Earlier, I told you that strings in
- 48:52Python are objects. Objects in
- 48:55programming are kind of like objects in
- 48:57the real world, like your mobile phone,
- 48:59your bicycle, the remote control of your
- 49:01TV, and so on. They have certain
- 49:02capabilities. So, if you type a string
- 49:05here,
- 49:06and then press dot, we can see all the
- 49:08functions or methods available in a
- 49:10string object in Python.
- 49:12Now, lists are also objects, so they
- 49:15have a bunch of methods for adding
- 49:16items, for removing them, and so on. Let
- 49:18me show you. So, I'm going to declare
- 49:21a list of numbers.
- 49:22Let's say one, two, three, four, and
- 49:24five.
- 49:25Now, to add a new element at the end of
- 49:27this list, we can use the append method.
- 49:30So, we type numbers
- 49:31dot append,
- 49:33and here we type six.
- 49:35Now, let's print our list.
- 49:39So, here's our updated list. Beautiful.
- 49:42Now, what if you want to insert a number
- 49:43somewhere in the middle or at the
- 49:45beginning?
- 49:46For that, we use the insert method.
- 49:49So, we're going to call the insert
- 49:51method.
- 49:52Now, on the top, go to the view menu,
- 49:55and look at parameter info. Look at the
- 49:58shortcut. On a Mac computer, it's
- 50:01command and P.
- 50:02On Windows, it's probably control and P.
- 50:05If we use this shortcut, we can see the
- 50:07values that this method expects.
- 50:10So, the first value that this method
- 50:12expects is an index value, and the type
- 50:15of this value is an integer.
- 50:16So, if you want to insert a value at the
- 50:18beginning of this list, I should pass
- 50:20zero as the index of the first element,
- 50:23right? So, let's pass zero.
- 50:25Now,
- 50:27the second value is highlighted. So, the
- 50:29second value is an object, and the type
- 50:31of this is T. That basically means this
- 50:34can be any type. We can pass a number,
- 50:36we can pass a boolean, we can pass a
- 50:38string, we can pass a list or any type
- 50:40of objects in Python.
- 50:42So, I'm going to pass -1.
- 50:45Now, let's run our program.
- 50:47We can see -1 appeared at the beginning
- 50:49of our list.
- 50:51We also have a method for removing
- 50:52items. So,
- 50:54let's call remove
- 50:56three.
- 50:57Let's run the program.
- 50:59Three is gone. We only have 1 2 4 and 5.
- 51:03Now, if you want to remove all the items
- 51:04in the list, we call the clear method.
- 51:06So, clear.
- 51:09This method doesn't expect any values.
- 51:11So, let's run our program. Our list is
- 51:13empty.
- 51:14Now, sometimes you want to know if a
- 51:16given item exists in our list or not. To
- 51:18do that, we use the in operator. So,
- 51:21let's remove this line.
- 51:24Instead of printing our numbers list,
- 51:25I'm going to print an expression.
- 51:28One
- 51:29in numbers.
- 51:31So, here we're using the in operator.
- 51:33We're checking to see if one is in the
- 51:35numbers list. So, this is a boolean
- 51:37expression. It returns a boolean value.
- 51:40Take a look.
- 51:41So, we get true.
- 51:43Obviously, if we search for a value that
- 51:45doesn't exist in this list, like 10,
- 51:48we get false, okay?
- 51:51Now, finally, there are times you want
- 51:52to know how many items you have in a
- 51:53list. To do that, you can use the
- 51:55built-in len function. So, let's print
- 52:00len of numbers. So, len is a built-in
- 52:04function, just like the print function.
- 52:06That is why it's highlighted as purple.
- 52:08It returns the number of elements in a
- 52:10list. Take a look.
- 52:13So, we have five elements in this list.
- 52:20When writing Python programs, there are
- 52:22times you want to iterate over a list
- 52:25and access each item individually. Let
- 52:27me show you.
- 52:28So, I'm going to declare
- 52:29a list of numbers,
- 52:311 2 3 4 5.
- 52:33Now, if we print this list,
- 52:36it comes out exactly like how we wrote
- 52:38it, using the square bracket notation.
- 52:41But, what if we wanted to print each
- 52:42item on a separate line?
- 52:44That is where we use the for loop. Let
- 52:46me show you. So, we're not going to
- 52:48print the entire list.
- 52:50Instead, we're going to type for.
- 52:52Now, we declare a variable, which is
- 52:53called a loop variable.
- 52:56Let's call it item.
- 52:57Then, we type in
- 52:59numbers.
- 53:00Next, we add a colon to start a block of
- 53:03code. So, this is what we call a for
- 53:05loop.
- 53:06With this for loop, we can iterate over
- 53:08all the items in this list. In each
- 53:11iteration, this item variable is going
- 53:13to hold one value. So, in the first
- 53:15iteration, item is going to be equal to
- 53:17one, in the second iteration is going to
- 53:19be equal to two, and so on.
- 53:21So,
- 53:22now if you print item
- 53:25and run our program,
- 53:26we see each item on a new line.
- 53:28So, this is how we use a for loop. Now,
- 53:31we could also achieve the same thing
- 53:32using a while loop, but our code would
- 53:34be a little bit longer. Let me show you.
- 53:37So, we would have to start by declaring
- 53:39a loop variable outside of our while
- 53:41loop. Let's say I. We set it to zero.
- 53:45Now, we say while I is less than
- 53:48Here, we need to find out how many items
- 53:49we have in this list.
- 53:51So, we use the len function.
- 53:53Len of numbers.
- 53:55So, as long as I is less than the length
- 53:58of the list,
- 53:59print
- 54:00Now, we can use the square bracket
- 54:02notation to get the element at this
- 54:04index. So,
- 54:06numbers of I.
- 54:10Now, we need to increment I by one. So,
- 54:13I equals I + 1.
- 54:15Let's run the program and see what we
- 54:16get.
- 54:19So, we get the numbers 1 to 5. These are
- 54:21coming out from our for loop. And then,
- 54:24we get the numbers 1 to 5 one more time.
- 54:26These are coming out from our while
- 54:27loop. Now, if you compare these two
- 54:29approaches, you can definitely see that
- 54:31the implementation using the for loop is
- 54:33shorter and easier to understand. We
- 54:35don't have to use the square bracket
- 54:37notation. We don't have to call the len
- 54:39function. We don't have to declare a
- 54:41variable, a loop variable, and then
- 54:43increment it explicitly. So, with the
- 54:46for loop, in each iteration, the item
- 54:48variable will automatically hold one
- 54:50value in this list.
- 54:58In this tutorial, we're going to talk
- 55:00about the range function in Python. We
- 55:02use the range function to generate a
- 55:04sequence of numbers. Let me show you.
- 55:06So, we type range.
- 55:08This is a built-in function, just like
- 55:10the print and input functions.
- 55:12Here, we can pass a value like five,
- 55:14and this will return a range object. A
- 55:17range object is an object that can store
- 55:19a sequence of numbers. Let me show you.
- 55:22So,
- 55:23let's call that numbers.
- 55:25So, this is a range object. In this
- 55:27object, we're going to have the numbers
- 55:29zero to five, but excluding five.
- 55:32Now, if we print
- 55:34numbers,
- 55:37we're going to see this message, range
- 55:39of zero to five, not the actual numbers,
- 55:41because this is the default
- 55:42representation of a range object. To see
- 55:45the actual numbers, we need to iterate
- 55:47over this range object using a for loop.
- 55:50In the last video, you learned how to
- 55:51iterate over a list using a for loop,
- 55:53but we can also iterate over a range
- 55:55object using a for loop. Basically, we
- 55:57can use the for loop with any object
- 56:00that represents a sequence of objects.
- 56:02So, instead of printing numbers,
- 56:05we're going to use a for loop.
- 56:07For number
- 56:08in numbers,
- 56:10colon,
- 56:11we're going to print
- 56:13number.
- 56:15Take a look.
- 56:17Now, we see the numbers zero to four.
- 56:20So,
- 56:21range of five
- 56:22generates a sequence of numbers starting
- 56:24from zero up to the number we specify
- 56:27here.
- 56:28Now, if we supply two values,
- 56:30the first value is going to be
- 56:32considered the starting value,
- 56:34and the second value is going to be
- 56:36considered the ending value, and it's
- 56:37going to be excluded. So, range of five
- 56:40to 10 is going to generate the numbers
- 56:42five to nine. Take a look.
- 56:45There you go.
- 56:46Now, we can also supply a third value,
- 56:49and that will be used as a step. So,
- 56:51let's say instead of having a sequence
- 56:53of numbers like 5 6 7 8 9, we want to
- 56:56jump two numbers at a time. So, 5 7 9,
- 56:59and so on.
- 57:00So, here we pass two as the step. Take a
- 57:03look.
- 57:05So, we get these odd numbers, 5 7 and 9.
- 57:10So, this is the range function in
- 57:11Python. Like quite often, you see the
- 57:13range function used as part of a for
- 57:15loop, because we don't really need to
- 57:17store the result in a separate variable.
- 57:19We can call the range function right
- 57:21here, where we are using the numbers
- 57:23variable. So, we can type range of five,
- 57:27and this will return a range object
- 57:29holding the numbers zero to four. Take a
- 57:31look.
- 57:32There you go.
- 57:34So, we don't really need to store the
- 57:35result in a separate variable like
- 57:37numbers.
- 57:39There you go.
- 57:47In this tutorial, we're going to talk
- 57:48about tuples in Python.
- 57:50Tuples are kind of like lists. We use
- 57:52them to store a sequence of objects, but
- 57:54tuples are immutable, which means we
- 57:57cannot change them once we create them.
- 57:59Let me show you.
- 58:00So, I'm going to start by defining a
- 58:02list of numbers, 1 2 3.
- 58:04Now, we use square brackets to define a
- 58:06list,
- 58:07and parentheses to define a tuple. So,
- 58:10now this numbers variable is storing a
- 58:12tuple.
- 58:13If we try
- 58:15to reassign, let's say the first
- 58:16element,
- 58:18we're going to get an error.
- 58:20Tuple object does not support item
- 58:22assignment.
- 58:23So, this is what I meant by tuples are
- 58:25immutable. They're unchangeable.
- 58:28Also,
- 58:30if we type numbers {dot}
- 58:32you don't see any methods like append,
- 58:34insert, remove, and so on. We only have
- 58:37count and index. Count returns the
- 58:40number of occurrences of an element. For
- 58:42example,
- 58:43if we have, let's say, two threes in
- 58:46this tuple,
- 58:47and call count of three, this will
- 58:49return two.
- 58:51The other method we have here is index,
- 58:53and this returns the index of the first
- 58:56occurrence of the given element.
- 58:58Now,
- 58:59these other methods you see here that
- 59:00start with an underscore, they're called
- 59:03magic methods. It's an advanced topic,
- 59:05and I've covered it in detail in my
- 59:06complete Python programming course. If
- 59:09you're interested, the link is down
- 59:11below this video.
- 59:12So, tuples are immutable. We cannot
- 59:14change them once we create them.
- 59:16Now, practically speaking, most of the
- 59:18time you would be using lists, but there
- 59:20are times that once you create a list of
- 59:21objects, you want to make sure that
- 59:23somewhere in your program, you or
- 59:25someone else is not going to
- 59:26accidentally modify that list. If that's
- 59:29the case, then you should use a tuple.
- 59:33Hey guys, I just wanted to let you know
- 59:34that I have an online coding school at
- 59:36codewithmosh.com where you can find
- 59:38plenty of courses on web and mobile
- 59:40development. In fact, I have a
- 59:41comprehensive Python course that teaches
- 59:44you everything about Python from the
- 59:45basics to more advanced concepts. So,
- 59:47after you watch this tutorial, if you
- 59:49want to learn more, you may want to look
- 59:50at my Python course. It comes with a
- 59:5230-day money-back guarantee and a
- 59:54certificate of completion you can add to
- 59:55your resume. In case you're interested,
- 59:57the link is below this video.
- 1:00:01Hey Mosh, hi Mosh.
About this transcript
This page contains the full transcript of Python for Beginners - Learn Coding with Python in 1 Hour by Programming with Mosh, generated from the public captions YouTube serves with the video. The transcript has 9,840 words across 1,642 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.