📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Python Full Course for Beginners

Programming with Mosh2:02:21

Transcription

[Music] welcome to the complete Python Mastery course. In this course, you're going to learn everything about Python, from basics to more advanced concepts. So, by the end of the course, you'll be able to confidently use Python for AI, machine learning, web development, and automation. If you have been looking for a comprehensive, easy-to-follow, well-organized, and practical course that takes you from zero to hero, this is the right Python course for you. You don't need any prior knowledge of Python to get started. I will explain everything step by step in simple terms, so you can build a solid foundation. I'm M. Hamadani, a software engineer with over 20 years of experience, and I've taught millions how to code and become professional software engineers through my YouTube channel and online school, codewithm.com. If you're new here, make sure to subscribe, as I upload new videos all the time. Now, let's jump in and get [Music] started.

In this course, you're going to learn everything you need to get started with Python. Just be aware that I've designed this course for beginners. So, if you have some programming experience, check out my other Python course for developers. You can see the link on the top right corner of this video.

So, Python is the world's fastest growing and most popular programming language, not just amongst software developers, but also amongst mathematicians, data analysts, scientists, accountants, network engineers, and even kids. Kids! In fact, it's the ideal programming language to learn first. But what makes Python so special? Here are six reasons: With Python, you can solve complex problems in less time with fewer lines of code than many other languages. That's why huge companies like Google, Spotify, Dropbox, and Facebook have embraced this beautiful and powerful language. Here is an example: Let's say we want to extract the first three characters of the text "hello work." This is the code we would have to write in C. This is how we would do this in JavaScript. And here's how we would do it in Python. See how clean and simple the language is? And that's just the beginning. Python is a multi-purpose language, and you can use it for a wide range of jobs, such as data analysis, AI and machine learning, writing automation scripts, building web, mobile, and desktop applications, as well as software testing, or even hacking. So, if you want a high-paying, long-lasting career in any of these areas, especially AI and machine learning, Python is the language to put those opportunities at your fingertips. In fact, according to indeed.com, the average salary of a Python developer in the US was over $115,000 in March 2018.

And here are four more reasons that make Python the most desirable language: Python is a high-level language, so you don't have to worry about complex tasks such as memory management, as you do in C++. It's cross-platform, which means we can build and run Python apps on Windows, Mac, and Linux. It has a huge community, so whenever you get stuck, there is someone out there to help. And it has a large ecosystem of libraries, frameworks, and tools. Whatever you want to do, it is likely that someone else has done it before. Because Python has been around for over 20 years, there are two versions of Python out there: Python 2, which is the legacy version of Python and is going to be supported until year 2020, and Python 3, which is Python for the future. In this course, you're going to learn Python 3.

Hi, my name is M. Sh. Hamadani, and I'm going to be your instructor in this course. I'm a software engineer with 18 years of experience, and I've taught way over a million people how to code or how to become top professional software engineers. To learn more about me and my courses, head over to codewithm.com. All right, now let's get started.

All right, the first thing I want you to do is open your browser and head over to python.org. On this page, under downloads, you can download the latest version of Python. At the time of this video, the latest version is Python 3.13. Chances are, in the future, when you're watching this video, there is a new, newer version of Python available. Don't worry, what I'm going to show you in this tutorial will apply to future versions of Python as well. So, go ahead and download the latest version now.

If you're on Windows, before you click install, make sure to check this little box here that says "Add Python to PATH." This step is very important, and it will save you a lot of headaches later. So, check this box and follow the installation.

Now, to verify that Python is successfully installed, click this magnifier and here in this search bar, type "terminal." Now, here in the terminal window, type `python --version`. This verifies that we have successfully installed Python 3.13.

Now, if you're on Mac, press Command and Space to bring up the Spotlight search. Here, type "terminal." Now, to verify that we have installed Python correctly on Mac, we should type `python3 --version`. So, as you can see, I've successfully installed Python 3.13 on this machine.

So, this environment you see here is what we call the Python interpreter, which is basically a program that executes Python code. We can type our Python code in a file and give it to this interpreter, or we can type our code directly here in this interactive shell. So, here we can write an expression like `2 + 2`. In programming, an expression is a piece of code that produces a value. So, here, when we add `2 + 2`, we get a value, that is why we refer to this piece of code as an expression. So, enter, we get four. Let's try a different kind of expression. Let's see if `2 is greater than 1`. We get `True`, which is an example of a Boolean value. You're going to learn about these Boolean values in the next section. Now, what if we type `2 is greater than 5`? Enter, we get `False`. So, in programming, we have `True` and `False`, which are similar to yes and no in English.

Now, what if we type `2 is greater than` but we don't add a second value here? Just press Enter. We get a syntax error. In programming, syntax means grammar. So, just like we have the concept of grammar in the languages that we speak, we have the exact same concept in programming. If we write a sentence that is not grammatically correct, chances are some people may not understand that sentence. So, in this example, we have this expression which is incomplete. It doesn't have the right grammar or syntax. That is why the Python interpreter is complaining by returning an error.

So, this interactive shell is a great way to quickly experiment with a bit of Python code, but that's not how we build real-world applications. To do that, we need a code editor, and that's what I'm going to show you in the next lecture.

[Music] when it comes to typing Python code, you have two options. You can use a code editor or an IDE, which is short for Integrated Development Environment. An IDE is basically a code editor with some fancy features like auto-completion, which means as you type code, this feature helps you complete your code so you don't have to type every character by hand. It's a productivity-boosting feature. It also gives you additional features like debugging, which means finding and fixing bugs in your programs, testing, and so on. For both code editors and IDEs, there are so many options out there. The most popular code editors are VS Code, Atom, and Sublime. You can use the code editor that you prefer. In terms of the IDEs, again, there are so many options out there. The most popular one is PyCharm. In this course, I'm going to use VS Code, or Visual Studio Code, because that's my favorite code editor. Later in the course, I will show you how to install a plugin or an extension that will convert VS Code to a powerful IDE.

So, before going any further, head over to code.visualstudio.com and download the latest version of VS Code.

Now, with VS Code open, on the top, from the File menu, go to Open, and somewhere on your disk, create a new folder. Let's call this folder "hello_world" and then open it. Beautiful. Now, click this icon on the top. This opens up the Explorer panel. In this panel, you can see all the files and folders in your project. So, let's add a new file and call that `app.py`. So, all our Python files should have the `.py` extension. Press Enter. Now, let's close this and type a bit of Python code.

In this lecture, we're going to use one of the built-in functions in Python called `print`. So, in Python, we have a lot of built-in functions for performing various kinds of tasks. For example, as a metaphor, think of the remote control of your TV. On this remote control, you have a bunch of functions like "turn on," "turn off," "change the channel," "change the volume," and so on. These are the built-in functions in your TV. We have the same concept in Python and many other programming languages. So, one of these built-in functions that comes with Python is `print`, and we can use this to print something on the screen. Now, whenever you want to use a function, you should open and close parentheses. In programming, we say we're "calling" the `print` function. Calling a function means executing it. Now, let's display the "hello world" message on the screen. Whenever you want to work with text, you should put your text in between quotes, either double quotes or single quotes. Now, I'm going to go with double quotes and add "hello world" and then put a happy Persian cat here. Beautiful. Save the changes with Command + S on Mac or Control + S on Windows.

Now, to execute this code, we need to go back to Command Prompt on Windows or Terminal on Mac. But the good news is that we don't have to switch programs. Here in VS Code, we have an integrated terminal. So, press Control and backtick. That is the key before number one on your keyboard, that is just below the Escape button. So, this is our integrated terminal. Now, if you're on Windows, type `python app.py`. If you're on Mac or Linux, type `python3 app.py`. And here's our "hello world" message in the terminal. Beautiful.

Now, let's take this to the next level and make it a little bit more interesting. Let's close this terminal window by pressing Control and backtick and add a second line of code. So, one more time, `print`. This time, let's add quotes with a star in between them. Now, let's say you want to repeat this star 10 times. So, here we can multiply this star by 10. Save the changes, open up the terminal, and run our program. And you can see this star is repeated 10 times. So, as you see, the instructions in our program are executed from top to bottom, in order. In the next lecture, I'm going to show you how to convert this VS Code to a powerful IDE for building Python applications.

[Music] in this lecture, I'm going to show you how to convert VS Code to a powerful IDE by using an extension called Python. With this extension or plugin, we get a number of features such as linting, which basically means analyzing our code for potential errors. We also get debugging, which involves finding and fixing errors. We'll look at this later in the course. We also get auto-completion, which basically helps us write code faster, so we don't have to type every character. We get code formatting, which is all about making our code clean and readable, just like how we format our articles, newspapers, books to make them clean and readable. We get unit testing, which involves writing a bunch of tests for our code. We can run these tests in an automated fashion to make sure our code is behaving correctly. And finally, we get code snippets, which are reusable code blocks that we can quickly generate, so we don't have to type them all by hand. Now, don't worry about memorizing any of these. As we go through the course, you're going to learn about these features.

So, back to VS Code, on the left side, click this icon. This opens the Extensions panel, where we can install additional extensions to enhance VS Code. Up here, in the search bar, search for "Python." All right, look, we have an official extension for Python from Microsoft. So, go ahead and install this. Now, you might see a box here saying "Reload." If you see that, make sure to click it to reload VS Code.

