Python Full Course for Beginners [2025]
![Python Full Course for Beginners [2025]](https://videodownloadbot.com/images/video/8aa/8hi9gg7o67306shsbozuh4pu4qd0209i_standard.jpeg)
Download information and video details for Python Full Course for Beginners [2025]
Uploader:
Programming with MoshPublished at:
2/12/2025Views:
2.1MDescription:
Master Python from scratch 🚀 No fluff—just clear, practical coding skills to kickstart your journey! ❤️ Join this channel to get access to perks: 🚀 Want to dive deeper? - Check out my Python mastery course: - Subscribe for more awesome Python content: 🔗 Related videos ✋ Stay connected: - Full Courses: - Twitter: - Facebook: - Instagram: - LinkedIn: 📖 Table of Content 0:00:00 Introduction 0:00:56 What is Python? 0:04:11 Installing Python 0:05:36 Python Interpreter 0:07:30 Code Editors 0:08:49 Your First Python Program 0:12:25 Python Extension 0:14:26 Linting Python Code 0:18:40 Formatting Python Code 0:22:51 Running Python Code 0:24:30 Python Implementations 0:26:59 How Python Code is Executed 0:29:45 Quiz 0:31:17 Python Mastery Course 0:31:44 Variables 0:34:48 Variable Names 0:37:51 Strings 0:43:20 Escape Sequences 0:46:01 Formatted Strings 0:48:09 String Methods 0:54:03 Numbers 0:56:50 Working With Numbers 0:58:59 Type Conversion 1:04:03 Quiz 1:06:43 Comparison Operators 1:08:46 Conditional Statements 1:12:56 Ternary Operator 1:15:04 Logical Operators 1:19:07 Short-circuit Evaluations 1:21:13 Chaining Comparison Operators 1:22:35 Quiz 1:24:18 For Loops 1:27:56 For..Else 1:30:42 Nested Loops 1:33:26 Iterables 1:36:34 While Loops 1:41:33 Infinite Loops 1:43:10 Exercise 1:45:13 Defining Functions 1:47:37 Arguments 1:49:57 Types of Functions 1:53:59 Keyword Arguments 1:55:59 Default Arguments 1:57:34 xargs #Python #AI #MachineLearning #WebDevelopment
Video Transcription
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 Mash 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, CodeWithMash.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 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.
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's an example.
Let's say we want to extract the first three characters of the text, hello world.
This is the code we would have to write in C sharp.
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 multipurpose 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 you 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 Mosh Hamadani, and I'm going to be your instructor in this course.
I'm a software engineer with 18 years of experience, and I have 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 codewithmosh.com.
All right, now let's get started.
Alright, 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 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 dash dash 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 python 3 space dash dash 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 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 plus 2 in programming an expression is a piece of code that produces a value So here when we add 2 plus 2 we get a value.
That is why we refer to this piece of code as an expression So enter we get 4.
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 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
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's 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 desk, create a new folder.
Let's call this folder hello world.
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 that PI so all our Python files should have the PI extension press Enter now let's close this and
and type a bit of Python code.
In this lecture, we're gonna 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 and S on Mac or control and 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 back tick.
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.
If you're on Mac or Linux, type Python 3.
And next to that, add the name of our file.
That is 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 back tick 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 we want to repeat this star 10 times.
So here we can multiply this 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.
In this lecture, I'm gonna 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
Alright, 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 gonna show you 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 pilot and here's the error message missing Parentheses in call to print did you mean print with parentheses?
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
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 plus 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 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, and 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 pilot 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 gonna add three here save the changes and you can see the problem disappear 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 and P on Mac or shift ctrl 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 a 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 pilot So as you're reading tutorials or talking to other people you will hear about linters such as flake 8 my pie Pep 8 and so on different developers prefer different linters I personally prefer pilot that is the most popular one and that is the default linter set in the Python extension of vs code and
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.
The 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 slash dev slash PEPS.
Let's have a quick look here.
So here are the peps.
We 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 PEP8 documentation.
But if not, don't worry because throughout this course, I'm going to explain the key things in PEP8.
In this lecture, I'm going to show you a tool that helps you automatically format your code according to PEP8.
So back in VS Code, let's write some Python code.
x equals 1.
Here I'm declaring a variable and setting it to 1.
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 and 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 auto pep 8 is not installed so there are a bunch of tools for formatting python code the most popular one is auto pep 8 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 auto pep 8 using the extensions panel so once again on the left side click this icon and search for auto pep 8 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 gonna declare another variable, y, and set it to two, and a variable with a long name, like unit underline price, I'm gonna set this to three.
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 gonna show you how to have your file automatically formatted as soon as you save the changes.
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 gonna 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, beautiful.
Alright, 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.
If you're on Mac, type Python 3, followed by the name of the file.
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 and p, on windows is 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 ctrl and r, okay?
Now we press enter, with this in place, we can go back to app.py and press ctrl 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 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 sharp, 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 C Python 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, e-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 JSON 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 JSON instead of CPython.
Similarly iron Python is written in C sharp So if you're a C sharp developer and want to bring some C sharp code into a Python program, you will have to use iron Python Next we'll look at how exactly C Python executes Python code.
So
The programming languages we use, like C, C sharp, 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 you 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 Sharp 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 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 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 Java Virtual Machine.
So I've got a few questions for you because 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.
What do you think is the value of this expression?
Well, here we have this string, we're multiplying this by 3.
So the result will be a string of 3 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, 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 gonna look at the fundamentals of 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 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 computer's memory.
Here are a few examples.
I'm gonna define a variable called students underline count and setting it to 1,000.
When we run this program, Python interpreter will allocate some memory and store this number 1,000 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 thousand so this is the basic of variables
What kind of data can be a store in computers 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 and
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 this 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.
So false or true.
And finally, let's take a look at an example of a string So course under like name we set this to a string like Python Programming so 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
So these are the variables from the last lecture now.
I've got a question for you There are four things that have 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
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 Entitled case all letters are lowercase, right?
Let's delete these
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 and
Again, that's one of the issues I see a lot amongst beginners.
They write code like this.
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 pep8 in Python.
Now the good thing is if you forget these rules when you save the changes AutoPep 8 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 gonna look at strings in more detail.
So
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 Marsh from code with Marsh calm 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?
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 0 indexed which means the index of the first character or the first element is 0 So now when we print this
get P okay now you can also use a negative index like minus 1 what does that mean well if 0 represents the first character here what do you think negative 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 gonna duplicate this line and remove negative one now Let's say we want to extract the first three characters in this string.
So here we need two indexes and
Start index colon the end index So this will return a new string that contains the first three characters in this course variable That will be P Y and T. So the index of these characters are 0 1 & 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 gonna 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 zero 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.
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 a 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 string 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 and
What's going on here?
We don't have the backslash because we use that to escape this double code 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 additional note that we add to our program.
It's not executed by 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 code 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 one backslash programming.
And finally we have backslash N, which is short for new line.
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.
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 and
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 lengths of this string Let's run this program one more time.
So we get for it
We can also replace last with an expression like this, two plus two.
Let's run this program, we get four and four.
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 kind 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 dot C
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 is 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 the upper returns a new string a new value.
We can store it in a variable like course underline capital and
Now to keep this demo simple and consistent.
I'm gonna revert this back and use a print statement We also have the lower method to convert a string to lowercase.
I 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 white space 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 white spaces at the beginning of this string When we call course that strip those white spaces will be removed Take a look
So note that in the first three examples, we have those white spaces, but in the last one it is removed So a strip removes the white space from both the beginning and end of a string We also have L strip which is short for left strip and our strip which is short for right strip so it will remove the white space from the end of a string and
If you want to get the index of a character or a sequence of characters in your string, you should use the find method.
And we'll show you.
So course that find.
So 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.
The program so the index of pro is 9.
So if you 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 negative 1.
That means this string was not found in the original string.
So
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 geogramming 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 We write an expression like this 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 you 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 as 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 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
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 plus B I Where I is the imaginary number 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 plus 2 J so X now is a complex number and by the way as I told you before this is just a Comment or an additional note in our program when we run this program anything after this hash 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 and
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 double slash run the program we get three 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 3 we can write an expression like this X equals X plus 3
We can use an augmented assignment operator.
That is a little bit shorter.
So we write X plus equal 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.
So
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'll get three.
We have another useful built-in function called abs, which returns the absolute value of a number.
So if we pass negative 2.9 here, we'll get
positive 2.9.
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.
An example we have math dot seal for getting the ceiling of a number.
So if you 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.seal.
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.
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 the pass a string This will be a label that will be displayed in the terminal.
You will see that in a second.
So let's add X colon now this function returns a string so we can store it in this variable and
Now let's imagine that y should be x plus 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 and backspace Once again, if you're on Windows type Python if you're on Mac or Linux type Python 3 and then app.py So here's our label.
Let's enter a value like one We got an error type error.
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.
String one plus one.
Note that the number one is different from string one because these are two different types.
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 one 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 and
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 gonna comment out these few lines
Let's print Type of 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 one 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 a str or a 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 quotes now here we add a label like x then we'll add a field so here we want to print the value of x variable after that we add some more text and finally we want to print the value
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 falsi.
So they're interpreted as a boolean false Number zero is also falsi We have an object called non 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 will get false Anything else will be true.
Let me show you a few examples.
So
So in this interactive shell in Python, let's convert number zero to bool.
That's falsy, so we get false.
What about bool of one?
We get true.
If we pass a negative number, we'll also get true.
If we pass a number larger than one, like five, we still get true.
So we only get false when we try to convert zero 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 of 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 negative 1 here?
Well, using the syntax, we can slice a string.
Our start index is 1, and our end index is negative 1, which refers to the first character from the end of the string.
Now, when slicing a string, the character at the end index, or negative 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?
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 of false?
Well, earlier I told you about falsy values in Python.
So number 0, an empty string, and the non-object, these are all falsy values.
Anything that is not falsy is considered truthy.
Here we have a string that has 5 characters, it doesn't matter what those characters are, this is not an empty string.
So it's not false C, it's true C. So when we convert it using the bool function, we'll get the Boolean true.
And this brings us to the end of this 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 gonna start this section by exploring comparison operators.
We use comparison operators to compare values Here are a few examples.
So 10 is greater than 3 we get true So what we have here is a boolean expression because when this expression is evaluated will get a boolean value That is true or false
Here's another example 10 is greater than or equal to 3 once again we get true We also have less than so 10 is less than 20 We have less than or equal to Here is the equality operator.
So 10 is equal to 10 What about this expression?
What do you think we're gonna 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 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 is greater than Apple and
We get true because when we sort these two words bag comes after so it's considered greater Now what about this one bag?
equals capital 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 gonna 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 and
Here's an example.
Let's say we have a variable called temperature.
We set it to 35.
Now if temperature is greater than 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 is greater than 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 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
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 auto pep-8 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 and
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 beginners 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 Now what if 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 is greater than 20, once again, colon,
Enter.
Now by default here, VS Code is using two white spaces.
So 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, annotation print.
Here we can add a message like 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 called.
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 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 is greater than or equal to 18 colon print eligible
Remove the indentation, else, colon, 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 equals this string 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 few lines is to assign a value to this message variable, right?
So we start with message.
We set it to eligible if age is greater than or equal to 18, else we set it to 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 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 underline 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, we add the colon and 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 L 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 gonna define another variable student Set it to true Temporarily, I'm gonna remove this expression and simplify it.
We'll come back to this later so let's say if the person is eligible if they are not a student and
The not operator basically inverses 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.
They are not eligible if student was false.
I
When we apply the not operator will get true.
So our condition will be true and we'll see it eligible Let's run it one more time.
There you go With these operators we can model even more complex conditions.
So
Here is 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, we want at least one of these conditions to be true.
So we put these in parentheses.
We want to separate these from the other condition, which is not a student Now the result of this should be true, which means at least one of these conditions should be true after that we'll add and not student and finally call so with these operators you can model all kinds of real-world scenarios and
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 are not a student
One thing you need to know about these boolean operators is that they are short-circuited What do I mean by that?
Well when 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.
So
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 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
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 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 is greater than or equal to 18, and age is less than 65, then we 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 age should be between 18 and 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 4 and line 3 are exactly equivalent but as you can see line 4 is cleaner and easier to read so let's get rid of line 3 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, continue watching.
Alright, 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 is greater than apple, that is true, because when we sort these words, bag comes after apple.
But look at the second part.
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.
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 is ugly That's when we use a loop.
We use loops to create repetition So here is how it works We start with four number in we have a built-in function called range and
Now, how many times we want to repeat this task?
Let's say three times.
So we call range and pass three as an argument.
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 the statements that should be repeated three times.
Let's do a print a message like 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 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 and
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 one, attempting number two, and so on.
We can take this to the next level so we can pass another argument here I'm gonna add an expression one more time number plus one So we'll get one two three 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 and
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?
Let me show you one more thing before we finish this lecture as you saw this range function generates numbers Starting from 0 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 and
With this change, we don't need to add one to number every time because in the first iteration, this number variable will be set to one.
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 two as a step.
Look at the result.
These are the numbers we get, one, three, five, and so on.
So pretty useful, you're gonna 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 gonna simulate the scenario where we can successfully send a message So we define a variable successful and set it to true and
Here after this print statement will have an if statement if successful colon 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
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 gonna change successful to false Now at the end here we can add an L statement.
This is what we call a for L statement and
what we put under this l statement will only be executed if this loop completes without an early termination so if we never break out of this loop then the l 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.
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 gonna start with this loop for X in range 5 colon Now inside of this loop.
I'm gonna add another loop.
So for Y in range 3 colon
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 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 and 0, 0 and 1, 0 and 2, then we get 1 and 0, 1 and 1, 1 and 2, and so on.
Now let me explain how exactly 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 zero.
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 zero because we are still in the first iteration of the outer loop.
Y is also 0 because we are in the first iteration of the inner loop.
That is why we get 0 and 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 and 1 and Similarly in the third iteration of our inner loop will get 0 and 2 in the terminal
We're done with the execution of the inner loop.
So the control moves back to our outer loop here will 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 and 0 then we'll get 1 and 1 and 1 and 2 you got the point So this is all about nested loops.
So
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 and
Now let's look at the type of the value that we get from the range function So as an argument we pass range of a number
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 Boolean, 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.
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 gonna 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
We 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 a 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 is an example.
So let's define a variable number and set it to a hundred now we use while and here we add a condition and
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 equals number Use the integer division to divide it by 2 or we can use the augmented assignment operator to shorten this code Like this
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 plus 2 it will evaluate it and ask for the next input We can add another expression like 10 is greater than 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 and
Now, here we need a while loop.
We want this while loop to execute as long as command does not equal to quit.
So command does not equal to quit.
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 this.
Get the result and store it in the command variable.
Now at this point, Python Interactive Shell will evaluate this command.
We are not gonna 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, and as a second argument, we pass this 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 control and back tick, and run Python or Python 3 app.py.
Here's our command prompt.
Let's type 2 plus 2 it echoes back.
Let's type 3 times 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 and
now to solve this problem an amateur programmer may do something like this and command does not equal to capital quit so while command does not equal quit in lowercase and quit in uppercase continue getting input from the user let's run this program in terminal and see what happens
So one more time, python app.py.
We type quit, beautiful, it works.
We type quit in uppercase, that would work too.
But what if I type quit with an uppercase Q and lowercase UIT?
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, first 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.
We'll 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 to 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 does not equal 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 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
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 and
Alright, 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 is a quick hint before you get started.
You should call the range function with 1 and 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 to 10, colon, we check to see if the remainder of division of this number by 2 equals 0.
So if number module is 2 equals 0, then reprint this 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 plus equals 1, and finally after our for loop, we can print
a formatted string, 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 this 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 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 a 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 learned 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.
Hi there and 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 Auto pep 8 will automatically add these line breaks for you.
Let me show you so I'm gonna 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
here is a question for you what is the difference between the grid and print functions the difference is that this print function takes an input whereas our grid 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 underline name and last underline name now when calling this function we need to supply two values for those parameters we refer to them as arguments
So Mosh Hamadani, these are the arguments to the grid 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 and
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 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 Mosh Hamadani and hi John Smith.
Note that by default all the parameters that you define for a function are required So here our greet function takes two parameters if I exclude one of these arguments and save the changes You can see we have this red underline.
So pilot is complaining and saying there is no value for argument lasting Also, if we run the program we get this type error greet missing one required positional argument
So let's put this back now later.
I'll show you how to define optional parameters So this is the simplified version of this great 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 and
Here are some examples both the print and greet functions are examples of type 1 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 and
Now, let me show you how to rewrite this great 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 gonna delete all this code Define a new function but call it get underline 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 underline greeting, pass a name like mosh, because it returns a value, we can store that value in a separate variable like message.
Now you might be curious, which form of this greeting functions is better?
Well, with this first implementation, we are locked to printing something in the terminal.
In the future, if you want to write that message in a file or send it in an email, you have to create another function.
So we cannot reuse this great 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
Don't worry about these two lines later in the course.
I'm gonna 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 grid function and as you can see, we're simply printing a string now
if we call greet, give it a name, run the program, we get this message, hi mash, but what if we put this inside of a call to the print function?
Let's see what we get.
we get hi mosh followed by none what is this none is the return value of the great function so in python all functions by default return the non 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.
You want to use this function to increment a number by a given value.
So here we simply return number plus five.
Now we can call this function like this.
Increment two and one.
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 6 so we don't really need it So on line 6 we can replace result with a call to increment function like this so
when 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 5 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 and
Now we can read this code almost like plain English.
Increment two by one.
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, y equals one is a keyword argument.
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 equals 1 every time we want to call this increment function you want to use this function to increment a value by 1 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, we'll 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
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 times Y Now we can call this function like this
So far so good.
But what if you wanna 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 two, three, four, five.
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 use square brackets to create lists and Parentheses to create tuples.
Some people call it tuples or tuples.
So
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 gonna 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 I
Let's write for number in numbers, colon.
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 multiplied by the current number.
Or we can rewrite this statement using an augmented assignment operator.
So total times equal number.
line five and four are exactly identical.
So I'm gonna use line five because it's shorter and cleaner, delete.
And finally we'll return the total.
One of the issues I see often in beginners 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 will return from this multiply function and
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.
Similar videos: Python Full Course for Beginners

Python for Beginners - Learn Coding with Python in 1 Hour
![Python - Полный Курс по Python [15 ЧАСОВ]](https://videodownloadbot.com/images/video/703/dvplm301rspoqfaty78qbbffzx5o6mqx_medium.jpeg)
Python - Полный Курс по Python [15 ЧАСОВ]

PYTHON Full Course for Beginners in 16 Hour (with Projects + Questions) | Learn Python Tutorial

Python Full Course for free 🐍 (2024)

Curso COMPLETO de Python DESDE CERO para Principiantes 2025
![Python Automation Tutorial – How to Automate Tasks for Beginners [Full Course]](https://videodownloadbot.com/images/video/080/8hqsp71a00ajr8s1z87nkwhwblwdftkr_medium.jpeg)