Now, with this extension installed, we have a ton of new functionality in VS Code for writing Python code. The first one I'm going to show you in this lesson is the ability to run our code. So, back to `app.py`, look, with this extension installed, now we have this play icon on the top for running our code. So, if we click it, we can see the output of our program in the terminal window.

In this lecture, I'm going to show linting in action. So, let's start by writing some invalid code, like this: `print` space with no parentheses, and then "hello world." Earlier, I told you that `print` is a built-in function, and whenever you want to use or call a function, you should always use parentheses. Now, to be more precise, this is actually valid Python 2 code, but because we're using Python 3 here, this is invalid code from Python 3's point of view. So, now when I save the changes, you can see this red underline here. Let's hover our mouse over this underline. You can see this tooltip. It's coming from Pylint. And here's the error message: "Missing parenthesis in call to 'print'. Did you mean 'print(...)'?" So, this is the benefit of linting. As you're writing code, you can see potential problems in your code. You don't have to wait to run your program to see these errors. So, now if we put parentheses here and save the changes, you can see that red underline is gone.

Let's look at another error. Let's type `2 +` and then save the changes. Earlier, we ran this code in Python interpreter's interactive shell, there we got a syntax or grammar error. So, if you hover your mouse here, one more time, you can see Pylint is telling us that this is invalid syntax or invalid grammar. It's like an incomplete sentence. So, this is linting in action.

Now, let me show you a couple of useful shortcuts here. On the top, look at the View menu. Here we have this "Problems" menu. Look at the shortcut: on Mac, it's Shift + Command + M. On Windows, it's probably Shift + Control + M. So, as you're working with VS Code, try to memorize these shortcuts because they really help you write code faster. Now, let's take a look at this Problems panel. So, this Problems panel lists all the issues in your code in one place. So, if you have an application with multiple files, this is really useful because some of those files may not currently be open. So, this linter, Pylint, will analyze all your files, and if it finds any issues, it will list them here in the Problems panel. Now, you can also put this on the right side of the screen. So, let's put it here. So, as you write code, these problems will appear here. Now, let's fix this issue. So, I'm going to add `3` here, save the changes, and you can see the problem disappeared.

And one last thing before we finish this lecture. Once again, on the top, let's go to the View menu. The first item is "Command Palette." This is a very important feature in VS Code. Once again, look at the shortcut: that is Shift + Command + P on Mac, or Shift + Control + P on Windows. With this Command Palette, you can execute various commands in VS Code. If you type "lint" here, you can see all commands related to linting. As you can see, all these commands are prefixed with "Python" because these commands come with the Python extension that we installed earlier. So, these are additional features available to us in VS Code. The first command here is "Select Linter." In this list, you can see various linters available for Python. Pylint, so as you're reading tutorials or talking to other people, you will hear about linters such as Flake8, MyPy, PEP8, and so on. Different developers prefer different linters. I personally prefer Pylint, that is the most popular one, and that is the default linter set in the Python extension of VS Code. If you're adventurous, you can try using other linters on your own. The difference between these linters is in how they find and report errors. Some error messages are more meaningful or more friendly, others are more ambiguous. So, that's all about linting. In the next lecture, we'll talk about formatting code.

In Python community, we have a bunch of documents called Python Enhancement Proposals, or PEPs. Here on Google, if you search for "Python PEPs," you can see the list of all these PEPs under python.org/dev/peps. Let's have a quick look here. So, here are the PEPs. You can see each PEP has a number and a title. The one that is very popular amongst Python developers is PEP 8, which is a style guide for Python code. A style guide is basically a document that defines a bunch of rules for formatting and styling our code. If you follow these conventions, the code that you write will end up being consistent with other people's code. Now, if you have time, you can go ahead and read this PEP 8 documentation, but if not, don't worry, because throughout this course, I'm going to explain the key things in PEP 8.

In this lecture, I'm going to show you a tool that helps you automatically format your code according to PEP 8. So, back in VS Code, let's write some Python code: `x = 1`. Here, I'm declaring a variable and setting it to one. If you're not familiar with variables, don't worry, in the next section, you're going to learn about them. So, according to PEP 8, this code is considered ugly because, by convention, we should add a space around this equal sign, or the assignment operator. Now, since you're starting out with Python, you probably don't know these rules. So, let me show you a tool that helps you automatically format your code. Let's revert this back to its original state.

Now, we need to go back to the Command Palette, remember? So, it's right here under View, and the shortcut is Shift + Command + P. Here, if you search for "format," you can see this command: "Format Document." The first time you execute this command, you're going to see this message here: "Formatter autopep8 is not installed." So, there are a bunch of tools for formatting Python code. The most popular one is autopep8, and this is the tool that this Python extension we installed uses to format our code. Now, if you don't see this, you can install autopep8 using the Extensions panel. So, once again, on the left side, click this icon and search for "autopep8." There it is. Let's install it. Good. So, let's go ahead and install this. Good.

Now, one more time, let's open up the Command Palette and execute "Format Document." See, this tool automatically formats our code. Beautiful. Let's take a look at another example. I'm going to declare another variable, `y`, and set it to `2`, and a variable with a long name like `unit_price`, and we set this to `3`. Now, some developers have this habit of formatting their variable declarations like this: so they put all these equal signs in the same column. According to PEP 8, this is considered ugly. So, once again, let's format our code. That is better. Beautiful.

Now, let me show you a trick. Opening up this Command Palette and searching for "Format Document" every time is a little bit time-consuming. So, I'm going to show you how to have your file automatically formatted as soon as you save the changes. On the top, let's go to the Code menu, Preferences, and Settings. Here, in the search box, search for "format on save." So, we have this option: "Editor: Format On Save." Take this. Now, back to `app.py`, I'm going to change the formatting of these lines, make them really ugly. Now, as soon as I save the changes, you can see my code is reformatted.

[Music] beautiful. All right, now let's talk about a few different ways to run Python code. As I told you before, one way to run Python code is by opening the terminal window. If you're on Windows, type `python app.py`. If you're on Mac, type `python3 app.py`. This approach is useful in situations where you don't have access to a code editor.

Okay, now with the Python extension in VS Code, there is a simpler way to run Python code. We get this play button on the top. When we click it, we see the output in the terminal. But clicking this button every time we change our code is a little bit tedious. So, let me show you how to associate a shortcut to this button. First, we close this. Next, we bring up the Command Palette. The shortcut on Mac is Shift + Command + P. On Windows, it's Shift + Control + P. Here, we search for "Open Keyboard Shortcuts." Look, we have this command up here. Now, on this window, we can see all the commands in VS Code and the shortcuts associated with them. Here, in the search bar, search for "Run Python File." Okay, so this is the command that is associated with the play button. As you can see, we don't currently have any key bindings or shortcuts here. So, double-click in this column. Now, here you can press any key combination for creating a shortcut. I'm going to press Control + R. Okay, now press Enter. With this in place, we can go back to `app.py` and press Control + R, and here we see the output. Beautiful.

When we talk about Python, we mean two separate things that are closely related: Python language and a particular implementation. Python as a language is just a specification that defines a set of rules and grammar for writing Python code. A Python implementation is basically a program that understands those rules and can execute Python code. Earlier in the course, we downloaded Python from python.org. This is the default implementation of Python called CPython. It's a program written in C, that's why it's called CPython. So, here in the terminal, when we run `python`, we get this CPython. This is the default implementation of Python. There are a few other implementations out there, such as Jython (written in Java), IronPython (written in C#), and PyPy (written in a subset of Python itself). As new features are added to the Python language, they are first supported by CPython because that's the default implementation, and then they will gradually come to the other implementations. In theory, if we give some Python code to any of these implementations, we should get the same result. But in practice, that's not always the case. Certain features may be available in one implementation but not another, or they may just behave a little bit differently in a particular implementation.

Now, you might ask, what is the point of this? Why do we have several implementations of Python? Wouldn't CPython be enough? Well, it's for the same reason that we have multiple operating systems, or multiple browsers, or multiple programming languages. After all these years, we programmers haven't agreed on a single programming language, and that's the same story with Python implementations. However, there is one technical reason behind these implementations that you should be aware of. Since Jython is implemented in Java, it allows you to reuse some existing Java code in a Python program. So, if you're a Java developer and you want to import some Java code into a Python program, you should use Jython instead of CPython. Similarly, IronPython is written in C#, so if you're a C# developer and want to bring some C# code into a Python program, you will have to use IronPython.

Next, we'll look at how exactly CPython executes Python code.

[Music] the programming languages we use, like C, C++, Java, Python, these are all simple text-based languages that we humans understand. Computers don't understand them. They only understand machine code. So, if we have some code written in C, we should convert it to machine code, and that's the job of a C compiler. So, a C compiler is a program that knows how to convert or compile C code into machine code. However, this machine code is specific to the type of CPU of a computer. So, if we compile a C program on a Windows machine, we can't execute it on a Mac because Windows and Mac have different machine code. Just like how people from different countries speak different languages. Java came to solve this problem. Java compiler doesn't compile Java code into machine code. Instead, it compiles it into a portable language called Java bytecode, which is not specific to a hardware platform like Windows or Mac. Now, we still need to convert Java bytecode to machine code. So, Java also comes with a program called Java Virtual Machine, or JVM, for doing this. When we run a Java program, JVM kicks in. It loads our Java bytecode, and then at runtime, it will convert each instruction to machine code. With this model, we can run Java bytecode on any platforms that have a JVM. We have JVM implementations for Windows, Mac, and so on. So, the JVM implementation on Windows knows how to convert Java bytecode into machine code that a Windows machine can understand. C and Python have also taken the same route, so they are platform-independent. When we run a Python program using CPython, first, it will compile our Python code into Python bytecode. Then, it will pass that bytecode to the Python Virtual Machine, which will in turn convert it into machine code and execute it. This is how CPython works.

In the last lecture, we talked about various Python implementations. I told you that if you want to reuse some Java code in a Python program, you should use Jython. Now, let's see how Jython makes this possible. When you use Jython to run a Python program, instead of compiling your Python code into Python bytecode, it will compile it to Java bytecode. So, we can take this Java bytecode and run it using the Java Virtual Machine. And that's why you can import some Java code into a Python program when using Jython, because the end result is Java bytecode, which will eventually be executed by the Java Virtual Machine.

[Music] so I've got a few questions for you, cuz I want to see if you have been really paying attention to this video or not. You better have. So, here's the first question. For each question, I want you to pause the video, think about the answer for a few seconds. When you're ready, continue watching. So, here's the first question: What is an expression? An expression is a piece of code that produces a value. Here's an example of an expression: `"*"` * `3`. What do you think is the value of this expression? Well, here we have this string, we're multiplying this by three, so the result will be a string of three asterisks like this: `***`. Here's another question: What is a syntax error? A syntax error is a kind of error that is due to bad syntax or bad grammar in the code. And finally, the last question: What does a linter do? A linter is a tool that checks our code for potential errors, mostly in the category of syntactical errors. So, if you have grammatical issues in our code, the linter will tell us before running our program. Okay, okay, that's it for now. If you like more quizzes and programming exercises, look at the link below this video. And if you have enjoyed this video, I hope you have, please support me by giving a thumbs up. Please like this video and share it with others. In the next section, we're going to look at the fundamentals of [Music] Python.

Hey guys, I just wanted to let you know that this tutorial is actually the first two hours of my complete Python Mastery course. If you're finding this helpful and want to dive even deeper, the full course covers everything from beginner basics to advanced concepts like machine learning, web development, and automation. You'll also get hands-on projects to build your skills step by step. I've put the link in the description box. If you're ready to take your Python knowledge to the next level, now let's continue.

Let's start this section by a discussion of variables, which are one of the core concepts in programming. We use variables to store data in the computer's memory. Here are a few examples: I'm going to define a variable called `students_count` and set it to `1000`. When we run this program, Python interpreter will allocate some memory and store this number `1000` in that memory space. Then, it will have this variable reference that memory location. So, this variable is just like a label for that memory location. We can use this variable, or this label, anywhere in our program to get access to that memory location and the data stored there. So, now if we print `students_count` and run our program, we will get the number `1000`. So, this is the basic of variables.

Now, what kind of data can we store in the computer's memory? Well, we have several different kinds of data. In this section, we're going to look at the built-in primitive types in Python. Primitive types can be numbers, booleans, and strings. Let me show you. So, here we have a whole number. We refer to this as an integer. In programming, we can also have numbers with a decimal point. Let's take a look. So, `rating` we set this to `4.99`. This is what we call a float, or a floating-point number. And this terminology is not specific to Python. In the future, when you learn a new programming language, you're going to hear these terms again.

Now, let's take a look at an example of a boolean: `is_published` we set this to `True` or `False`. These are examples of Boolean values in programming. So, Boolean values can either be `True` or `False`, and these are exactly like yes and no in English. Later in the course, you will learn that we'll use these Boolean values to make decisions in our programs. For example, if the user is an admin user, perhaps we want to give them extra permissions. So, these are the Boolean values. Now, take into account that Python is a case-sensitive language, which means lowercase and uppercase characters have different meanings. So, Boolean values should always start with a capital letter, like what you see here. If we type `false` or `false`, these are not accepted Boolean values in Python. Only what you see here is a valid Boolean value: `False` or `True`.

And finally, let's take a look at an example of a string: `course_name` we set this to a string like "Python Programming." So, a string, as I told you before, is like text. Whenever you want to work with text in your programs, you need to surround your text with quotes. So, these are the basics of variables.

So, these are the variables from the last lecture. Now, I've got a question for you. There are four things that I've consistently used in this program. Can you spot them? If you want, you can pause the video, think about this for a few seconds, and then continue watching. So, here are those four things: The first thing is that all my variable names are descriptive and meaningful. So, `students_count` represents the number of students for a course, or `course_name` clearly explains that this variable holds the name of a course. One of the issues that I see a lot amongst beginner programmers is that they use mystical names for their variables, something like this: `cn` as in short for course name. When someone else reads this code, they have no idea what `cn` stands for, or they use variable names like `c1`. When I look at that code, I wonder, where is `c2` and what is the difference between `c1` and `c2`? So, these variable names are very mystical. That's a bad practice. Make sure your variable names are always descriptive and meaningful because this makes your code more maintainable. Now, there are times that you can use short variable names like `x`, `y`, `z` if you're dealing with things like coordinates, so that's an exception.

Now, the second thing that I have consistently used in this code is that I have used lowercase letters to name my variables. So, here we don't have `course_name` all in capital or in title case. All letters are lowercase, right? Let's delete this.

The third thing that I've consistently used here is that I have used an underscore to separate multiple words, and I've done this to make my variable names more readable because in Python, we cannot have a space in variable names. So, we cannot have `course name`. And if you put these two words together, it's a little bit hard to read. That's why we use an underscore.

And the fourth thing that I have used consistently here is that I have put a space around this equal sign. Again, that's one of the issues I see a lot amongst beginners. They write code like this: `x=1`. This is a little bit ugly. This is what we call dirty code, dirty, stinky, smelly. You should write code that is clean and beautiful, so other people can read it like a story, like a newspaper article. It should be formatted properly, and that's why we have PEP 8 in Python. Now, the good thing is, if you forget these rules, when you save the changes, autopep8 kicks in, and it automatically reformats your code. But that aside, you should always give yourself the habit of writing clean code without relying too much on the tooling.

So, these are all the best practices about naming your variables. Next, we're going to look at strings in more detail.

So, here we have this `course` variable set to "Python Programming." As I told you before, whenever you work with text, you should surround your text with quotes. You can either use double quotes or single quotes. That's more of a personal preference, but quite often we use double quotes. We also have triple quotes, and we use them to format a long string. For example, if you have, let's say, a variable `message` that is the message we want to include in the body of an email, you can use triple quotes to format it like this: "Hi John, this is M. Sh. from codewithm.com. Blah blah blah." So, that's when we use triple quotes. Now, we don't need this in this lecture, so delete.

Let me show you a few useful things you can do with strings. First of all, we have this built-in function in Python for getting the length of strings. What is a function? A function is basically a reusable piece of code that carries out a task. As a metaphor, think of the remote control of your TV. On this remote control, you have buttons for different functions like "turn on," "turn off," "change the channel," and so on. These are the built-in functions in your TV. In Python and many other programming languages, we have the exact same concept. So, we have functions that are built into the language on the platform. You can reuse these functions to perform various tasks. So, here we can use the built-in `len` function to get the length of a string, which means the number of characters in that string. Now, whenever you want to use a function, you should use parentheses. Now, we say we're calling this function, which basically means we're using this function. Now, some functions take additional data, which we refer to as arguments. These arguments are inputs to these functions. So, this `len` function takes an input or an argument. Here, we pass our `course` variable, and this will return the number of characters in this string. So, let's print that and see what we get. Run the program. We get `18` because we have 18 characters here.

Let's look at another example. If you want to get access to a specific character in this string, you use the square bracket notation. So, here we add `course` square brackets to get the first character, you use the index `0`. So, in Python, like many other languages, strings are zero-indexed, which means the index of the first character, or the first element, is `0`. So, now when we print this, we'll get `P`. Okay, now you can also use a negative index like `-1`. What does that mean? Well, if `0` represents the first character here, what do you think `-1` represents? That takes us back to the end of the string. So, that returns the first character from the end of the string. Let's run this program. You will see we'll get `g`. There you go.

Using a similar syntax, you can slice strings. Let me show you. So, I'm going to duplicate this line and remove `-1`. Now, let's say we want to extract the first three characters in this string. So, here we need two indexes: the start index, colon, the end index. So, this will return a new string that contains the first three characters in this `course` variable. That would be `P`, `Y`, and `T`. So, the index of these characters are `0`, `1`, and `2`. So, that means the character at the end index is not included. Okay, let's run the program and make sure we get the right result. There you go: `PYT`.

Now, what if we don't include the end index? What do you think we're going to get? It's common sense. We start from index `0` and go all the way to the end of the string. So, this will return a new string that is exactly the same as the original string. Let's take a look. So, we get "Python Programming."

Now, what if we don't include the start index but include the end index? What do you think we're going to get? Once again, it's common sense. So, by default, Python will put `0` here. So, it will start from the beginning of the string. So, when I run this program, we should get `PYT` one more time. There you go.

And finally, as the last example, if we don't include the start and the end index, this will return a copy of the original string. Let's look at this. So, we get "Python Programming."

Now, you don't have to memorize any of these. Just remember, we use the `len` function to get the length of a string. We use bracket notation to get access to a specific element or a specific character. And we use this notation to slice a string.

So, we have this string here: "Python Programming." Now, let's say we want to put a double quote in the middle of this string. There is a problem here. Python interpreter sees this second double quote as the end of the string. So, the rest of the code is meaningless and invalid. How do we solve this problem? Well, there are two ways. One way is to use single quotes for our string, and then we can use a double quote in the middle of the string. But what if, for whatever reason, perhaps for being consistent in our code, we decided to use double quotes? How can we add another double quote in the middle of this string? Well, we can prefix this with a backslash. Backslash in Python strings is a special character. We have a jargon for that called "escape character." We use it to escape the character after. Let me show you what I mean. So, let's print this `course` and run this program. What's going on here? We don't have the backslash because we use that to escape this double quote and basically display it here. So, backslash is an escape character, and backslash double quote is an escape sequence in Python strings. We have a few other escape sequences that you should be aware of. Let me show you. So, in Python, we use a hash sign to indicate a comment. A comment is like an additional note that we add to our program. It's not executed by the Python interpreter. Okay, so here are the escape sequences: You have seen backslash double quote. We also have backslash single quote, so we can use that to add a single quote here. Let's run the program. Here it is. Beautiful. We also have double backslash. So, if you want to include a backslash in your strings, you should prefix it with another backslash. Let me show you. So, when we run this, we get "Python\" programming." And finally, we have backslash n, which is short for newline. So, now if I add a backslash n here, see what we get? We get a new line after "Python," so "Programming" will end up on the second line. So, these are the escape sequences in Python.

Here, we have two variables: `first` and `last`. Let's say we want to print my full name on the console. So, we can define another variable `full`, set it to `first`, then concatenate it with a space, and one more time concatenate it with `last`. Now, when we print `full`, we get my full name on the console. Beautiful. Now, this approach of using concatenation to build a string is okay, but there is a better and newer approach. We can use formatted strings. So, here we can set `full` to this string and prefix it with an `f`, which can be lowercase or uppercase. This formatted string doesn't have a constant value like these two strings here. It's actually an expression that will be evaluated at runtime. So, here we want to add our first name. We use curly braces to print the value of the `first` variable. After that, we add a space, and then we add curly braces one more time to print the `last` name. So, at runtime, this expression will be evaluated. What we have in between curly braces will be replaced at runtime. Now, let's run this program one more time. We get the exact same result. Just be aware that you can put any valid expressions in between curly braces. So, earlier you learned about the built-in `len` function. We can call `len` here to get the length of this string. Let's run this program one more time. So, we get `4`. We can also replace `last` with an expression like this: `2 + 2`. Let's run this program. We get `4` and `4`. So, when using formatted strings, you can put any valid expressions in between curly braces.

In this lecture, we're going to look at a few useful functions available to work with strings. So, earlier you learned about this built-in `len` function. This function is general purpose, so it's not limited to strings. Later, I will show you how to use this function with other kinds of objects. But in Python, we have quite a few functions that are specific to strings. Let me show you. So, here, if we type `course.` see all these are functions available on strings. Now, in precise terms, we refer to these functions as methods. This is a term in object-oriented programming that you will learn about later in the course. For now, what I want you to take away is that everything in Python is an object, and objects have functions we call methods that we can access using the dot notation. So, here `course` is an object. We use the dot notation to access its functions, or more accurately, methods.

Let's take a look at a few of these methods. We have `upper` to convert a string to uppercase. Now, let's print this and run the program. Here's what we get. Beautiful. Now, note that the methods that you call here return a new string. So, the original string is not affected. Let me show you. So, `print(course)`. Run the program one more time. Look, this is our original string, right? So, `course.upper()` returns a new string, a new value. We can store it in a variable like `course_capital` like this. Now, to keep this demo simple and consistent, I'm going to revert this back and use a print statement.

We also have the `lower` method to convert a string to lowercase. We also have `title`, which will capitalize the first letter of every word. So, if our string was like this, when we call the `title` method, we get "Python Programming," as you see here. Okay.

Another useful method is `strip`, and we use it to trim any whitespace at the beginning or end of a string. This is particularly useful when we receive input from the user. Let me show you. So, let's imagine the user entered a couple of whitespace characters at the beginning of this string. When we call `course.strip()`, those whitespace characters will be removed. Take a look. So, note that in the first three examples, we have those whitespace characters, but in the last one, it is removed. So, `strip` removes the whitespace from both the beginning and end of a string. We also have `lstrip`, which is short for left strip, and `rstrip`, which is short for right strip. So, it will remove the whitespace from the end of a string.

If you want to get the index of a character or a sequence of characters in your string, you should use the `find` method. Let me show you. So, `course.find()`. As an argument here, we pass another string. We can pass a character or a series of characters. Let's find the index of "pro." Run the program. So, the index of "pro" is `9`. So, if we start from `0` here, all the way to `9`, this is the index of "pro." Okay. Now, as I told you before, Python is a case-sensitive language. So, if I pass a capital `P` here, obviously we don't have these exact characters in our string. So, let's see.

what we get we get -1. That means this string was not found in the original string.

Another useful method is `replace`. So we call `replace` with this. We can replace a character or a sequence of characters with something else. So let's say we want to replace all lowercase P's with J. With this, we get "jython durog gramming whatever that means".

And finally, if you want to check for the existence of a character or a sequence of characters in your string, you can use the `in` operator. Let me show you. So, `print("Pro" in "course")`. So this is an expression. As I told you before, an expression is a piece of code that produces a value. So this expression checks to see if we have "Pro" in "course". The difference between this expression and calling the `find` method is that the `find` method returns the index of these characters in our string, but this expression returns a Boolean, so it's a true or false. Let me show you. So, run the program, we get the Boolean `True`.

And finally, we have the `not` operator. And we use that to see if our string does not contain a character or a sequence of characters. So let's change this to `"Swift" not in "course"`. When this expression is evaluated, what do you think we're going to get? Well, we don't have "Swift" in this string, so `not in` will return `True`. Let's take a look. There you go.

So these are the useful string methods. Next, we'll look at numbers. In Python, we have three types of numbers. Two of these you have already seen before. They are integers and floats. We also have complex numbers. So complex numbers in math are in the form a + bi, where i is the imaginary number. Now, if you're not familiar with this concept, don't worry. This is something that is used a lot in mathematics and electrical engineering. If you want to use Python to build web applications, you're never going to use complex numbers. But let me quickly show you the syntax for representing complex numbers. Instead of i, we use j. So here is an example: `1 + 2j`. So `x` now is a complex number. And by the way, as I told you before, this is just a common or an additional note in our program. When we run this program, anything after this '#' sign will be ignored.

So these are the three types of numbers we have in Python. For all these types of numbers, we have the standard arithmetic operations that we have in math. Let me show you. So we have addition, subtraction, multiplication, division. But we actually have two different types of divisions. Let me show you. First, let's run this program. So with this division operator, which is a slash, we get a floating-point number. If you want an integer, you use double slashes. Let me show you. So, `//`. Run the program, we get `3`. Okay. We also have modulus, which is the remainder of a division. And finally, exponent, which is left to the power of right. So 10 to the power of 3 will be a thousand. These are the standard arithmetic operators.

Now, for all these operators, we have a special operator called augmented assignment operator. Let me show you. So let's imagine we have `x` set to 10. We want to increment `x` by, let's say, three. We can write an expression like this: `x = x + 3`. Or we can use an augmented assignment operator that is a little bit shorter. So we write `x += 3`. These two statements are exactly the same. Now, here I'm using addition as an example. You can use any of these operators here.

Next, I'm going to show you some useful functions to work with numbers. In this lecture, we're going to look at a few useful functions to work with numbers. So we have this built-in function `round` for rounding a number. So if we pass `2.9` here and print the result, we will get `3`. We have another useful built-in function called `abs`, which returns the absolute value of a number. So if we pass `-2.9` here, we'll get positive `2.9`.

Now, technically, we have only a handful of built-in functions to work with numbers. If you want to write a program that involves complex mathematical calculations, you need to use the `math` module. A module is like a separate file with some Python code. So in Python, we have this `math` module, which includes lots of mathematical functions for working with numbers. But we need to import this module so we can use it. On the top, we type `import math`. Now, `math` in this program is an object. So we can use the dot notation to see all the functions, or more accurately, all the methods available in this object. As an example, we have `math.ceil` for getting the ceiling of a number. So if we pass `2.2` here and run this program, we get `3`.

Now, in this `math` module, we have lots of functions. Let me show you how to find the complete list. Here, on Google, search for "Python 3" (make sure to add the version number) "math module". On this page, you can see all the functions in the `math` module. So in this lecture, we looked at `math.ceil`. We also have `math.copysign`, `fabs`, and so on. As an exercise, I encourage you to play with a couple of functions in this module.

All right, now let's take a look at another useful built-in function in Python. We use the `input` function to get input from the user. As an argument, we pass a string. This will be a label that will be displayed in the terminal. You'll see that in a second. So let's add `name: `. Now, this function returns a string, so we can store it in this variable. Now, let's imagine that `y` should be `x + 1`. Save the changes. Now, don't run this program using the Code Runner extension, because Code Runner by default runs your program in the output window, which is read-only. So you won't be able to enter a value. So open up the terminal using `Ctrl + \`` (or `Cmd + \`` on Mac). Once again, if you're on Windows, type `python`. If you're on Mac or Linux, type `python3` and then `app.py`.

So here's our label. Let's enter a value like `1`. We got an error: `TypeError`. What is going on here? Well, when we receive input from the user, this input always comes as a string. So this expression at runtime will look like this: `"1" + 1`. Note that the number `1` is different from the string `"1"` because these are two different types. Now, when Python sees this expression, it doesn't know what to do because two objects can be concatenated if they are of the same type. So here, we need to convert this string `"1"` to a number. In Python, we have a few built-in functions for type conversion. We have `int` for converting a number to an integer, we have `float`, we have `bool`, and `str` or string.

Now, in this case, we don't need to convert `x` to a string because `x` is already a string. If you don't believe me, let me show you. So I'm going to comment out these few lines. Now, let's print `type(x)`. So `type` is another built-in function. We pass an object as an argument, and it returns its type. Also, I'm going to comment out this line because that's the bad boy we don't want to execute this. Save the changes. Back in the terminal, let's run this program one more time. Enter `1`. Look, this is what the `type` function returns. Now, don't worry about the `class`. We'll talk about classes later in the course. So the type of `x` is `str` or string.

So let's delete this line. To fix this problem, we need to convert `x` to an integer. And then we can print both `x` and `y` using a formatted string. Remember? So we add an `f` before quotes right here. We add a label like `x: `. Then we'll add a field. Here we want to print the value of the `x` variable. After that, we add some more text, and finally, we want to print the value of `y`. Let's run this program one more time. So here in the terminal, let's enter `1`. And here's the result: `x is 1` and `y is 2`. Beautiful.

Now, all these built-in functions are self-explanatory. The only tricky one is `bool`, because in Python, we have this concept of truthy and falsy values. These are values that are not exactly a Boolean `True` or `False`, but they can be interpreted as a Boolean `True` or `False`. So here are the falsy values in Python: empty strings are considered falsy, so they're interpreted as a Boolean `False`. Number `0` is also falsy. We have an object called `None`, which represents the absence of a value. We'll look at this later in the course. So whenever we use these values in a Boolean context, we get `False`. Anything else will be `True`. Let me show you a few examples. So in this interactive shell in Python, let's convert number `0` to `bool`. That's falsy, so we get `False`. What about `bool(1)`? We get `True`. If we pass a negative number, we also get `True`. If we pass a number larger than one, like `5`, we still get `True`. So we only get `False` when we try to convert `0` to a Boolean.

Now, with strings, I told you that an empty string is falsy, so here we'll get `False`. Anything else is `True`. So even if I have a string that is `"False"`, we'll get `True` because this is not an empty string. It's a string with a few characters. That's why it's evaluated as `True`.

All right, once again, it's time for another quiz. Let's see if you have been really paying attention to this tutorial. So here's the first question: What are the built-in primitive types in Python? We have strings, numbers, and booleans. Numbers can be integers, floats, or complex numbers.

Here's the second question: You have this variable `fruit` set to `"apple"`. What do you think we will see on the terminal when we print `fruit[1]`? Well, using square brackets, we can access individual characters. The index of the first character is `0`, so this expression returns the second character, which is `p`.

What if we add a colon and `-1` here? Well, using this syntax, we can slice a string. Our start index is `1` and our end index is `-1`, which refers to the first character from the end of the string. Now, when slicing a string, the character at the end index (`-1`) is not included. So with this expression, we'll get all the characters starting from the second character, which is `p`, all the way until we get to `e`. So the result of this expression is `"ppl"`.

Here's another question: What is the result of this expression: `10 % 3`? Well, this is what we call the modulus operator, and it returns the remainder of a division, which is in this case `1`.

And finally, the last question: What do you think we will see when we print `bool("False")`? Well, earlier I told you about falsy values in Python. So number `0`, an empty string, and the `None` object, these are all falsy values. Anything that is not falsy is considered truthy. Here we have a string that has five characters. It doesn't matter what those characters are. This is not an empty string, so it's not falsy. It's truthy. So when we convert it using the `bool` function, we'll get the Boolean `True`.

And this brings us to the end of the section. In the next section, you're going to learn the fundamentals of computer programming. I hope you have enjoyed this section and thank you for watching.

We're going to start this section by exploring comparison operators. We use comparison operators to compare values. Here are a few examples. So `10 > 3`. We get `True`. So what we have here is a Boolean expression, because when this expression is evaluated, we'll get a Boolean value that is true or false. Here is another example: `10 >= 3`. Once again, we get `True`. We also have less than: `10 < 20`. We have less than or equal to. Here's the equality operator: `10 == 10`. What about this expression: `10 == "10"`? What do you think we're going to get? We get `False` because these values have different types and they're stored differently in the computer's memory. And finally, we have the not equal operator: `10 != 20`. So now with this expression, we should get `True`. Beautiful.

We can also use these comparison operators with strings. Let me show you. So we can check to see if `"bag" > "apple"`. We get `True` because when we sort these two words, "bag" comes after "apple", so it's considered greater. Now, what about this one: `"bag" == "Bag"`? We get `False`. Here's the reason: every character you see here has a numeric representation in programming. Let me show you. So we have this built-in function called `ord`. Don't worry about memorizing this, because you're probably never going to use this in the future. But let me show you the numeric representation of the letter `b`. So that is `98`. In contrast, capital `B` is represented as `66`. That is the reason these two strings are not equal.

So these are the comparison operators in Python. Next, we'll look at conditional statements. In almost every program, there are times you need to make decisions, and that's when you use an `if` statement. Here's an example. Let's say we have a variable called `temperature`. We set it to `35`. Now, `if temperature > 30`, perhaps we want to display a message to the user. So we use an `if` statement. `if` after `if`, we add a condition, which is basically a Boolean expression, an expression that produces a Boolean value. So `if temperature > 30`. Here we have a Boolean expression. If this expression evaluates to `True`, the following statements will be executed. Let me show you.

Now, here's the important part that a lot of beginners miss: when you use an `if` statement, you should always terminate your statement with a colon. Now, let's see what happens when I press Enter. Our cursor is indented. So here we have two white spaces. This is very important because using these indentations, the Python interpreter will know what statements should be executed if this condition is `True`. Here we want to print a message like "It's warm". We can print another message as well: "Drink water". So we can have as many statements as we want here, as long as they are indented, they belong to this `if` block.

Now, when we finish here, we should remove indentation to indicate the end of this `if` block. So here we can add a `print` statement with a message like "Done". This statement will always be executed, whether this condition is `True` or not. Now, note that when I save the changes, this indentation you see here is going to be doubled up. Take a look. Save. There you go. So when we save the changes, `autopep8` reformats our code and uses four white spaces for indentation. So one, two, three, four. It uses four white spaces because that's what PEP 8 recommends.

All right, now let's run this program. So because `temperature` is greater than `30`, we see the first two messages, and we see the "Done" message regardless. So if I change the `temperature` to, let's say, `15` and run the program one more time, look, this "Done" message is executed whether our condition is true or not. So pay great attention to these indentations. That's one of the issues I see in beginner's code. Let's say they want both these print statements to be executed if the condition is true. Accidentally, they remove the indentation on the fourth line, and that's why their program doesn't work as they expect. So be careful about this.

Let's say you want to have multiple conditions. We use an `elif` statement. So `elif`, that is short for "else if". Here we can add another condition, another expression. So `temperature > 20`. Once again, colon, Enter. Now, by default, here VS Code is using two white spaces. Don't worry about this. As soon as you save the changes, those two white spaces will be converted to four white spaces. So let's print a different message: "It's nice". Save the changes. Now, look, all these lines are indented consistently. You can have as many `elif` statements as you want. And optionally, you can also have an `else` statement. So if none of the previous conditions are true, then what you have in the `else` block will be executed. Once again, we add the colon. `print("It's cold")`. Save the changes. In this case, `temperature` is `15`, so none of these two conditions will be true, and we will see "It's cold". Let's run the program. There you go.

In this lecture, I'm going to show you a technique for writing cleaner code. So let's say we're building an application for a university and we want to check to see if the person who's applying for this university program is eligible or not. So we start by defining a variable called `age`, set it to `22`. Now, `if age >= 18: print("Eligible") else: print("Not eligible")`. Let's run the program. Make sure it works. Beautiful. Now, there is nothing wrong in this piece of code, but I want to show you a cleaner way to achieve the same result. Instead of having a `print` statement here, we can define a variable like `message` and set it to this string. That is the first step. So `message = "Eligible"`. And then we will print this message.

Now, when you have an `if-else` statement with this structure, where you're basically assigning a value to a variable, you can rewrite this in a simpler way. So this is how it works. All we want to do over these four lines is to assign a value to this `message` variable, right? So, `message = "Eligible" if age >= 18 else "Not eligible"`. This statement is almost like plain English. So what we have on line seven is exactly equivalent to these four lines of code. Delete. Save the changes. Run the program. You can see this person is eligible. If I change the `age` to `12` and run the program, we get "Not eligible". So what we have here is called the ternary operator.

In Python, we have three logical operators, and we use these operators to model more complex conditions. So these operators are `and`, `or`, and `not`. Let's see a real-world example of using these operators. So imagine we're building an application for processing loans. So we need two variables: `high_income`, we can set this to `True`, and `good_credit`, we set it to `True`. Now, here's the condition we want to implement: if the applicant has high income and good credit score, then they are eligible for the loan. So `if high_income and good_credit: print("Eligible")`. Now, note that here I have not compared the value of this variable with `True`. That is one of the issues I see in a lot of beginners' code. This is redundant and unprofessional because `high_income` is a Boolean, so it's either `True` or `False`. We don't need to compare `True` with `True`. So if this condition is `True` and this second condition is `True`, then we will print "Eligible" in the terminal. So save the changes and run the program. Obviously, this person is eligible. However, if one of these conditions is `False`, we will not see "Eligible" in the terminal. So let's add an `else` statement here and print "Not eligible". Run the program. We see "Not eligible". So this is how the `and` operator works. With `and` operator, if both conditions are `True`, the result will be `True`.

In contrast, with the `or` operator, as long as at least one of the conditions is `True`, the result will be `True`. So if I replace `and` with `or` here, we should see "Eligible" in the terminal. Let's run it one more time. There you go. So these are the `and` and `or` operators.

Now, let's take a look at an example of the `not` operator. So I'm going to define another variable `student` set it to `True`. Temporarily, I'm going to remove this expression and simplify it. We'll come back to this later. So let's say, `if not student: print("Eligible")`. The `not` operator basically inverts the value of a Boolean. So in this case, `student` is `True`. When we apply the `not` operator, the result will be `False`. So in this case, our condition will be `False`, and that's why this `print` statement will not be executed. Let me show you. So save, run the program. "Not eligible". If `student` was `False`, when we apply the `not` operator, we'll get `True`. So our condition will be `True`, and we'll see "Eligible". Let's run it one more time. There you go.

With these operators, we can model even more complex conditions. Here's an example: a person can be eligible if they have either high income or good credit, and they should not be a student. Let me show you how to implement this condition. So `if (high_income or good_credit) and not student: print("Eligible")`. So with these operators, you can model all kinds of real-world scenarios.

So here's the example from the last lecture: a person is eligible for a loan if they have high income and good credit and they're not a student. Now, one thing you need to know about these Boolean operators is that they are short-circuit. What do I mean by that? Well, when the Python interpreter wants to evaluate this expression, it starts from the first argument. If this is `True`, it continues the evaluation to see if the second argument is also `True`. So it continues the evaluation all the way to the end of this expression. However, as soon as one of these arguments is `False`, the evaluation stops. Let me show you what I mean. So if I change `high_income` to `False`, when the Python interpreter sees this expression, it starts here. It knows that `high_income` is `False`, so it doesn't matter what comes after; the result of this entire expression will always be `False` because at least one of the arguments or one of the operands is `False`. This is what we call short-circuiting, just like the short-circuit concept we have in electronics. So the evaluation stops as soon as one of these arguments evaluates to `False`.

We have the same concept with the `or` operator. So if I change these `and` operators to `or`, let's see what happens. With the `or` operator, we know that at least one of the arguments should be `True`, so the evaluation stops as soon as we find an argument that evaluates to `True`. In this case, when the Python interpreter evaluates this expression, it sees that `high_income` is `False`, so it continues the evaluation, hoping that the next argument will be `True`. Here, `good_credit` is `True`, so evaluation stops, and the result of this entire expression will be `True`. So in Python, logical operators are short-circuit.

In this lecture, I'm going to show you how to chain comparison operators. This is a very powerful technique for writing clean code. Here's an example. Let's say we want to implement a rule that says age should be between 18 and 65. Here's how we can implement it. So we define a variable like `age`, set it to `22`. Now, `if age >= 18 and age < 65: print("Eligible")`. Now, here's a question for you: how do we write this rule in math? We can write it like this. Well, more accurately, we should have an equal sign here. So `18 <= age <= 65`. This is how we write this rule in math. Now, I've got some good news for you. We can write the exact same expression in Python. So I'm going to move this up. Put an `if` statement here. Line four and line three are exactly equivalent, but as you can see, line four is cleaner and easier to read. So let's get rid of line three. This is what we call chaining comparison operators.

All right, here's a little quiz for you. I want you to pause the video and think about this quiz for 10 to 20 seconds. What do you think we'll see on the terminal when we run this program? So pause the video, figure out the answer. When you're ready, come back and continue watching.

All right, let's see what happens when we run this program. First, we get this `if` statement. In this case, we're comparing two different objects for equality, and these objects have different types. We have a number compared with a string, so number `10` and string `"10"` are not equal. That is why `"a"` will not be printed on the terminal. So the control moves to the `elif` part. Here we have two Boolean expressions. Here's the first one, here's the second one, and they are combined using the logical `and`. So if both these expressions are evaluated to `True`, then this entire expression will be `True`, and we will see `"b"` on the terminal. Let's see if both these expressions are evaluated to `True`. Here's the first part: `"bag" > "apple"`. That is `True` because when we sort these words, "bag" comes after "apple". But look at the second part: `"bag" > "cat"`. This expression is evaluated to `False` because "bag" is not greater than "cat". So when we apply the logical `and` between `True` and `False`, the result will be `False`. That is why this statement will not be executed. So the control moves to the `else` part, and when we run this program, the letter `"c"` will be printed on the terminal.

There are times that we may want to repeat a task a number of times. For example, let's say we send a message to a user. If that message cannot be delivered, perhaps we want to retry three times. Now, for simplicity, let's imagine this `print` statement is equivalent to sending a message. In a real-world program, to send a message to a user, we have to write five to ten lines of code. Now, if you want to retry three times, we don't want to repeat all that code. That's ugly. That's when we use a loop. We use loops to create repetition. So here is how it works. We start with `for number in range(3):`. Now, similar to our `if` statements, we need to terminate this line with a colon. Enter. We get indentation. So in this block, we can write all these statements that should be repeated three times. Let's do a `print("Attempt")`. Save the changes. Run the program. So we have "Attempt" printed three times. Beautiful.

Now, what is this `number`? Let's take a look. It's a variable of type integer. So let's pass it as the second argument to the `print` function: `print("Attempt", number)`. Run the program. This is what we get: `0`, `1`, `2`. So here we have a `for` loop. This `for` loop is executed three times. In each iteration, `number` will have a different value. Initially, it will be `0`. In the second iteration, it will be `1`. And finally, in the last iteration, it will be `2`.

Now, here we can do something fun. We can add one to this. Run the program. And now the messages that we print are kind of more meaningful or more user-friendly, like "Attempting number 1", "Attempting number 2", and so on. We can take this to the next level. So we can pass another argument here. I'm going to add an expression. One more time: `number + 1`. So we'll get `1`, `2`, `3`. Now I want to put this expression in parentheses. So let's select this, put it in parentheses, and then multiply it by a dot. So here we have a string that is multiplied by a number. The result will be that string repeated that number of times. Let's take a look. So run the program. See? That's pretty cool, isn't it?

Now, let me show you one more thing before we finish this lecture. As you saw, this `range` function generates numbers starting from zero all the way up to this number here, but it doesn't include this number here. We can pass another argument, say, start from `1` and finish before `4`. With this change, we don't need to add `1` to `number` every time, because in the first iteration, this `number` variable will be set to `1`. So we can simplify our code and make it cleaner. Let's run it one more time. We get the exact same result. We can also pass a third argument as a step. So I'm going to change the second argument to `10` and pass `2` as a step. Look at the result. These are the numbers we get: `1`, `3`, `5`, and so on. So pretty useful. You're going to use this function a lot in real-world applications.

Continuing with the example from the last lecture, let's imagine the scenario where after the first attempt, we can successfully send the message. In that case, we want to jump out of this loop. We don't want to repeat this task of sending a message three times. Let me show you how to implement this. So in this demo, I'm going to simulate the scenario where we can successfully send a message. So we define a variable `successful` and set it to `True`. Now, here, after this `print` statement, we'll have an `if` statement. `if successful:`. Then perhaps we can print "Successful". Now, here we want to jump out of this loop. For that, we use the `break` statement. Let's run this program and see what happens. So there you go. After the first attempt, you're successful, and there are no more attempts. So once again, I want you to pay great attention to the indentation here, because that's one of the common issues amongst beginners. So here's our `for` loop. These two lines are indented with four spaces, and they belong to our `for` loop. In every iteration, these two lines will be executed. Now, when we get to line four, if this condition is `True`, then these two lines will be executed because both these lines are indented below this `if` statement.

Now, let's take this program to the next level. What if we attempt three times and we still cannot send an email? Perhaps we want to display a different message to the user. We say, "Hey, we tried three times, but it didn't work." So I'm going to change `successful` to `False`. Now, at the end here, we can add an `else` statement. This is what we call a `for-else` statement. What we put under this `else` statement will only be executed if this loop completes without an early termination. So if we never `break` out of this loop, then the `else` statement will be executed. So here we can print a message like "Attempted three times and failed". So run the program. See what we get. Three attempts followed by this message: "Attempted three times and failed". In contrast, if we change `successful` to `True`, because we terminate this loop using this `break` statement, what we have in the `else` block will not be executed. Take a look. Run the program. We have "One attempt successful". Done.

In programming, we have this concept called nested loops. So we can put one loop inside of another loop, and with this, we can get some interesting results. Let me show you. So I'm going to start with this loop: `for x in range(5):`. Now, inside of this loop, I'm going to add another loop: `for y in range(3):`. And then in our second loop, I'm going to add a `print` statement. Here we can use formatted strings to display coordinates. Remember formatted strings? So we have `f` followed by quotes. Now, here we add parentheses for our coordinates. First, we want to display `x`, and then a comma, followed by `y`. Let's run this program and see what happens. There you go. Pretty cool, isn't it? So we get `(0, 0)`, `(0, 1)`, `(0, 2)`, then we get `(1, 0)`, `(1, 1)`, `(1, 2)`, and so on.

Now, let me explain how exactly the Python interpreter executes this code. So here we have two loops. This is what we call the outer loop, and this is the inner loop. So the execution of our program starts here. In the first iteration of this loop, `x` is `0`. Now, we get to this statement, which is a child of this `for` statement because it's indented four times. This statement itself is a loop. So what we have inside of this loop will be executed three times. In the first iteration, `x` is `0` because we're still in the first iteration of the outer loop, and `y` is also `0` because we are in the first iteration of the inner loop. That is why we get `(0, 0)`. Now, we go to the second iteration of this inner loop. In this iteration, `y` will be `1`, whereas `x` is still `0`. That is why we get `(0, 1)`. And similarly, in the third iteration of our inner loop, we'll get `(0, 2)`.

Now, we're done with the execution of the inner loop. So the control moves back to our outer loop. Here, we'll be in the second iteration, so `x` will be `1`. And then we start here again. So we have to execute this inner loop three times. In the first iteration, `y` will be `0`, and `x` is `1`. So here we have `(1, 0)`. Then we'll get `(1, 1)` and `(1, 2)`. You got the point. So this is all about nested loops.

So you have learned how to use `for` loops to repeat one or more statements in your programs. Now, let's dive deeper and see what this `range` function returns. So earlier, you learned about the built-in `type` function. With this function, we can get the type of an object. So if I pass `5` here and run this program, this is what we get. So the type of this number, or this object, is `int` or integer.

Now, let's look at the type of the value that we get from the `range` function. So as an argument, we pass `range(5)`. Let's run this program. So this `range` function returns an object of type `range`. So in Python, we have primitive types like numbers, strings, and booleans, but we also have complex types. `range` is an example of one of those complex types. Throughout this course, you're going to learn about a lot of other complex types.

Now, what is interesting about this `range` object is that it's iterable, which means we can iterate over it or use it in a `for` loop. That is why we can write code like this: `for x in range(5): print(x)`. So this `range` function returns a `range` object, which is iterable, which means we can iterate over it. In each iteration, `x` will have a different value.

Now, `range` objects are not the only iterable objects in Python. Strings are also iterable. So here we can add a string like `"python"`. Now, in each iteration, `x` will hold one character in this string. Let me show you. So `print(x)`. And I'm going to delete these two lines here. Let's run this program. So in each iteration, we'll get one character and print it. We have another complex type called `list`, which we use to store a list of objects. So we add square brackets. This indicates a list. Now, we can add a list of numbers or a list of strings, like a list of names. You will learn about lists later in the course. So let's run this one more time. As you can see, we can iterate over lists. In each iteration, we'll get one object in this list.

Now, later in the course, I will show you how to create your own custom objects that are iterable. For example, you will learn how to write code like this: `for item in shopping_cart: print(item)`. So `shopping_cart` is going to be a custom object that you will create. It's not going to be an integer or string or boolean. It's a custom object. It has a different structure, and we'll make it iterable so we can use it in a `for` loop, and in each iteration, we can get one item in the shopping cart and print it on a terminal.

So you have learned that we use `for` loops to iterate over iterable objects in Python. We have another kind of loop that is a `while` loop, and we use that to repeat something as long as a condition is true. Here's an example. So let's define a variable `number` and set it to `100`. Now, we use `while number > 0:`. Here we add a condition. As long as `number` is greater than `0`, we add a colon. Once again, we have indentation, so we can repeat one or more statements. We can print this `number`, and then we can divide it by half. So `number = number // 2`. Or we can use the augmented assignment operator to shorten this code like this: `number //= 2`.

Now, let's run this program. So here's what we get. Initially, our `number` is `100`. We divide it by half, we get `50`, then `25`, and so on. So as you can see, in this example, we are not iterating over an iterable like a `range` object or a string or a list. We are evaluating a condition and repeating a task.

Let me show you a real-world example of a `while` loop. In this interactive shell, Python is waiting for an input. We can type something like `2 + 2`. It will evaluate it and ask for the next input. We can add another expression like `10 > 2`. So these steps will continue until we press `Ctrl + D`. So behind the scene, we have a `while` loop that continues execution until we press `Ctrl + D`. That is the condition that causes the `while` loop to terminate. Let me show you how to build something like this in Python. So let's define a variable `command` and set it to an empty string. Now, here we need a `while` loop. We want this `while` loop to execute as long as `command` does not equal `"quit"`. So `while command != "quit":`. Colon. In this loop, we want to continuously get input from the user. So we use the built-in `input` function. We add a label like `Enter command: `. Get the result and store it in the `command` variable. Now, at this point, Python interactive shell will evaluate this command. We're not going to do that in this lecture because that's way too complex. For simplicity, we can just echo back what the user entered. So `print("Echo:", command)`. So this is our `while` loop. It will execute until we type `"quit"`.

Now, as I told you before, don't run this program using the Code Runner extension, because by default, it will run your program in the output window, which is read-only. So open up the terminal using `Ctrl + \`` (or `Cmd + \`` on Mac) and run `python` or `python3 app.py`.

So here's our command prompt. Let's type `2 + 2`. It echoes back. Let's type `3 * 2`. There you go. If we type `"quit"`, our program terminates.

Now, let's try it one more time. What if we type `"quit"` in uppercase? The program doesn't terminate because, as you learned before, lowercase and uppercase characters have different numeric representations. So `"quit"` in lowercase is different from `"quit"` in uppercase. Now, to solve this problem, an amateur programmer may do something like this: `while command != "quit" and command != "Quit":`. Continue getting input from the user. Let's run this program in the terminal and see what happens. So, one more time, `python app.py`. We type `"quit"`. Beautiful. It works. We type `"Quit"`. That would work too. But what if I type `"quit"` with an uppercase `Q` and lowercase `u`? Our program doesn't terminate. So this is a poor way of checking for the quit command. What is a better way? Let me show you. So we don't need this `and` operator here. Instead, because `command` is a string, we can call the `.lower()` method. So whatever the user types in, we'll convert it to lowercase and then compare it with `"quit"` in lowercase. With this change, it doesn't matter how the user types the word `"quit"`, it will always terminate the program.

Now, the last thing I want to discuss in this section is the concept of infinite loops. An infinite loop is a loop that runs forever. So if I change this condition to `True`, because `True` is always `True`, this `while` loop will run forever. So to jump out of this, we need a `break` statement. So after we get the input from the user, we can get the `command`, convert it to lowercase, and see if it equals `"quit"`. If that's the case, we want to `break`. Now, with this change, we no longer need to initialize `command` to an empty string. Previously, we needed this because we had a `while` statement like this: `while command != "quit":`. So we had to define this `command` variable, and that's why we have set it to an empty string. Without this line, when the Python interpreter tries to execute this code, it doesn't know what `command` is. So now that we have an infinite loop, we no longer need to define `command` and set it to an empty string. So in terms of functionality, this program is exactly the same as the program we wrote in the last lecture. Just be aware of these infinite loops because they run forever. You should always have a way to jump out of them, otherwise your program will run forever, and this can sometimes cause issues because if you're executing operations that consume memory, at some point your program may run out of memory and crash.

All right, time for an exercise. I want you to write a program to display the even numbers between 1 to 10. So when you run this program, you should see `2`, `4`, `6`, and `8`. And after these, I want you to print this message: "We have 4 even numbers". Now, here's a quick hint before you get started: you should call the `range` function with `1` and `11` (not `10`). Do not use the third argument, which is called `step`. So basically, I want you to iterate over all the numbers between `1` to `10`, check if each number is an even number, and then print it on the terminal. So pause the video, spend 2 minutes on this exercise. When you're done, come back and continue watching.

So we start with a `for` loop: `for number in range(1, 11):`. We check to see if the remainder of the division of this number by two equals zero. So `if number % 2 == 0: print(number)`. Now, let's run this program. So we get `2`, `4`, `6`, `8`. Beautiful. Now, to count the even numbers, we need a separate variable. So let's call that `count`. Initially, we set it to `0`. Now, in this `if` block, every time we find an even number, we need to increment `count`. So we set `count += 1`. And finally, after our `for` loop, we can print a formatted string: `f"We have {count} even numbers"`. Let's run the program. And here's the result.

So that brings us to the end of this section. In the next section, you're going to learn how to create your own functions. I hope you enjoyed the section and thank you for watching.

So far, you have learned how to use some of the built-in functions in Python, such as `print`, `round`, and so on. In this section, you're going to learn how to write your own functions. Now, you might ask, but why do we even need to write our own functions? Well, when you build a real program, that program is going to consist of hundreds or thousands of lines of code. You shouldn't write all that code in one file like we have done so far. You should break that code into smaller, more maintainable, and potentially more reusable chunks. You refer to these chunks as functions.

So let me show you how to create your own custom functions. We start with the `def` keyword, which is short for "define". Next, we need to give our function a name. So let's call this `greet`. All the best practices you learn about naming your variables also apply to naming your functions. So make sure your function names are meaningful, descriptive. Use lowercase letters to name your functions, and an underscore to separate multiple words. Now, after the name, we need to add parentheses. You will see why shortly. And then we'll add a colon.

Now, what is going to happen? You know it. We're going to get indentation, which means the following statements will belong to this function. So here I'm going to add two statements: `print("Hi there!")` and `print("Welcome aboard!")`. Both these lines belong to this function because they're indented.

Now, we're done with this function. We need to call it. So we remove the indentation and we add two line breaks after this function. This is what PEP 8 recommends to keep our code clean and maintainable. Now, if you forget to add two line breaks, don't worry. As soon as you save the changes, `autopep8` will automatically add these line breaks for you. Let me show you. So I'm going to remove these line breaks and call this function `greet()` with parentheses, just like how we call the built-in functions. Now, save the changes. There you go.

So we get two line breaks after our function. Now let's run this program. So we get these two messages on the terminal.

Now here's a question for you: What is the difference between the `gre` and `print` functions? The difference is that this `print` function takes an input, whereas our `gre` function doesn't take any inputs.

So let me show you how to pass inputs like first name and last name to this function. When defining a function, in between parentheses, we list our parameters. So here we add two parameters like `first_name` and `last_name`.

Now when calling this function, we need to supply two values for those parameters. We refer to them as arguments. So "M Hamedani" - these are the arguments to the `greed` function. That's one of the terms that a lot of developers out there don't know. They don't know the difference between parameters and arguments. A parameter is the input that you define for your function, whereas an argument is the actual value for a given parameter.

Okay, now let's change line two and instead of saying "Hi there," we can greet a person by their full name. So we can convert this to a formatted string and pass two fields here: `first_name` as well as `last_name`. Save the changes, run the program, and this is what we get in the terminal.

Now this function is more useful. We can reuse it and call it with different arguments. So let's greet John Smith as well. Run the program. So we get "Hi Msh Hamedani" and "Hi John Smith."

Now, note that by default, all the parameters that you define for a function are required. So here our `greed` function takes two parameters. If I exclude one of these arguments and save the changes, you can see we have this red underline. So Pylance is complaining and saying there is no value for argument `last_name`. Also, if we run the program, we get this `TypeError: greet missing one required positional argument: 'last_name'`. So let's put this back.

Now let's learn. I will show you how to define optional parameters. So this is the simplified version of this `greed` function we created earlier.

Now, in programming, we have two types of functions: functions that perform a task and functions that calculate and return a value. Here are some examples. Both the `print` and `greed` functions are examples of type one. They're performing a task, which is printing something on the terminal. In contrast, the `round` function is an example of a function that calculates and returns a value. So the functions that you create fall into these two categories.

Now let me show you how to rewrite this `greed` function, but in the second form. So instead of printing this string on the terminal, we simply return it. Let me show you. So I'm I'm going to delete all this code. Define a new function, but call it `get_greeting`. We add the `name` parameter and simply return this formatted string: "Hi, {name}". That's all we have to do. So we use the `return` statement to return a value from this function.

Now we can call this function `get_greeting`, pass a name like "Msh". Because it returns a value, we can store that value in a separate variable like `message`.

Now you might be curious, which form of these greeting functions is better? Well, with this first implementation, we logged to printing something in the terminal. In the future, if we want to write that message in a file or send it in an email, we have to create another function. So we cannot reuse this `greed` function in other scenarios. In contrast, this second form is not tied to printing something on the terminal. It simply returns a value.

Now we get this value, and we can do whatever we want with it. We can print it on the terminal, or we can use the built-in `open` function to write this message to a file. So we can create a file like `content.txt`, open it for writing. This returns a file object, and then we can call `file.write(message)`. Now, and don't worry about these two lines. Later in the course, I'm going to talk about working with files. But what I want you to take away here is that we have this `message` variable, and we can do whatever we want with it. We can print it on the terminal, write it to a file, send it in an email, and so on.

And one more thing before we finish this lecture. So here's our `greed` function, and as you can see, we're simply printing a string. Now, if we call `greed("Msh")` and run the program, we get this message: "Hi Msh". But what if we put this inside of a call to the `print` function? Let's see what we get. We get "Hi Msh" followed by `None`. What is this `None`? It is the return value of the `greed` function. So in Python, all functions by default return the `None` value. `None` is an object that represents the absence of a value. Later in the course, you're going to learn more about `None`. What matters now is that all functions return `None` by default unless you specifically return a value. So here, if we return some string, `None` will no longer be returned.

Now I just want to clarify something. Earlier I told you that we have two types of functions in programming: functions that carry out a task or functions that calculate and return a value. So back to the code we previously had. So even though this function returns `None` by default, it is still classified as a function that carries out a task.

Let's create another function. We call it `increment`. We want to use this function to increment a number by a given value. So here we simply return `number + by`.

Now we can call this function like this: `increment(2, 1)`. This returns a value, so we can store it in a variable like `result` and then print it on the terminal. Let's run the program. We get three. Beautiful.

Now we can simplify this code. We have used this `result` variable only in a single place, that is line six. So we don't really need it. So on line six, we can replace `result` with a call to the `increment` function like this. So when the Python interpreter executes this code, first it will call the `increment` function. It will get the result and temporarily store it in a variable for us. We don't see that variable, and then it will pass that variable as an argument to the `print` function. Now, if we run this program, we get the exact same result. Beautiful.

Now we can make this code more readable. If someone else looks at line five, they may not know exactly what these arguments are for. We can use a keyword argument to make this code more readable. So this one here is the value of this `by` parameter. We can prefix it with the name of the parameter like this. Now we can read this code almost like plain English: `increment(2, by=1)`. So if you're calling a function with multiple arguments, and it's not quite clear what these arguments are for, you can make your code more readable by using keyword arguments. So here `by=1` is a keyword argument.

Earlier I told you that all the parameters that you define for a function are required by default. In this lecture, I'm going to show you how to make the `by` parameter optional. So let's say we don't want to explicitly pass `by=1` every time we want to call the `incr` function. We want to use this function to increment a value by one. So we remove the second argument. Now we need to give this parameter a default value. So we set it to one. Now, if we call this function and don't supply the second argument, this default value will be used. Otherwise, the value that we specify here will be used. Let me show you. So we run this program. The result is three. But if we pass the second argument here, `increment(2, 5)`, it will increment two by five, so we will get seven. So you can see it's pretty easy to make a parameter optional. Just be aware that all these optional parameters should come after the required parameters. In other words, I cannot add another required parameter here. Let's call that `another`. I cannot add that here. If I save the changes, you can see we get a red underline here. So all the optional parameters should come after the required parameters. Now, obviously, in this case, we don't need the second parameter, so let's delete it.

There are times that you may want to create a function that takes a variable number of arguments. Here is an example. Let's define this function `multiply` that takes two parameters `x` and `y` and simply returns `x * y`. Now we can call this function like this: `multiply(2, 3)`. So far so good. But what if you want to pass one or two more arguments here? That doesn't work because our `multiply` function takes only two parameters. To solve this problem, we need to replace these two parameters with a single parameter. We use a plural name here to indicate that this is a collection of arguments, and then we prefix it with an asterisk. This is the magical part. Let me show you what happens when you use an asterisk here. So temporarily, let's delete this line and simply print `numbers`. Let's see what we get here. So run the program. You can see all our arguments, and they're packed in parentheses. What is this? Well, earlier you learned about lists. I briefly mentioned that you can use square brackets to create a list of objects like `[2, 3, 4, 5]`. Now, later in the course, we have a comprehensive section about lists. So don't worry about the details of lists and how they work. But what I want you to note here is that the only difference between this list and what we have here is in the notation. So we use square brackets to create lists and parentheses to create tuples. Some people call it "tupels" or "tuples". So a tuple is similar to a list in that it's a collection of objects. The difference is that we cannot modify this collection. We cannot add a new object to this tuple. Once again, later in the course, we're going to have a comprehensive section about lists, tuples, and other data structures. What matters now is that these tuples, just like lists, are iterable. So we can iterate over them, which means we can use them in loops. Let me show you. So let's write `for number in numbers:`. Let's just print one number at a time. Actually, we don't need this line. So delete and run the program. So we iterate over this tuple, and in each iteration, we get one number and print it on the terminal.

So now, with a simple change, we can calculate the product of all these numbers. All we have to do is to define a variable like `total`, initially we set it to one, and then in each iteration, we get `total` and multiply it by the current number. Or we can rewrite this statement using an augmented assignment operator: `total *= number`. Line five and four are exactly identical. So I'm going to use line five because it's shorter and cleaner. Delete and finally, we'll return the `total`.

Now, one of the issues I see often in beginner code is that they don't use this indentation properly. So they put the `return` statement here, and then they wonder why their function doesn't work properly. If you put the `return` statement here, it will be part of the `for` loop. So it will be executed in each iteration. In this case, after the first iteration, because of this `return` statement, we will return from this `multiply` function, so the `total` will not be calculated properly. We need to put this at the same level of indentation as other statements in this function. So here we have our `for` statement, we loop over all the numbers, we calculate the `total`, and then finally return it.

So with this implementation, we can get the result and print it on the terminal. Let's run the program, and you can see the product of these numbers is 120.

Hey guys, I just wanted to let you know that this tutorial is actually the first two hours of my complete Python Mastery course. If you're finding this helpful and want to dive even deeper, the full course covers everything from beginner basics to advanced concepts like machine learning, web development, and automation. You'll also get hands-on projects to build your skills step by step. I put the link in the description box. If you're ready to take your Python knowledge to the next level.