This 13-hour course teaches Python from absolute beginner level to advanced application, covering fundamentals such as installing Python, using comments and print(), variables, and user input. It progresses through Python data types, string operations including indexing, slicing, search, validation, and case conversion. The course details numeric operations with integers and floats, mathematical functions, rounding, random number generation, and validation. Logic and operators are explored through boolean values, comparison, logical, membership, and identity operators. Conditional statements include if, else, elif, nested conditions, inline if, and match case. Loops cover for loops with break, continue, pass, for-else, nested loops, and while loops. Data structures are thoroughly explained: lists (creation, access, unpacking, adding, removing, updating, sorting, combining, filtering), lambda functions, and list comprehension; also covered are tuples, sets, and dictionaries. Functions are introduced with parameters, return values, different types of functions, and writing clean, efficient code. The course is structured into eight chapters with precise timestamps for each topic. It is designed for data analysts, data engineers, data scientists, AI engineers, developers, and students seeking to build real programming confidence. Additional resources include a Python Handbook, full learning package with projects and certificates, and links to Udemy and DataCamp courses.
Learn Python Programming: A Complete 13-Hour Tutorial
Added:Hey friends and welcome to this very unique course to master Python. And of course you might ask what makes it unique and different from all other Python courses. First I will not only share my screen and start showing you how to write Python code and hope that you're going to remember it tomorrow.
Instead I'm going to show you how Python actually works behind the scenes. How Python executes your code step by step.
How your data and values moves inside your code. How the functions behave. how decisions are made. So, I'm going to explain everything and break all the complex concepts into very simple animated sketches and visuals. Why this is very important? Because visuals and sketches, they stick in our head way longer than code and text. Because we are simply visual creatures and we remember images longer than text or code. Once you understand how things works behind the scenes and the logic, then writing a code going to be very simple for you. Now the second reason why this course is unique, well it is taught by me. My name is Barra and I have more than 17 years experience working with Python and real industry projects. I led one of the biggest data and AI project at Marcus Spence and other large companies. That means you are learning from someone that were in the industry for many years using Python. It's not only theory. You're going to learn how professional things and how we use Python in real world projects. And now who is this course for? Well, it is for anyone who wants to master Python. Whether you are a data analyst, data engineer, scientist, web, game developer or you are just students that want to learn Python for the future. So, it's going to be very generic for any career and role. And as well, I have designed this course for total beginners. So, I assume that you might only heard about Python and that's it. I'm going to take you step by step from the very basics of Python and keep progressing into advanced Python topics.
So by the end of this course, you're going to feel very confident using Python in real projects. So now my friends, as you can see, I'm really excited about it.
>> Very very excited because I enjoyed really every moment making this course.
If you are ready, then join me. It's going to be amazing. Now let's go.
All right, friends. Now I designed this course in very specific way so that you don't just jump randomly between topics but to make you think in Python. So instead of having like chapters I build it in thinking layers. So now let me show you quickly what I mean and the road map. So now of course the first step you need to build the foundations before writing any code. I want you really to understand what is Python, why we even use it, how does Python works behind the scenes and after the theory, you're going to learn the basic tools that you're going to use everywhere in Python like the printing variables, input, comments. Those tools are very simple and you're going to use it in daily basis. Now, after the foundations, we have two chapters with one goal. You will learn how to work with the data. So you're going to learn all the tools that you need to work with the two most common types in real words. We have the text, we call it strings and as well the numbers. Now why we need this? Because in projects we have to do a lot of things with the data. So we transform it, clean it, change the format, validating the data, manipulating things, searching, analyzing, extracting. So you're going to learn all those tools to make you ready to do any task about working with data. And now after that we're going to move to something deeper. Here we have three chapters that all focus on one idea. How to control the execution of your Python code. In the first chapter you will learn how Python understands the true and false. We call it the boolean types.
And then you're going to learn how to build logic, how to build conditions. So you're going to learn all the operators and tools in order to build the brain inside your code. After that, we're going to learn the conditional statements. So here you're going to learn how to make your code decide based on the logic using statements like if else. And then the last chapter in this category, we're going to learn about the loops. And here we have mainly two types, the for and the while. So here you will learn how to automate things instead of writing the same thing over and over. So with those three chapters, you're going to learn how to have the full control of your code. And now the next chapter we have the data structures. This is going to be the most important chapter and as well the biggest chapter in the whole course. Now you already learned how to work with data with single values. But in real words data come in collections. So they come in groups. That's why you have to learn how to work with advanced data types like the lists, tpples, sets, dictionaries. And of course here you will be geared with all tools that you need in order to work with those advanced types. So how to create, how to change, analyze, combine, how to split, how to iterate, filter, so many important topics. And by the way, this is very important for job interviews and as well for real projects. Now the final layer, we have the functions. It's all about how to think like professional Python developers. So you have to learn how to design reusable logic, how to break complex problems into clean pieces and of course we're going to learn that using functions. So you're going to build your own machine inside your code.
So my friends, this is the whole road map. If you go through all the layers step by step, you're going to have enough confident to solve most of Python tasks. And of course, not only by memorizing the syntax, but by understanding how Python works. I hope that you are excited about this because I am. Let's start with the foundations with the first chapter and we're going to start with very simple question. What is Python? Let's go.
Okay. So now [music] let's start with basics. What is programming language? So now imagine that you want to give your computer a task and you say hey computer calculate 5 + 5. Well your computer will not understand what is exactly you're talking about and nothing going to happen. So we cannot use the natural human language in order to give tasks for your PC. So now instead of that we have to give the instructions in a language where the computer understand it. We write a short piece of code like for example here in Python print 5 + 5.
Now the computer going to understand the instruction and give us a result 10. So a program it is like set of instructions that is written in a language the computer can follow. But my friend not all languages are the same. Some are made for people and others are made for machines. So now at the top we have the natural language like you say hey please calculate 5 + 5 and we have a lot of natural languages we have the English, Spanish, Hindi they are easy for us but far too complex for computer to understand. Now moving on then we have the highlevel languages. So they are programming languages but they are made for human because they are very simplified, logical and really easy to write and to read. And we have a lot of languages like Python and JavaScript.
They are high-level languages. And now next we are going lower. We are going to the low-level languages. Programming languages can talk directly to the machines. But they are really hard for humans. It's hard to read, to write, and as well to understand. And here we have examples like the assembly and the C language. And now at the very bottom, we have the machine language. And this one is made of binary code, a combination of ones and zeros. And this is exactly what your computer can understand but it is impossible for humans to read it to understand it to write [music] it. So now if you look at these levels each step down it going to bring us closer on how the machine thinks and each step up it going to bring us closer on how human thinks. So now if you're looking at Python the highle language it is closer to the natural language than the machine language. So that means it is really easy to learn and to write and it's like an abstraction that hides the complexity of low-level and machine languages. So it's like a bridge between both worlds.
So this is exactly what are programming languages and what is Python.
Okay. So [music] now I would like you to understand exactly how Python works. So let's say you open your editor and you start writing Python code and usually we write the code inside a Python file with the file extension of py. So here you are writing the source code. Once you run your code, the computer cannot immediately execute your instructions because it is really high level and the computer needs a lower level of the code. So what happens? There is something called compiler in Python. It going to take your Python code and translate it to another code. It's called the pite code with the extension pyc python compiled [music] and this can happen automatically and you will not even notice it. So what happened here the compiler [music] of python going to translate the highle language the python code to a low-level language the bite code. So as you can see it's really hard to understand the bite code compared to the python code. So now our code is not yet executed, right? Python just did a translation. But now before anything to be executed, what can happen? Python might as well link some libraries. It is like pre-written chunks of codes that helps your code to do something specific like working with files, handling data and so on. Now, Python has everything the bite code, the libraries and now Python can run your bite code using something called Python virtual machine.
So it's like a software that can understand the bite code of Python and take care of running it. So the Python virtual machine going to finally converts your instructions into machine codes once and zero because this is the only thing that your computer can really understand. And once it runs you will see at the results whatever your program was designed to do. And now all those three steps compile, translate and execute we call them the Python interpreter. So it's like a toolbox that handles everything needed to [music] run a Python code. So again my friend you write Python code in a high language then Python compiles it and translate it into the bite code the low language and then the virtual machine going to take care of everything going to take your bite code some libraries and then it going to run it at your computer to see the results. So this process happens every time you run a Python code and everything is of course automated and [music] behind the scenes.
Okay, [music] so now let's talk about why we should learn Python in the first place. There are like too many programming languages out there. So why millions of people choose Python? So the first and most important reason is Python is very powerful and as well at the same time very simple. You can build really serious stuff with few line of codes. So if you compare like Java and Python, in Java or C++ you're going to write like a lot of lines in order to do a simple task compared to Python where you need only like one or two line. So as you can see Python is very simple compared to other programming languages.
Okay, moving on to the second reason.
Python is used literally everywhere and you can use it in order to build everything. So you can use it to build websites, to automate tasks, to work with data, to build games and even control robots. So whatever direction you want to go in tech, you will find probably Python. Okay, moving on to the next one. One of the best things about Python is the community. You are my friend never alone. There are thousands of developers and experts like me sharing their knowledge in tutorials or they are like writing blog posts and as well developing and sharing free libraries, open-source projects in GitHub. And if you are stuck or have any complex task, I'm sure there is like someone already solved it and probably made a YouTube video or a GitHub repo about it. So this type of community makes the language alive. And when it comes to AI, Python is leading the way.
Almost everything you hear about today like Shbet, image generations, self-driven car models, it's built with Python [music] because Python has an incredible ecosystem for AI and machine learning. So my friends this means the programming language of AI is Python and if you are interested in the future then Python is the right language. And now because of all these reasons Python is one of the most in demand programming language in the world right now. If you check any job description in the tech world you will see everyone across all industries finance, healthcare, logistics, car manufacturing, everyone is requesting Python skill. So my friends Python is really easy to learn has incredible use cases. It is shaping the future with the AI. It has huge community and it is amazing for your career. So this is exactly why millions learns [music] and works with Python.
Okay. Now the first thing that we need to do is to go and download Python. So open your browser and go to python.org.
And then we're going to go to the download section. And here you're going to see one big yellow button that says download Python. And then we have like a version. For me now it's like 3.13. You might see here like higher version that I'm showing now. And that's totally fine. So let's go and click on that. And then you will get like the installation.
So let's go and click on it. And now very important before we start installing now. We're going to go and check this one. So we're going to say add python.exe to path. It's just going to make your life easier. Trust me. So now let's go to install now and click on that.
So with that we got Python in our PC. So now what we installed now is the full Python [music] engine. So we have like the interpreter, the standard libraries, the built-in stuff, all those basics that you need in order to run Python code at your BC. Okay. So now we have to check whether everything is installed correctly. So now go and search in your system for terminal and let's run that.
Now here in the terminal type python then dash version. Let's go. And our system returns Python with the version.
So if you get it like me that means my friends we have now installed [music] Python at our PC.
And now the [music] next question is where we going to write our Python code.
And here we have two options. Either we're going to use a code editor or we're going to use an IDE. So a code editor it is like smart notepad for developers. So, you're going to have like some nice stuff like syntax highlighter, autocomplete, file navigations, many extensions and add-ons. And we have very famous editors like the visual studio code and Atom.
And from the other side, we have the IDE, integrated development environments. This one is like heavier, more advanced than the code editor. You will get a debugger, compiler, version control, and many more heavy stuff inside it. And a very famous one is like the PyCharm and the Visual Studio. Be careful, it's not the Visual Studio Code. And now, of course, the question is which one we going to use. Well, we're going to go with the code editor, Visual Studio Code. It is just free, lightweight that has more that we need in order to learn or work with Python.
And I can tell you, my friend, this editor is not only like for beginners, for someone to learn a programming language. This one is as well for professionals. You're going to be using it in your projects. I use this editor as well for my real projects. So, now enough said. Let's go and get it.
Okay. So now let's go and download Visual Studio Code. So we're going to go to code.visisualstudio.com.
And once you are there you can go and click download for Windows. And if you are using Mac you can go to the docs over here and then to the setup on the left side and here you can see for Linux for Mac for Windows and if you go to the Mac you can see as well here the downloads. So now go download and install Visual Studio Code at your PC.
After installing the Visual Studio, we're going to go and open it. And now I totally understand if this is like little bit overwhelming. You have a lot of icons, menus, and stuff. But don't worry about it. I'm going to guide you like step by step. So now we have here like the basic Visual Studio, but we want to go and upgrade [music] stuff. We want to go and add like upgrades and power to our editor. And we can do that by adding extensions to the Visual Studio. Now, if you go to the left side, you can see here we have a lot of icons.
One of them called extensions. So go and click on that. So now what we can do, we can go and search for add-ons and extensions to our Visual Studio. So what you have to do is to go and search for Python. And now the one that we need is Python by Microsoft. So click on that.
So this is an official Python extension by Microsoft. So let's go and download it. We're going to go over here and click on install. And with that, the extension is installed. This is really nice one in order to get smart suggestions, to highlight errors, to run things easier, and so on. And with that, we got our first extension in Visual Studio [music] Code. And now in the next step, I want you to go to the file over here in the menu. And then let's open a folder. And what you're going to do now is to create a folder where we're going to put all our Python codes. So make sure to put it somewhere safe. So we're going to go and create a new folder.
Let's call it, for example, Python_arning.
And select the folder. So now the window switch to the explorer, the first icon over here. And now we can see in this panel our folder [music] the python learning and here we can see all the folders and files that we are creating in the projects. Great. Now we have the folder and now inside it we're going to create our first python file. So in order to do that we're going to go to this small icon over here and click on that. It's going to create a new file.
Call it whatever you want. So let's call it hello. And now we have to tell our system this is not a normal file. This is a Python file. And for that we have to use the correct extension. PY stands for Python. So hit enter. And now our system understand. Aha this is a Python file. And as you can see we have here like nice small icon of Python. Now in the middle here you can see already our file is opened. And the editor is waiting for you to write something. And now we are ready to write our first Python code. And now don't worry about the syntax what we're going to write here because I will be explaining for you everything step by step. Don't worry about it. So just follow me now. We're going to write print. This is a function that's going to print something in the output. And after that, we're going to go and open parenthesis here. As you can see, as you opened parenthesis, the editor closed it immediately because you need it at your code. So, we have opened and closed parenthesis. And now we go inside them and we type double quotes.
So, as you can see, we have now two double quotes because this is the syntax in Python. So, as you can see, the editor here is helping us and adding stuff. So now in between of all those stuff we're going to write something like for example we can say hi this is my first Python [music] code something like this. So now we have our first code in Python and the next step is to go and execute it. So we're going to go to this play icon over here and click on it. And now my friends if you see in the terminal this message like this I can say congrats you have written your first Python code. So now in the terminal you can see the output of your code. So the first few stuff is just like where your file is located. at what is very important is this message over here.
This is the output of your code. We said in our code print this message and we got the message in the output. So with that we have told our PC to do something. We have written here like an instruction and our PC did understand what we want and executed the task and this is exactly what is programming. All right. So now what is next? We're going to talk about shortcuts. Now what you have done in order to run this code, we went to this icon over here and clicked on [music] it. And now my friends, we want to be professional, right? We don't want each time to go and click on the icon in order to execute our codes.
We're going to go and use shortcuts. So now we're going to go to the command section. For Windows, hit shift control and P. And if you are using Mac, shift, command P. And now we're going to go and search for the following preferences.
And then double points. We need the open keyboard shortcuts. The one over here.
So we can see now all the commands in the visual studio and the key binding the shortcuts. And now what do we want?
We want to make a shortcut for running Python. So we're going to go and search for run Python file. So now we can see this command run Python file. And as a default, Visual Studio will not add any shortcut for it, but we're going to add it manually. So double click on the key binding. And now choose the shortcut that you want. I usually use the CtrlR.
So that's it. Hit enter. So now you can see our nice shortcut and the source you see here it's user. Perfect. Now let's go back to our code. You can see here in the tab hello.python and now we don't want to hit the play button. What we're going to do we will just going to use the shortcut. So ctrlr and perfect with that we have quickly executed our code like an expert. All right my friends. So with that we have prepared your PC with everything that we need in order to run and learn Python. Now, we're going to move to the bonus section to the luxury where we're going to add some decorations to our Visual Studio. It is not a must. It is nice to have. And this is the style that I like. So, let's go.
Okay. So, now in order to add more decorations to our Visual Studio, we're going to go to the extensions. And the first thing that we can add is a theme for our Visual Studio. There are a lot of amazing themes for the coloring and stuff. So, I'm going to show you my top four themes and the one that I usually follow. So one of the very famous one is called one dark pro. This one over here.
This is very famous theme and you can go and install it and check whether you like it or not. And if you scroll down you can see more screenshots and informations about this theme. And you have to be careful on the marketplace.
Some stuff are like free to use and others you have to pay like license. But for this one currently it is free.
Another one that is very popular is the IO. So this one over here click on it.
And this one as well very famous between developers. You can see there's like light mode, dark mode, and like different coloring. It's really here there's like no right and wrong. And this is as well for now it's free. And a third one that is very famous, Monokai Pro. This one sadly you have to pay for it. But this is as well really awesome.
Like each time I try it, it feels really professional. But now we go to my favorite one. It's going to be Dracula.
So dra and this one over here. So click on that. You can try it and if you like it, you can use it like me. So we're going to go and install it. Trust and install. And now you can see immediately all the coloring of the Visual Studio did change. So now if you go back to our code, you can see the coloring did change. The print now is green. It is just clean, beautiful. I just like it.
So try it out. It is free through the course. I will be using the theme but again it is not a must. Okay. So now moving on there is like one more thing that I like to add is different type of icons. You can see we have this small snake here for the python. But if you want to go and add some more fancy stuff you can search for material icon theme.
[music] The first one over here. So click on that. And if you scroll down you can see we will get like more colorful icons and stuff. Again this is only luxury. So I'm going to go and install it. Trust and install. So now if you go to the explorer over here you can see we have now nice colorful icon. It is just small stuff. Okay. So now let's go to the next one. This one going to be more serious not only colors and stuff.
So now go and search for auto hip 8. So the first one here as well from Microsoft. So now this extension going to go and automatically format your code following the standard format and the styling guide of pip 8. Now what is exactly pip 8? It is a standard on how to format and style your Python codes using like a set of rules that you can follow in order to have clean, readable and professional looking Python codes.
So we can see here in the PIP 8 styling guide there are like a lot of rules that the developers should follow, right? And what this extension does is to go and format automatically your code using this standard style. So it is really nice. Go and [music] install it. And after that we can go back to our code over here. So now let's add some few bad styles to our code. So maybe few spaces before the parenthesis and after it and as well here. So this really doesn't look good. So now in order to format this we're going to go to the command section. So shift control P. So now we're going to search for format documents. This one over here. So click on it. And perfect. You can see it removed all those unneeded spaces. And with that we have automatically formatted our codes using standards. And of course, you can go and create like shortcut in order to quickly format your code. So, my friends, those are the add-ons that we can add to our Visual Studio. And of course, if you installed something and you don't like it anymore, you can go back to your extensions. You can see we have here the installed section and you can go and uninstall your extension. So, my friend, that's all for now. We have prepared your setup in order to start learning Python.
And now my friends to the last bonus.
[music] I have prepared here the full Python road map using notion templates.
You can go to the link in the description and you can download it. So this is really nice in order to track your progress. So for example you can see here the main phases of learning Python. We have beginner, intermediate and advanced and as well nice status how much you have already covered and then below that we have the course index.
Here we have everything. So we have the faces, the chapters and as well the lessons. Now we are currently at the introduction. So it is in progress. We already covered what is Python. So it's done. And as well we set up our environment. So we can go over here and say this is as well done. Now we can go to the next step and say this is in progress. And this is the first topic that we're going to cover in Python the comments. And as well you can go and add a plan when you will learn the next steps. So it's really nice way in order to organize your learning process. And now you can see here we have like many stuff like the board. So you can see immediately the chapters that are to be done, how much in progress and how much you have covered. It's called like a combo [music] board. And once you have planned everything, you can check the timeline, a calendar, and as well here we have a nice chart and how many topics that we have already covered. So it is a really great way in order to feel like there is an achievement, there's like a progress. So if you'd like to plan and track your progress learning Python, you can go and use my notion templates.
The first thing that we usually learn about any programming language is how to add comments to our code. So what [music] are comments? A comment is a line of text in your code that Python totally ignores. It is there only to help us in order to understand what the code is doing. Okay. So now let's understand how this exactly works. So now let's say that you are coding a script in Python and now you would like to add a comment. So now in order to do that we use the special character hash and after that whatever you want like for example start and then you say this is my code and then an end. So now once you execute this what going to happen?
Python going to say okay this is not a [music] code this is just for the humans and I'm going to go and totally ignore it so that means actually nothing happens it will not affect the logic inside your code or anything in the output it is just a comment for you so now if you say you know what why do we need comments now let's check [music] this on the left side we have a code that is actually working but it is kind of mystery right there is like some kind of filtering and we are multiplying stuff together but it's really hard to understand and to Now if you look to the right side we have the same code but this time we are adding comments in order to explain the steps. So first we are saying okay we are loading the data then we are filtering the data based on the month and the region and then we are computing the revenue and at the end we are finding the total revenue and we are displaying it. So this is exactly the power of the comments. They're going to make your code understandable, readable, and professional. And believe me, you're going to thank yourself later once you come back to your code after a week or even a year or someone even from your team once they open your code, they will see these amazing comments in order to understand what is the logic of your code. So this is exactly why we add comments to our code. Okay. So now back to Visual Studio Code and let's go and add a comment. So we're going to start with a simple hash and then after that we can write whatever we want. So this is a comment and now if you go and execute it you can see in the output we are not getting anything because Python at the start sees okay we have this hash symbol. So that means whatever comes after it it's for human. So nothing to execute and python can go and ignore it.
And now of course this comment that we just created is a single line comment.
So this is a single line but sometimes in our code we would like to add more description and we would like to have like multiple lines. And of course we can go and do that if you start always with the hash. So this is multi-line [music] comments. And now we can go and add more lines of comments. Like we say first create user name. Then maybe a second step we print message to user and a third step data. So as you can see we have now multi-line comments. This is really nice if you have like complex logic and you want to explain it like step by step. Okay. Okay. So now my friend there are like different styles on how to place the comments inside your codes. So now let's say that in our code we have something like this X is equal to 10. Now if I would like to add a comment for this in order to explain it what we usually do is we go and place the comment on the top. So for example we say store the final exam score so that I understand okay I'm storing now the score 10 to the variable X. Don't worry about the syntax here. We're going to learn it in the next videos. So what we have done, we have placed a comment on top of our code that explains the next line of code. And now there is like another style where we add comments. Let me go over here and say x is equal to 9.
Now instead of putting the comment on top of the codes, we will place the comment at the end of the code. So we're going to say hash final exam score. So as you can see everything is in one line and we call this comment as inline comment. this type of comments it is really amazing in order to give a quick explanation and we usually keep it short as you can see I didn't say store the final exam score I just want to make it quick and short and say here final exam score so now if you go and execute this Python will not go and ignore the whole line because the line has two things first we have a code so Python going to go and execute this x is equal to 9 but the next part Python going to say okay we have here a hash symbol so that means Python going to go and ignore everything after [music] it, not the whole line. So again, if you want to make a long and detailed explanations, place your comment above your code and you can go and add multiple lines and so on. But if you'd like to give short and quick explanation or a label, you can go and place your comment in the same line exactly at the end of your code. Okay, so now a quick recap. Comments are line of text that Python totally ignores. It is written only for us for humans and we add comments to our code in order to make it understandable, readable, professional and easy to maintain. It's going to help you and your team in the future to understand the thought process inside your code. So that's all about the comments.
All right friends, so now we're going to talk about the very first command in Python and the most used piece of code that you will see everywhere in Python projects. We are talking about the [music] print function. So what is print? Print is a built-in Python function that displays a text or a result in the output screen in order to communicate something to the users. So what this really means? So now let's say that you have a Python code and you are building like an application. Now in order to show a message in the output, we can use the function print. And the syntax for that is very simple. We use a function named print and then we put inside parentheses and double quotes a message like for example let's say hi.
Now if you go and execute it what can happen? Python going to go and execute the function print where it's going to go and take whatever you written inside the message and move it and display it in the output and that's it. All what we are doing is we are sending a message from our code to the output in order to communicate something to the users.
And now of [music] course the question is what is function? Now we have like a dedicated complete chapter about the functions where we're going to deep dive into all details step by step. But now think about the function is like a mini machine in programming. You give it an input. It does some work and process your input behind the scenes and at the end it gives you back a result and output. So think of it, it's like a coffee machine. You give it the coffee beans and the water. Then the machine it does the work and at the output you will get your coffee. So you don't really care about all details how the coffee machine did make the coffee. It's enough for you to know what you want to give the machine and what you are expecting from the output. And this is exactly what are functions in Python or in programming languages. And now you might ask where these functions come from.
Well, we have three main sources for the functions. The first one is the built-in functions. They are functions that come with Python out of the box. You can just use them anytime you want inside your code. For example, we have the print function. That's why I said it is built-in function. And we have other functions like the input, the length function, max and so on. Now, the second source of functions, we have the third-party [music] functions. They come from external libraries like pandas, numpy, pispar, glotty and so on. Those libraries are developed from a third party from someone else. It's not from Python but still [clears throat] you can install them and use them in your code. And now the third source of functions we have the userdefined functions. These are the ones that you my friend creates. So you can go and build your mini coffee machine. But now my friends we are focusing only on the built-in functions for now and we are learning those important functions that comes from Python like we are learning now the print function. Okay. So now back to Visual Studio and let's practice this function. Let's start with something basic. So we're going to use the function name print and then we're going to open parenthesis and close it. And now in between we have to go and give the message and we're going to do that using double quotes. And now we are ready to put our message. And you can write whatever you want. So for example, hi Python. And that's it. That's all.
Let's go and execute it. Now you can see in the output we have as well the same message. Hi Python. So this is the most basic form of the print [music] statement. We are just telling Python print this message to the user as it is.
And now my friend there are like many special things that we can do inside [music] the print and I'm going to walk you now through them one by one. So the first thing is that we don't have to use always double quotes. We can go and use a single quotes and inside them we can go and write again any message. So if you go and execute it same thing going to happen. Python going to go and print the message. So you can use single or double quotes. Python going to accept them as long you are using the same type at the start and at the end. So for example, if I go and say over here print and let's say I start with double quotes and then I say hi and then I close it with single quotes. If I go and execute it, I will get a syntax error. So use the one that you like but always start and end with the same type. So this one is not working. I'm going to go and put it in a comment. Now my friends, you can use the print in order to show anything in the output. And you can style and design the output as you want. So for example, if I go over here and say let's go and print for example like a lot of minuses and then in between I'm going to go and say print and few spaces learn Python and as well few spaces. It's like I'm making a title and at the end I will have as well few minuses. So now if I go and execute it I will get this cool design in the output. It's like a banner or a title or whatever. So you can go and have fun with the print in order to design anything in the output message. I just found this nice sketch in the internet of printing a dinosaur. So you can see you can use your imagination in order to draw something using [music] print in Python.
Okay. So now we're going to talk about special characters in Python. Now we have a lot of characters that has no special meaning in Python like regular letters A B Z and numbers 5 9 0 and as well a lot of symbols like at and question mark. They don't mean anything special and they are treated as normal text. But now in Python there are characters that have special meaning when you are writing text. They all start with backslash and once Python sees those characters it's going to understand immediately aha we have to do something extra. So they have special purpose. So now let's say that we have this text. So we have a sequence of characters numbers. This is for Python is totally normal and there's like no extra job to be done. But now if we put slash before the n then what we have done? We just broke the normal sequence and now Python going to see special combination between the backslash and n and this has a special job where Python going to go and create a new line. So what we have done here is we have escaped a normal sequence and we have in Python different ways of escape [music] sequences like for example we have the backslash and the double quotes or a backslash with a single quotes. We use them in order to include those quotes in the text. And we have as well double backslashes in order to include a real backslash in the text. And then we have the backslash and n in order to create a new line in the output. And as well a backslash and t in order to add horizontal tab just to have like spaces between words. And as well we have the back slash and b for backspaces. All right. So now let's go back to our code in order to understand exactly what this means. Now let's say that I would like to have in the output double quotes like for example I'm going to say over here hi Python but I'm going to go and put Python into double quotes because I would like to see that in the output.
Now if you go and execute it you will get an error. So you can see Python saying okay in your file there is something wrong with your print. It is an invalid syntax. So let's go and check our code. You can see Python is now like in different color. It is not inside our message anymore because Python going to say okay this is the first message it start with the double quotes and ends with the double quote and then there is something called Python it will not understand what do you mean with that because it is not inside the message and then we have another message where it start and ends and inside it there is nothing so you cannot do that so now how we going to fix this we have to tell Python in some way that this double quote is the content of the message it's not the end of the string is just part of the message and we can tell that for Python using the magic of the backslash.
We can use it in order to escape a sequence. So now how we going to do it exactly before the double quotes the one that is part of the message we're going to type a backslash. So now we can see we are getting like different color and with that Python going to say okay there is something to skip here. The double quote that comes after the backslash it is not the end of the message. Now we are not done yet. We have as well this double quote after Python. This one should be as well skipped because it is part of the message. So we're going to add as well a backslash for it. And with that Python going to understand aha this is the content of the message. And the last double quotes we don't have before it any backslash. Python going to understand okay this is the end of the message. So if you go and execute it you will see in the output we have hi Python and Python inside double quotes. So again by looking to this the first double quote is the start of the message. So, Python understand okay I have to print everything after it. Then the second double quotes we have before it a backslash. Python can understand I have to print it so it's not yet the end of the message. The third one is the same story. Python going to see before it there is a backslash. So it is as well the part of the message. And the fourth one we don't have a backslash before it. So it is the end of the message. This is exactly how Python going to read this print. [music] Now there is like another way in order to have double quotes in the output. you can go and use the other type with the single quotes. So you say hi Python and you can put Python in double quotes. So if you go and execute it, it will work as well because you are using at the start and at the end single quotes and this will be working. And of course if you want to have like single quotes in the message, it will not work. So if I go over here and execute it, you will see we will get an error. So in order to have the single quote in the output you have to go and use as well the backslashes and if you go and execute it it will be working. So that means my friends you can use the backslash in order to skip a character to tell Python this is part of your message especially if you want to use the same type of code in the message in the output. Now the escape character the backslash can help us in many scenarios like for example let's say that I would like to go and print the path of something. So I'm going to say path double point and we can say the path looks like this. The folder is C and then we have the folder users and then the folder of the user bar. Now if I go and execute it, you will see again I'm going to get an error. It says there is something wrong in the syntax. So what is going on over here? Backslashes are always a special character in Python. And each time you use them, Python going to think you are telling something. So for Python is not understanding what to do with this character B after the backslash. It's not command because Python going to expect from you using the backslash to do something like skipping special character or making a new line and so on. So now in order to tell Python this is actually the content of my message you have to have double backslashes. So that we are telling Python, hey I want to have a real backslash in the output.
I'm not using this one in order to skip any character and this is the only way in order to print a backslash in the output. The same thing over here. Now if you go and execute it and now Python going to say aha okay I understand you [music] you are just using the backslash to be printed in the output. You don't have any extra command for it. So in order to have a real backslash in the output you have to use double backslashes. As I told you there are a lot of special things about the print and the backslashes. Let's have the following scenario. I'm going to go over here and say message one and then I'm going to have another print message two.
So now if you go and execute it, I will have those two messages in the output.
Now if you say I would like to have like space or like divider a blank between those two messages in the output. Of course you cannot go over here and add a blank between the two prints and execute it and then expect to have as well blank in the output. This of course will not work because in the line number two there is nothing to do for Python. So it will not go and print like a blank. Now there is like two ways in order to do that. Either you can go and say print and then you don't put anything inside this print. So there is no message it's [music] blank. If you go and execute it, you will have here like space between them. But there is like smarter way in order to have a blank or a new line in the output by using as well backslash.
So how we can do it? We can go and get rid of this third [music] print. And now just at the end of the first message we can go and use backslash and then the character n. So now the combination of the backslash and n you are telling python at the end of my message go and create a new line new empty line. So if you go and execute it you will get the same results. The first print going to go and print the first message. But at the end of this message we are telling Python to go and create an extra new line. And this is exactly why we have here a new line. This one came from the first print. And then after that we have the third line, the message two. This came from the second print because Python going to create a new line for each print. So this is exactly how it works. And if you say, you know what, I would like to have like three new lines.
So all what you have to do is to put them after each other. So back slashn back slashn. If you go and execute it, you will get here three blanks, three new lines. So this is really nice. And now of course you can go and put everything in one print. So I can take the message number two and put it exactly after the new three lines and then I can get rid of the second print [music] and execute it. So what is happening here? We have the message number one. This [music] is the first line. Then we have a new line that came from the first n. Then a second new line came from the [music] second back slashn. And then a third line came from the third backs slashn. But it's not empty because after it we have immediately a new message. So in the third line we have message number two.
And now if you don't want to see any empty or blanks between those two messages, but you want to have everything in one print, you can go and get rid of two new lines and leave only one. So if you go and execute it, you are having two messages in the output, but each message has a new line. And that's because of this backslash n. So these two prints going to give you exactly the same result as this one over here. So this is really amazing use case for the backslash. We can compact everything in one print and we start splitting the messages using backslash n. Okay. So now I have one more thing about the backslash for you. We can use it in order to add a tab space between the words. So let me show you for example if you go and remove this back slashn and go and execute it. Of course you can go over here and add like space in order to make separation between the words as well. You can go and use the backslash. So now in order to add a tab in [music] Python you can use back slash t. So now once Python sees this combination back slash anti it going to go and add a tab. So if you go and execute it you can see we have now a tab [music] between those two messages.
Okay. [music] So now I have a challenge for you. Now I want you to print in the output the following your learning path and then we have three bullet points. So Python basics, data engineering and AI.
But very important those bullet points they do not start immediately as a new row. they have like a tab before them.
It just looks nice, right? But now the real challenge here is to have only one print. Now pause the video and go and do that.
Okay, so I hope that you have done it.
Now we're going to do it together. So we're going to go and use print only once. I'm not allowed to use multiple prints and then double quotes. So let's start your learning path. Double points.
And now we need the first bullet points.
Python basics. But now if I go and execute it, I will have everything in one line. But I would like to have Python basics as a new line. That's why I'm going to go exactly over here and make a backslash and n. So if I go and execute it, I will get it in one new line. But we are not there yet. I have to have like a tab. So exactly after the new line, we're going to have a tab. So back slash t. And if I go and execute it, I will get my tab as well. So perfect. I have my first point. Let's go to the second one. We're going to have as well minus data engineering.
So if I go and execute it, everything going to be in one line back slash n back slashd and execute it. So with that I got a new point in a new line and as well I have a tab before it. Now we're going to go to the last one. So minus ai and if I execute it I will see it exactly after the engineering. I don't want that. I want it in a new line and as well with the tab. So if I go and execute it, I will get the last point in the output. So as you can see the backslash gives me a lot of flexibility in order to put everything in one print and I can control exactly how to design the output of my message. Okay my friends now to the last technique. As you can see over here our message is not really nice like everything in one big line and you have to scroll left and right to see the whole message. Now if you have like a long message and you don't want to put everything in one big line, you can go and split your message into multiple lines and as well using only one print function. So now let me show you what I mean. Your learning path going to stay over here and then in order to see the rest I'm going to go and hit an enter in order to go a new line in my code. But as you can see things going to get red, right? But I'm going to go and keep doing that. So for the second message a new line and for the third one a new line. Now, of course, if I go and execute it, you will get an error because Python will not allow it. Now, in order to fix this, we can use the triple quotes. So, now at the start, we're going to go and add triple quotes. And as well at the end, we will add triple quotes. Now, we can see in our code, we don't have anything red anymore. And if I go and execute it, you will see it is working. But we are getting as well extra new lines. So actually if you're using trouble quotes if you start a new message using a new line in your code it going to be considered as a new line. So that means we can go and get rid of the new lines.
So we don't need those back slashes and n and if I go and execute it I will get the exact same results. So this is really nice way in order to add multiple lines in your code and as well multiple lines in the output using one single print. You can do that only if you introduce three quotes. All right my friends. So with that I have covered let's say the most important tips and tricks that we can use inside the print.
It is really fun in order to design an output [music] for your users.
Now my [music] friend if you are total beginner in Python I know that you will have always the same question why do we need this stuff? How we going to use what we are learning in real projects?
Let me show you real use cases in projects for the print function. So for example we have this code. You don't have to understand all details. But I want you to understand the use case of the function. So now we have here a lot of values and calculations and logics.
Now if you run this code, you will not get anything in the outputs because everything is happening behind the scenes and we don't have like a window to what is going on. And this is exactly when we need print. [music] Now in order to see more what is going on in our code and to see inside the box, what you're going to do, we're going to go and print the last results. So I'm going to go and say final total double points and then the final total. So if I go and execute it now you can understand aha the final total is this value. So we do it like this in the projects. In order to see the results we use a print. So we are sending the value from our code to the output. Now let's say that there is something wrong. I'm not really sure whether this value is correct. And in our code we have like many steps. And here again we use the function print in order to understand each substep. So for example I can go and place a print in between here. And now this time we're going to say here subtotal is equal to as well subtotal.
Now my friend don't worry about the syntax and what is going on here. I just want you to understand when we use print in real projects. So now if I go and execute it now I can see and understand aha the subtotal is this number and this might help me to understand the substeps inside my code and whether my calculations are correct. Now I can see the subtotal and maybe I want to see as well the discount. So I'm going to go over here as well and print as well this intermediate result the discount. So discount and as well execute. So now I see a lot of stuff the subtotal the discount and the final total and actually the calculation is correct. So my friends, this is a very important use case in real projects. We use it in order to print the results of our calculations in the output that's going to make things more transparent for us and can help us in testing or debugging if there is something wrong in our calculations or logic. So this is how we use print in real world projects.
Okay my friends, so now let's do a recap. Print is a built-in function in Python and we usually use it in order to communicate something to the users to show the progress and the steps of your logic in your code and as well we use it in order to debug and test. So the print function is very simple but yet very powerful and my friend I'm going to promise you something that you will end up using the print function [music] in every Python scripts from the day one in the beginner level until the very advanced levels in Python. you will end up always using print. [music] So now we're going to talk about a fundamental and very important [music] concept in Python. We have the variables. So what is a variable? A variable is a name that you create to store value inside it in order to reuse it later in your program. So think about variables like a box and you give for this box a label like a name for it. It could be anything like a username, a country, an X. And we can put stuff inside this box. Anything you want like a number, a string, maybe a logic or calculations. So now once you have stuff inside this box, you can move this box everywhere in your code. And you can use this stuff wherever you want inside your code. And of course anytime you want, you can go and open the box, take everything out from it, and put the new stuff inside it. And then again, you go and close the box and start using it around in your code. So this is what we mean with variables.
[music] All right. So now as usual I'm going to show you a sketch in order to understand how Python execute variables behind the scenes. Okay. So now again we have our code and the output and we would like to go and create a variable. So we say the variable name is X and we want to store the number one inside it and we call this assignment. That means we are assigning the value one to the variable name X. So now once we execute it what's going to happen Python going to go and create let's say a box and call it variables. This box going to be storing like values and data in the memory that is ready to be used whenever we want. So Python going to create in the memory variable called X and we'll assign for this variable the value one inside it.
So so far what we have done we have created the variable we have assigned for it a value and now what is left is to use this variable. So let's say for example we use the function print. But now inside the parenthesis we will not use any value. We will use the variable name. So now if you go and execute it, Python cannot go and immediately print the value in the output. First Python has to go and get the value of the variable. So it's going to go and search in the memory for a variable called X.
And once it finds it, it's going to go and return and reads its value in order to use it in the print. So now during the execution, instead of having X, we're going to have one. Now we have everything ready for the print. Python going to go and print this message in the output. So you will see in the output one. So this is exactly how we work with the variables. We create one, we assign for it a value and then reuse it later. And now let's say that in the next steps in my code, we have to update the value of this variable. So we want to say forget about the one, go and store two instead. So it's very simple.
In order to update the value, we're going to use the same syntax. So we're going to say x is equal to two. So now once you execute it Python going to go to the variables and see aha I have already an x. So python doesn't have to create a new one. All what's going to happen is that it going to go and update its value. Instead of one we're going to have to. So that means from this point x now is equal to [music] two inside the memory. And now for example if you go and print the x again the same thing going to happen. Python going to go and search for the x but this time it's going to find two inside it and returns it to your code. And python going to print two instead of one. Okay. Okay. So now let's keep going. Now I would like to create a new variable called y. And now as usual we would like to assign for it a value. But this time we will not assign a direct value to it. We're going to assign for it like a calculation. So we're going to say the value of y going to be the result of x + 3. So now if you go and execute it, Python cannot go and immediately and create the variable y.
First it has to do the calculation. And now in order to do the calculation, we need to find the value of x. So again, Python going to go and access the variables in order to find the variable x and then it going to read its value back to your code. So now we're going to get the two inside our code. And now Python can do the calculation. So 2 + 3 the result going to be five. And now it has everything in order to create the variable. And Python going to go and create a new variable called y and assign for it the value five. And now if you check the variables box in our memory, we have two variables and each of them holding a value. And you can use them anytime you want in your program in order to do calculations, comparisons to display them anything you want. So for example, I can go and print the Y. So Python going to search for the variable Y and return its value and then print it in the output. So this is exactly how we work with the variables inside our code.
And this is as well exactly how Python execute the variables behind the scenes.
And of course, if you want in the future more content like this for free, then support the channel by subscribing, liking, and commenting. This is going to really help pushing on that channel to reach nice people like you. And as well, it's going to push on me to make more content for you. If you would like to support more, then you can go to the data academy and as well you can purchase my courses there. In the data academy, it's going to be easier to follow the structure because it is designed for courses and YouTube is actually not designed for that. So, you're going to follow a clean structure and you're going to follow the progress and at the end you're going to get the certificate of completion and as well I have their quizzes and tasks to challenge your new skills. So now let's go back to the course. [music] Okay, so now back to our code and I'm going to show you exactly why do we need variables. So now let's go and print free stuff. Like for example, print and I'm going to say my name is Bar. And of course you can go and use your name. You don't have to use mine. Let's go to the second print and we're going to say bar is learning Python. Now we're going to go to the third one and we say print bar wants to become Python expert. So that's all. Let's go and execute it. And we can see our messages exactly like in the print. So my friend, this is working.
But we still have an issue. Now let's say that I would like to go and change the name from Bar to Maria for example now to do that you have to go everywhere and start manually changing the name from bar to Maria. Right? And this is of course not really smart. Imagine you have like a big complex code. You have to go everywhere and start changing the values. So for sure this going to go out of control. But now if I go and execute this I will get Maria instead of bar.
Now we don't want this. We want to make it smarter, right? So let's get everything back like before and this time we're going to use the power of the variables. So let's go and create a new variable. Let's call it a name. So this is the variable name. And now we want to go and store inside it a value. So in order to do that we're going to use equal and after that we're going to have double quotes and we're going to use the name. So again this is the variable name and this is the value that will be stored inside the name. So we are assigning a value. So with that we have created a variable and we assigned for it a value. The next step we want to go and use this variable right. So what I'm going to do I'm going to go and copy the whole thing and put it below the variable. So now what we're going to do we're going to go and replace all those static values of the first name bar and we're going to replace them with the variable name. So we're going to have a name here as well here and here. But now if you go and execute it what's going to happen you will see in the output my name is name and that's because the variable name is inside the double quotes and Python going to think this is just a text. Now how we going to tell Python this is a variable this is not a part of the message. So that means in one print I want to have like static part and another part that is dynamic using the variables. So now in order to do that we use the comma and then after that we use the variable name. So we're going to go and get rid of this static part. And now you can see the name is outside of the static part. So it's not inside double quotes and Python can understand. Okay, this is a variable. So now if you go and execute it, you will see over here my name is bar and this value came from the variable. Now one more thing that you might notice that there is a space between the static part and the variable and this space here it comes from the comma. So each time you use a comma, Python going to go and automatically add a space between the parts. So now let's go and fix the second message by looking to this what is the dynamic part and what is the static part is learning Python is the static part and the name is variable. So in order to fix it we're going to go and remove the name from here and this time we're going to put the variable at the start and then split it from the static part using a comma. So variable is learning Python. If you go and execute it, you will see here bar is learning Python. And with that, we made the first part of the message dynamic. And the same thing for the third message wants to become Python expert is the static part. And the first part is the dynamic using of course the variable. So now if you go and execute it, you will see in the output everything is now working correctly. So now I'm going to show you exactly why do we need variables. At the start in the first section, you saw me changing the values manually. So I go in each print and start modifying the values and this is really annoying right but now after using variables all what I have to do is to go and change the value only once only at the variable. So now let's say instead of having bar we're going to go and use Maria and if I go and execute the code you will see everything change as well in my code. So my name is Maria Maria is learning Python Maria wants to become Python expert. So you can see now Maria everywhere and this is exactly why do we need variables. We go and create it in one place and then we reuse it multiple times inside our code and if I want to change something I change only the variable and this going to affect the whole code. And now I would like to show you something quickly. If I go and say name is equal and let's say I'm going to go and put my name here. So that means I'm assigning a new value to the variable name. And if I go and execute it, you will see nothing going to happen. That's because Python execute the code line by line. So at the line five, name is equal to Maria. At the line six, the variable name contains the value Maria. And Python has no idea that later I have updated the value. And the same thing for the line seven and 8.
Python only knows that the name is equal to Maria and only at the line 10. Python going to go and update the value in the variable. So now if I go and take everything from here and put it after the update and I go and execute it, you will see in the third section the name is equal to bar because at the line 11 now if Python goes to the variable name it will find the value bar and not Maria because we have updated. So this is how Python works step by step line by line.
Now let's go and introduce a new variable to our code. So if you check again our messages we can find another repetition and that is the Python. So we have it twice here. If you have the feeling that you are repeating the same value over and over, you can go and create for it a variable. So we can go and create a new variable called language and we can go and assign for it the value Python. We have to use this variable inside our code. So how we going to do it? In the first print, we don't have Python. So there is no need to use it. But in the second print, we have here Python. Now instead of having it static like this, we're going to use our new variable. So I'm going to go and remove it. And now after that, we're going to go and use language. But of course we cannot do it like this. We have to split using a comma. So now by looking to our print we have a variable comma then a static value is learning and then comma and then again a variable. So if you go and execute it everything going to be working. Maria is learning Python. So perfect. Let's go and update the last print. So now Python is in between like two static values. So we're going to go and replace this with a variable language. But we cannot leave it inside two double quotes. we're going to split things. So [music] after become, we're going to have the double quotes and then a comma and after the language we have another comma because the expert here is a static value. But what is missing here is a double comma in order to have the right syntax.
[music] So this time we have four parts.
We have variable, a static part, again a variable and then a static part. Let's go and execute it. So now as you can see everything is working. So now our code is fully dynamic and we can change stuff very easily. So now let's go and change the values. Instead of Mario, I'm going to go and use my name. And this time I'm not learning Python. I'm going to go and learn JavaScript. So now if I go and execute it, you can see my name is Bar.
Bar is learning JavaScript. And Bar wants to become a JavaScript expert. All right, my friends. Now you can have the feeling that our code is smarter, right?
Our code is completely dynamic and we have a full control on the values. And this is exactly the power that the variable gives us. It makes everything more dynamic and smart inside our code.
All right, my friends, [music] let's have a quick recap about the variables.
Variables is the foundations in order to make your code dynamic. We use it in order to create a name and we store inside it a value and we use the equal operator in order to assign values to the variables and Python keeps those values inside the memory in order to have a quick access. And once you create a variable, you can go and reuse it wherever you want inside your code. And of course, during the execution, you can go and update the value of the variable.
So my friends, you can find variables in every real world applications from a very simple script to advanced one. So [music] variables are amazing.
All right. Now I [music] have the following challenge for you. You have to go and print those three lines. But not only that, take a look to the text to introduce at least one variable in your code to make things dynamic. So now pause the video and write your solution.
All right, my friends. So now we're going to talk about the second most important function in Python. We have the input. So so far what we have learned is we can use the print in order to show something to the user. But now we're going to go and flip things. We're going to go and get something back from the user. So for that we can use the input function. It is as well a built-in Python function that lets you get input from the user. And my friend, this input function is used almost in any application or website you are interacting with. Like for example, if you go to LinkedIn, first you have to go and log in or register. So the application wants an input from you. You can go on here and add your email and some passwords. So the LinkedIn is using an input in order to get something from the user and as well the same thing in shbt the place where you go and ask something where you're going to go and write like a prompt like explain input function. This window is using an input function in order to get something from the user. So my friend this function will be used almost in any application or website and we use it in order to get something from the user. Okay. So now let's understand how this exactly works behind the scenes. Now we are writing our code and we would like to get something from the user. So we write in our code input and in between parenthesis we write a message. So for example enter a value. Now if you go and execute it what going to happen? Python first going to go and take the message and display it in the output. So you're going to see in the output enter a value. And now what going to happen?
Everything going to pause and wait for an input from the user. So now our application going to wait until the user type something. It could be any value anything. So for example the user enters 50. Now once the user hits enter the value 50 will be returned back to our code to our program. So as you can see now we have like interaction. We are not only displaying something in the output like the print. Now we are asking for the user to give us something to give us a value. So this is exactly how the input works in Python. Okay. So back to Visual Studio. Let's go and practice the input. So now let's go and ask the users for their names. So in order to do that we're going to use the function input.
And now then we open a parenthesis. And now inside it we're going to go and write a message to make sure that the user knows we are waiting for an input.
So we're going to give a command like enter your name and then double points in order to indicate that we are waiting of course. So actually that's it. So let's go and execute it. Now you can see in the output our message enter your name. But this time the program is waiting for an input from us. So now everything is waiting and paused until you give a value. So for example we're going to go and give my name bar. And now as you can see as I'm typing the program is still waiting until I hit enter. So once I hit enter the waiting is ended and the value is returned back to my code. But now in this example we have an issue. The value that I give to the program is completely lost and nothing happened because I'm not doing anything with the returned value. So it is like a total waste. Now instead of that we're going to go and do something with this value with this returned value from the user. So what we're going to do, we're going to go and store it inside a variable like for example name and then we're going to go and assign the returned value from the input to our variable. So now I'm saying whatever value that we're going to get in return from the input should be stored inside the variable name. But we are not done yet because okay we have just created a variable in the memory and we have a value for it. But so far we are not using this variable anywhere. Now as we learned in order to use a variable we can go and print it. So let's go and print this variable. We're going to say you are. So this is the static part. And then we're going to use the whatever value that is stored inside the variable name. So now let's go and test it. Now again in the output the program is waiting to enter your name. So let's go and do that and hit enter. And now we can see another message. It says you are then we have our name bar. So again what happened here? We ask the user for their name. We stored the returned value inside a variable and then we printed this variable. So that means we are getting something from the user and then we are printing it back. All right. So now let's understand in details what is hard-coded values and dynamic values from input. Let's go and create another variable called country. And let's assign for it your country. So I'm going to go and add Germany. So now by looking to this that means we have two variables. We have the name and the country. And of course those variables needs values. Now if we check the country over here, we call the value Germany as hardcoded value because we have manually entered this value inside our code. So that means the value will never change unless you go over here and you type something else. So that means this value is predefined before the execution. I have written this value Germany before running the program. So that means anyone opens your code going to see immediately you are storing the value Germany inside the country. But now if you look to the variable name, no one knows about the value before the execution. It is not hard-coded inside the code. The value going to be assigned during the execution to whatever value came from the user. So that means the value going to be totally dynamic depends on the user input. It is not hard-coded. So now let's make use of those two variables inside the print. So I'm going to go and rewrite the whole thing like this. We're going to say name then comma and then we're going to make static parts comes from and then we're going to use the country variable. So let's go and execute it. Now again we have to enter the name. Let's say this time we're going to say Maria and hit enter. Maria comes from Germany. So again the first part of the message came from the user and the Germany word here came from our code. So if I execute this like 100 times [music] I will get always comes from Germany. But I might get different like input from the users. So these are two different ways on how we assign values to the variables either using hard-coded value like this or by using an interactive function like the input where we're going to make everything dynamic. So now using the input and the variables you have the feeling that our code is not only smart and dynamic but also interactive. So this is like an interaction between our code and the user. We are asking the user for a value and then we are getting this value and doing something about it.
So things are live and interactive and this is exactly what we do if we are building applications. So my friends this is the power of the input.
So so far we have learned many things.
We have learned two built-in functions in Python the input and the print and as well we have learned about the comments and the variables. So now I'm going to show you how everything works together behind the scenes. So now let's say that you have the following code and you went and execute it. So as we learned Python going to go and execute it line by line.
The first line is about the variables.
So, Python here going to go and create a new variable inside the box in the memory called X. And then it going to go and assign for it the value A. So, a going to be stored inside a variable called X. And that's it for the first line. In the second line, we are saying print X. So, that means first Python has to go and get the value from the variables. It's going to go and search for the variable name X and read and returns the value A back to your code.
So now we have everything the value and Python can go and print this message in the output. So you will get in the output a and now python going to go to the third line and going to say aha now we have like the special character hash.
This means here it's like a comment. It is a note for the human. I don't really care about it. So I can go and skip it.
So nothing going to happen in the third line. We made this line only for us. Now Python going to go to the next row. And here we have many things. So we are saying I would like to go and create a new variable called y. But the value for this variable we're going to get it from the user. And in order to do that we are using the input function. So first Python going to go and print the message enter a value in the output in order to tell the user to enter something.
[music] And now everything going to pause. So the whole program going to wait and pause until the user enter something. So let's say our user go and hit B and then enter. Now this value B will be returned back to our program to our code. And now Python has a value for our variable Y. So now it can go to the variables box and create a new variable called Y and assign for it the value B.
Now to the last line print Y. The same thing going to happen. Python going to go and access the variable Y in order to get its value the B and then go and print it in the output. [music] So again by looking to this the value of X it is specified and hardcoded inside our code.
But the value of Y it is completely dynamic and we got it from the user. And as you saw we use the print function in order to show something to the user and we use the input function in order to get something from the user. So my friends with that you have understood many basic commands in Python in order to make your code smart, dynamic and interactive. So as you can see my friends programming is amazing right?
Hey friends. So now if you are learning any new programming language like Python, you have to understand and master at early phase one concepts the data types. [music] So that's why now we're going to go and deep dive into the data types in Python. And we're going to start with the first question. What is a data type? So now let's say that I got a box like this and I don't know what is inside it but there's like something.
Now the first question that I'm going to go and ask myself should I be careful with this box? Is it like inside it glass, electronics or cookies? Or can I go and throw it around in my room? Is it like papers or let's say clothes?
>> Open the box.
>> He can still catch. Okay, come on. Get out of there. Get out of there.
>> So now I'm wondering about the type of the thing inside this box. And this is exactly what Python needs to understand once you present a new value inside your codes. Python want to understand the type of this value, the type of the data so that it can understand how to treat your data, how to treat your values. Can Python go and count it and do like mathematical operations on it or can we go and combine it or maybe compare it with other values and so on. So this is exactly what are data types in programming languages. So now let's understand exactly what we are talking [music] about. Let's have this sketch.
Now you remember if you want to create a value in a variable like a is equal to 10. So now what can happen? You will have like a box and this box has a label. The label is the variable name a now inside this box we're going to put the value. So our value our data is 10 and this number going to be stored inside this box. So so far we have two informations. The variable name and the value itself. Now we [snorts] have a third information about this box and that is the type of the data inside this box is an integer. So that means for different data types now we have different type of boxes and the only thing that define which box we're going to need is the value itself. So let's see how Python works behind the scenes.
Okay. So now let's say that in the code you said a is equal to 10. Now the first thing that Python going to do is going to check the value. So it's going to go and ask itself what type of data is it.
So that means Python here is inspecting the values inside your code and it is smart enough to say uh-huh this is an integer. And now Python behind the scenes has a lot of empty boxes with different sizes and shapes. So now since it is an integer Python going to go and choose the right box with the right size. So it's going to go and put the label of a the variable name. Put the value inside it and then put the whole thing in the memory. All right. So now let's move on. In the next line we have another variable called B. But this time inside it there is hello. Again the first thing that Python going to do is to inspect the value. So Python going to check this data and going to find aha we have here like characters. This is a string. So it's going to go and start searching the boxes for the correct type and as well the correct size and once inside it it's going to put as well a label on it the variable name put the value inside it and then move everything to the memory but of course in different place. Now moving on to the third value we have C is equal to true. Python going to go and check the value and this time it's going to say aha this is not a number. This is not between like double quotes. So that means this is not a string and this value true is a boolean.
So with that Python understand the type of the datam and the same thing happens but I'm going to go and find the right box for this value put the variable name the value inside it and move everything to the memory. Okay. So now we're going to go and do something tricky where we're going to reuse the variable a but this time we're going to store inside it different type of value. So we are storing ABC. Now previously we have a is equal to 10. So it was a number but this time it is a string. So now what's going to happen? Python going to go and check as well the value and say uh-huh this is a string. We have characters but we have already a variable in the memory called a. So Python will not go and create like two variables with the same name. So what going to happen? It's going to go and grab the old box and totally destroy it. So it's going to remove the variable and the data inside it and it going to go and pick the right box now for the new value. This time we are talking about the string. So it will put the label a and inside it going to store the new value but this time in a new type of box. And then as usual going to go and put it in the memory but together in the same shelves with the variable B. Now by looking to this we can understand Python automatically detect the type. So that means in your code you don't have to go and tell Python about the data type.
It's not like Java or C++ where you have type declarations before each variable.
Python is simple and smart enough to understand the data type. And we have understood that Python is very dynamic.
You can go and change the data type of your variables anytime you want in your code. And as well we have understood that our values, our data will be stored in different types and as well in different sizes. Okay. So now let's understand the real reason why Python needs data types. Let's say that we have a value like hello and you go and apply for it a function called upper. Now if you apply this function to this value, what can happen? You will get the same word but everything is written in uppercase. So you will get hello with all capital letters. Now let's say that I would like to apply the same function but for the number 50. So now in this scenario what can happen? Are we going to go and get like very big 50? Well, there is no upper cases for the numbers and this [music] makes totally no sense.
But my friend things don't work like this. The value hello has a data type.
It is the string and the data type of the 50 is integer. Now based on the data type, Python can restrict which types could be used with this function. So if you try to apply this function to the integer, Python going to throw immediately an error and say this is not allowed. You can use this function only for the data type string. So that means my friend, Python uses data types to make sure you don't do wrong things on the wrong kind of data. It's like you are trying to microwave your keys. Just because you can doesn't mean that you should.
Now there is like another reason. Let's say that we have two values. You have two and three. Those values are integers. And now if you use the operator plus between them, what can happen? Python going to go and answer with five. So because they are integers, Python understands I can go and add those numbers. But now let's have the same scenarios, the same values, but we are using here double quotes. Now for Python, those values are string values.
And if you go and apply the operator plus between them this time Python can show in the output 23. Well this time since your values are string Python will not go and apply mathematical operation between them. All what happens here is Python is concatenating your string. It is joining the text. So as you can see Python behaves very differently based on the data type. So this is exactly why programming languages needs data types.
It use it in order to prevent you doing something wrong and as well it can understand how to treat and operate on your data.
Well, there are like many ways on how you categorize the data types in Python.
But I do it like this. I think about it like we have three categories, three baskets. One basket holds one fruit, one value. Another one holds many fruits, many values. So this is a second category and the third one don't hold any value. It is an empty basket. So let me walk you through them one by one. The first basket, the first group, it is completely empty. There is nothing inside it. And in Python, we have a special value for that. It's called none. Now moving on to the second category, we have the single value category. And here we could have values like 50. And as we learned, this is an integer. But we have another type for the decimals like for example 3.14. This is not a whole number. We have here decimals. So the data type of this numeric value is float. Now moving on.
If you are using characters like hello as we learned this is a string. And one more if you have like true then this is as well a new data type called boolean.
Now if you are looking to those values and data types they always hold one value. That's why I group them together under this category. Now moving on to the third category the multiple values.
Here we have many values like for example 1 comma 2 comma 3 you have the data type called list or let's say that you have like three values a b c and with that you are forming different data type called dictionary dict and we have other types like the set and the tubble.
So as you can see those data types holds multiple values. So it's not like the other types where we have only one value like the integer float and boolean and there are like many different names for this category. we call it a data structure or collection. Some other call it containers. So they are like advanced data types in Python and they are very important. Of course I'm going to have a dedicated chapter for that. And on the other side the single values sometime we call it primitive data types. They are the core and the very basic data types in any programming languages. Even in SQL they are very similar. So those are the famous data types inside Python that we're going to learn through the course.
Okay. Okay, so now let's go and create some basic data types in Python. And we're going to start with the first one.
Let's say that a is equal to 10. Now Python going to go and check the value.
10 is a whole number. It has no decimals. That's why it going to treat it automatically as an integer. So this one is an integer int. Now next let's go and create another variable. And let's assign for it the value 3.15. Now Python going to say aha this is a decimal point. It is not an integer anymore. In Python we have different type for that.
is going to be the float and of course what I'm writing here is again a comment we are not declaring anything that's enough for Python it is smart enough to find out the data type now the next one we learned about it if you put the value between two double quotes and you say for example hello this is in Python considered to be a string so this one Python going to go and assign it for a string data type so if you use double quotes or even if you go and use single quotes like this and you say hi this considered to be a string as well it doesn't matter whether you are using double quotes or single quotes. Once Python sees it, it's going to assign it for data type string and you can put [music] inside it anything. So you can use letters like this or even you can go and say double quotes and you put number inside it. This still to be considered as a string because Python once says the double quotes or single quotes it will automatically assign it to a string without going deeper in order to understand whether they are numbers or characters or even special characters.
So all those stuffs considered to be a string. Okay. Okay. So, so far we can have numeric values, text values. Now moving on to the third very famous type.
We can say if is equal to true or another value we say it is equal to false. Okay. So what do we have here? We have here like words true and false. But they are not between like double quotes or single quotes. So they are not string. Those two are special values for the data type boolean. Okay. So that means the boolean data type has only two values. It could be true or false. there is no third value. It's like you are saying for my question there is like only yes or no one and zero. So true and false. So again that means for the other data types you could have any value that you want. But for the boolean it has only two values. This is a great and very important data type in order to build a logic inside our program. And the boolean usually is like an output of a lot of functions. So if you compare like two values if they are equal we will get true. If not we will get false.
So this is very important data type. And now one more thing this is case sensitive. That means the first character must be capitalized and the rest must be lowercase. If you go and say true like this it will not work. As you can see it has like different color even. So it is very important to have the first letter as capitalized and the rest as lowerased. Okay. Moving on to the next one. We have something special and this one called none. So again this is a word. It's not between double quotes. So this is not a string. This is a special value. And now since we are saying none that means it is nothing. I don't have a value for this variable yet. It's like saying okay I'm going to have now a variable but I don't want to assign for it any value. I don't know yet which one can be. Or you say for this variable we will not assign anything for it. So none means nothing.
It is special value to say there is no data. There is no value. So that means we don't have a data type for this. Now since we are talking about the nan I would like to explain something that confuses a lot of developers. You could have this value where you say I'm going to have like double quotes but nothing inside it. So there is nothing. This is totally different than the none because in this scenario where you have double quotes and nothing inside it. Python understand already we are talking about a string value. So it going to do everything as any other string. It going to reserve for it to place in the memory. It going to treat it as a string. And we call this as a blank.
blank has already a data type string and we're going to treat it in the code as a string. But the special value none. We don't know yet which data type. So, Python will not do anything for it. Will not go and create anything in the memory. Will not assign it to any data type. And another one we could have as well double quotes and inside it a space. This we call it an empty space.
The same thing like the blank it is string. It will has a place in the memory and even here we have a value with the space. So let's write a few comment. This is boolean. Same thing is here. Now for the none, we call it none type. For the blank, it is a string but it is empty. It is blank. And the last one, this is as well string and it has empty space. So again, what we have learned, we have numeric values like the int and float. And for the text, we have a string data type. Once you open two double quotes or single quotes, you can write inside it whatever you want. It could be letters, it could be numbers.
[music] Python going to treat it all as same data type string. Another data type which is really cool where you have only two values true and false for the conditions. And then we have special case where you don't want to assign any value and we don't know yet the data type we call it none and it is different than the plank and the empty space. So those are the basics of data types.
Now you might ask me what is the big deal about the data types? [music] Why do we have to learn them? Well, you have to understand your tools that you have in the programming language in order to work with the data. And you cannot use all the tools for all values. There are specific tools for specific data types.
[music] And that's why I need you to understand now a little bit more about the functions and data types. So now there is like one thing that we are always doing as we are programming. We want to do something to the data. So this has two parts. Now we have learned data are values. And now in order to do something to our data, we use functions.
And functions are very simple. You have a value, you pass it to the function.
The function does something and then in the output you will get a new value. Now in Python there are like three main types of functions. The first one we call it the standalone functions. They are functions that you can go and use them directly like the function that we have learned before print and type.
Those are standalone functions. They are not complicated. they just work. Now to the second type of functions, we have something called method of a class. So methods are as well functions but they are a little bit more complicated where they belong to something. You can't just go and directly use them. You have to prepare something before using the methods. And for that we have methods or functions like the upper to make a string capitalized. And as well we have another method called replace to replace something inside your string. Now there are like third things that you can do with your values. We call it operations and yes my friend operations are functions but it is something completely behind the scenes and you don't have to worry about those functions for now just learn they are operations and that's it they are very easy you just go and use symbols like plus divider greater we have equal all those are operators they are like shortcuts in order to use a hidden function let's say now of course the question is where do these functions come from the first and the very important one is the standard Python library. At the start, as we have installed Python at our PC, we have actually not only installed the Python engine, we have installed as well this standard library. So, this one is the huge toolbox that comes with Python full of very helpful built-in functions that we use every day in Python. Now, if this is not enough, we have another source of functions. We call it the thirdparty libraries. They are actual tools that made by the community. You can install them in order to extend the power of Python. And there are very famous libraries like the pandas, numpy, tensorflow and many other amazing things that can help us with many different tasks using python. And now we come to the third source of functions. We have the userdefined functions. It is something that you code and create on your own in order to do something to our values. So those are the different types of functions and the different sources of functions. And now we're going to understand one more very important thing about the standalone functions and method and what this has to do with the data types. Okay. So now we're going to go and put all the pieces together. Now we start at the top. We have the standard Python library. So it is the Python toolbox and if you open it you will find many stuff but we find something called modules. A module is like a Python file that contains reusable code. So you don't have to write it and program it each time. Now the most famous one that we have in Python is the built-in module. So this built-in module is always there and ready to be used. So it is always connected and you don't have to do anything. That's why you don't see us importing the built-in module. Now what is inside this built-in model? We can find the famous standalone functions that we already used. We have the input, the print type function, links functions, and usually we call them the built-in functions. Now not only that, we can find as well classes. Again, we're going to deep dive into the classes later, but think about it. It's like a blueprint and we have a class for each data type in [music] Python. And now for each class, for each data type, we have special functions. We call them methods. And those methods are attached to only those classes. So for example, for the string class, we have methods like and replace. And there are many other methods. For the integer, we have a method like for example to bytes. And as I said, for each data type, we have a class. Now inside this library we have as well other modules like for example the math and this is very powerful in order to do mathematical calculations and so on but this is not a built-in it's not already connected it is there in the library but if you want to use it you have to go and import it import means I'm installing this in my code now inside this module we can find as well functions like for example we have seal and floor and of course many other stuff so as you can see there is like not one place for all functions it's like spreaded into multiple modules and as well classes. So now let's have an example. Let's say that we have the value hello. So now in Python this value, this data it's not like a value in the air. It is an object of string.
And since now this value is attached to the class, we can use without any limitations all the methods of this class. So without any limitations, I can go and apply the method or replace to this value. Now of course the question is can I use the other functions? Well, it depends on the Python rules and whether it makes sense. Of course. So for example, from the built-in functions, I can go and use the type and the length for this value without any issues. But now if you try to use the functions from the math module, it will not work. It is restricted because of course it makes no sense to apply mathematical operations on a string. So about the functions, it really depends.
Some functions will work and others will not. But what is totally not allowed is to use the methods of other class like for example the method of the integer because they are special functions only for the integer. So this is actually how it works. Let's have another example.
Let's have the value 50. Now again this value 50 is not like in the air. Python going to go and connect it as an object to the class integer. So what this means my friend I can go and use any method inside this class for my number for the integer and of course about the other functions it depends. So for example for the built-in functions I can go and use the type function but I cannot go and use the lengths function because for [clears throat] Python it makes no sense and in the other hand in the mathematical module we can go and use almost all the functions because we have an integer and number and we can go and apply mathematical functions. And now we come to the point if you try to use any methods from the string class nothing going to work because no string methods will be allowed to be used for integers only for the string. Okay. So now let's quickly recap your data your values in Python they are objects that is connected or belong to a specific data type class and this class has multiple methods. Now this value can use any method inside this class. So that means those methods are dedicated only for one data type for this class. But it is totally forbidden to use the methods of another data type class. And about the other functions, it depends and depends of the Python rules. Some functions will work and others will not work because of course it makes no sense. So my friend, this is the big picture. This is how things works in Python. And now it's totally fine if you didn't understand everything like 100% because as we progress in the course I'm going to explain more and more in details. I'm sure at the end you're going to be master in Python. Okay. Now we're going to do quick comparison between the functions and methods. So the functions are independent block of code. The methods are functions. So again they are like codes but they belong to objects and classes. This is not the only difference. There are differences as well in the syntax. So now as we learned the syntax going to be very simple for the functions. We say the function name and then between parenthesis we put a value or a variable. For example we have learned print hello. So print is the function name and between parenthesis we pass the value and we have another function called type. So this is the function name and we pass for it a value like 50. So I think nothing new here.
But now the syntax for the methods looks different. So this time we start with the value or the variable name. then we use dots and after that the method name.
So it's the way around. We start with the value then the method name. In the functions we start with the function name then the value and the value that we are manipulating will not be between the parenthesis. We can of course put some other stuff but the main value that we are manipulating it will be outside after a dot. So let's have an example.
Our value hello and then dot and the method name upper. So it looks different but it's not that hard. The same thing here we have the value 50 dot bit_length the method name. Of course we can go and use variables not only values. So that's the main differences between functions and methods. Okay. Now we're going to have few examples. Let's go and create two variables. One of them is a string with any value and another one a number like 10. So we have a string and integer. Now let's try out few built-in functions in Python. We already done that many times. So we're going to go and print the text and as well let's go and print the number. So let's go and execute it and that means the print function can work for any data type.
Okay. So now let's go and use another built-in Python function. And this time we're going to go and use the function type. So for both of them the type function is really nice one in order to understand the data type of your values because as we learned Python going to automatically detect the data types. But maybe you are wondering which data type did the Python detect. So now if you go and execute it, you will not see anything in the output and that's because type will not print anything in the output. That's why we have to use the print. [music] So that means we have to go at the start and say print and now we are doing the nesting function. So we are using function inside another function. So print [music] and now let's go and execute it. Now in the output we are seeing the result of the type function. So from the first variable the type is class string because it is a string value and for the second variable the number it is from the class integer. So with that we can understand clearly which type Python is using for my variables and my values. So as you can see this function works for any type. Okay. So now let's keep going.
Let's move to another built-in function in Python. We have the length len. So let's do it for both of our variables.
This function is really simple. It's going to go and calculate the length of your data the value. So now if you go and execute it you will find at the start we have the number two and this comes from the first print because our value has here like two characters. The length of this string is two. So that's why we are seeing two but for the second print we are getting an error and if you check the error message it says the object of type int has no length. So that means my friend this built-in function the length will not [snorts] work for the data type integer. So again not all functions will work for all data types. So that means we cannot do that and we comment that out and then execute we will get two. Okay. So now we're going to move to the methods of the data type classes. Let's try for example the method and go and apply it for both of the functions. So let's start with the first one. We're going to say this time the syntax like this text dot and then open and close parenthesis like this. So as you can see the syntax is different than functions. Now if I go and execute it, you will see we will not get any error but I will not see anything in the output and that's because I'm not printing. So we're going to put the whole thing in print. So now I'm putting a method inside a function. Yeah, let's go and execute it. You will see now the same value but everything is written in uppercase. And this is exactly what the upper method does. It converts everything to an uppercase. Now let's go and apply the same thing for the number.
So we're going to say number dot upper.
And now you can see we are not even getting the green color. Now if you go and execute it, you will get here an error and it says the int object has no attribute upper. As we learned the value 10 is an object from the class integer and in this class there is no method called upper. We can find this method only in the class of string. That's why Python is not finding this method and we are getting an error. So it is not allowed to use the method of another class. So now let's go and apply a method from the integer class. For example, we're going to use the bit length like this. This method is very simple. It's going to go and calculate how many bits are needed in order to store my value. So now if I go and execute it, nothing going to happen. But of course, we can go and print the value. So we're going to say print and execute it. So it says we need four bits in order to store the value 10. So as you can see, it is allowed to use this method because it is an integer method.
Now if you go and apply the same method for the text, it will not work. So if you go and execute it, it going to say the string object has no attribute bit length. So it will not work. You can use only the methods from the same class. So now we have an understanding about what is allowed and what is forbidden. So this is exactly why you have to understand how things works in Python between the functions and methods and the data types. [music] Okay my friends. So this is the full map of Python data types that we are going to visit each one of them step by step in this course because it is very fundamental in Python to understand how I'm going to store structure manipulate transform my values and data and what are my tools what are the functions that I can use in each data type so we're going to understand them one by one and one of the most important data type here is the string so we're going to cover that first in our course okay friends so now let's have a quick recap Each value in Python has a data type. It tells Python how to treat and operate on that value. And Python is smart enough to automatically detect the data types. So you don't have to declare any types for Python. And data types can change anytime. It is very dynamic in Python.
So you can switch the types of variables anytime you want. And for the data types, we have three main categories.
The no value, so none type when you have nothing. Then the second category, we have the single values. Those are the very basic and primitive data types like integer, float, string and boolean. And then a third category we have the multiple values. Those are the advanced data types. We call them data structures, containers, collections. And here we have very important data types like list, double, set, dictionary and we have learned values are objects that belongs to a class and each data type class has its own methods. [music] So each data type comes with its tools in Python. So this is what we mean with data types.
Now we have [music] a challenge for you.
Get five variables where you can enter your age, height, name, are you a student and something with no value yet.
Then print the values, the types and the length of each variable. Now pause the video and solve the task.
All right friends. So now let's have a look back to our road map. With that, we have completed the first chapter, the foundations of Python. So, you learn some theory and as well you know the basic tools that you're going to end up using each time you write Python code.
So, that was easy stuff. Now, we're going to go deeper. We have two chapters for one idea, how to work with data. And here we have two very common data types.
We have the text, the string, and we have the numbers. So, we're going to start with the first one, how to work with the strings. This chapter going to be a big one because my friend in companies almost all data is text. And here I'm going to gear you with all tools in order to work with this type of data. So how to transform it, process it, clean it up, validate it, extract things, search, analyze it. So many tools and we will do that right now. And of course we're going to start with the first question. What are string values?
As we learned before, the string value is just a text in between double quotes or single quotes. And you can put between the quotes anything you want like a name, country, maybe as well numbers with special characters. So there are no rules and everything considered to be a string. Now you might ask why we should care about the string functions. Well my friend, text or string data is everywhere in the file names in the user input in web data APIs logs. So I can say like 80% of the real world data is just text and usually text has really bad data quality. Everything could happen like typos, extra spaces, weird characters. So it is highly likely to be a messy and noisy. But at the same time text informations could contain a lot of informations like we could have in one string value a name, age and country. And it doesn't matter whether you are a data engineer, analyst, data scientist, you have always to manipulate and prepare the data before doing anything. So you need to learn all the tools in order to work with the string values. And this is exactly what we're going to do now. So [music] let's go.
So now before we go into details of each function [music] and operation that we can do on the strings, I have prepared here like a road map or let's say a cheat sheet sheet where I have grouped the different operations and functions under specific categories. For example, if you want to go and search for something, we have here like three methods and one operator. And now about the coloring, the red ones, they are the built-in functions in Python. And the blue ones are the method that is specialized only for the class string.
So as you can see we have a lot of very important methods and then we have the yellow ones. They are the operators that you can use in order to manipulate the string values. So this is the road map and let's start with the first group.
We're going to talk about the types. So here we have like two built-in functions in order to handle the string data type.
So let's go.
All right. So let's start. Now we're going to go and get any value. So let's have a variable and inside it we put a string value like your first name. And of course, you can put this in single quotes or in double quotes. It's all the same for Python. Now, the first built-in function that we can use is the type.
You can use it in order to check the data type of your value. Like for example, let's go and test it. We say type and then we use the variable then execute it. You can see we will not get anything in the output because we are not printing. So in order to see the output, we have to say print and then execute again. And now you can see your value belongs to the class string. And that means the data type of my value is a string. So this is really nice in order to check the data type of your value. Now let's go and create another variable. But this time we're going to go and put a number like for example 24.
I'm not 24. Now of course if you go and print the data type of the age we will get a different value. So let's go and execute. Now you can see the class is integer because we have here a number.
So so far everything is good. But now let's go and do the following thing.
We're going to say print and then we can say your age is double points and now we go and do operation like the plus and here at the end we say age. So now we'd like to see like a nice message about the age. Now if I go and execute it I will get an error. We are trying to do operation between an integer and a string value and this will not work.
This can work only if you have like two integers or two strings. So now we cannot do it like this. We have to go and convert the data type. And here comes the second built-in function. We have the str. It's going to turns any value into a string data type and this is exactly what we need in this scenario. So what we're going to do we will go and pass the age to the function str and in return we will get a string value then it going to be okay because we are doing a plus between two string values. So if you go and execute it everything will be working your age is 24 and Python will accept it. So this is exactly what the str function does converts the data type from any value to a string. So now there is like something important to understand the variable age contains a number. So here as we converted the value to a string that doesn't mean Python going to go to the variable and change the data type there we just change the data type only in the print and here we don't have any new assignments. So if you go afterward and print the data type of the age it will stay as an integer. And this is of course okay if you want to go and do something like this. So you want to go and add like five years. This will work because the age is number and as well five is number. But if you say you know what I would like to change the value of the age to be always as a string. So you have to go like this and say str age. So that you are grabbing the 24 as an integer. Convert it to a string and then assign it again to the age. And with that if you go and print a data type of the age you will get here a string. And of course you cannot later go do something like this. And if you execute it, you will get an error because now age at this point is a string. So as you can see, Python is very dynamic and you can go and change the data type anytime you want. But you have to be careful once you change the data type to for example a string. Then you and Python has to handle it as a string and you can apply only the method of string to this value. So if you change the data type, be careful what you are doing otherwise you will get errors like here in my code. Okay. So that's all about the two functions and this category. We're going to move to the next one. We have here one built-in function add a methods [music] in order to do few calculations on the string. So it's like we're going to go and measure something with the string values. So let's check those two.
[music] Okay. So now let's have this example.
Let's say that we are asking the user to give us a password and the password going to be something like this 1 2 3 a.
So now let's say this is the input of the user. And now as you know in any application if you are adding any password there are many rules and one of them is to check the length of the password. So now let's say that in your application you say you know what the password must be at least eight characters. So that means I have to go and calculate the length of the password. And to do that we have the built-in function leen. You can use it in order to count how many items inside your value. And in this scenario we count how many characters inside our string value. So it is very simple.
We're going to go and say len and then pass to it our variable our value and if you go and execute it of course nothing going to happen because we don't have a print. So if you go and say print and then execute. So the length function is going to return a number and it's going to be the number of characters inside your password. And now of course we can go and use this information to check whether the password is too short or too long. So for example we're going to learn it of course later the if statements. We say if the length of the password is shorter than eight double points and then new line we say if this is true then print your password is too short. So let's go and execute it. You can see our program saying your password is too short. And now let's say the user goes and adds more numbers and characters to the password and then execute it again. You will see the length of the password is 11 and we are not getting anymore this warning. And one more thing, it's going to go and count everything in the string. So if you have like your white spaces and you execute, it's going to go and count it.
So now we have like 13. So everything going to be counted whether you have numbers, characters, special characters, spaces, everything has a space and going to be counted. So as you can see this function is amazing and there will be a lot of use cases where we use this function in our code. So as you can see we can use it in our logic to build rules in our application to prevent values that are too short and as well to prevent values that are too big.
Sometimes we don't want to store like huge values inside our database or in our application and we use the length to check the size of our data. Okay. So now let's move to the next one. Let's say that we have the following text and we're going to have like multiple lines.
That's why I'm going to go and use the triple quotes. This of course still count as a string. I just want to have it multiple lines. It's easier to see.
So now let's start with the first one.
We're going to say Python is easy to learn and I think so. Then we say Python is powerful. And the last sentence we say many people love Python. So like this. Now what I'm trying to show here is that like we have an article or a long description of something. And now we can do analyszis in Python about how many times a word appears in the text.
So now in order to do that in Python we have a special methods for the string values. It is called count. So it going to returns how many times specific value appears in a text in string. So it is very simple. Let's go and apply that. So this time we are talking about the method and the syntax for that going to be like this. The value or the variable dot and then afterward the method name.
So the method name going to be count and as well we need double parenthesis. And now inside the parenthesis we're going to pass the value that Python has to count. So for example I'm going to go with the value Python. We have here Python many times. If you go and execute it of course nothing going to happen because we are not printing. So we go and put the whole thing in print and let's go and execute. So now as you can see in the output we are getting two. So this method returns a number how many times something appears in your string.
Now you might say but wait a second we have here two but if you check the text python appears three times. Well that's because my friend Python is case sensitive and if you write it exactly like me here with the third one everything is like lower case then this considered to be no match. We are searching for Python but the first character is capitalized. So, Python is very sensitive about the upper and lower case and it will not consider this as a match. Now, for example, if I go over here and say Python where everything is lowered, I will have only one match.
This is exactly the last one. So, this is really an amazing method especially if you are doing [clears throat] text analyzes. It is one of the main thing that we do there. We count the frequency of a term or a value. And one more thing that I usually use it in order to check whether we have any special or unwanted characters in my code. So for example, I don't want to have like the dollar sign in my code. And if I go and execute it, I will see okay there's zero that means everything is clean in my code. But if you have somewhere the dollar sign like here, I can understand ahuh the code is not clean. There's somewhere special character. So this method as well helps you to check the quality of your code and to detect any issues. Okay. So that's it for this group. We have learned how to measure our string values. All right. Moving on to the next one. We have here the transformations.
So here we have two methods and [music] four operators that's going to help us in order to reshape the string value whether combining, splitting, extracting stuff from our string values. So they are very important and very useful and we're going to check them one by one.
Okay. So [music] now let's say that we have the following value. So we have like a date. It starts with the year then slash and after that comes the month then another slash and at the end we have the date. Now as we know that we have like different formats on how we format a dates and let's say that in our data we don't use like a slash as a separator between the date parts we would like to have like a minus. So actually I would like to replace those slashes with a minus. Now in order to do that we can use the method replace. So we can go and use replace and for that we have to specify two things. old value the slash this is our old value and then we're going to go and replace it with a new value which is the minus so this is new so you have to specify always those two things for the replace function and now once you apply this to our value so it's going to start searching 2 0 2 6 nothing so far but here we have a match so here we have a slash and Python going to go and take it out from the string and instead of that going to go and replace it with a replacement with a new value and then it continues. So 0 five and then here we have another slash. So same thing happen it going to go and remove the old value and then put here the new value. So as you can see it is very simple all what we are doing is replacing the old value with a new value and this is my friends very useful. All right. Now for the replace function, there are many real world scenarios and use cases where we have to use this function and I can show you more than like 20 use cases. And now I'm going to show you some common issues that we have in data and how we solve it using replace. So for example, let's have a price and it is stored let's say in string. In Europe the decimals are splitted using comma not a dot. So we have this number and of course we cannot work with this number. We have to go and format it differently. So we have to bring it to standards. So that means we have to replace the comma with a dot. So we're going to do it like this. Price dot replace. And now we have to define two things. The old value which is the comma and the new value which is the decimal dot. So let's go and execute it.
Now of course we will not get anything in the output. As usual we have to print and let's go and execute it. Now as you can see we have a dot instead of comma and of course this still as a string and we can go and format it as a float later. So as you can see we use the replace in order to bring standards to our values. Now let's have another very famous issue. Let's say that we have phone numbers and phone numbers are usually catastrophic. They are just many different styling and formats on how to write a phone number. Let's go with this one like this. Now as you can see here we have different sections of the phone number and things are separated with a minus. Now let's say that this is not the format that we work in our application. We would like to format things using slash. So that means we have to replace the minus with a slash.
And in order to do that we use our replace function. So the old value going to be the minus and the new value going to be the slash. And of course the whole thing we have to do it in print. So let's go and execute it. Now as you can see our phone number is splitted using slashes. And with that again we are formatting our value differently using the replace. Now there is like another use case for the replace. We don't have always to replace a value with a new value. we can replace the old value with nothing like for example let's say that I don't want to see any special characters between the numbers I would like to have really clean phone number just numbers no special characters no characters and we can do that so that means we have to go and replace the minus with nothing and this is exactly what we can do with the replace we can go and say as a new value nothing so as you can see it is blank it is empty and now if you go and execute it you can see in the output Python replaced all those minuses with nothing and now you have only numbers. Of course you can go and add space here again but space considered to be a character. So again you have like special characters in between but if you don't specify anything it's like we are removing a character. So it's not anymore replace.
It's like remove. So this is a trick. If you want to remove something from your values go and use replace and the new value should be just nothing. So let's have another example. Let's go back to the price and now let's have the following number. So we're going to have a dollar sign at the start then 1 comma 299 dot 999. So now we could have such a price and now I would like to convert the whole thing to a real float number.
So we have to get rid of all those decorations that we have inside the price. And of course we can do that using the replace. So now let's do it step by step. We want to get rid of the dollar sign. So the old value going to be the dollar sign and what we're going to replace it with nothing. So we are removing the dollar sign. No new values is needed. Of course, let's go and print it. So, let's go and execute. Now, as you can see, we don't have the dollar sign. But still, we have an issue. We don't need as well a comma. This is as well just a format that is not needed for a float number. Now, in order to do another replace, what we're going to do, we will add another method just at the end. So, after the end of the first replace, we're going to do another dot.
And again, we're going to use the same method replace. So, what we are replacing now? We are replacing the comma as well with nothing. So how this going to be executed? First Python going to go and get the value in the price.
Then apply the first replace by removing the dollar sign and then the whole result going to be as well applied to the next methods where it's going to go and remove the comma from the string. So now let's go and execute it. So with that as you can see we don't have any decorations. The dollar sign is away and as well the comma and this is what usually happens in projects. We have a lot of issues in one value and we keep adding replace after replace until we cover all the scenarios.
Okay friends, now time to challenge your new skills. I want you to convert this missy phone number into a clean number with only digits. So no need for all those characters like plus spaces parentheses and the minus.
And now we're going [music] to learn how to join text, how to concatenate two strings in one string using the plus operator. So now let's have very simple example where we have the first name, let's say Michael, and the last name Scott. And now I would like to have like everything in one value in one string value. In order to do that we can simply use the plus operator. So now in order to do that we're going to say last name is equal to the first value plus the second value. So as you can see it is very simple. We don't have to use any function or whatever. We just use this nice plus. Now we go and let's say print it the last name and let's go and execute it. Now as you can see everything in one value Michael and Scott. And with that we have concatenate two values or let's say joint two text.
Now of course you might say well let's have something in between the first name and the last name. It doesn't really look good. So that means we have to go and add in between like space. Space is like a string value something like this.
And then we add a space in between. So that means I'm joining the first name together with the space. And now we have to join the space with the last name. So we have to go as well use another plus.
Now we are like connecting three values first name the space and the last name.
Let's go and try it out. Now as you can see we have here nice space in between.
And if you say you know what let's have a minus instead you can have that. So as you can see this is very simple. This is how we join multiple values together using the very simple plus operator. Now I'm going to show you very common example that we use in our project for the plus operator. So let's say that we have a folder and we have here any path.
So C users bar and we have like a file name let's say it is the report dot CSV.
So now usually in projects we create different variables and so on and usually we split the folder path from the file path so that everything stays dynamic and I can go in one place and change the folder path. Now of course if I want to go and open any file using Python I have to go and combine both of the folder and the file to have the full path. And of course we can do this very easily using the plus operator. So we can say full file path and we just go and concatenate the folder with the file. Now let's just go and print it full file path. Let's go and execute.
And with that we have the full path. But we have here an issue. We are missing here a final slash. Well either you correct your folder path like this or you can go and add it in between the folder and the file. But let's go and do it on the folder and execute. And with that we have a full path of our file. So this is how we use the plus operators in real projects. [music] Okay. So now let's [music] talk about the f string in Python. So the f strings are modern super easy way to format and build strings in Python. And the f stands for formatted. So basically the f string going to help us to put variables expressions directly inside your string without have to worry about the data types in very easy and friendly way.
Okay. So now let's go and create few variables like your name. So Sam let's go with the age for example 34. And now let's go and create a boolean value like is students let's say false. Now if you want to go and put everything in one string value in order maybe to print a message we have learned so far to do it like this. So print and then we start with string for example my name is and then the value comes from name. So we have to go and add plus and then the variable name then another plus in order to concatenate the values. And now we can go and add another message like I am. Then we go and enter the age. Then another plus and then the variable age.
Then another plus and then we continue with the message years old. And then we say and student status is space. And then we go and get our last variable is students.
And maybe I would like to have at the end a dot. So another plus. Let's go and test this first. Uh of course we cannot do that. We have to go and change the data types, right? Of course, we cannot go and concatenate numbers and boolean with the string. We have everywhere to go and change the type to string. So, we're going to go and use the function str to cast our values. Let's go and try again. All right. Now, it is working. My name is Sam. I am 34 years old and student status is false. So, this is how we learn to do it so far. And you can feel the struggle, right? We have always to go and add the plus. We have to take care of the data types and it feels really frustrating right now. We will never do that again. We have new modern way in Python on how we construct a string value if we have multiple variables like this. So let me show you how we're going to do it easier and more professional. So we're going to go and say print and we're going to have our double quotes or single quotes. And now exactly before the first quotes we go and add the letter F. So now with that Python understands uhhuh now we have to go and format everything as a string. So now how we're going to do it we're going to say my name is and now after that we need the value from the variable name.
Previously we have used the plus operator in order to join or concatenate the text. Now with the if string we don't have to go outside of the double quotes. We're going to stay inside the double quotes. We don't need the plus operator. All what we need is the curly brackets or braces whatever. and we put inside it the variable name. So without having any plus or without exiting the double quotes and now we continue like normal I am. Now here about the age in the previous way we have used of course the plus in order to concatenate and as well we have converted the data type. We don't need to do anything anymore. All what we have to do is to use the curly brackets and just put the variable name inside it. Python going to deal with the rest. So Python going to go and convert the age to a string and as well concatenate it with the rest of the text. So as you can see this is very simple and after that we continue with our text. So years old and student status is and now we come to the same scenario. We have a variable where it contains a boolean data type. Previously we have to apply the function str and plus operator. We don't need anything anymore. All what you have to do is to open and close curly brackets and just put inside it the variable name. And of course at the ends we need the dots. So let's go and execute it. And with that as you can see we are getting exactly the same results. Okay. So now if you go and compare the old way with the new way you can see the new way is shorter. It is easier to read. As you can see my name is name I am age years old and so on. Nothing is disconnected using the plus operator or using this string function. So it is short, readable, super cool and looks like exactly what you want to see. So now as we go on the course, we will not use anymore the plus operator and converting data types if you want to build a string value. From now on, we're going to use the modern way on how we construct string value, especially if we have multiple variables included. And now about the f formats, we can use as well expressions inside our string. So for example, let's go and print this one. And of course, we use the f letter before. So we say 2 + 3 equal. Now what we can do we open and close curly brackets and we can go and add an expression like 2 + 3. So now if you go and execute it you can get 2 + 3 is equal to 5. So not only variables you can go and add expressions inside those two curly brackets. And now of course I know some of you going to say wait a second those curly buckets I really like them and I want to print them in the output. So how we going to do that if we are using it for variables and expressions. So for example, let's say we have a string value and we are formatting it with the f string and then you say I like this brackets and inside them there is something this is me. So now if I go and execute it of course I'm going to get an error because this is not variable this is not an expression.
So that means we have somehow to tell Python to print the curly buckets and we are not using them for variables. So that means if you want to show the curly brackets as a part of your text, you have to go and escape them by adding double brackets like this. So that means at the start we're going to have two and as well at the end. So let's go and try that out. With that you got your nice curly brackets in the output. So if you want them in the output just go and use them twice. Okay my friends. So that's all about the modern and super easy F string.
Okay. So now let's move to the next one.
[music] It's going to be exactly the opposite. We're going to go and split our string into multiple string values.
So we have here method called split.
It's going to break a string value into smaller pieces. So let's understand how this works. Okay. So now let's say that we have the following value. So we have like a first name and then minus. After that let's say that we have the age and then minus and at the end we have like the country USA. Now of course the whole thing is one value. So the whole thing is one string value. But we have the feeling that we have here inside it like three informations, right? We have the first name, we have as well the age and as well the country. And this happens a lot in real world where in some databases they're going to put many informations in one value in one string.
Now of course as we are doing analytics we would like to have those informations splitted each one of them is really important and I would like to have like different operations on each value. So that's why I would like to split this one value into three values. Now in order to do that in Python it is very simple. We're going to use the method split. So we have to tell Python what is the separator. And as you can see from example, it is those minuses between the values. So we go and pass this value inside the methods. Oh, this looks like a face. Now, now if you go and apply this, what's going to happen? Python in the output going to return multiple string values. And for that we're going to have like a special data type called list. Don't worry about it. We're going to cover everything in that data structure. So the output going to be a list of multiple string values. So now we're going to start with the first value and put it in the output. So this is the first string value. So we're going to have the first name like this.
And in the list we separate the multiple values using a comma. Then Python going to see the separator and take the value after it. And as well the same thing put it in the output. So we're going to get the age 24. So this is our second value and then a comma. Again Python sees here a separator and then get the third value and put it in the output. So we will get the third value the country as well in the list. And with that my friends we have splitted this one value into three string values. So those are three separated string values.
And if you look at this you can see there is something disappeared in the outputs and that is the separators. We don't have those values in the outputs.
[music] So if you specify the separator and the split it will not be of course included in the final output. So this is really nice. This is exactly how the split function works in Python. All right. Now, let me show you this issue that we have usually we have a stamp and let's say that inside it we have a date together with a time. So, let's say we have the year 2026 and after that we have space and then the hour. Something like this. Now, as you can see in this string, we have two informations. We have the date and time. Now, what we can do, we can go and split those two informations into two different strings.
And in order to do that, we can use the methods split. And then we have to specify the separator. So now the separator between the date and time is actually a space. So we're going to go and give Python the separator as one space. And now of course in order to see the results we have to go and print it.
And let's go and execute it. So now as you can see in the output we get a list with two values. The first one is the date and the second one is the time. And this is exactly what we need. Now let's have the same example but this time we don't have the time. And this time I would like to have each part of our date into one separate value. So I would like to have the year separated from the month and the day. Of course the question is how they are separated? Well using a minus between them. And we can do the same thing. And now we say the separator is the minus. And if you go and execute it you will get in the output three values year month and day.
So as you can see in string values we have a lot of informations and we can use the split in order to get each information in separate value. All right. Now let's go to another example.
Let's say that we have CSV file and as you know we have the values always separated with a comma or maybe a semic or whatever. So we could have like something like an ID then like a first name and then a country and maybe afterward like we have a birth date something like this and maybe as well the gender information. So now if you worked before in CSV files you will find all those informations together in one row and all those informations are splitted with the comma. Now of course what we can do we can go and split this string into matable strings using our split function. So CSV file split and then the separator here is a comma right and as usual we have a print and let's go and execute it. Now as you can see each column is separated with a value.
So we have the ID, the first name, the country, the birth date and the last one the gender. As we are handling and working with CSV files. This is as well very nice use case for the split in order to separate the values maybe in CSV files. Of course, there are many other methods on how to do that. [music] This is just an example.
Okay, moving on to the next one. We have another operator, the multiplier. Well, this one is crazy little bit and you can use it in order to repeat a specific value multiple times. So what you can do actually for example let's take this value h how and then multiply it let's say three times now you might say okay I'm multiplying a string value with a number well that's fine it's not like the plus operator this is allowed in Python and let me show you what can happen if we print this let's execute you will get haha [laughter] so if you find yourself you are repeating the same value like multiple times you don't have to do it just decide on how many times you want to repeat it like For example, maybe you find sometimes we do something like this in the logs where we say a special character like the equal and then we do it like multiple times to have a nice separation in the output. So now if you go and execute it, you're going to have like nice separations. It's easier to read if you have like huge logs in the output and you can organize stuff using this kind of separations. Now of course if you have a very long code and you are adding those separations every time you might do something like this like miss few equals and things will not looks nice. So now in order to control how many like equals you are putting the output in the logs you can use one equal and then multiply it maybe 30 times.
[music] So now I'm going to do it over here and as well here and execute. And with that we're going to have nice separation in the logs. And it is really quickly if you want to go and change your mind to say uh let's go and have hash instead of equal and execute. Now you can style your output very easily only once here. You don't have to repeat it like 30 times. So this is what happens if you multiply your string value with a number. All what you are doing is repeating these values multiple times. Well, this is the only use case that I can think of now. Okay. So that we have learned two methods and two operators. And now next we're going to learn about something special. how to extract specific part of our string.
This going to be very interesting. So let's go.
Now let's take this example. So we have a simple hello. Now we usually think about this as like one value, one string value. But behind the scenes, Python think about this as a sequence of characters. So we have like a list of characters.
>> [music] >> And it's very very important to understand that each character has a position and we call it an index. So it's like we have an address for each character. And we have two types of indexes. We have the positive indexes and the negative indexes. So let me show you what I mean with that. Now about the positive indexes, we start from left to right. So we start with the first character, the H. This has the position of zero and not one. This confuses a lot of people. The second character is one then two and three four. And now we come to something very confusing the negative indexing. We start from the right to the left. So the first character this time going to be the O. And my friend the first character will not be zero. It will start with the minus one. The second one going to be minus2 the L three and until the last character going to be the H minus 5. So as you can see the negative indexing starts with the minus1 and ends with minus5 where the positive starts with zero and ends with four and those are the indexes that are assigned for each character in this example. Now of course you might say okay why should I care about this let's say that I would like to extract only the first character and in order to do that we open and close brackets and now inside it we specify the position number and if you do that what's going to happen Python going to go and extract exactly one character at the position number zero. So you will get an H. And you can do the same task but using the negative numbers as well. The same thing open brackets put inside it the negative position and Python going to find the position of course here and you will get the same value in return. So you will get as well here an H. Now let's say that I would like to go and extract another character for example the O.
Same thing you have to go and open a brackets and then choose whether you use the positive or the negative. So if you want to use the positive you can go and use four. And with that Python going to go and check the position number four.
extract the character for you and in the output you will get an O because same thing going to happen if you use the minus one Python going to find it over here and we'll return as well the same character back so you will get an O for the minus one now let's go crazy and pick something in the middle the same game you go and use the brackets put the position number of your character and extract it for you and with that you will get the first L or of course we can go and use the negative index and same thing going to happen you will get the same character So with that my friend you can go and extract whatever you want by just specifying the index or the position of your character and you can use either positive or negative values.
Now let's take things to the next level where I don't want to extract only one character from my string. I would like to go and extract like a group of characters. And now in order to do that we have to specify for Python two informations. First the starting index and as well the end index. We have to tell Python when to stop. So we have to provide those two informations and the syntax going to be very simple. So brackets start [music] then double points and then an end. Now there is something very tricky to understand. The starting position going to be included in the output. But my friend the end position will not be included. So not included.
So let's have example to understand it.
So we open bracket. What is the starting position? It's going to be the zero right and then double point. And now since the end position is not included, we cannot go and say the end is two. We have to say the end is three. So we have to go and specify three for that. So now if you go and execute it, Python going to do the following. It's going to say okay, this is the starting position. And I'm going to say okay, we start from here. And then it's going to say okay the end position going to be three. So this is the end. And then Python going to make like a wall just before it. And now it's going to go and extract everything between those two boundaries.
So that's why you will get hell. Well, with only ONE L.
>> NO. GOD, [screaming] PLEASE NO. NO.
>> SO, THIS is what I mean that the end is not included. So, that means we will not get the character of the end position in the output. Now, let's see how this works. If you want to use the negative numbers, two brackets as usual. Now, we have to specify the starting position.
So, we're going to say here minus 5 double points. And think about it. This is the first boundary. And now, the same thing, we're going to stop over here at the second L. We need only the first three characters. And since we are stopping at the minus2, you go and specify the end as minus2. And my friend, the same thing will happen. You will extract everything in between. So you will get in the output as well. Hell with one L. So now of course the positive numbers are easier. But always you have to think of the first position is zero and the end index is always not included in the range. So this is how you can extract multiple characters from a string. All right. Now there is like another trick with the start and end where we say I would like to go and extract starting from specific position and then I would like Python to include everything as well after this character.
So I don't want Python to stop at specific position. I would like to get everything. So that means my friends I don't have to specify an end at all. So that means I would like to get this part and in order to do it it's very simple.
we say okay start from the position number one then double [music] points and after that don't say anything leave it empty so with that Python going to start from the first position and then get everything afterwards so this is really nice we are saying start here and go to the end of the string and with that we are skipping the first character now be careful with one thing if you go and say only one without double points do you remember what's going to happen Python going to go and extract only one character you will get only an e but if go and use the double points. You are saying start with the e and go and get me everything until the end of the string. Now we go to the last phase and this is going to be the most complicated one but I think once you understand that it's going to be easy. So what we can do we can specify the start position double points and then the end position and now we can go and put another double points.
So now what is going on? What is step?
This is of course an optional one. If you specify here a number you're going to tell Python how many characters to skip as it walk through the string.
Okay, let's take one example. So we start at the position zero. So we start from here and we build the first boundary and then let's say the end going to be four. So this is the end position and with that we have specified the second boundary. So if you leave it like this you will get the first four characters of course. Now if you say let's have the step as two. So now with that we are telling Python to skip every second character. Let's see exactly how this can work. So we're going to go and focus on all the characters inside the boundaries inside this range and Python going to start with the first character.
So the H. So this is the first character and it will be included in the output.
So H going to see it here. So now moving on to the second character the E. Since we are saying step two, it will be not included. So we will not include every character. We're going to include every second character. So E will not be included. And now moving on to the third character, the L. The L going to be included. And now moving on to the second L. it will not be included since we have bigger steps. We have two steps.
So it will be skipped and not included in the output. So with that as you can see we are skipping characters using the step. Now of course if you want to get all the values you can go and specify here one. So Python will not skip anything. We'll include all the four characters and this is by the way the default. So if you don't specify anything, Python is actually having the step one. So this is how the step work.
It is not that hard. Now if I look to this, I don't really use a lot the steps. Usually we only use the start and end to extract substrings. All right.
Now let's practice in Python directly.
Let's have this word Python. Now let's say that I would like to extract the first character. In order to do that we're going to apply the index. So we open and close brackets. And now we have to use either the positive or the negative. So I'm going to go with the positive one. And actually that's it.
The syntax is very simple. And then we print it like this. And now let's go ahead and excuse it. So as you can see we are getting the P. And of course if you want to go and use the negative number you have to count. So we start from the right side. Min - 1 - 2 3 4 5 6. So minus 6 like this. Now if you go and execute it you will get the same results. Okay let's move on to the next one. Let's extract the last character.
So print text the same thing. Now we have to choose a positive number. So the last character is the n. So we have to count. So 0 1 2 3 4 5. So the positive number is five. Let's go and execute it.
So let's go and execute it. With that we are getting the last character the n.
Now what is the negative number to get the n? It is the first one and the first one is minus one. So let's go and execute it. The same results. Now let me just put it like this. So here it was zero.
It was the five. Now let's go and extract let's say the h. So extract H.
Now the positive number we start from zero. And let's test the dots we have an H. And the same thing if you want a negative number for the dots we start from the right side - 1 2 3. So as you can see it is very simple. We call this technique as indexing. Only one character from a string. And if you want to get multiple characters part of the substring, we call this technique as slicing. All right. Now let's have an example about the slicing. Let's say that we have a date and we have the year, the month and the day. Here again we have multiple informations in one string and I'm interested only on part of this string. So let's say I'm interested only on the year. So now we have to go and extract only the year from this string. So let's go and do that. So extract the year. So we're going to use our variable date and then open and close brackets. This time we're going to do slicing. And you have to specify the start and the end. So what is the starting point? It is the first character. So we start from the two.
That means the starting point is zero.
Then double points. Now we have to specify the end. And don't forget the end is not included. So 0 1 2 3 4. Four is the minus and minus is not included.
That's why we're going to specify it as an end like this. So let's go and print it as usual and execute it. So as you can see with that we got the first part the year. It is simple, right? It's not that hard. Now there is like another way on how to do the slicing for this. Since we are starting at the position zero, we can go and only specify the end. So there's no need to say zero like double point. We can just leave the start as an empty and as a default it's going to be the starting point zero and only specify the end. So with that I'm saying the end going to be the minus here and get me everything before it. It is just a lazy way and you will get the same result. So let's go and print it and you will see we'll get the same value. So if you find yourself starting always from the zero, you can go and skip it. Okay. Okay. So now let's move to the next one. We want to extract guess what the month. So of course guys what I recommend you is that if I'm giving task like this you don't have to sit and watch and just see their solution. So just pause it think about the numbers and check whether I have the same solution. Now how we going to do this? We're going to have as well dates.
Now what is the starting point of the month? Let's go and count. So 0 1 2 3 4 and five. So the first digit from the month going to be at the position number five. And then we have to specify the end. It's only two characters. So 5 6 7.
The seven is not included. That's why it is the end here. And we can go and print it. So let's go and check that. And with that we are getting the month. Of course you can go and use the negative numbers.
And there is no shortcut in order to get the month. Now let's go to the last one.
Extract the day. Let's check the numbers. Now what we said at the zero we were at five. So 6 7 8. So we start at H and then after that double point. And now we don't have to specify the end. We want to get everything afterward. So we don't have to specify the end. We just leave it like this and let's go and execute it. So that we are getting the day. So now of course the question is when do we use positive and negative numbers? Well, this is my recommendation. If you are extracting the parts on the left side at the beginning of the string, it is easy to use the positive numbers because you just start counting from the left side.
So you will have something easy like this. Four, zero. You don't have to count a lot of stuff until you reach the starting position. But now if you want to extract something at the end at the right side easier to use the negative numbers because we start counting the negative numbers from the right side from the end of the string. So that means for the year I'm going to go and stick with the positive numbers because it is on the left side on the start. But for the day I will use the negative numbers because as you can see here we have eight. We have counted a lot of things until we reach the day. But it is way easier if you go and use the negative numbers. So for example, what is the starting position? It is minus1 and then minus2. That's it. So all you have to do here is to say minus2. I don't have to go and count like eight characters. And what is the end is again like the rest. I don't have to specify anything. So if you go and print this, you will get the exact same results. So if you want to extract something from the left side, use the positive numbers.
If you want to extract something from the end, use the negative numbers. It's way easier. And now you might ask, okay, what about the middle? Well, it depends.
If the middle is close to the left side, I would go with the positive. But if the middle is like closer to the right side, I'm going to go with the negative numbers. So that's it. This is about the indexing and slicing. All right, friends. So with that, we have covered everything about the transformations that we have in order to reshape our string value. Now we come to the most important group. We can learn how to clean our string values. So previously as I said string values are text and text usually in real world has bad data quality and we spend most of our time like cleaning up the data before doing anything and those are your tools in order to clean up the string values and I have splitted this group into two subgroups. The first subgroup is all about how to clean up the white spaces.
This is very commonly thing that you have like evil tricky spaces in your text and we're going to learn how to clean that up and in the second subgroup we're going to learn how to clean the cases. So let's start with the first subgroup. How to clean the white [music] spaces. So let's go.
All right. So now let's say that we have a normal value like max. This is totally fine. We don't have any white spaces from left or from right. We have on the left side entered a white space. So now of course this white space is really bad and we would like to get rid of it. And since it is on the left side we can use the method left strip. So now what going to happen? Python going to go and trim this white space from the left side and remove it completely. You have again the original value without any spaces from the left side. Now let's say that we have the scenario on the right side where we have here white space. So this time we have it from the right side. And of course this is really a bad thing. We don't need usually like spaces at the end of any value. Now to fix it we have the opposite function write strip. And if you apply it the same thing going to happen. Python going to go from the right side and remove any white spaces from the right side. And with that you will get again your original value without any spaces from the right side.
Okay. Now we're going to come to the scenario where we have from the left side and as well from the right side white spaces. So of course you can go and apply both of the methods on this value. But we have another methods called strip. So without left or right without specifying left and right. And if you apply to this value it's going to do exactly both of the job. So that means Python going to go and clean the spaces from the left side and as well from the right side. And with that you will get again your clean value without any spaces from the left or from the right. So my friends, if you want to remove the white spaces only from the left side, use the L strip. If you want to remove only from the right side, use the R strip. If you want to remove both of the sides, then use the strip. So this is very simple and nice. All right.
Now let's go and practice. We're going to have this example where the user going to go and enter a value like engineering. We don't have any unwanted spaces at the start and at the end. So we don't have to do anything. But now let's say that our user did add a start by mistake. Hit and enter and then the value engineering. So now with that we have data quality issue. We have unwanted spaces and this can cause a lot of problems later. We have to go and clean this up. So what we're going to do since it is on the left side we can go and use the method left strip and with that we are removing this white space from the left side. Of course, we can go and print the value to check the final results. So, let's go and do that. As you can see, we have now engineering and at the start we don't have any white space. Now, let's have another example where the user going to say now let's move on and let's say that now this time our user going to go and enter engineering but at the end by mistake type space and in order to solve it since it is on the right side we're going to go and use right strip. So, this time we are removing unwanted spaces from the right side. We go and print it. So let's go and do that. You will see as well we don't have any spaces from the right side. All right.
Now we can move to the last scenario. I think you already got it. At the start by mistake we have a white space and as well at the end like this. So we have unwanted spaces from left and from right. In this scenario we can go and use the strip in order to remove everything from left and right. Now if you go and print it you will get the same result engineering without any spaces from left or right. And with that we improve the data quality. And now of course here the best practices if you are getting any value from your users using the input function. You cannot expect whether the space going to be at the start or at the end. That's why always go with the strip function just to make sure you are cleaning all the white spaces from the left and right. Of course this method is going to go and remove multiple spaces. So let's go and add few spaces here. So we have here like five spaces before the engineering.
If you go and execute it, you will see it's still going to go and remove all the white spaces from the left side. Now one more thing let's say that we have this value data engineering. So now as you can see the space now in the middle in between it's not on the left not on the right. If you go and apply the method strip and then print text. So let's go and highlight it and execute. You will see in the output we still have the white space. So this is very important. The strip method will not remove the white spaces inside your text in the middle. It's only going to remove the spaces from the left and the right. And of course, we have other method in order to remove the space in the middle. And by the way, those methods will not only remove the white spaces, it could remove anything that you want. So, for example, let's have this text hash hash and then a b c and then three hashes. Now, we don't have any white spaces. You would like to go and remove those special characters at the start and at the end. We still can use the strip function in order to do that. So, we're going to go and use again the strip, but this time we're going to pass a value for this method.
This is the special character that we want to remove. So that means if you don't specify anything inside the method between those two parenthesis, it's going to be always the white space. But if you specify special character or any character here, then Python going to go and remove those characters from our string value. So if you go and print it that text and execute now, you can see the output our string value don't have at the start or at the end any hash value. Okay. So now I'm going to show you something that I do a lot in my projects. Now let's have this value like engineering. Sometimes we have a lot of values and it's going to be really hard to understand whether we have white space or not. I'm checking just the quality of my data. Now it's going to be really hard by just looking to the data to see ah is there any space here or not. We can go and test that using the following methods. So what I usually do is that I go and print the length of my data. So if I go and execute it, I can see okay there is like 12 characters. I will not go and count now anything.
Don't worry about it. It will not be possible if you have like huge data. But now instead what we can do we can say okay print the length of the same value.
So text but this time after applying a strip methods. Now if you go and execute it and now by looking to this you can say ahuh we have one white space. Of course if I go and add like here two white spaces and execute it I can immediately calculated a 131 that means I have two white spaces. So this is really amazing technique in order to check how many white spaces, how many problems do you have inside your data.
Now you can do very nice stuff like this. We say the length of the original data minus the length of my data but after cleaning it up. So let's go and execute it. Now you can see in the output we have two. That means we have two white spaces. Or you can do something like this where you say equal equal and then execute it. You will get false. So that means that text is not equal to the same text but after cleaning it up. Now if everything is correct you should get here zero and you should get here true. Now of course we can make this a little bit nicer where we say number of spaces equal and then we put this formula here another one we say is clean and we put the second formula over here. Now we go and print something nice. Number of spaces, comma number of spaces and horror is data clean and as well here the second variable. So let's go and execute it.
Now you can see very nicely number of spaces is two. And is my data clean?
Well, no. So this is the trick. This is how I usually check the quality of my data. So if I go and remove now all the unwanted spaces and execute, you can see the number of spaces is zero. And is my data clean? Well, true. So as you can see, this is amazing, right? We are combining now the power of the built-in functions >> Avengers >> like the length together with the method of string the strip in order to quickly check the quality of your data, the string values without you having to look is there any spaces left or right. So no need to do that. So we have now a really nice automated program to check my data for me. Okay, so that's all for this group. We're going to go now to the second group. We're going to learn how to clean the cases of our string values.
So [music] let's go.
Okay, so [music] now let's have the following example where we're going to say the user went and enter this value like this Python. So everything in lower case and afterward we have programming where the user enter everything in upper case. So now of course this doesn't look really nice and sometimes we have rules in our project where we say everything should be in lower case or upperase. Now let's say that I would like to go and convert this value to lowerase. In order to do that we can go and use the method lower. It's going to go and convert everything to lowerase. So let's go and try that out. So we're going to say text dot the method lower. And now let's go and execute it. You can see Python programming and now programming is in lower case. And the first value Python it is anyway lower case. So nothing going to happen. Now let's say that in our project we would like to see everything in uppercase. Then you go and use the opposite where you going to say text dot and then the method upper. It's going to go and convert everything to an uppercase. So let's go and execute it.
Now this time we're going to see the opposite. Python now is an uppercase.
Initially it was lowerase. So it is converted to uppercase. For the programming nothing happened because it is already an uppercase. So as you can see those two functions going to bring like standards to your data. you are like cleaning up the data right now.
There are like many use cases for those two functions but the one that is very famous is we use them in order to clean up the data before searching. Let me give you this example. So now let's say that the user is searching for this term email and in your data you have as well email but in different case everything is lowered and the user just capitalize the e at the start. So now in searching we don't have to be very sensitive about the values whether the user write it capitaliz or like lower case we want to find a match whatever the case is but of course the values in Python are case sensitive. So Python going to say oh those two values are completely different. So if you go and print for example search is equal equal to data and execute it. As you can see Python going to say false they are not equal.
Now of course we don't want that. We would like to have it as true. What we can do is that we go and convert both my data and the search term to one case. So for example I go over here and say and as well for my data I'm going to say lower. Now it is already lowered but let's say that we have here a big a we don't know whether everything is lowered in my system. Maybe in our system we did not store everything in lower case. So just to make sure everything is matching. So now if you go and execute it you will get true because now we are comparing the exact same values with the exact same cases. Now of course to make this perfect we have as well to think about the white spaces. Let's say the user is searching for email but at the end he enters a space and maybe in our data we don't have like high quality and we have a space at the start. Now if you go and execute it you will get false and we don't want that as well. It's still the search should find the data because it is same word email. So what we can do we can go and apply our second methods that we just learned. So strip it doesn't matter whether it's before or after and the same thing for our data.
So if you go and execute you will get true and with that we are cleaning up the data twice. So we are converting the cases to a standard case the lower case and as well we are removing the white spaces from left and right. So this is how you clean up your data before matching it. So now slowly you are getting the feeling how powerful are those functions and mythos right let's move to [music] the next one.
Okay friends, now time to challenge your new skills. And if you are able to solve like 80% of it, then it's going to be amazing. So now we have this messy string that has a lot of informations. I want you to clean this up and make it more organized in one string. So here we have a name, ro and age. I know this one going to be challenging. So now go pause the video.
All right, my friends. So as you can see this group [music] is very simple but yet very powerful. And with that we have learned how to clean up the string values and the text. This is very important. Now moving on to the next group. Now we're going to learn how to search for something. And here it's important to understand this [music] group will not go and modify anything in your string values. So all what is happening here is we are searching for something and we need answers. So the original values will not change. We will just get answers. [music] So let's check this group.
Okay. So now let's see that I have the following value. So I have again a date where we have a year then a month like February and then a day. Now of course this is a string value and as you can see as I'm writing it from left to right we consider this to be the start and then the end of the string going to be of course the end and then we have here everything in between going to be the middle. Now again our string values contains a lot of informations. Here we have the year, the month and the day.
And now I would like to go and search for specific information. Let's say that I would like to go and search for the first part the year. Now I want to check whether my values always starting with the 2026. And for that we can use the method starts with like this and then we have to give it the searching value. So in this example we are searching for 2026 like this. So now once you execute it what going to happen? Python going to check okay we are talking about the star. So we're going to start from the left side and then it's going to say okay we need here like four characters.
So it's going to start counting 1 2 3 four like this. And now all what's going to happen is that Python going to go and compare this value to the one that you are searching for. Now of course it is matching and that's why in the output you will get true. So as you can see we are just checking. We need only answer yes or no, true or false. So that means the output of this methods will not be like another string value. It's going to be the result of the check. So it's going to be boolean. Now let's say that I would like to go and check the end value of my string. So I'm checking the day the last two characters. So now since we are searching from the end we're going to use another method the exact opposite of the start. So it's going to be ends with like this and as well we have to go and pass for it the value that you are searching for. Let's say that this time you are searching for the value 30. Now once you execute it Python going to check okay we are talking now about the end. So that means Python has to check the end of the string and then it's going to go and check how many characters we are searching for. It is two. So it's going to start like this from the right side and then count two characters until here. Now Python going to go and compare the data together with the searching value. Now as you can see it is not matching that's why you will get no and no in pulon going to be false. So now as you can see those two methods are really strict where they always have like starting position and then they start searching for something the same thing on the left side on the start. So it has starting position but sometimes this is very restrictive and I would like to search for a value anywhere and I don't care whether it is at the start or at the end. It could be anywhere in my string. So now my friends for that we don't have like specific method use the operator in so this is not method this is an operator in order to search for specific value in a string and in the output you get like yes or no true and false. So you say is February for example in our string. So Python going to start searching for this combination starting from the left and cover the whole value and of course in this scenario Python going to find the combination inside the value and in return we will get true. So as you can see it is not very restrictive like the start and the end if there is a match anywhere then we will get a true. So now my friend those two methods and as well the operator they are just checking if a data exists. But now sometimes we want to search inside our data to find where specific like value starts. So we are searching for specific position not for an answer yes or no. And for this kind of search we can go and use the methods find and it is very similar. We pass to it the value that you are searching for.
So let's say again we are searching for the three character of the month. Now if you go and apply it what can happen? So Python going to start from the left side and start checking the index or the position. So we have 0 1 2 3. So far we still don't have any match. And then four. And now comes something very important. The position number five.
This character is matching the first character in our substring. And now it is very important that all the following characters must be matching. And of course everything is matching in this example. And now the question is what we will get in return. we will not get like yes and no true and false we will get the starting position of our value. So that means in the outputs we will get an integer. So we will get five. So this is the starting position. So this is exactly how the find method works but of course for a different purpose compared to the others. So with that my friends we have learned four different techniques how to search for a substring in a string. All right. Now I'm going to show you how I use those functions in my real projects. Now again we have the same example with the numbers. So here we have like many informations like the country code then we have the number itself. So now let's say that I would like to check whether my phone number is a German one. And in order to do that we have to check the country code at the start. So we have to search for the combination plus 49. And now I'm going to ask you which methods we going to use. Well since we are searching at the start we're going to use starts with.
And now we have to tell Python what you are searching for. We want this combination plus 49. So let's go and check the result. Now as you can see it is true. So yes my phone number is a German one. And if we have here some other country code and executed, you will get false because there is no match and the first three characters is not matching what we are searching for.
That's why we are getting false. So this is really nice use case. Another one let's say that we have an email and we have something like this. Let's say bar atgmail.com.
Now I would like to go and make a check whether the email comes from the domain gmail.com. So that means we have to search for this domain. So gmail.com and since the domain is always at the end of the email that means we have to use the method. I think you already know it ends with and here we have to pass the domain name. So gmail.com you can add the at or not it doesn't matter. And then we have to go and print. So let's check that with that we are getting true. So that means this email is using the domain gmail.com and if you are using something else like for example outlook and execute it you will get false. So this is a nice use case with the endsw in order to check the domains of an email.
One more nice use case just quickly let's have a file and we have the data backup CSV now I want you to check whether the extension of this file is a CSV. Now again if you look to this send CSV at the end. So that means I have to go and use the ends with and we search for the extension dot CSV and as usual print then execute. So yes my file is using the extension CSV. So this is really nice in order to validate the file names in your system. All right. Now let's have another example about the emails.
Now if the user is like giving us their email address we have to check for something whether the user is giving a valid email. And one very important character in our emails is the at. So we have to check whether the emails are valid and they contain an at somewhere in the email address. Now since the at is somewhere in the middle, we cannot go use start with or end with. We have to go and use the operator in. So we can do it like this. We have now the value that we are searching for in double quotes and then the at and then after that the operator in and then the value that you are searching inside it. So it is the email. Now of course if you go and execute it you will not get anything in the output. We have to print as usual.
So let's go and execute. Now you can see we have true. So yes in our email we have an at. Now if I go and remove it from here and then execute you will see it is false. And this is really nice indicator that this email is not a valid one without an at it is not an email address. So this is really nice use case for the in operator to validate the emails. Another example that I used last time. And let's say that we have this URL where we have the https double point slashes and then api dot say company.com then version one data something like this and I was validating the URLs whether they contain the API. So I wanted to detect whether there is like an API call or not in the URL and I have solved it like this. So I said I'm searching for / AI and then the operator in our URL. Let's go and execute with that. Well yes. So we are getting true.
There is an API call in this URL and I used it in my logic in order to do something. So this is really nice, easy and very useful. Okay. So now let's have a real example about the find. And here I have to tell you something that find is always combined with some other task.
So it is like assisting us to do another thing. Let me show you what I mean exactly. We have this phone number. So this is the first one. And let's have another phone number. So phone two. And this time we're going to have it like this like 4H. then some other number.
Now I would like to get only the phone number without the country codes. So I would like to extract a part of the phone number and I don't want all those stuff the plus 4 8 minus. Now we can do that of course using the slice. So for the phone number one we can go and open two brackets and we start counting like 0 1 2 3 and four. So we start with the position number four and then we want to get everything else. So we don't have to specify an end. Let's go and print it and then execute. So that we are getting the first phone number. Okay. So now let's go and extract the number from the second phone. Again the slicing. Now here we don't have a plus. So we cannot start from four. We can start from 0 1 2 3. So we're going to start from three and the rest. Let's go and execute it.
So that I'm getting as well the phone number without the country code. Now of course this is annoying, right? For each phone number I have to go and start counting and the data is not clean.
Sometimes it start with a plus, sometimes without. Maybe in other cases we have a third phone like this where we have two zeros. So the starting position is always like different and I am lazy.
I don't want each time to go and count.
Now we need an assistant. We need another methods to find for us the starting position. And my friends we can go and use the find function. So let's see how we can do that. I would like to find the first minus. So it's very simple. We say phone one dot then find and we are searching for the first minus. So let's check what we will get and then execute. So as you can see we are getting the position number three which is totally correct. 0 1 2 3. Now this number three is not the starting position for our slicing. We want to start from the next character from the position number four. Well it's going to be always like this. It's always like + one. So let's go and test that. Now instead of four we're going to go and completely replace it with the find. So now instead of having static number the four we are having the output of the method find. So as you can see now things are not static now things are more dynamic and we are nesting stuff together. So again this is the start and then double point and we don't have an end. Let's go and execute it. And here as you can see we have an issue that we have here a minus. Now all what you have to do is to go over here and say + one.
So 3 + 1 you will get four and you will get again the exact same starting position. So let's go and execute it.
Now we got the same result. So 176 this is our phone number without us at all counting the positions. So now it is way smarter than before. Of course it is little bit more complicated but it is dynamic. Now let's go and take the whole thing and replace the three with our smart code phone two. This is the issue of copy and paste. So we are finding now in the string two character minus. So 0 1 2 2 + 1 you will get three. It should be fine. Let's go and execute it. Now as you can see we are getting as well the phone number without specifying any static value for the starting point.
Totally smart. Now let's go and test it for the phone number three. I'm just going to go and copy paste and let's go and say phone three. Phone three. So without counting without checking anything I'm going to go and execute it.
And I'm pretty sure I'm getting the phone number. So you can see things are a little bit complicated but if you do it step by step and you understand each step what you are doing it will be very easy to make smart thing and this is exactly how we use the find in order to find the index or the position of a specific character and then we use this information to pass it to another task.
So this is why we have find and how we use it. Okay. So this is how you can search for things in Python. Now we're going to move to the last group. We will learn how to check and validate our value. We have here two methods in order to check and validate the content of our string. So let's [music] check this group.
Now all what we [music] are doing here is we are checking and validating we are checking the quality of our string values. So now sometimes we have like expectations on our values and we would like to check these expectations like for example we make some expectations that the country should be always characters. So we should not have any numbers or any other special characters.
So for that we can use the method is alpha. It is very simple. It can go and check whether the string has only letters. So it is only made of alphabetics. So we can go and validate our country by using the method is alpha like this. We don't have to pass any value for the methods. And then of course we're going to go and print it.
So let's go and check. Now we are getting true. That means the value of the country here has only letters. Now of course let's say the user give the country but at the end like we have a typo. Now let's check whether it's going to pass our quality check. Well no we have a false it means this is not a valid country for us and there is like something wrong inside it. And the same thing if you have like any special character like hash and if you go and execute it as well you will get false.
So we are expecting only alphabetics only letters. This is really nice if you have some expectations on your values to have a quick check on your data. Okay.
So now moving on we have another method.
This time we're going to check whether the string value has only numbers. So it contains only digits. It has no characters or any special characters. So again our nice example with the phone and here we have the phone number. Now I would like to go and check whether we have a clean phone number without any characters. So we can go and apply our nice method is numeric and print. So let's go and execute it. So as you can see we are getting true because the string contains only numbers. But now let's say that we have here somewhere a minus a special character and we check we will get false because of this special character. It is not a number.
So if your string has only numbers you will get a true. And one more thing about the is numeric. If you have something like this it will not be accepted. So I know this is like float number but the dot considered to be a special character and it will not be accepted. We are talking only about numbers. So as you can see those methods are really amazing in order to check the content of your string and to validate your expectations and with that you can build quality checks and gate in order to prevent having let's say garbage and bad data inserted to your application.
So with that you can check the content of your string values. Okay my friends so that's all for the last group and as well that's all for the road map on how to do things to our string values. So now you are equipped with a lot of tools. Now you know how to convert the data types. How to measure and do calculations on our string values. How to transform and reshape your string values with our combining, splitting, extracting and as well how to improve the data quality by cleaning up the data and bringing the data into standards.
And we have learned as well how to search for specific thing in our string.
So if you are building any search functions in your application, I'm sure you will be using those stuff a lot. And the last one, how to check and validate the content of your string. So with that you have gained skill now about something very important in the data world that you might hear. You might hear someone saying ah I'm doing data transformations or data manipulations or we are building pipelines to clean up the data. So actually all what they are doing they are using those functions to prepare the data for analyzes. So as a data engineer or a data analyst you will be using those stuff for sure. Okay. Now looking back to our road map with that you have learned all the tools in order to work with the strings. But of course not everything is strings. we have as well numbers. So that's why we have the next chapter where you're going to learn as well the tools in order to work with this type. So let's dive in. By the way friends, if you'd like to have an access to all those sketches and my notes for Python, you can find my Python handbook.
There you're going to see around 200 pages only sketches and notes about the course. You can check the link in the description. Now let's go back to the course.
So let's start with the first question.
What are numeric values in Python? We have learned that the numeric values or the numbers they are just a data type that belongs to the primitive data types or the single values. The numeric data like exist in any programming language.
So once you create a number, Python has to treat it as a number. And in Python, we don't have like one numeric data type. We have different ways on how we represent numbers in Python. The first data type we call it integer. This is the most famous one. So here we have a whole number. No decimal points. Just a whole value positive or negative. Like for example 5 - 12 10,000. All of those are whole numbers. They are integers.
And now we have another data type called a float. They are just numbers with decimal points like 3.15 -0.5 100.0. So that means we have always a decimal point and decimal numbers after the points. Well, we use this format in order to represent for example percentages or as we are calculating the average or measuring the temperature. So floating are awesome when the details matters of course. Now moving on to another data type called complex. So they are like numbers with two parts real number and imaginary parts. Like for example 2 + 3g. Well I know that sounds a little bit complicated but don't worry about it. Most of you will not use it. This is only for advanced math, engineering, science, physics, and Python supported in order to do some advanced scientific work. So those are the three numeric data types. And each one of them has a job. And my friends, we have a lot of functions and operators that we could use in order to manipulate the numeric values in Python. And like the string data type, I have grouped those functions and operators under categories and purposes. We have here like four built-in functions that we could use in order to check and convert the data types. And as well we have many operators in order to do the basic math in Python. So the usual stuff the plus, minus, multiply and so on. Then we have very important group how to round numbers. Those functions are the most used one if you are dealing with numbers. Then after that we have advanced stuff. If you want to do advanced mathematics you have here as well those methods the sign, cosine, log, pi and so on. And then we have another group in order to generate random numbers in Python. This is as well useful one for projects. And the last group we have a method and a function in order to validate our numbers. Now about the coloring again those red ones they are the built-in functions in Python. The yellow one are the operators. And now if you look to this we don't have here like methods belongs to the class number in order to manipulate the numbers like we have in the string. In the string there are a lot of methods of the class string in order to change the value and the data.
But here we don't have any but instead we have a lot of green stuff. Those are functions that are not built-in. Those are from the module math or random. Do you remember in the data types intro as we talked about the libraries and the module we have understood in Python there is like standard library and then inside it there are modules. So far we have used a lot of functions and methods inside the built-in and here we have the functions like the type print length and as well we have the methods that belongs to each data type. You remember in the string we have the upper, lower, replace and so on. And the same thing for the integer but we don't have like important stuff. Now in Python in order to do advanced mathematical stuff with the numbers we have to go and import a module called math or random. And there you can find very useful functions for the numbers. And again the big difference between this module the math and the built-in is that each time you are using those functions in this model you have to go and import it. This will not happen automatically. You have to go in your code and say okay import for me the mathematical module otherwise you cannot use the functions underneath this module. We didn't face this issue because Python thinks the string function is very important and we're going to end up each time like doing some manipulations for the text. That's why we have it here everything underneath the string. And for the integers we have only the basic operators. But if you want to get advanced stuff you have to go and import it. That's why we have here a lot of functions from another module the math and the random. So now what we're going to do, we're going to go and check those functions and operators. And we're going to start with the first group, the types. So let's learn how to check the types of the numbers and as well how to do data type conversions. Let's go.
Okay. So now let's start with the basic stuff. We're going to go and create a number like for example an integer. So since we don't have any decimal number, this going to be an integer. Now let's go and create another one. This one going to be a decimal number. So it's going to be floats. So 5.7. Now we are talking about another data type. This is not anymore integer. This is a float.
And the crazy one we have the complex data type. For example, 2 + 3g. So now those are our values, our numbers. And now we can do something about it. Like for example, we can go and print the type of our value. So for example, let's type the first one and execute it. You can see it is from the class integer. So it is working fine. Let's go and print the type of the second value. So let's go and execute. You can see it is a float because of the decimal points. And let's go and print the type of the last value and execute. You can see it is from the class complex. All right. So this is the first thing that we can do to our numbers. Let's move to the next one. It's all about how to convert the data type to numeric data type. Like for example, you might be in situation where you have a value like this where you have everything between two double quotes and you have a number. So now if you go and print the type of this value, you will get a string. And since your number is a string, Python going to hand it as a string. Like for example, if you say I would like to go and multiply my number, my value like three times. If you go and execute it, Python will not go and multiply the number itself.
Python going to go and repeat the value three times. So that means Python handle this as a number and I don't want that.
I would like it as a real number, not anymore as a string. So what I can do, I can say X and then we can go and use the int the built-in function and pass it this value. So now if I go and print the type it will looks like this. So this time we have it as an integer and if you go and multiply it now for example three times you will get 72. So this time Python is handling this value the 24 as a number and multiply it correctly as a number not anymore as a string. Let's have another example. Let's say that I have a floating number like 3.14. Now this is a float and I can go and convert it to an integer to a whole number. So for example I can go and do it like this. then we can say integer the variable x. So if you go and execute it, you will get only three only the whole number and you will not have any decimal numbers. So this is one way in order to get rid of the decimal numbers if you want. Now the same thing if you have like a number like three. So this is of course an integer you can go and convert it to a float. So we can go and use the built-in function float and give it our value is then execute. Now as you can see it is still three but we have here a decimal point and then zero. So that means this is not a whole number this is a float number. So you can convert between integer and float easily using those built-in functions. And as well the same things if you have like a string value let's say4.
So this is string value and Python going to hand it as a string. But if you want to go and convert it to a float you can go and use the float function. So let's go and execute it. So now it's not anymore a string value. It is a float.
So again this built-in function going to go and convert any value to a float data type. Now about the complex data type you need two values. Let's say we have x is equal to three and y is equal to four. You can go and say complex and then go pass to it the x and y. So x is the real parts and four is the imaginary parts. So now if you go and execute it you will get a complex data type. So again it is for advanced scientific stuff. So my friends that's it for the first group. So it is very simple. And now moving on to another very simple group. We have the math operators as well. This is something that you can do in order to manipulate your numbers. So let's go.
All right, let's go quickly through all those operators. I know this is a little bit awkward. We are doing those simple operators, but we're going to go through them. So the additional operator 2 + 3, we have five. And let's move to the next one. 5 minus 3. Guess what? Two. And the next one, the multiplier. Four multiplied with two, eight. And the next one, the division. So we'll divide 7 by two and in the answer we will get float number. So 3.5. So now sometimes it might be really annoying to have those decimal numbers. So I would like to go and divide two numbers quickly and the result I would like it to have it as an integer. So now after it is divided it should be like rounded. And of course we can do that in Python. If we divide two numbers using division we call it floor division. It's actually not round or ceiling. It is floor. We're going to learn about those mechanism later. So now all what can happen here? We're going to have three. So we will not get floating point. It's going to return [music] an integer and the numbers are like rounded. Now we have another one which is really nice. If you use the percentage between two numbers, this operator going to return the leftover after the division. So now in the output you will get the leftover after dividing seven by two. It's like this like we have three groups. So the total of this is six and we have one left over that does not fit with the two. Right? And now you might ask when we're going to need this type of operator. Well, usually we use it in order to detect whether our numbers are even or odd.
Like for example, if I go over here and say 10 and execute it, you will get zero. So that means 10 is an even number. But if you have an odd number, you will get one. So this is really nice operator to detect whether your values, your numbers are odd or even. Now moving on to the last one, we have the exponents two and then twice the multiplier and then another number like three. So the first number is the base and the second number is the exponents.
It tells you how many times you want to multiply the base number. So if you go and execute it, you will get eight. So all what we are doing here, we are multiplying the two three times. So as you can see, be careful whether you are using one division or two divisions. It has different meanings for Python. And as well here, be careful with the percentage. It is not division. It is a reminder. Okay. Moving on, we can talk about the shortcuts that we can use for the operators. Now let's say that we have the following scenario where we say we are creating variable and inside it the number two. Now after that we might go and add to this value like three.
Usually we do it like this. We say x is equal to the current value of the x then plus three. So now as we learn python going to go and grab the value two add to it the three and then assign everything to the x again to itself. So that means x now we're going to have five right. So if you go and print it, it will get five in the output. Now for exactly for this scenario, we have a shortcut in Python where we can say okay x is equal and then we say before the equal we're going to assign the operator. So I'm saying I would like to add something to the current value of the x and then afterward we say the value. So now those two are doing exactly the same thing. It is just shorter and I don't have to repeat like twice the x. I'm saying for Python go and add the value three to whatever the current value inside the X. So now if I go and comment this out and then execute you will get five. So this is like a shortcut. If you want to do a mathematical operation on your variable you don't have to repeat it twice. Just put the operator that you are using before the equal and then the value itself and we can go and use any operator of course. So we can say minus equal then let's say one print X again then execute. Now it is four and you can go as well multiply it with two and then of course prints you will get eight. So this is way nicer than saying x is equal to x multiply with two. So as you can see this one is longer than the previous one and this is just the quick and nice one. Okay my friends. So that's all about the basic math the basic operators that you have in Python. Now moving on to the next one. This one going to be interesting. We will learn how to round numbers. [music] So let's go.
Let's say that [music] I have this scenario. I want to measure the distance between two points. The first point is two and the second one is 10. Now if you go and print the distance between those two points, you will get minus 8. Now if I want to get rid of the sign, so that means I'm interested only on the absolute number. So now in order to do this in Python, we have the built-in function APS. It is very simple. It's going to return the absolute number without having any minus sign. So let's go and apply it. It's very simple. APS and then put the whole thing inside it.
Now let's go and execute it. You can see now I get rid of the minus sign and I have the absolute number. This is very practical if you want to measure the distance or the size of something and you are not interested whether it is like positive or negative. So this is only dedicated for numbers. Okay. So this is very nice and simple function.
Now moving on to the next one. We're going to talk about rounding numbers. So now as we learned before in the text and the string values they might be missy a lot of like problems and data quality issues. Well not only that our numbers could be as well bad like for example let's say that we have a price and we can have like some crazy stuff like 35 and then we have a lot of numbers after the decimal and this is of course could be the output of some complex calculations or someone in your application did add those complex numbers. So this is not friendly, hard to read and so on. And I would like to make it smaller and reduce maybe the number of the decimal numbers. Now in order to do that we have multiple options and now we're going to learn them one by one. Okay. So now let's say that we have two whole number the one and two and in between them we have decimal numbers. Now if you have a decimal value between those two we call the two as the ceiling and one as a floor. It's like your apartment. You have a ceiling and floor and you have to decide whether you want your values to go to the floor or to the ceiling. So for example, you have the method floor and your decimal number is 1.3. The floor method going to go and drop down everything to the floor. So the 1.3 going to go to the floor one. And if you have like another value exactly in the middle 1.5, this value going to go as well to the floor to the one. And even if you have 1.7 which is very close to the ceiling, if you apply the floor, it's going to go as well to the floor.
So floor means down to the ground. You always round down even if you are just about to reach the ceiling. Now let's switch to the exact opposite the ceiling. Now as the name says it's going to take you to the ceiling. So again the same examples if you have 1.3 this will jump up to the two to the ceiling. Now if you have in the middle 1.5 it's going to jumps you up to the two. And the last one the 1.7 this as well going to take you to the ceiling to the two. So if you are optimistic you're going to go and use the ceiling where you're going to lift everything from down to top. Now we come to the third one. This is the built-in function round. And of course Python decision is based on which whole number you are closer to. So for example the 1.3 this one is close to the floor.
So that means Python going to round it down to the floor to one. And now if you have the value 1.7 this time you are closer to the ceiling. You are almost there. That's why Python going to go and round it up. With that you will get two.
But now of course you're going to ask what about the 1.5. It is exactly in the middle. For that Python has a special rule called round half to even.
Sometimes we call it bankers rounding.
So now Python going to check okay I have here one and two. And since two is an even number that means Python going to round it up. So it going to bring it to the ceiling. It is not anymore about the distance. It's all about whether it is odd or even. Okay. So let's have this example to understand what this exactly means. Now we have four boundaries 1 2 3 4. And let's take the middle values.
Like for example between 1 and two we have the 1.5. As we learned it's going to go to the even number and the even number here is two. So we are doing round up. Now let's take the middle value between two and three. So we have 2.5. Now what is the even number between three and two? Well still two. So that's why Python this time going to go and round it down. So you will get in the output two because of the rule. Now let's go to the last one. Between three and four we have in the middle 3.5. And between the two boundaries what is the even number? It is the four. So what's going to happen? Python going to go and roll up to the four. So this is exactly what going to happen in Python if you are rounding the half. Okay. So now we have this annoying price and let's go round it using the three functions.
Let's start first with the built-in function that we have from Python. So we're going to say round the price. So let's go and execute it. So now as you can see everything is rounded up to the 36. We don't have any more those annoying decimal numbers. So with that as you can see we are moving our numbers to the closest boundary. But I would like to go and move everything to the floor always. So for that we're going to go and use the function floor and put the price inside it. But now as you can see there is like something wrong. We are not getting like a new color and if you go and execute it you will get an error because Python don't understand what is this. Python don't know anything about this function and that's because this is not built-in function. This function exists somewhere in the standard library but first we have to import it. And in order to do that we're going to go at the start of our code and we say import. And then we need the module math. So each time you need this function the floor you have to go first and import it. It's like you are installing the module in your code. And of course if you go and create a new Python code you have to import it again.
So this is only available for this script where I'm doing now my code. Now still Python don't understand what is the floor. And that's because we have first to say math dot and with that we are saying okay this functions comes from the module math and with that you can see we are getting the color of it and if you go and execute it you will not get an error but we will not see anything because we are not printing. So you say print then let's go and execute.
So now as you can see our price now is 35. So we are bringing the prices down.
Now of course if you want to take everything to the ceiling you use the function seal and the same thing bison does not know this function. you have to go and say this one exist in the math module and now we will get the color and you will not get an error. So let's go and execute it. So it's going to be very similar to the round because we are rounding up. Okay. So now there is like something special about the round function. We could use it in order to maintain some decimal numbers. Like for example I'm going to say okay this is really ugly. I don't want to have all those decimals. I still want to see some details like maybe only two decimals. So we can go and use the round like this and we say price and then after that we say comma and we decide how many decimals should be left in the output like for example two. So this time Python will not go and like remove everything after the decimal it's going to round it until the second decimal number. So let's print it and check that out and then execute. Now as you can see our number is 35.55.
So we are having here float and not anymore an integer. So all those other numbers are integer but if you here specify like two in the output of the round function you will get a float number and of course you can go and say I would like to have only one decimal like this and you will get only five.
This is only available for the round.
You cannot use [music] it for the floor or the ceiling. Okay. So now you might say you know what I don't want to round any numbers. I don't want to decide between the ceiling and the floor and all those complicated stuff. I just want to get rid of all those decimals. I just want to get the first part the whole number. Well, you can use that of course using the function trunk. So the function trunk going to strictly remove all the decimals. No rounding at all.
It's going to leave only the integer part. So let's try it out. It's math trunk. And then our price we're going to print it. Now let's go and execute. It is the last one. It's going to be 35. So it is really fast. Python will not go and round anything. It will just get you the integer part at the start. Now you might say, you know what? If you don't want to round anything and you want to just get the integer part, why don't we just go and use the int function, it's going to go and convert the float to an integer, right? And with that, we're going to remove all the decimals. So, we're going to say integer the price and you go and print it. So, if you go and execute it, you will get as well the same results. Well, my friends, you are totally right. This is another way on how to get rid of the decimal numbers.
And now you might say, okay, then when to use the trunk and when to use the integer. And now I'm going to say it like this. If you want to go and import the math only for the trunk only in order to get rid of the decimals, then just go use the int. But if you are already having the math because doing any other operations, it is not really bad to go and use the trunk because it is easier to understand. By just looking to this code, I know okay, I'm using the trunk in order to get rid of the decimals, which is really clear for me.
But if I look to the int, I might wonder is my price here like a string? And maybe I'm converting the string maybe to an integer. By just reading the code, it's going to be really hard and I might think that you are using it because the price is a string. And now you might still ask me when do I use round, floor, and ceiling. Well, I can tell you most of the time I use the round function cuz I can control exactly how many decimals I'm going to leave. Especially if you are doing data visualizations and reporting, you don't want to show such a big numbers in your reports. You would like to make everything like smaller because you don't have space for that.
That's why we usually use the round function in order to make our float numbers smaller. And now there is like another scenario where I use the ceiling. It is not in data analysis. I use it in data engineering. Especially if I'm doing something like cold pageinating, batching or let's say resource allocations. I want to be optimistic and to make sure that I have always enough resources to make sure that I'm not missing anything. So if you want to make sure always that you have enough resources, go with the ceiling.
But for that analysis and reporting and stuff, most of the time you're going to end up using the round function. Okay, my friends. So with that, we have covered everything in this group.
Important functions that we could use in order to manipulate your numbers and I usually use those stuff more than the other ones. Now moving on to the next one. We have the advanced math. In this module, the math, we have a lot of advanced stuff that we could use in order to make complex formulas like the s, cosine, log, and so on. We will not deep dive into that. That's why we have documentations. So you can go and try them out. But now we'd like to go to the next group. We have the random. And now this one is more fun than the mathematical stuff. So here we have another module called random. And we can generate random numbers using those functions. [music] Okay. So now what we're going to do first we have to go and import the module random. And then all what you have to do is very simple. We're going to say random. random. So this one is a function and going to go and generate random decimal number between 0ero and one. So let's try this out. We're going to say print uh let's check the random number that we're going to get. So we have it here 0.49 blah blah blah. And of course you will not get the exact same number. Each time you execute it, you will get different numbers. So now we have 0.78.
So each time I execute it, I'm going to get a random number. Now if you want to say, yeah, I didn't expect to get the floating number. I would like to have like a normal integer number like I'm rolling a dice or something. So in order to do that we have another function. We have random dots then rand in t integer.
This function going to go and generate an integer number between a range that you specify. Like for example let's say the range is between 1 and six. So I want a random integer number between this range. Now let's go and print it and check. Let's go and put this in comment and execute. So for me I got five. If I go and repeat again, I'm going to get three and here six, four, and so on. So, as you can see, each time I execute, I'm going to get an integer number between the boundaries one and six. So, now this might sounds like funny rolling a dice and so on. But when do we use this in real projects? Like, is there any like real serious use case for this? Well, of course, there is like an important one. We use it in order to generate dummy data or simple data for testing. Sometimes as we are testing like functions, pipelines, dashboards, we want to have like sometimes fake and dummy data. And in order to do that, we could use those random functions in order to generate the dummy data. For example, an age. I'm going to go and use the rand int where I say okay the customers range is between for example like 20 and um 50 or you can go and use the random in order to generate prices and as well maybe the random integer to generate the user ID and the customer ID. So we could use it in order to generate dummy data for testing. So what we can do use the random function in order to generate like random 10 ids and then pick randomly 10 customers from the large data set. We call this like random sampling. So my friend there is like real use cases for those functions. It's not only fun one and there are like many scenarios that I can think of for those random functions. Okay. So that's all about how to generate random numbers in Python. Let's move to the next one. We have the validation and here we have a methods and built-in function in order to validate our numbers. So let's go.
So now let's say that we have a float number like this 7.0.
Now I would like to check whether my float number is actually a whole number.
So here we have like dot0ero. This looks like a whole number. Right? Now I would like to check whether I have something after the decimals or not. And with that we have a built-in method for the class floats and it is the is integer. So we can do it like this. X dot since it is a method we have to make dot and then after it we write the method name. So is integer like this and of course we have to go and print it to see the output.
This can return either true or false. So in this scenario it is true because we don't have real decimals after the decimal points. But now let's go and copy this to another number. Let's say this time we have 7.1 and let's go and execute it. So this is not a whole number. This is a decimal number because here we have one not zero. So with that I can check whether my floating numbers are actually a whole number or are they real float numbers.
This is very important if you have like a new data and you are checking the quality of the data. So if you open like a file you will see like a lot of values but they always end like let's say with zeros maybe it is like an parsing issue or an export issue. My data is actually whole numbers but in the output somehow they are converted to float numbers. Now, of course, as I'm doing like data engineering, I don't want to round numbers and maybe make something wrong because I don't really understand the business behind those numbers and whether it's allowed to be rounded and so on. So, first I check are they really whole numbers. If it's like this like it's always zeros then I can go and make it as an integer without losing any data. So, this is really great check in order to understand are my float numbers are actually an integers but they just have like a bad format. Okay. So this one is nice. Let's move to the next one.
Let's say that I have the following variable like I have 70 and I would like to check the data type whether it is an integer or not. So I can go and use the buildin function is instance. So it's going to check whether my value belong to specific data type and it needs like two things the value itself and the data type that I am expecting like for example an integer. That's all. Let's go and print it. Now I'm asking Python is my value an integer. So let's go and execute it. You can see it's true. Now let's go and convert it to a float and then execute it. You can see it says false because my value is not an integer. And of course you can use this for any value and any data type. Like for example the same thing is print is instance. And now I'm going to say is this a float? You can see it's going to say yes true. This is of course an easy to see in my code. But if I'm getting like a value from my users, I would like to go and validate it first whether it meets the expectations or not. And this function we usually use it in order to build a logic and conditions and this is exactly what can happen in the next chapter. So those are your tools your operators functions methods and as well the modules that you can use for the numeric values and I would say use this as a cheat sheet. If you have any numbers you can check this quickly and to understand if I want to do rounding I have those like five options. So that's nice and to be honest I'm using this as well for my project now. Okay I'm not going to leave you like this. We're going to have like one challenge.
Generate [music] a random integer from 1 to 100 and check the output if it's even. So it is simple. Go pause the video and solve it. [music] Okay. So now by looking back to our road map actually that's it for the numbers.
It was quick. So with this we have completed the two chapters and now you can say I can work with data. Now after that we're going to move to something deeper. We have here three chapters with one idea. how to control the execution of your Python code. So, we're going to start with the first chapter where we're going to learn logic and operators. So, we're going to learn how Python understand the truth and false, how to do comparison, how to build the logic, how to build the conditions, how to combine them. So, we're going to learn all the important tools in order to build something like brain inside your code. So, let's dive in for and while. So now let's start with the first question. What are you talking about? What is a control flow in a code?
[music] And why do we need it in the first place? So let's go. Control flow is like a logic you build inside your code to control the path your program follows. So this logic going to tell Python should I run this part? Should I skip it? Should I repeat it? So it's a logic in your code to decide what to do next. Let's understand what this means exactly. So so far what we have done is we have a Python script and we start from top to bottom writing commands step by step like print this assign that get an input from user print again so it's like you are creating to-do [music] list for Python and once you execute it Python has to go from the top and start executing your command step by step until it reach the bottom of your code.
So as you can see here we don't have any control flow everything going to be executed like a straight line. So, it is like driving your car in an empty straight highway. There is no turns, no traffic lights, just single line from start to end. Sounds peaceful, right?
But here's the thing. Real life isn't like that. You don't just go in straight line forever. Life is messy. Sometimes you drive into a busy city. There are forks, bridges, hundreds of signs, traffic lights left and right. And sometimes the conditions change like the engine is too hot or maybe there is like a storm. Sometimes you have to stop and exit. And this is exactly what happens in coding. We have complex tasks and we have to control the flow of our codes.
Okay. So let's check this. The first way to control the flow of your code by building something called conditional statements. So how this looks like in your code? Let's say that you start writing your code as normal. And then you come to a point where you have two blocks of codes, one on the left and one on the right, but you don't want to execute both of them. So you want to execute one of them based on a question.
So that means you build like a question inside your code. We call it condition and based on the answer if the condition is true you go to the right block of code but if the answer for the question is false you go to the left side. So with that you are building like a logic or a decision. So now let's say that you executed this what going to happen the first block of code going to executed as normal and then the question going to be asked inside your code and if it's true then it's going to go to the right side and start executing the right block and then after that the last block of code going to be executed as normal. As you can see now we have a piece of code that is not executed. So that means with that we are not executing the whole thing.
Now let's say that you have executed again and after asking the question the answer was no it is false. Then this time the left part can be executed and then Python goes to the last part and executed as well. So as you can see this time we went to the left side and the right block of code is completely ignored. So my friend it's like a crossroads in your code. Python comes into intersection then checks the sign and choose one way or other. Once it goes to this path of course it keep going to the end of the code. So this is one way how to make decisions using conditional statements in your codes.
But now what about if you want to repeat something? So you want to keep going until certain condition is met. And for that we built something in our code called a loop. So think about it like this. Imagine you are cleaning your room. You just don't go and pick one t-shirt and stop. You keep going t-shirt after t-shirt and you keep going cleaning stuff in your room until one condition is met and that is your room is clean. The floor is clean. So what you have just done in your room a loop by the way if your room is not clean go and do that same thing my friend in coding instead of repeating the same task over and over what you can do we can go and build a loop to execute the same code multiple times. So how this looks like in coding you're going to start normally doing few stuff and then you come to piece of a task that you want to repeat it. So what you're going to do you're going to build again here a question or condition and based on the answer if it's like true then it going to go to the code that you want to keep repeating execute it completely and then go and ask the same question. So if the answer is now stills true Python going to go again and execute the same code from the top to the bottom and then go back and ask the same question. As you can see we have now the shape of loop and Python keep executing this block of code until we have different answer.
Once Python gets the answer that it wants then it going to break the loop and exit and go to the rest of your code. So as you can see we are controlling the flow here where we are making like a loop inside our code in order to repeat the same task over and over. So now my friend with that you have now two superpowers in order to control the flow of your code. You can make decisions using the conditional statements or you can repeat actions and tasks using loop. So by looking to this you're going to move your code from something simple to smart where your program going to think choose and act and believe me you will end up using these everywhere.
Now in order to build all those [music] stuff in Python we need tools. Now I'm going to show you the whole road map all the tools that we can use in order to control the flow. And I split it into two main sections. The first one it is the control flow statements. So they are like special instructions that you can use in Python to change the flow. And here we have like two subsections like conditional statements if else else if.
So this stuff going to help us to make decisions. And the other section here we have the repetition the loops. And here we have two important statements the for loop and the while loop. And as well we have some statements to skip stuff like the break continue and pass. So those statements are the core building blocks that we can use in order to build the logic control the flow of our code. Now the statement alone is not enough. We need something essential. We need to give them conditions, the questions, the rules that Python going to check. And here where the boolean expressions comes in. So now we have a lot of different ways on how to build the expressions like either using the values or maybe functions that returns true or false.
And as well we could use a lot of operators in order to check something like the comparison operator, the logical operator. We have membership and identity. So now my friend all those expressions are very important to understand so that you have enough tools to build a logic to build the conditions that you can use inside your statements and with that you can cover many different scenarios and requirements. So now what we're going to do I decided to deep dive into first the boolean expressions in order to get ourself ready with anything once we start building statements. So let's go first with the boolean expressions. So now before we start with any operators we're going to cover the values and the functions. Let's go.
Now let's start with the basic stuff. We consider the boolean values as an expression. So we have two values. We have the true and as well the false. So now if you execute it you will get true and false in the output. And as we learned before be careful with the capital T and the capital F. So those are the two values they represent like an answer yes or no. And of course you can go and check the boolean data type using the type function. So if you have here like true and you go and execute it, you will see the class boolean. All right. So those are about the values.
Now we have as well functions that returns true or false. Like for example, the buildin pool function. It's going to go and convert any value to true or false. So for example, let's have value 1 2 3 and let's print it. You can see we are getting true. So now as you can see in the output, we are getting true because this function going to check whether you have an empty value or you have a real value. So now let's take another example. For example, instead of number, let's have a string. So we have a high and if you go and execute it, you will get as well true because we have a value. It is not empty. So now let's go and try something that's going to return false. So boolean. And now we're going to leave it empty like this. So let's go and execute it. So as you can see, we are getting false because it is empty.
We don't have a value. Now we can go and check something else like pool and we can go with the zero. Now in the output, we are getting false. Now of course the zero is a value it's not like an empty but in the boolean function it's going to be considered as nothing. So there is no quantity. So for this function this going to be considered as an empty. And the same thing if you go and print and then you have only double quotes. So we have a blank nothing inside it. If you go and execute it this going to be false. So for this function this is empty. Now if you remember we have a special value called none. In Python this means there is no value at all. It is unknown and doesn't belong to any data type. So it's not a number, string, boolean. It is the real nothing and it is not equal of course to the empty string because empty string is already a string. But for the pool function, it's going to be considered as an empty. Now of course if you go and pass this value to the pool and print it, you will see in the output we will get as well false because this considered to be nothing.
So this is really nice function in order to quickly check your values whether they have a real value inside it or it is an empty. And this function going to return true or false. Okay. So now let's move on. We have a lot of functions that return true or false. And we have two important built-in functions called all and any. Let's understand what this means. Now let's say that we have multiple values like for example we have zero. And as we understood zero in boolean is false because the size is empty. It is zero. Now we have another box another value and inside it we have false. This is of course in boolean is false as well. And a third value and here we have string character a. Now since it is not empty, we have a real value. The boolean value of this going to be true because it's not empty. Now as you can see we have evaluated those values separately. But now what we can do in Python, we can evaluate them all together. And here in Python we have two options. Either you use the function all or any. So now if you go and use the built-in function any, it's going to be happy if one of those boxes is checked as true. So if there is like somewhere yes, then it is totally fine and in the output you will get true. But now if you go and apply the function all this one is really greedy. It's going to say I want everything to be checked and marked as true. So if there is like somewhere false you will get false. That's why in this example you will get false. Let's have another example where we have three values like one true and a. So now Python going to go and evaluate them one by one. So we're going to have here everywhere true. So if you go and use the function any it's going to be more than enough all what it ask for one true. That's why you will get true in the output. And now comes the greedy one. All this time it's going to be happy because everything is green and [clears throat] all the values is true.
That's why you will get in the output true. So as you can see those two functions are really easy. So now let's go back in order to practice. All right.
Now let's have a nice example where we have three variables like email, phone and username. Let's go and assign for them few values. For example, this going to be empty. And for the phone number, we have some kind of this number and the username going to be empty. Now, as you might already know, if you are registering to any websites, they're going to require from you few informations and each website has their own rule. Like for example, let's say that we have a rule in our website. So say we allow registration if one of those informations are filled. So we don't need all three. It's enough to have one to validate the registration.
So that means we have to check all those three variables whether we have informations or not and if there is like one of them is filled that is more than enough. So now since we want to check multiple informations we could use any or all. And now by looking to the task it is not restrictive it's enough to have one true in order to allow the registration. That's why we're going to go with the any. And here we're going to build the list. So our list is email, phone and username. Now in order to check the result we can go and print it.
So let's go and execute. You can see this is allowed because the phone here is true. So the email is false, username is false and the phone is true and that's more than enough for the any that's why we are getting true. Now if you go and remove the phone number and execute, you will get false because we are not fulfilling the minimal requirements. It should at least has one true. Now let's say that you are building some annoying website where we need all the informations in the registration form. So you have to give everything the email, the phone, the username and if one of them is missing it will not allow the registration. So that means it is a must to have a true in all those three parameters. And of course for that we cannot use the any we have to go and use all. So the same thing I'm going to go and copy the whole thing here and just replace the any with all. So let's go and execute it. Of course it's going to be false because our user didn't provide anything. Now let's go and give some informations. So now we have a number and we have a user ID. But let's say we didn't give the email. So let's go and execute it. For the first website, we will get true because we have more than enough. We have like two informations. But for the annoying websites, it will not work. You have to give all the informations. All those stuff should be true. So that means in order to get this true, you have to go and add here a website or like any value. We are not validating the emails here. So let's go and execute it. Only then you will get true. So this is a very simple use case. Why we need the any and all and those functions we could use it as an expression of course for our conditions later. Okay. So now let's keep going. We have another function that returns false or true and that is the is instance. As you remember we can use it in order to check whether a value belong to specific data type.
Like for example we can go and check whether the 1 2 3 is an integer. So let's go and print it. So as you can see in the output we are getting true because 1 2 3 is an integer. And let's go and try something else like for example I'm going to say here true and I'm going to check whether this value is a string. So let's go and execute it.
You will get false because true is actually a boolean not a string. So as you can see this function checks the data type and return true or false. And now for the next one of course not only functions we can use methods from the class string. Do you remember the one that we used in order to search in a string for example the ends with and here we can go and search for example for the O. Now we can go and print it.
So as you can see in the output we are getting true. That means this method return true or false. The same thing for the start with. So starts with and if you go and execute it you will get false because the string does not start with the O. It start with an H. All right. So that's it so far about the functions that could return boolean value and you can use them as a boolean expression.
And [music] after that we're going to deep dive into the operators. There are many different important operators that you could use as a boolean expressions.
And we're going to start with the first group, the comparison operators. They are very simple but yet very powerful and used a lot. [music] We use the comparison operators in order to compare two values and return true or false based on the results. And here we have many nice operators like the equal to where you have two equals and then the not equal. We have less than, less than or equal and the opposite the greater than, greater than or equal. So they are very simple. But now let's understand exactly what do we mean with the comparison operators. So now how we do it usually we have two values and in between we put the comparison operator and this is like a question and the result of this going to be either true or false yes or no. So now for example we have two values three and two and if you put operator between them the greater than and with that we are asking is three greater than two so Python going to return the boolean value true.
Now of course those are direct values numbers but you could go and compare not only values you could compare as well variables like for example if you have a variable x and you assign for it like a value five you could compare now the variable with a value and you put in between them the operator for example less and with that you are asking the question is x less than two so of course python going to go and get the value five and compare it with a two well five is not less than two that's why python going to answer for this question as false so as you can see for those questions there is only true and false.
There is nothing like in between. And what else we could put in this equation is expressions. Like for example 2 - 1 this is an expression and you can go and compare it to the value two and put in between for example not equal. So we are asking is 2 - 1 not equal to 2. Well that is true cuz 2 - 1 is 1 and 1 is not equal to two and Python going to say yes that's correct. And what else we could put in this equation is that we could use function like for example we could use the built-in function length and we could say okay is the length of high is equal to three. Well Python going to go first and execute the function and we will get two and then it's going to go and compare is 2 equal to three. Well the answer for that is false. So as you can see you can use anything as long as it return a value and then you can go and compare two values using the comparison operators and this going to form something called condition or let's say question and the answer for those questions is always true or false. So this is very simple this is how it works. Okay. So now let's go and try few operators like for example you can say 10 is equal to 10. So here you have to go and use two equals. If you go and execute it you will get true. But now if you go and use for example the not equal operator between them and execute it you will get false because 10 is equal to 10. Now another one we can go and check whether the value is greater than another value. So if you go and execute it you will get true 7 is bigger than three and the next one whether the seven is larger or equal to another seven. If they are equal like this it's going to be fine and even if it is like smaller it's going to be as well fine. you will get true. So here we have like two operators in one. Now the opposite going to be is three smaller than seven. Well you will get true or you say is three smaller or equal to 7. So it's again we are checking two things less and equal.
If you go and execute it you will get true. And as well if you have it like a seven it's going to be fine because 7 is equal to 7. So again here we have two things either smaller or equal. Okay. So now there is like few things I want you to know about the comparison operators that you can go as well and compare characters like for example is a smaller than b. So if you go and execute it you will get true. So here Python is comparing the string values alphabetically. And there is one more thing for example if you are comparing strings like this. So is a equal to b?
Well it's false. Well python here is comparing the strings alphabetically.
And what is very famous we can check the values using the equal. So is a equal to b? Well, false. And there is one more thing. Python is case sensitive. So this will not be as well accepted. It's going to be false. So a lower a is not equal to an upper a. So that means not only numbers. You can go and check as well string values. And there is like one mistake that many beginners do is that they don't use the double equal. So they make it like this. You will get an error because the one equal is for assignment.
As we learned before, we can do it like this. So we want to assign for the variable x a value like for example a.
So the one equal is used to do assignment for the variables but the two equals is used to check whether the two values are equal or not. Now there is like something cool in python we call it the chain comparison. So so far what we have learned we could do like comparison operator between two values right. So this is going to give you true. But now what we can do we can go and make a chain like we can go and put a third value like six and then put an comparison operator between the four and six. So with that we are doing like chain comparison. So now if you go and execute it you will get true. And now how Python did execute this. So it's going to go and execute the chain from the left moving to the right. So first it's going to go and execute this part.
Now Python going to check okay is one smaller than four? Well it is true. Then it going to go and check the second part is four smaller than six. Well, this is as well true. And now since everything is true, you will get in the output true. But now for example, if you have here like five and you execute it, it will going to be false because the first part is not true. It is false. And it is not enough to have like only one part as true. You need everything to be true in order to get in the output true. And now think of this if you know SQL. This is like the between operator. So we are actually checking whether this value between two ranges. So let's have a nice example. Let's say that we want to check whether the age between 18 and 30. So this sounds like a scale of course.
Let's go and create a variable called age and we have the value 20. So now we're going to build it like this. First we have to check whether the age is higher or equal to 18. So we start with the first boundary and we say 18 is less or equal to the age the value that we are checking and at the same time the age should be smaller or equal to 30. So as you can see we are checking a value in between two other values. And now if you go and print it you will get true.
So yes 20 is between 18 and 30. And now if you go to one of the boundaries like 18 it can be fine because here we have greater or equal. But now if you jump outside the boundary like for example the 35 you will get false because it is greater than 30. So this part is false and this part is true. So this is how we do between or let's say chain comparison. And of course you can go and add as much comparison as you want. But usually we don't use more than like three values. Okay. So that's all about the comparison operators. They are very famous and you're going to end up using them like almost everywhere. And now in [music] the next one we're going to start talking about the logical operators.
The logical operators and or [music] we use them in order to combine or connect multiple boolean expressions in order to build a logic or to build the rules. So let's understand exactly what this means. Okay. So now let's say that we have this condition. We are checking is three smaller than five and we want to check at the same time another condition like is 5 equal to 5. So that means my friend we are now evaluating two conditions not only one. So we have somehow to connect those two conditions so that Python evaluate both of them.
And here you have two options two logical operators the and or. So now let's see how they work. Let's pick the and if you use the and operator then you are telling Python return true if both conditions are true. So both of them should be true in order to get the output true. So now for example here the first condition is true. three is smaller than five and the second condition is as well true. Five is equal to five. So now Python going to say we have the and operator both of the conditions must be fulfilled in order to get true and that's why we are getting in the output a true but now let's take another two conditions like we are saying is three greater than five and the second condition is five equal to 5.
Now Python going to evaluate each condition separately. The first one is not true. Three is not greater than five and P but I'm going to go and evaluate the second condition. and I we will get true. Now after that Python going to execute the and but this time it will not work. We will not get true at the output because both of the conditions are not true. We have one false and one true. This is not enough for the and operator. That's why we will get in the output false. Now next let's try to put an or in between. So if you use the or you are telling for Python at least one condition must be true. So it's enough to have one true in order to get in the output true. So now for the first example we have both as true. it is more than enough. So we will get in the output true. And now for the second example we have one false and one true.
Well this time since we are using or operator it is enough at least we should have one true. That's why in the output you will get true. It's not like the and the and is more restrictive. And let's have another example where we say is three bigger than five and the second condition is five not equal to five. Now Python going to go and evaluate both of the conditions and both of them going to return false. And now it's going to check the or operator. At least we need one true. In this scenario, we don't have anywhere true. That's why we will get false. This is the only scenario where you get false in the output. If both are returning false and you don't have any true. So it is very simple. If you use the end operator, it's going to return true only if both of the conditions are true. But in the other side on the or operator, you will get true if at least one of the conditions is true. So this is really amazing operator in order to check multiple things together so that you don't check one thing at a time. Now let's go and practice. All right. Now let's have a little break. I would like to have a reminder over here that don't just consume a course. Don't just sit back and watch me like coding and explaining things. My recommendation here is really 20% consuming and 80% practicing. The real learning can happen once you start practicing coding and struggling. Don't worry about making mistakes. That's totally fine. So practice more than consuming and you will be fine. Now let's go [music] back. All right. Now let's start with a simple examples.
First we are checking whether three is higher than one. And let's say that we have another one. Five is less than one.
Now we have like two conditions and we'd like to go and connect them. Let's say I'm going to go with the and operator and then let's have a print and let's go and check the results. Now as you can see we are getting false because the first part is true but the second part is not true. So here we have false and true it will not fulfill the condition that's why we are getting false. But of course if you go and correct your conditions where you have the five is greater than one and you go and execute it you will get true and that's because both of the expressions are returning true. Now let's go and try out the or operator for both of them. So here we going to have an or and as well here or and I'm going to go and comment those out this as well. Let's go and execute the first one. You will get true because here we have in the first expression true and the second one is false. But all what you need is only one true. Now let's make both of them as false. So three is as well less than one. And you go and execute it, you will get false because you don't have any true. Both of them is false. So as you can see it is very simple right? Okay.
So now let's have a real example in order to understand when we use those stuff. Now let's say that we are building like monitoring system to check whether our system is under pressure like for example the memory or the CPU under high usage. So let's go and create two variables CPU usage and memory usage. And now let's go and give values for example here 70 and here we have the memory usage is 95. So now let's say that if something above 90 it's going to be critical. So we're going to go and check whether the CPU usage is higher than 90. And as well we're going to check the memory usage higher than 90.
But now we have to go and connect them.
So that means it's enough to have one of those conditions as true to trigger the alert. And now think about it which one we have to use. Should we use and or should we use or? Well, since I'm saying at least one of them is true, we have to go and use the or operator. Now let's go and print in the output and execute. So you will get true and that's because we have one of those resources under high usage. So we have here 95. Now let's say that it is cooling down and we have it around 50%. And if you execute it you will get false. So now that means everything is fine and we don't have any alerts. So as you can see we have here two conditions and we are checking multiple stuff in one go. All right. Now let's move to another real use case for the and this everyone knows it. We're going to check whether the user credentials before logging. So we usually give an email and password and we have to go now and check whether everything is correct. So now let's say that we have a parameter called email and a password and they are like boolean. So let's say here this is true but the password is false. Now we have to check those two informations and of course both of them should be true in order to allow the user to log in. So that means we have to check those two informations and we're going to connect them using the and operator. Everything must be true in order to allow the access. So now let's go and print this information and execute. So now as you can see in the output we are getting false because the password is not correct. But now let's say that the user entered a correct password. So that we have the email and the password as true.
If you go and execute it you will get true. And of course if everything is false that means it is as well false. So this is as well a quick use case where we use the and operator in order to check the email and password before login.
All right. Now we have a third logical operator the not. It doesn't combine condition but it flip the truth. It reverse everything. It going to switch the true to false and the false to true.
So let's understand what this means. Now so far we have learned how to check whether three is bigger than two in this scenario you will get true. And if you check is five equal to 8 that's clearly going to give you false. So so far nothing surprising. Now if you go and just add the not operator just before the expression then it's going to switch the whole reality. So the not operator going to go to the results and switch it. So in this scenario we have true and it's going to go and switch it to false.
So now the question is completely different than before. Before we have said is three greater than two but now we are saying is three not greater than two and of course the answer for this question going to be false. Now the same thing going to happen for the second example. If you add the not operator just before the check, it's going to go this time and switch the false to true.
So this time the question going to be is five not equal to 8? Well, that is true.
It's not equal to 8. So this is how the not operator work. Now let's go and practice a little bit. Okay. So now let's have a very simple comparison like three is higher than two. And of course if you print the results you will get true because yes three is higher than two. But now if you go and say not. So with that we are flipping the whole thing and we will get false and you can put it wherever you want before any boolean value. Like for example not true. So what is not true? It is actually false. The same thing if you go and put not false and execute you will get true. Now we're going to have something funny. If you say not not false so you can use it twice in order to flip it twice. So the not false going to give you true and the not not false guess what you'll get false. So actually this is totally useless. You can just remove it because it's going to gives you back the original value. And one more thing about the not let's say that we have a variable called name and you don't have anything inside it. So it's blank only double quotes. And now if you go and print it you will get as well nothing in the output. But if you go and say not name what can happen? You will get true. And as you remember the boolean of empty is false and if you say not false you will get true. The same thing if you go and say for example not zero you will get as well true because zero is false and not going to flip it to true. [music] So that's it about the not.
Okay. So now with that [music] we have learned how to combine two conditions using the and or operators. Now we're going to go to the next level where we're going to go and mix stuff together and we're going to have like multiple conditions in one line. So now let's say that we have multiple conditions where we say is x equal to 5. Another one we are checking is y greater than five and now we have a third one where we are checking is zed smaller than four. So that means now we are evaluating three conditions. And of course if you put them all in one line you can go and connect them using the logical operators. And now here you have to be very careful which one you are using because depend on your design you will get different results. So let's say that the first operator going to be or and the second one is and. And let's say that we are getting the following values from those variables 5 8 and six. And now if you go and execute it, it is very important to understand the order of the execution. So here it is very important to understand and operator has higher priority than the or operator. So that means and will be executed before or. So that means Python going to focus on the and operator and as well the expressions on the left and on the right. So first it's going to evaluate is 8 greater than five. Well, it is true and then evaluate the right side. Is six smaller than four? Well, it is false. Now after that Python going to go and apply the and operator. Now since it is not fulfilling the requirements, both of them are not true. We have one false. That means in the output you will get false. So this is the first part. And now the result of the and operator going to be used together with the or operator to get the final results. So now or operator has the second priority. On the left side we have five equal to 5. Well this is true.
So now Python has everything in order to execute the or operator. Well, it is fulfilling the requirements. We have at least one true. That's why in the output you will get true. So my friend this is the default execution order by default and has higher priority than or. But this is not really nice because we are not having the control of the execution.
And you say you know what I would like to have the or operator executed first before the ends. So I would like to have a control on the execution. Well in order to do that we use the parenthesis.
Now if you go and put the or operator with the conditions left and right between parenthesis with that you are telling Python hey don't go and use the default priority. Now I'm designing the priority. first go and execute everything between my parenthesis and then the result should be evaluated with whatever outside my parenthesis. So if you go and execute it it's going to work like this. First Python going to go and execute your parenthesis. So what do you have inside it? We have two conditions.
Is five equal to five? Well, we have true and the second condition is as well true. Well, we have more than enough for the or operator. That's why we will get in the output true. And now only after that Python going to go and grab the and operator and evaluate the part outside the parenthesis. So is six less than four? Well, no. That's why we'll get false. And now Python has everything for the end operator. So here it is not fulfilling the requirements. We have one false. That's why in the output you will get false. So once you have more than two conditions and you are using multiple logical operators, you have to be very careful designing your condition. You have to do it step by step and understand the order of the execution. So now let's go and practice about this. So now we have the following task and it says allow access only if the user is logged in or they are guest but they must not be banned. So imagine you are building like an online store.
You can allow the user to buy things if they are guest or if they are logged in and maybe you have like some blacklist where you banned people from buying things from your website for some reason. So now we start by creating variables. Now the user could be logged in. So we could have a variable like this is logged in and the user could be a guest. So we could have another variable is guest and the user could be banned. So this is a third status about our user is banned. Now in order to build this we have to check all those stuff. Let's start one by one. So we're going to say is logged in. This is the first one. And as you can see in English it's very easy or they are guest. So that means we are using the operator or and then is guest and then the last one but they must not be banned. Of course we're going to go and use the isb band but here we are saying not banned. So again English yeah not is banned but still we cannot use the not in order to connect our operators together. That's why we have to put in [music] between something like and or. Now of course we cannot cheat anymore. There is like no and or in our logic but actually this is an ant. So the user should be logged in or a guest but at the same time the user should not be banned. So here we have an ant. Now of course we have to play with the values. Let's go and print the whole thing and start adding few values. Now let's say that you are logged in but you are not a guest. So this is false and let's say that you are not banned. So now if you execute it you will get true.
So now of course the question how Python did executed first the and operator has more priority. So this part can be executed. What do we have here? Is guest is false not is banned is true. So false and true going to be false. So this part going to give us false. But the first part is true. So is logged in true. Now that means true or false you will get true. Well that is really good. Now let's go and try something else where we say actually you are banned. You managed somehow to log in but you are at the blacklist. You are banned. Now if you go and execute it, you will get true. Hm.
This is not working right. So what happens here? First the end operator worked. So here we have a false and not is banned. So here false and false you'll get false and the first one is true. So that's why we are getting still true. Well, the whole issue is that the order of the execution is not correct. I don't want to check those two together.
Actually, if you read it, we have here two parts. This is the first part. So, they belong together. Either you are logged in or you are guest. So, this is the first thing that has to be checked.
And then after that, we check whether you are banned. So, that means I would like to have this part first executed.
And that's why I'm going to go and use the parenthesis at the left and as well at the right. With that we're going to force Python to execute the or operator first. So now let's go and execute it.
You will get false. Now your logic is correct. That's why we have always to test and to be very careful with mixing or and controlling the order of the execution. So now what happened here?
Python going to go and first execute at the first part the or because we forced it to do that. So here we have is logged in. You are logged in but you are not guest. We have one true. So this going to return true and then Python going to go and execute the second part. The second part is false. You are banned. So we have now true and false. That's why we are getting now false. So as you can see we are getting completely different result as we have controlled the execution of our logic. And actually it is not that complicated. Just follow the logic in the text and this is what we have implemented step by step in our logic as well. So first this part can be executed and only after that we're going to check whether the user is banned or not. So now I know that might be a little bit complicated but only if you practice you can understand how things works.
Well the in operator going to check whether a value is inside another value.
It could be string value list or any sequence. So now the syntax for that is very easy where we say for example is the character a exist inside the string data. So we are using the n operator between the two string and if you read it in English it's very simple is a in data. So if you go and execute it python going to take the a and start searching inside the data for a match and it's going to find it in the second character. That's why you will get in the output true. So a is inside data.
Now sometimes what we can do we want to check whether it's not inside the data.
So we use the operator not just before the in. So again it's easy in English is a not in data. Well it is false. So the not operator going to go and switch the reality of the output. Instead of true you will get false. Now let's have another example where we have for example bar and then we're going to check whether it is a member of a list.
Don't worry about the list. We're going to learn about it later. So we have a list of different people. John, Kumar, Ali. Now again it is very simple. Is bar now member of this gang? Well, Python going to start searching and matching.
Bar is not member of this group. That's why we will get false. And now if you go and add the not operator, it's going to be like this. Is Bar not in not a member of this list? Well, it is true. He is not part of this group. So that's why the not operator going to go and switch the output from false to true. As you can see, Python is not that hard, right?
It is very friendly. So now let's go and practice a little bit. Okay. So now let's go and do some easy stuff. Like for example, let's go and search for the character O here in the word Python. So let's go and execute it. You will get true. And if you go and get like character like f is f in python well it's false. And now of course we can go and add here the not in. So is f not in python. Well that's true. So it flips the output. Same thing if you go and make like a list. So let's say that we are checking whether the three is member of this list. 1 2 3. So if you go and execute it, you will get true because three is member of this list. And if you say not member of this list, you will get false. So this is very easy. Now of course the question is how to use this in real use cases. Let's say that again we have a website and we are checking the registration. So once you give your email address, we can easily check the domain whether it is banned or not. You don't want to register like bad domains otherwise you're going to get hacked.
Let's say that the domain that the user entered like gmail.com. So this one is safe. Now of course we have to go and create a list of different domains that are banned or let's say blacklisted. So banned domains equal and here we make a list. I know we are using a lot of list but we're going to learn that later. So let's say that we have domain called spam.com another one fake.org and the last one put.net. So now what we want to do is to check whether the domain that's entered from the user is not a blacklisted domain. So this is really nice check. So domain not in the band domains as you can see it is very simple and as well secure. Let's go and execute it. Now as you can see it is true. So the domain that is provided from the user is safe. Now let's say that another user goes and enter a domain spam.com. If you go and executed you will get false. Well this is not allowed to register because this domain is a membership of this bad domains. So as you can see those expressions we use it in order to build quality checks and as well security checks and this is nice one right. So that's all about the membership operators they are very friendly. Let's move to the next group.
We have the identity operators. Again here we have only two is and is not.
They are not that famous like the other groups but still we have to learn how to work with it because we're going to use it in some scenarios. So let's understand behind the scenes what these operators do.
It's going to check whether two variables are referring to the same object in the memory. So this sounds a little bit complicated. In order to understand it, as usual, we're going to make sketch about it. So let's see what this means. So you remember on the left side we have our code and on the right sides, Python stores our variables in memory. So now let's say in code you created a variable called a and inside it you have a list 1 2 3. Now if you go and execute it, Python can go and create something in the memory. So it's going to store the variable and the values.
Now let's say that you created another variable called B and as well you have the same value 1 2 3 a list. So if you execute it the same thing going to happen. Python going to go and store the data the values inside the memory. Now the things are not exactly like this in the memory. Actually we don't have a direct mapping between the variables names and the values. The variables are actually pointing to an ID to an object ID. So for the first value it is stored with the object ID 30 and for the second value we have another ID 40. So by looking to this that means we have variables and the variables are pointing to an objects and each object has an ID and value. So that means we are connecting the variables using something called pointers. So this is exactly how it looks in the memory. And now let's go back to our code and let's say that you are checking is a equal to b. Well, if you're using the equal operator, you are comparing the values of the two variables. So, Python going to check the values, nothing else. And as you can see, the values are equal. That's why you will get in the output true. But my friend, now if you use the is operator, so you say a is b. So that means we are saying is a has the same identity as b.
And if you do it like this, then you are telling Python go and compare the ids of the objects. This time we don't care at all about the values. We care about the object ID. Are the ids equal? Well, if you look to this, we have here two different ids 30 and 40. And they are not equal. That's why you will get false. So here, my friend, we are checking with the is operator whether the two variables are pointing to the same object in the memory. We are not comparing the values. If you want to compare the values, go and use the equal operator. But wait, we are not done yet.
Things get more complicated. If you go and create simple values in the memory like you say a is equal to 5 and b is equal to 5. So now if you go and execute it what going to happen? Python going to check your values and say okay the five is very simple value. I don't have to go and create two different objects for it.
I can create one object and with that I'm saving memory and it is really easy to handle. So Python can create an object with the value five and as well each object has an ID like for example 98. And now what can happen since those two variables are having the same value it's going to go and point those two variables to the same objects. So that means we have two variables one ID and one value. Now if you check back our code we have a is equal to b. Well here we are comparing the values you will get anyway true. But this time if you say is a the same object as b python this time going to go and check the id and going to say actually both of the variables are pointing to the same objects. So both of the variables having the same ID the 89. That's why this time you will get true. [music] So this is actually how it works. If you have multiple variables with a very simple values, Python going to go and create one object for them and all the variables going to be pointing to the same objects. And if you use the is operator, you will get true. But if you have complex values, Python going to go and store them in different objects. And if you use the is operator, you will get false. So I hope this is clear and now we're going to go and practice. Okay. So now let's have a very simple example where we create a list of characters like A and then B and C. And now we're going to go and copy the whole thing but we store it now in different variable. Now my friend if you go and say is X equal equal Y and then print this in the output you will get true. Yes we are comparing the values and this is completely true. But now if you say and use the is operator between those two variables you will get false because Python going to go and create for each value a separate object and they have separate ids. So here we are comparing the ids using the is operator and the equal equal going to compare the values. Now if you go and copy the whole thing and use an easy value like 10 and here 10. Then let's go and execute. You will get true and true because Python going to go and optimize it and put those two variables or the values of the variables in one object. [music] So they're going to have the same ID and as well the same value. That's why you are getting for both true. Okay. So now there is one more thing about the assignments. Let's say that we are saying X is equal to this list. But now for Y, we don't repeat the same thing.
We say simply Y going to be equal to whatever value X has. Now what going to happen? Python will not go and create for the Y new objects. All what it going to do it going to go and point it to the same object for the X. So you're going to have again those two variables pointing to the same objects. If you go and execute it, you will get true and true this time because for the Y Python is not creating new objects. Now I was thinking about when we going to use this in projects. Let's say that we have the following example. We are doing some data quality where we are checking whether the email exists and it is not empty. So let's say that we have an email and now we have to check whether the user gave any email address like for example let's say the user didn't enter anything like blank and now we can go and check something like this where we say the email should not be a blank so this is our rule and if you go and print this so you will get false this is not allowed this should not be the case but now if you go and give any value any email and execute it you will get true so this is okay but now sometimes in Python it could be like this none. So the user did not give anything. And of course this is different than the plank.
This is nothing. Now if you go and execute it. So now as you can see we are getting true. This is not really good because we don't have actually an email.
This is nothing. And in Python none is not equal to blank. That's why this is not enough. We have to go and build another condition is not equal to none like this. And of course we have to go and connect those two conditions. So what we're going to do? We're going to go and use the and operator. So if you go and execute it you will get false because this condition here is false and this one here is true. So true and false you will get false. Now you might say okay but what has this to do with the is operator? Well there is like special use case for the none operator where you're going to see the developers in codes they never use the not equal or the equal operator with the none. They always use the is operator. So in this case for example they say the email is not none. Well, you might ask, but the not equal and the equal works. You are totally correct. This works, but this is like best practices. It is better to use the is or the is not with the special value none. Well, the reason behind that it is a little bit complicated. We will not deep dive into that. Well, what I think about it, it is just a habit from SQL because in SQL you are not allowed to use the equal operator with the nulls and the developers are used to use is and is not with the null operator and they brought it here to Python. So if they are dealing with a null or let's say here none, they don't use the operator equal. They use the is operator. And to be honest, I do this mistake a lot. I say is not null, not none because I'm used to SQL. And that's why you will find a lot of developers use the is none and the is not none. But if you go and use the equal your code can work. So that's it about the is operator.
[music] Well, if you think that we are done here, you are totally wrong because now it's time to challenge you. And this time it will not be easy. I have for you five challenges. The first challenge, check if a username is not empty and the age is greater than or equal to 18. I know this is an easy one. Let's move to the second one. Check if the password is at least eight characters long and does not contain spaces. Mhm. So it is interesting here. The third one, check if a user's email is not empty, contains ats and ends with.com. Okay. The fourth one, check if a username is a string, is not none, and is longer than five characters. So this time we are validating the username. And now we come to the final boss to the advanced challenge. Check if the user is either an admin or moderator and at the same time check either they are not banned or they have verified their email. So a lot of things is going on here. Pause the video and then go solve those tasks. All right friends, now by looking back to our road map or that we have completed the first chapter on how to control the execution in Python. So you know how to build logical expressions but logic alone does nothing unless you use it.
That's why we have the next chapter where you're going to learn how to make your program use the logic and make decisions based on it. So we're going to learn now the conditional statements.
Let's dive in.
Okay. So now what is conditional statements? It is very simple. It is like a checkpoint in your code that's going to check a condition. If the condition is true, it's going to run special block of code. But if the condition is false, it will skip it. So it's like you are making a decision inside your code. So we have only those two options. Either we're going to run the code or not. Think about it like the traffic light. If it is red, you stop.
If it is green, you go. So the rule is very simple. And this is exactly what we can do in coding. Normally our code, it's like straight line from start to end. But with the conditional statements, we can make like a question or a checkpoint. And based on the answer, if it is true, then you go to the right and do something. But if it is false then you go to the left and do something else. So with that we can cover multiple scenarios and based on the data we go either left or right. So now my friends there are like multiple forms of the conditional statements and we're going to start with the basic form with the easiest one and then we're going to do it step by step until we reach the advanced one. So let's get started. [music] A little bit story about this. I know some of you are wondering about whether this coffee is fake or not. Well I can tell you this is completely true. This is hot coffee. Now another fact this is not sponsored by Nes Cafe or something.
I just had the cup in my desk and it became the brand of data with borrow and even there's like some company contacted me whether I can use their cafe at my desk as a sponsors but we didn't came to a deal at the end. So no this is not fake and I don't drink this cafe. I drink actually cappuccino using espresso machine. I hope with this everything is clear and now let's go back.
Okay. Okay. So now let's start with the first level. The most basic form of the conditional statement. The if statements where we have only one single condition.
So the if statement is where everything starts. We use it in order to define the first condition the first question. Now in order to understand how things works, we're going to have a visual chart. So let's check this. All right. Now we always start at the top where our program begins and then we say we want to build now a question where we use inside it a boolean expression in order to have a condition. So it is like a question and the answer for that is going to be either true or false. So either yes or no. Now if the condition is true we going to have like a piece of code that going to assign the value a.
So that means Python has to do something if the answer is true. But in the other side if the answer is false, Python has to do nothing completely skip everything and jump to the end. Now in order to build this kind of question or checkpoints we use the conditional statement if. So this is the most basic form of the conditional statements where we say if the condition is true then run the code but if the condition is not true then skip it. So now let's have an example in order to see how Python can execute it. Let's say that we are evaluating the scores of the students and we want to give them grades. So we can go and build equation or condition like this. If the score of the student is greater or equal to 90 then this student going to get the grade of A.
Okay. So now let's have this example where we have a very good student with a score of 100. So now Python going to go and check it. Is 100 greater or equal to 90? Well the answer for that is yes.
It's true. So now what going to happen?
Python going to go and take the right path and execute it. So we're going to go to the code block where we assign the grade to A and after that Python going to jump out immediately to the end. Now let's have another student with a score of 80. So now Python going to go to the checkpoint and compare the values. Well, this time it is not fulfilling the condition because 80 is not greater or equal to 90. So it's going to fail and Python has to go to the false path. So that means this time Python didn't execute the block of code. We skipped it and actually nothing happened here. So that's it. This is how the single if statement works. So all what you are saying if this is true then do something if not just skip it. It is very simple right? So now in Python we have always to understand the rules of using something. So now let's say that we have like two blocks of code and in between we put our if statements. The rules are here that we can use only one if statements for each chain. So we cannot go and add multiple if statements. It's only one. The second rule, it must always come first. So the first thing that we do is to define the if statement. We will understand this later as we progress and it is required. It is not optional. So if you are building conditional statements, you must always include if. You cannot skip it and it can stand alone. So you can have only if statement like this without any other statements. Okay. So now let's go and create a variable like the scores and add to it a value 100. Now in my code I would like to go and ask a question about this value and based on the results Python has to do something. So in order to do that we use the conditional statement if and after that we have to add the condition or let's say the boolean expression. So we're going to say if the score is greater or equal to 90 then Python has to do something. So far what do we have? We have the boolean expression we have the if statements and after that we have to do an action if this is true. Now, in order to do that, we have to go and add the double points. This is something a lot of beginners forget to add. Just don't forget your double points after the condition. And after that, go and hit enter. Now, we're going to write what Python has to do if the condition is true. So, I'm going to say go and print a. And now, you might say, wait, the format is really ugly. Why do we have like spaces at the start? So, let's go and remove them. And immediately you will see everything is red and there is like something is wrong. Well, because my friends, you cannot do that. You have to go and put spaces before the print.
And those two spaces at the start, we call them intendations. They are my friend part of the syntax in Python. So that means my friend, you have to be pixel perfect in order to do those stuff in Python. So let me explain for you what are those.
The intendition means that you add spaces or tab at the start of a line so that you help Python to understand which lines belongs together. So now if you're looking to the second line to the print you see it is like pushed to the right side using four spaces and with that Python can understand the second line actually belongs to the first line. So that means Python cannot go and immediately like print the A. It has to consider first the if statements. If everything is fine, Python can go and print the A. And the same thing if you have like a second print, let's say great job. So that means both lines of the print are intended under the if. So they're going to run only if the condition is true. And if you don't do that, you will get an error. And the intendation is not something special only for the conditional statement if.
It is how Python defines the blocks of code. So you have to use it as well for the other stuff like defining a function or using the for loop. You can see in all those scenarios we have to add the intendance in order to make it clear for Python which code belong to which one.
So with that we can see the print a belongs to the if statements and print hello with the name it belongs to the definition of the function and the print loop with the i it belongs to the for loop. So you have to do this in different scenarios and as well in Python we can nest stuff inside each others. So for example here we are defining a function and after it we have the conditional statements. So with that we have three levels. The top level is without any indentance or spaces where we have the definition of the function.
The second level then after that we have the first level inside the function we have the if else. And then inside the if and inside the else we have a second level where we have the prints. And again we can organize this using spaces.
So you have to be pixel perfect here. So if you look to this as blocks the whole if belongs to the definition of the function as well the else. So both of them are the same level. And the first print is actually belongs to the if statements. And the second print belongs to the else statements. So each time you add four spaces or indentance, you are introducing a new level of nesting. And if you know any other languages like Java, JavaScript there as well, we have if else statements, but we use their brackets in order to mark the start and the end of a block. So in Java and JavaScript, there are no rules about the intendations. It's only styling. But the intendations in Python, it is part of the rule and the syntax. Of course, if you compare now Python with the other languages, you can see it is easier to read with the Python. We don't have to go and add the brackets, the space is going to control the structure of our code. But still, you have to be careful as you are adding spaces to your code that has a meaning for the interpreter of Python. Okay, now we are back. Now, as you can see, we have here only two spaces. But if you are following the standards of styling in Python, the pip 8, you have to add four spaces or a tab.
So I really recommend you to follow this rule from now on. We add four spaces. So now either you go here and add like two spaces in your visual codes or you can configure the visual studio in order to add always four spaces automatically. So now in order to do this, we have to go to the commands control shiftp and then go and search for something called preferences open user settings JSON. So this one over here. And now don't panic.
We're going to get like the settings in JSON formats. Of course, things might look different at your side. But what we're going to do, let's go to the ends.
Just add here at the ends like comma.
And then we have to place this piece of code. So now we are saying here only for Python go and add four spaces for the indentations. So now after that go and save it and then restart the visual studio code. So now if you go back to your code and remove this, we go back to the double points. If you go and hit enter, you will get your four spaces immediately. And with that, you are following the best practices. Now we can go and say print a. Okay. So that's all about the intendations. Now let's focus on the conditional statements. So again this code belongs to the if statements and going to be executed only if this condition is true. So now let's go and try this out. You can see we are getting a because the score is higher than or equal to the 90. But now if you go and make something 50 and execute it. So as you can see we don't have anything in the output. There is no print because the condition is not fulfilled. It is false. And with that Python is ignoring totally this line. So it's like we are locking this line of code. Python going to access it only if the condition is true. So it is very simple. That's all.
Okay. So now let's move to the next form. We have the two-way decision using else statements. The else statements runs a plug of code if the answer of the condition is false. So that means we can use it in order to do something if all the conditions are not fulfilled. Let's have a simple visuals in order to understand it. All right. Now by looking to this again you can see that for the false path we are not doing anything. So only things can happen if the answer is true. Now what we can do we can go and add a block of code to be executed on the false path. For example, we can go and assign the grade if if the answer is false. So now in order to do that, we have to use the conditional statement else in order to have it on the false path. So now again, let's have this example. The student has a score of 100.
So Python going to go and ask is 100 greater or equal to 90. Well, the answer for that is true. And Python going to go to the right side and going to go and assign the A grade for the student. And after that jumps to the end. But now if the student has the score of 80, Python going to go and check it and it going to fail the condition. So now this time we don't have only if we have now an else.
So Python will not go immediately and jump to the end. Python first has to do something in the false path. So it's going to go and execute the block of the else statement by assigning the grade to F and then jump out to the end of the chain. So now if you look at this we have now two-way decision. All what we are saying here if this is true do this else do that instead. And by the way very important to understand there will be no scenario where we're going to go and execute both of the codes. So either left or right. So with this we have now more options to our code. Okay. So now let's check the rules for the else statements. Well first it must come at the end. It is always the last thing that can happen inside our chain and it's very important. It does not contain any conditions. So it's not like the if or the else if statements. So nothing to be checked. It is optional. So you can go and skip it. You don't have always to have an else. But in the other side, it cannot stand alone. It always needs an if. So it makes no sense to have only else. And the last one, you cannot have multiple else statements in the same chain. So it's like the if you can use only one else. So of course, we're going to understand this as we practice. Okay, back to our same example. So as you can see we are not getting anything here because we are not fulfilling the condition and nothing going to happen.
Now if you go and add an else it is very important that you have to add it at the same level as the if. So without any intendations. So here we have to go and add as well double points. But as you can see between the else and the double points we don't have any condition. Now we're going to do the same thing. We're going to hit enter. And again we have our four spaces because this block of code belongs to the else. So print if and actually that's it. So now let's go and execute it. So we can see we are getting if at the output and that's because we are not fulfilling the condition. So that means Python is not executing this print. Python has to jump to the else statement and execute the second print. But now if you go and fulfill the condition, let's say we have 95 and execute. And with that you are getting a and that's because we are fulfilling the condition and Python is executing its print. And of course since this worked Python can ignore completely the else statement here. Python will not do anything about this section. So again about the rules you cannot have the else statement as like stand alone. If you go and execute it, you will get an error.
So else needs an if statement at the start. And of course it is optional. So if you remove it, your code going to work. So it is not must. You don't have always to go and include an else. And it has always to be at the end of your block. You cannot go and switch it like to have else and then if you will get errors. So first if and then else. You have to follow the orders my friend. And of course you cannot go and add any condition after the else you will get an error. So else and then afterward immediately the double points. As you can see it is very easy.
Okay friends. [music] Now, let's keep moving. Let's keep adding stuff. Now, we're going to talk about the multicondition statements using LF statements. The LF statement, it is short of else if, we use it in order to add a new follow-up question. If the result of the previous question was false. Now, as usual, in order to understand what this means, we're going to have a visual chart. Now, look at this again. We have only one question and two ways. But of course in real projects things get more complicated and you have to ask more than one question.
But now the [clears throat] big design decision is where we going to put this condition. So when we going to ask this question? Well, we have four options for different scenarios. The first one we can take this question and put it on the left side. So if the answer for the first question is false, we ask a follow-up question. And in order to do that we have dedicated conditional statement called else if or you say you know what let's move this question to the right side. So if I get a true from the first question I want to ask a follow-up question. Well for that we don't have like special conditional statements. We have to reuse the if statement and with that we are building something called nested if. Now to the third option what we can do with that we can ask this question together with the first question. And if you remember in the boolean expression if you want to connect two conditional statements we have to use the logical operators and or. So this is something that could happen a lot. Now if you look to this you can see that we are moving this condition like inside our chain. But what we can do we can ask this question completely outside of this chain. So after the first chain is completely done we go and ask our second question. So completely outside and we call this independent ifs. We're going to learn about that later. So now let's start with the first one. We're going to put our question on the left side. So after we get false from the first question, we're going to ask the follow-up question using the else if. Now let's say that we're going to add another check where we are asking is the score of the student is equal or greater than 80. And of course each question, each condition has two ways. So if it is true, we have to do something. If it's not, we have to do another thing. Now in this example, if it's true, then let's say we assign the grade B. And if it is false then we go to the else statement.
Now as you can see we are extending our chain and we are adding more scenarios.
Let's have an example. If the student has the score 100. So Python going to ask the first question is 100 greater or equal to 90? Well yes it is true. That's why Python going to go to the right side and execute the block where the grade is equal to a. And after that is very important. It's going to jump immediately out of the chain. So it's going to go immediately to the end and everything on the left side going to be ignored. That means we are not asking the second question. And this is exactly how things works with the conditional statements. Once you get the first row, the code that belongs to it going to be executed and then go out. So nothing else, no other questions going to be asked. Now let's have another score. We have 85. So Python going to go to the first condition. It's going to fail because it is less than 90. And now Python has to go and ask the second question. Is 85 greater or equal to 80?
Well, it is true. And now Python going to go to the right side and assign B to the student and as well it's going to go immediately to the end. Now let's have another score the 50. So it's going to fail the first question because it is less than 90. So we go to the left and now we ask the second question. Well, it's going to fail as well. It is less than 80. That's why Python going to go to the false path and execute the else.
So our student going to get if at the end. So now by looking to this our code has three ways, three scenarios. The one where the first question is true.
Another one where the first question is false but the second is true. And the last one where everything is false, none of the condition is met and the else is executed. And now it looks really nice if you compare the conditional statements. You can see if and else if both of them has question has condition but with the else we don't have here any question any condition. It's something that has to be done. And as well you can see with the else it's like a fallback a backup. If none of the above is true then we have to execute the else. So it is the full pack scenario and we use it in order to give default values. Okay.
So now what are the rules for the else if. So the first rule is about the position. It comes always after the if statements. So you cannot have it before it. Well the second thing is not a rule.
It's something nice. You can use as many else if blocks as you want. So you can use one two three as much as you want.
And the else if always needs as well condition. It's like the if statement.
And as well of course it is optional. So you don't have always to include it. So it is similar to the else. And as well it cannot stand alone. So you cannot have an else if without an if statements. So those are the rules. Of course we have to practice. Okay. So now let's go and add our else if. Now you can see here we have like two blocks.
The if and the else. In order to add else if you have to add it in between.
So you cannot add it at the start or at the end. you will get an errors. So that's why we're going to go over here and say else if and afterward we have to go and add our condition exactly like the if statements. So we're going to say the score is greater or equal to 80. So this is the condition and if it is true.
So we have to go and add the double points. Don't forget about it. And then we hit enter. And as you can see we are getting our intendations. So what going to happen? We're going to go and print the grade B. And actually that's it with that. We have now our two questions and we can try this out. So let's have the 100. You can see we are getting a. So Python is executing this print and everything else is ignored because the first condition is fulfilled. Let's go and try the 85 and execute it. You can see we are getting the B and that's because the first condition did fail.
That's why Python going to try the second condition and the second one with the else if it is true. That's why we are getting B. So that means the else if now is activated. Let's go and add the last one the 50 and execute. You will see we are getting the grade if and that's because this value did not fulfill any of our conditions. So that means we got false in all our conditions. That's why Python did execute the else statement. So as you can see it's really simple to read. If this condition is true then do this.
Otherwise go check another thing. If this is true then do that. And if none of the above is working, just go to the else statements and execute this. So it makes sense and as [music] well simple.
All right. Now moving on to the next one. We can make branching on the conditional statements. We don't have for this like a new keyword or statements. It's all about using the else if multiple times. So let's understand what this means. Now all what we're going to do is to add another follow-up question. For example, is the score is higher or equal to 70. And we're going to do the same thing. We're going to put it on the false path. So if the answer for the second question is false, go and ask third question. And the same thing either it's going to be true or false. If it's true, then we add and assign the grade of C. And if it's false, then we go to the else. So as you can see, all what we are doing is we are asking another question if we are getting false. So by looking to this, you can see we can connect else with another else if. Well, this is [snorts] really useful in many scenarios where for example we are building this grading system or we are assigning roles to the users or maybe we are working with the codes of status. There are many scenarios where we have to change the different conditions using the else if.
So for example if the score is 100 the first condition is true. So with that Python going to go to the right side assign the a and skip then everything jump immediately to the end. All the questions everything on the left side going to be ignored. So now if you have the score of the 85 the first condition is false then Python going to go and ask the second question it will be true and with that we will get B and immediately break the chain so get out to the end.
Now let's say the score is 70. The first condition is false. The second as well false. And now Python has to ask the last question. Is it equal or higher than 70? Well, yes. So that means it's going to go to the path of the true and assign C without executing the else. And to the last example where we have 40. So the first question is false. The second as well false. And now Python going to try the last question. Is it higher or equal to 70? Well, this is as well false. That's why nothing left. And Python has to go and execute the else because we didn't get any true to all our conditions. So with that, Python gonna assign f and jump out to the end.
As you can see, we are covering four scenarios, four ways. But as we are using more and more else if we are like branching to the left side. So visually it looks like you have steps going down or like waterfall. So you see coding is not that dry, right? We have like a visual behind the code. There is always like a design. So now let's keep going.
We're going to go and add another block of the else if. So, else if score is greater or equal 270 then double points.
If this is true, what can happen? We hit enter and print C. So now let's try this out and we're going to get the value 75 and execute it. You are getting now C.
So what happens here? The first condition is not true. That's why Python is jumping to the second one as well.
This condition is not true. That's why it's going to go and try the last one.
This time the score 75 is greater than 70. That's why we are getting the C. But of course if you go and do something below like 50, none of the conditions is fulfilled. That's why Python is executing the else statements. As you can see now, you can go and keep adding stuff to our code. Like for example, if it is like 60 or above, then let's go and print D. So as you can see, we are like chaining the else. This is how things works with those three conditions. So things are easy, right?
Okay. Now we're going to talk about the nested if form. So the nested if is all about to put if statement inside another if statement. Now as usual we're going to have a visual to understand it. Okay.
So now let's say that we have a question and as you can see we used to add the questions always to the left side with the false path where we use the else if.
But now how about to add our follow-up question after the true. So we go to the right side and we ask it immediately after the true. Well, for that we don't have dedicated statement like the else if. We have to go and reuse the if statements. And with that we have like an if statement inside another one.
That's why we call it nested if. So now what going to happen? We have to ask another question. For example, has the student submitted any projects? And for this question we have either yes or no, true or false. If it's true then this is really an excellent student and he or she going to get a plus. But now if it is false this student is still good. So we still have like above 90 that's why we will get a but for that to happen we have to do exactly like the first if we have to include an else for it. So this get a little bit confusing. Each if statement has its own else statement. So the one that we have in the left on the bottom it belongs to the first if but the one with the a it belongs only to the second if statement to the nested one. So again each time you start a new if statement you can add to it an else.
So now let's have an example. Let's say the student has 100 and as well he submitted a project. So we have true. So now Python going to start with the first question. Is it higher than 90? Well it is true. That's why Python going to go to the right side and then ask the second follow-up question. has the student submitted any project? Well, it is yes. Then it's going to go to the right side and the student going to get a perfect A+ and Python going to go and break everything and go to the end. But now let's say that our student didn't submit any projects. So for the first question, we have true but as Python going to ask the follow-up question about the project. Well, this time going to get false and Python has to go and execute the else of the second if statement. So this student going to get an A and as well jump to the end. And of course the if the first condition is not met then Python has always to go to the left side and ask the follow question like before. And with that we can see we are covering now five different ways and we are branching differently in the left side and on the right side. So we are forming something like a tree right we have branches on the left and as well to the right and there is one thing that you have to be careful that with the conditional statement there is only one path going to be executed. So with this my friend you have like now full control on building complex and flexible logic inside your code. Okay. So now back to our code. Let's go and create a new variable submitted project and let's assign to it the value true. So this is a boolean value and we can go and ask a question about it inside our chain. Give an A+ if the student submitted as well a project and we can go and ask this question inside the first block. So if the score is equal or higher than 90 then go and ask another question and another question means we need another if statement. So if the submitted projects now we don't have to go and add any operators. We will not do something like this because it makes no sense.
It's enough to say if submitted project like this. So if this returns true then do something about it. As you can see we don't have always to use the compression operators. It's enough to use a boolean expression. anything that you have learned before it could be function it could be like boolean value anything that returns true or false could be used after the if statements so now if the second question as well true what going to happen we're going to go and hit enter and now as you can see the intendations are eight spaces so that Python knows whatever now I'm writing here it belongs to the previous question so this print actually belongs to this if statement and it doesn't belong to the first one right now if both are true our student going to get a plus and otherwise so we have here an else as you can see then go and print a but here we have an issue this print now belongs to the else statements and you have to go and give it a tab so we need here eight spaces in order for python to understand aha this one belongs to this else and not to the previous one so as you can see the intendations are really important as you are building such kinds of logic if you do it wrong then you will get errors now let's go and try this out for example let's have like 95 and the project is submitted. If you go and execute it, you will get the A+ and that's because here the first condition is fulfilled and as well the second condition. That's why Python going to go and execute the A+ and then jump outside the whole code. So nothing else can be evaluated at all. But if we have here like false and execute it, Python going to evaluate this one going to be true and go inside. So now for the second one, this one is false. That's why Python has to go and implement the else statement and that means only this code is activated. Now if you go and add this true here and let's say we have for example 50 and you go and execute it you will get an if because in our logic it doesn't matter now whether you have a true or false. This is actually only relevant for the first block. We are not asking this question afterward in the else if or the else. We are just checking the scores. So in this example, none of our conditions are fulfilled.
That's why Python is printing if. So my friends, as you can see, we are nesting an if statement inside another if statements. And of course, you can keep nesting stuff like I can go over here and say if like I don't know if true and then double point and then hit enter. So as you can see, we have an if inside another if inside another if and with that you can do nesting in Python. But of course, we don't want to do that here. So that's it. This is how we do the nested if.
Okay, again the same scenario. We have a new condition and it's always the question where to place it. Previously we have learned how to put it as a follow-up question on the left side using the else if and as well how to put it on the right side using a new if statement. But now we don't have always to have it as a follow-up condition. We can ask it at the same time with the first condition. So we're going to put them together at the same level. And with that we have to evaluate two conditions at the same time. But you know we have to connect those two conditions using the logical operators and or. And of course both of them has different behaviors. Like for example the ant operator it going to return true only if both of the conditions are true.
So for example if the first condition is true and the second one as well true then this going to be fine for the and operator. And Python has to go to the right side to the through path and assign a and then go to the end. But now let's say that the first condition is true but the second one is false. So the student didn't submit any projects. This is not okay for the end operator. It expect both of the conditions to be true. So that's why Python going to go to the left side and execute the else statement. So the student going to fail.
But now of [snorts] course this makes no sense. This is really restrictive.
Instead we can go and use the or operator. And here at least one of the conditions should be true in order to get the result true. So now if you go and do it like this, the condition is fulfilled because we have one condition as true and Python going to go to the right side instead and assign a to the grade. So now you have understanding why we have learned the boolean expressions and the logical operators exactly to build this kind of logic. Okay. So now back to our example and now let's focus on this part of code. Now using the nested if statement, it is usually hard to maintain and as well hard to read.
And all what we are doing here is we are checking two conditions. The score and as well the submitted projects. Now instead of having it like this we could do everything in one go. We can go and take this over here and put it side by side with the first condition. So with that we can go and evaluate both of them. But of course we have to connect them using the logical operators. Now we're going to see it like this. If the score is higher or equal to 90 and as well the project is submitted then this student should get a plus. So we can go get rid of this nested F and we don't need those eight spaces. We can go back.
So if this happens the student going to get an A+. But now what we going to do with this one here? We still need the A, right? But now we have to do something about this over here. We cannot have two else, right? We have now only one if and you are allowed only to use one else.
And of course we want to use the last one. That's why we have to go and remove this. Now what we can do? We can go and use else if and we have to specify the condition. So we're going to say again if the score is higher or equal to 90 and after that double points and of course here what going to happen we're going to print the A but we don't need eight spaces it's in a four so that we have everything at the same level. Now you might say why we are not checking again the submitted project. So why we are not doing something like this and not submitted projects right well we have checked this already at the start.
So if it is not true that means it is false right? So to check again it makes no sense we have to remove it. So of course we can go and test it. Let's go and have here 100 and the submitted project is false. Let's go and execute it. You will get a and that's because the conditions over here is giving false. The score yes is over 100 but it's not enough. Both of them should be true. That's why this going to fail and Python going to go to the second one.
Now this time we have only one condition. It is true. And in this case we are sure the submitted project is false. That's why the print a going to be executed. But now if you go and turn it to true and execute you will get the A+ and that's because the first two conditions are giving true and Python going to go execute this one and jump immediately to the end of the chain. So nothing else going to be executed over here. Now let's try one more thing.
Let's say that our student has a score of 50 but our student did submit project and it was successful. So it makes no sense to give this student an if. We could give this student a last chance by the last check. So we check if the score is higher or equal to 60 or let's say if our student did submit a project then it's fine to give the student D. So if you go and execute it you will get D here. So this time we are not giving our student an if because he submitted a project. So at this scenario you have to go and use the or operator like this.
And the only way as a student to get an if your score is less than 60 and at the same time you didn't submit a project.
So you have here false. If you go and execute it, you will get an if. So there is no chance to help here. You have either to have a nice score in the exam or you submitted the project. So this is how we can ask two questions at the same time using the logical operators.
Okay. Now we have another type or form.
We call it the independent if statements. So they are like separated checks that's going to be evaluated one by one. and they don't have like dependency between them. So let's see what this means. Okay, again here we have our extra condition and we are wondering where we're going to place it.
So far we have learned how to position this question inside the same chain. So we are always putting the condition between the start and the end. Doesn't matter whether left, right or at the start. But now we're going to break the chain and we're going to put our question completely outside. So that means once everything is done with the first if statement chain, we're going to go [clears throat] and add another new if statement and they have nothing to do with each others. So that means now our second question is actually completely independent from the first question. So this is what we mean with the independent if statements. All right. So now let's try this independent if statements. Now we have two variables and I would like to check those informations separately. So now we start with the first if the score is higher than 90 then we print let's say high score otherwise if it is false then we need an else and we print low score. So this is the first chain it starts with the if and ends with the else. Now if you go after the else and you start a new if statement it's going to be completely independent from the previous one. And as you can see we are starting as well at the same level. So here I'm going to go and evaluate the submitted projects and if it is true then print project is submitted and if it's false so we need an else print project is not submitted. So now let's check this. We have the score 50 and the project is not submitted. If you go and execute it you can see now we are getting two results. Low score and the project is not submitted. Usually if you have like one chain you get only one answer and that's because Python is executing those if statements independently. So what happens here?
Python going to see okay we have here an if statements where we have a question well it is not fulfilled that's why it's going to jump to the else statements and print low score. So that's it for the first chain and everything is done and Python going to see another if statements and say how now we have a new chain a new story. So it's going to check okay is the submitted project equal to true? Well, it is false. That's why as well here Python going to go and jump to the else statements and print project is not submitted. So as you can see those two if statements are separated and we don't have here any nested if statements. So there is no dependencies between them. And as well if this means if we have here like a true that doesn't mean that Python going to go and ignore the second chain. They have nothing to do with each others. So this is how we can do independent if statements.
All right friends, now we're going to talk about something cool. The inline if statements or we call it sometimes turnary operator. So instead of writing the FL statements in multiple lines, you can go and put the whole thing in one quick line. So it is quick, shorter and doesn't take a lot of space. So let's understand what this means. So we start as usual with the if statement and after that we have our condition. So nothing new so far and as we learned before next we're going to use double points and then a new line where we're going to tell Python what to do if it's true.
This time instead of all of this we're going to stay at the same line but this time before the if statements we're going to write a value like for example a and with that we are saying if the score is greater or equal to 90 then go and assign the value a. So that means we define what can happen if it's true before writing the if statements and the condition. So this is the syntax and now let's say that it is false. The score is not higher or equal to 90. Then after the condition we use the statement else and after that we define what's going to happen if it's false. So as you can see now we have everything in one line. We don't have to have multiple lines. And here we have new rules on how to use this. The first one this time you cannot skip the else. You must include both if and else in each inline if statement.
The other one if you have multiple conditions you cannot go and use the else if. So in this structure you have only if and else and there is no else if and there is the one more thing that is really amazing. You can say the output of this inline if statements going to be stored in a variable. So it is quick expression to calculate the value and then store it in variable. So as you can see it is very powerful, simple, readable and really easy and quick to write. But here the tradeoff you're going to use it only if the logic is very simple. So let's go and practice about this. Okay. So now back to our example where we have the classical if else statements with multiple lines. Now we're going to go and convert this to one inline. So let's do it step by step.
I'm going to rewrite first this part over here the true. So if the score is higher than print a but now instead of saying print a I'm going to go and assign the a first. So I'm going to say okay it is a if the score is greater or equal to 90. So as you can see I'm reading it differently. I'm saying here it is a if the score is greater or equal to 90. But in the classical one we read it like this. If the score is greater or equal to 90 then print a. So it is like the way around. And now this is of course not enough. We have always to include an else. So if this is not true what going to happen? We're going to print f. Now as you can see the whole logic is in one line instead of having multiple lines. Now of course if I go and here comment if all those stuff and execute nothing can happen in the output because there is no action. I'm just like saying the value is a. So instead of that we're going to go and put the whole thing in print. This is something that you cannot do with the classical if statement. So I cannot go over here and put everything in one big print. It will not work. But this is why this inline if statement is cool. So if you go and execute it, you will get an A. So as you can see, we are getting the same result, but this is way cooler, shorter, and easier to read. Now what else we could do is that we can assign it to a variable and then we can go and print the variable later if you want or do any other manipulations. So we're still going to get the same results. All right, so that's it. Let's go and try something else. Like for example, we can have something more complex where this time we have an else if. So let me just zoom out. Now we have like three values and we have to try to put everything in one line. So now as we learned we cannot go and use the else if here in the inline. We have only to use the if and the else. So this is how I do it. I usually like read it. So we say assign a if the score is equal or greater to 90.
Else don't go and assign f immediately.
We still have to check something. So we're going to say else assign B if the score is greater or equal to 80. So this looks exactly like the first part. So it's like we are starting completely new if and this if needs as well an else.
You cannot skip that my friend. So if it is not true only then you can write an if. So you can see we have completely new if statement exactly after the else.
So with that we have like two inline in one line and let's test it. I'm just going to go and commit everything here.
So let's go and execute. We will get a because this condition was true and the value here get assigned. Let's have something like 85 and execute. You will get b because this part failed and then python went to the else and the other condition is checked. Well, it is true.
That's why we got a b. Now let's have 50. As you can see, we're going to get the final else. So yeah, as you can see, each if need an else and if you are putting everything side by side, the last else should has the last value. And now there is like one trick still you can put this in multiple lines. If you put everything in parenthesis.
So you can say the first part could be here, then the else, and the final else.
If you go and execute it, you're still going to get the same results. But now, my friends, this all makes no sense. If you have complex logic where you have things are nested like this, you have two conditions and you are checking multiple stuff. When things got complicated, go back to the classical if else statements because it is then easier to read. Don't misuse the inline if statements for complex logic. It is here only for quick stuff. So you want to quickly check something then assign a value. So this is really cool way in order to write our conditions.
>> [music] >> All right friends, now we're going to talk about the second special type of the conditional statements which is really new in Python. We call it the match case. So it's all about we evaluate an expression or a value against multiple possible values. And Python going to execute the block of code for the first match. So now let's understand what this means and why do we have it. So now let's say that we have the following task where it says convert the long name of the countries into two letters abbreviation. So that means we have to convert the long form into short form for the names of the countries.
Okay. First let's go and write it in the classical way. So we say the country is equal to for example let's say United States. And now what we're going to do we're going to go and check the value of this variable and then print the abbreviation. So we're going to say if country is equal equal to United States then what's going to happen? We're going to go and print the abbreviation right?
So it's going to be US. And if the value is unknown for us, let's say we want to print unknown country. So now if you go and execute this, you will get us. Now of course we have to go and cover all the countries, right? So let's go to the next one. Let's say else if let's go and cover for example India. So if the country is equal equal to India, then print the abbreviation in. So now if we have India over here in the country and execute it you will get en now what can happen my friends we're going to have really very long list. So for example the next one let's say Egypt eg and another one or Germany de and so on. So you can imagine how long this code going to be. So now in Python and as well in any other programming language if you have this scenario where you have a value and you are comparing it to multiple other options using the equal operators you can use the match case. So the syntax for that is very simple. We start with the keyword match and then the value that we want to evaluate it is the country. So after that we have double points. So with that we said okay I'm going to go and evaluate this value and after it you're going to write all your cases or let's say the patterns that you are searching for. So how we do it we hit enter and then case and now all what you have to do is to write the value that you are searching for. So we're going to say United States and then double points and another enter. Be careful of the abundance. Now we are at the second level. So we say print US. So actually that's it. We have covered the first case. Let's go to the second one.
case India then double point print en.
So again we have another one case Egypt then print EG and the last one case Germany print D. So now with that we have covered our four cases over here but what is missing is the else right? So now here if we didn't find any matching with those cases we can write the default case. So if none of the above did match, you can do it like this. You say case and then underscore like this and then double points. So we are saying if none of the above is matching then go and execute this case the default one.
So it's going to be print unknown country and that's it my friends. So now let's go and compare them side by side.
Now by just the visual of this this one is really easier to read than the previous one. Right? You have here sometimes if then else if then else. And the variable is repeated like here five times. In the match case we have it only once and as well the equal operator is everywhere. So the things that you have here to do is way more than the match case. The match case looks really clean, neat and very organized. So now let's try this out. So we have here the country India. And if you go and execute it, the second in came from our match case. And if you go here and say Egypt execute you will get ag. So my friend it is working. Now of course here the big rule is that you can use it only if you are matching values. So for example in the previous example with the student scores we have greater or equal those stuff will not work because you cannot use it. As you can see over here we don't have here any operations because this one is matching the values exactly using the equal operators. So we cannot go and use any operators like greater, less or even the logical operators. So this is only for matching one to one. If you have complex logic where you have multiple stuff, you still have to use the classical if else. And by the way, this works only with the Python version 3.10 and newer. So if you have an older version, you cannot use this at all. And what else we can do? H by the way, we can go and add a pipe over here. And we can check multiple values like this. So if it is United States or let's say USA.
So if it is one of those two values we can print USA. So so let's try this out.
Let's have here USA and execute. You can see we are getting US. So if you have like multiple variants that has the same output you can separate them using the pipe. But of course that's it. We cannot go and use any other operators. So this is another special case that you can use in order to write the conditional statements that is clean, structured and easy to read and to write.
All right friends, so now let's have a quick recap about what we have learned so far. So now we use the if statements in order to build the first condition.
We use the else if statements in order to have a follow-up question. If the answer of the previous condition or answer was false and we use the else statement as a fallback. So if all the previous conditions are not true, the last thing that's going to be executed is the else statements. Now as we have learned we have like different designs for different purposes. If you use only the if statements you are building just one quick condition. So you want to do a quick check before doing something. Like for example in data engineering we check if the file exist before loading it. Now the other design we have the if else. So if you have like two codes and you say either execute this or that. Now another design if you have a lot of else if so you are doing like branching this you going to use it if you have many options but only one should be chosen so only the first match wins and then we have learned that we can do if statement inside another if statements and this is very helpful if you want to make a decision inside another decision. So it's like you are saying okay first check whether the user is logged in and then we can say okay let's check whether the user is an admin or a guest. It's like you are building a decision tree and then we have learned we can design it like this. We could have like independent ifs. So if after if after if. So this you have to use it if all the conditions must be checked. Nothing must be skipped. All the conditions are equally important and you want to check everything not just the first match. And we have learned as well if you have this scenario where you want to do quick and short decision you could use the single line if statements or we call it inline if statements. So everything is compacted and in one line. But don't forget we are allowed only to use if and else. You are not allowed to use the else if. And the last thing if you want to match exact values and you would like to have clean and readable code then you can go and use the modern match case. So you can use it if you want to have clean, readable and fast code. But of course you cannot use it for complex logics or combined logics. It's only for matching exact value. So those are the types and when to use what.
Okay. So now it's [music] challenge time and I have for you two challenges. The first one we're going to do it together but the second one you have to do it alone. So the first challenge is to validate the values of the emails. So we have to make quality checks in order to detect the issues. So the rules are like this. The email must not be empty. It must contain a dot and at it must contain [music] exactly one at symbol.
The email must end with com.org orn net.
And it must not be longer than 254 characters. And the last one, it must start [music] or ends with a letter or digits. So now pause the video, do it alone, and then we're going to do it together.
All right. So now I hope that you are already done. Now I'm going to show you how I'm going to do it. Now first we need a variable and let's start with an empty string like this. So now let's start with the first one. The email must not be empty. So that means we have to build a condition to check the values.
And since this is the first condition, we start with the if. Now the condition is very simple. So we're going to say if the email equal equal to double quotes without anything inside it then this going to be an issue and we're going to go and print it to the output. So we're going to say the email cannot be empty.
So this can happen if the email is empty otherwise we're going to say else print the email is valid. So now let's go and try this out and execute. You can see the email cannot be empty. So we have here an issue and if you go and add anything inside it, you will get the email is valid. Well, this is not really correct because we didn't apply all other rules. But now what could happen that we could have like multiple spaces inside the email address and this considered to be as well empty. We don't have any like real email. If you go and execute it, you're still going to get the email is valid. But actually for us it is empty. Now how we going to fix it?
Usually first we clean up the data before doing any checks. So what we're going to do, we're going to remove all the white spaces in our email and then start applying the checks. So we're going to say first clean the string. So we're going to do it like this. Email is equal to email dot and here we're going to use the methods remember strip. So if you use it, you are removing the leading and the trailing spaces. So that means we are first cleaning the emails and then we are checking stuff. Let's go and try this out. Now you can see the email cannot be empty. That's because we remove the spaces and then this is equal to an empty string. Great. So with that we are done with the first check. Let's go to the second one. The email must contain a dot and add. So that means we have to go and add another condition another check. And what we're going to do we're going to go and use the else if. So if it is not empty then we have to check another thing. So let me just grab the comment over here. So now how we going to do this? You remember we had like special operator in order to check the existence of string inside another string. So we use the in operator. So we're going to say that dot in email and the other one going to be at in email.
So now let's go and add a double point and we're going to print email must contain dot and at. So now let's try this out. Let's have any email without a dot or let's say at. So if you go and execute it. So now as you can see this is not working. We are getting the email is valid. Well we have something wrong with this condition. Right? Now what this condition is returning is dot in email. So this is false and at an email this is as well false. But actually what we need we need to get a true at the output. So we're going to go and reverse everything with the not operator. So with that we are saying if this rule is not true then we have an issue right. So now if you go and execute it, you will get the issue. Email must contain dot and ads. Now if I go and add only the ads, let's go and try this out. You can see we still are getting the issue. But if I go and add now the dot and execute the email is valid. So again what happens here? Dot in email this is true.
And at an email this is a second true.
We are using the and operator. So the whole thing going to return true. But as we convert it to not the condition is not fulfilled and Python will not print it. So with that Python going to go to the else and print email is valid. So with that we have solved this check.
Let's move to the next one. The email must contain exactly one at. So that means we have to go and add another check using the else if. Now it must contain exactly one at symbol. So how we going to solve it? We have to go and count how many ads we have in our email.
And if it is exactly one then it's fine.
And you remember for that we have a methods called count. So we're going to say email dot count what we are counting. We are counting the ads right?
So if this is not equal to one then we have an issue. So we're going to say email must contain exactly one at. So let's go and try this out. Now we have only one at. So if you go and execute it you will get the email is valid. But now let's say that I have here another at in my email. If you go and execute it, you will get this issue. Email must contain exactly one at because the count of this going to be two and two is not equal to one. So this is correct. That's why we are getting this message. Okay. So as you can see things are really easy.
Let's keep going. Another check. So we are checking the email must end with com.org or.net. So another else if email. So now how we going to solve it?
we have to check the end of the string.
So the end of the email must match one of those values, right? So for that you remember we have a special method to check the end of the string. So it is ends with we have to pass for it a value and we're going to say here.com and then we can say or and the whole thing again for theorg and of course we have to go and repeat that for the net. Well, this going to work, but we have some smarter solution for it by using the sets. I know we didn't talk about it yet, but it is not bad to see how those stuff works here. So, we're just going to make like a sequence of string values separated with a comma. So, or as well.net.
So, now if the email ends with those stuff, everything going to be fine. But we are not checking whether everything is fine. We are checking the opposite.
So, that's why we're going to go and add a notch. And then we're going to say print email must end with com.org or.net like this. So now for this example if you go and execute it, it will be bad because we still have an ad.
So let me just remove it. Then again you can see the email is valid. So we are good with all those checks. But now if you have something like this io and execute it, you will get an issue. Email must end with com.org or.net. And this comes exactly from this check. Okay. So let's keep going. The next one, the email must not be longer than 254 characters. So another else if. So it's not longer. So we have to calculate how many characters we have inside our emails, right? And you remember we have a built-in function. Really nice one. So the length of the email and with that we're going to get exactly how long is our email. And we're going to say the result must not be greater than 254 like this. And if this happens then we have an issue with the email must not be longer than 254 characters. So now let me just remove this and execute it. We will not catch this error because it is less than 254. But if you go now and write a big email with all those characters, you will get the issue. So it is very simple. Let's go to the last one. This going to be interesting one.
So the last check the last else if. So what do we have here? The email must start and end with a letter or digit. So that means we have to go and check the first character and the last character of my email. They could be only letters or numbers. We cannot have special characters. So now in order to get the first character you remember we have to go and use the index. So the index of the first character is email. And then we have zero over here. And the last one, of course, you cannot go and start counting the index of the last character like this because each email has like different length. But instead, we count from the right side. And remember, we use the negative values, the minus one.
And now one more thing, we're going to go and add an and in between. But still we need some kind of function or method, right, to validate this. And in Python, we have a special method called is aln.
So this one going to check whether the string value is alpha numeric. So it contains only letters or digits. If yes, then we're going to get true. Otherwise, we're going to get false. So it is really nice. We didn't learn about this before. This is the first time we talk about it. So we're going to have this check on the first character and the last character. And of course, we're going to put everything between not. So we are saying if this is not following our rule, then of course we have an issue. So we're going to say print email must start and end with a letter or digit. Now as you can see everything is okay. But if you go for example at the start and let's have like a minus. Let's go and execute. You can see we are getting an error because the first character is actually a special character. That's why we are getting the issue. Let's go and try the end. I'm going to go and have hash. Let's execute. Surprise surprise we are getting something else. The email must end with com.org andnet and this is really nice because Python going to execute this one over here and then stops. So that means the other data qualities will not be checked actually.
So now here we have like a decision to make. Do we want our code to stop checking once we find the first issue or we would like to see all the issues inside our codes. Well currently our design going to stop once we encounter the first issue. But now if you say you know what I would like to have equality check to see all the issues then you cannot go and use the else if you have to go and use the independent if statements for each check. So now what we can do we can go over here and convert everything to the if and as well over here we can of course do find and replace. So now if you go and execute we will get both of the issues. The email must not end with com.org.net it and the email must start and end with a letter and digit. So this is really nice and as you can see now we are understanding why we have those different designs right now we can go and add like a third issue for example two outs and execute with that we are getting now three issues in the output. So with that we are getting a full report about the issues of our email and here we have like small issue where the last else is actually not for everything it is only for the last if statements. So now if you want to have this else you have to include it in each if statements or you can go and simply remove it like this or another solution if you would like really to have this else that everything is fine what we can do we can introduce a new variable like let's call it valid and it is equal to true and now what we can do if we encounter any issues we're going to make this valid is equal to false so we are printing and at the same time we are changing ing the flag. So, we're going to go and add this everywhere where we have an issue and as well over here and the last one.
But here we don't use actually an else.
What we're going to do, we're going to use an if statement in order to check this flag at the end. So that means if this value is true, then go and print the email is valid. Let's try this out and execute it. Now as you can see we are not getting the email is valid because everything is really crazy here.
Let's go and have another clean one and then execute. You can see the email is valid and that's because everything did return false and none of them got executed. So the value inside the valid is actually coming from the first one here. So it's going to stay true and none of our condition did converted to false. With that we are getting this nice message at the end. The email is valid. All right, my friends. I know this was not part of the task, but it was interesting how to solve it. And now we're going to move to the second challenge. So now you have to check the quality of the passwords. So here are the rules. The password cannot be empty.
The password must be at least eight characters long and it must include at least one uppercase letter and one lowerase letter. Another one, the password must not be same as the email address. So this going to be a weak password if you do that. And the password must not contain any spaces. So one space, multiple spaces, taps. And the last rule, the password must contain only letters or digits. So good luck with that. Now pause the video and go solve this challenge.
All right, my friends. Now by looking back to our road map with that, you have learned the conditional statements. So you learned how to make your program make decisions based on logic. And now we come to the last chapter in this category control the flow of your code.
We have the loops. And here we're going to learn two types. The for loop and the while loop. This going to be your introduction to automations. So they're going to help you to make smart code instead of repeating the same code over and over. So now let's get started with the loops in Python.
So now let's start with the first question. What are loops? A loop is like a tool that we use to control the flow of our codes. And if you use it, you are saying like, hey, I would like to repeat this code, this thing over and over until I say stop or something happened.
Maybe a condition is not fulfilled anymore. So now let's have a sketch to understand what this means. So now normally as we are coding, we write instructions one after another. And if you go and execute it, Python has to go from the top to the bottom executing all your instructions one after another and all your instructions will be executed from Python. So it is a straight line code and that's great for simple stuff.
But in reality as you are coding what could happen to that you will notice there is like a block of code that keep repeating. It could be repeated like five, 10 or even 100 times. That means we are repeating ourself and this is not really good because our code can explode. Instead what we could do we could put our code in autopilot. So what we can do we write this block of code only once and then we attach it to a loop. This loop going to go and execute the block of code over and over as long as the condition is true. So that you write your code only once and you repeat it as much as you need. And in Python and as well in many other programming languages there are two main types of loops. We have the for loops and the while loops. And each type has different types and purposes. Now we're going to deep dive into the for loops. So let's start.
A for loop [music] lets you go through a group of items one by one in order to do something for each item. So it's like you are saying do something for each item in my list. So let's understand exactly what this means. So now we start in our code with the keyword for in order to build the loop. And once Python sees the four keyword, it's going to go immediately behind the scenes and start building a logic, a process. So it has a start and as well a condition. And of course, this condition is not specified from us in the code. It is something that Python do once it sees four. So the condition going to be always like this.
Is it the last item? So it is actually asking am I done? Is it the last item in the group of items? You will understand exactly what I mean with this once we have an example. And as usual the answer for each condition could be either true or false. So this what going to happen once you specify a for. Now next we have to define the loop variable. So it's like a variable that we're going to use to assign the current value of a sequence. You could name it whatever you want an i, item, user whatever. And then we use the operator in and after that very important is the sequence. You define here the group of items that should be iterated. So with that we give Python the list that we would like to iterate and this is what we call a sequence. And now Python behind the scenes going to take this sequence and create it out of it an object in the memory called iterator. This iterator is responsible for knowing where we are currently in the loop and what is the next item in the sequence. So it keep tracks on what is already done and what is next. So with that we have now the setup the configuration of our for loop.
We know how we have to iterate three times because we have three items. But still, we have to tell Python what to do. Which line should be repeated, right? Of course, in order to do that, we have to do the double points and then go to a new line. And don't forget here the intendations. So, we have to leave four spaces and then write the line that should be iterated. Like for example, it could be print the item. And now, Python knows this block of code should be repeated. And this going to be if the condition is false. So as long as we still have items, this code should be repeated. And of course after the execution of the print, it's going to go back to the start in order to grab the next item. And of course, if the condition is true, so we are at the end, Python going to go and end the loop. And that means we can go and execute the next instructions. So now let's see how Python going to execute this. So as usual, we start from the beginning. And now it goes to the condition. It going to says is it the last item? So, Python going to go and ask the iterator what is next and since we are at the beginning, it's going to go and answer okay, the first one is one. So, the iterator going to answer Python with the value one. And this means the condition is not fulfilled. It is not the last item. We still have values in the iterator.
That's why it's going to go to the path of the false. Now, it's going to go and execute our block of code. Print I. And here, Python assigns the value one to the variable I. And that's why we need the loop variable in order to assign the values that we are getting from the iterator or the sequence. And now once our code is done, it's going to go back to the start. And then what going to happen my friend, it's going to go and ask again, give me the next item. So now the iterator going to say, okay, we are done with the first item. Then it's going to go to the second item and return two. So Python sees, okay, we still have values that's why we are not done yet. It's going to go back to the print and this time I is equal to two and it's going to go and print it in the output. And again it's going to go back to the start and ask the same question.
Are we done? Do we have anything next?
Now the iterator say okay we are done with the two. Let's go to the next one the three. So Python going to get three.
It is not done yet. It's going to go and print it in the output and again go back to the starts. But this time if Python ask the iterator what is next? It's going to see ah the three is the last item in the group. So it's going to say for Python sorry we don't have anything else. You have to stop. Now once Python gets the stop it's going to say okay we reached the end we have processed the last item that's why this time the condition is fulfilled and it's going to break the cycle and get out of the loop.
So we reach the end. So as you can see our block of code the print I is executed three times. We didn't have to repeat it three times in our code. We just build a for loop for it and it get iterated again. It's like autopilot. All right. Now let's say that we have the following example that we are printing like rounds. So we are seeing round one and then we repeat this like five times.
So round one 2 3 4. So now let's go and execute it. You can see we are getting the exact same stuff. So everything so far static of course and of course we are repeating the same code over and over but just the only thing that is changing is the round number. Now, of course, this is not very smart because we are repeating the same code over and over and instead of that we can write our code once and then put it in a loop.
So, we can go and get rid of everything and then we say for I this is our variable and then we define the sequence like this 1 2 3 4 5. Now we go and add double points with that. We have defined the structure of the for loop. We are saying let's iterate through this sequence and then we hit enter. Now we have to be careful with the spaces. The intendations here should be four spaces for example. And then we can go and grab our prints and put it over here. So now the print is inside the for loop. And now if you go and execute it, what's going to happen? Python going to go and repeat this code five times. And that's because in our sequence we have five values. So that means we have five items in our list and Python has to go through them one by one. And with that we are getting the output. But we are not there yet because we are printing the same code. I would like to have the value of the sequence in the output as well. Of course the value of the sequence are always assigned to the variable. So what you can do we can go and use our string format. And then we have to go and replace this static part with the loop variable. So we don't need you anymore.
And here we have an i. So now for each round we will get the value from our sequence. Let's go and execute it. Now you can see we are getting the exact same output. So round 1 2 3 4 5 and those values are coming actually from the sequence. So it is very simple right? You just have to define the loop and then tell Python what to do for each iteration. Now I would like to show you something like a rule for the naming of the variable and the sequence that we usually use. So for example, we could put our sequence in a variable called items equal to this value and then we could use this variable inside our for loop. You can see items but usually we're going to call it like this for item in items. So the variable name is a singular and the sequence going to be like plural for the same name and it is really nice to read for item in items and if you have like tables you could say for table in tables for file in files. So this is really nice just use the same name as a singular for the variable and plural for the sequence.
And of course we can go over here and call item. And if you go and excuse it, you'll get the same results. It is really cool. You know, immediately this variable here gets its value from the items.
Now, there is something important to understand about the for loop that we have always to specify a sequence, a group of items that has a start and end.
In the current example where we have the numbers 1 2 3, this is what we call a tpple. It is a data structure that we're going to learn about it later. Don't worry about it. But we could use as well other sequences like for example we could use a list. A list is as well a sequence. Let's have an example. So now let's check that out. If you go and remove this and have the brackets at the start and as well at the end. So if you go and now execute it, you will get the same results. This is as well a sequence of numbers and Python going to treat it as a sequence. So we could use as well list in our example. And by the way, for example, let's go and remove the five and add a string value like high this.
And now if you go and execute it, you will get the last one round high. So there is no rule in the for loop that the items must be either be numbers or string values. You can go and do like a mix. Okay. The next one that we could use is a string. A string value. It might like looks like one value, but actually it is a sequence of letters. So each character, each letter is actually one value. That means we can go and iterate through a string value. Let's have an example. Okay. So let's try this out. I'm going to go and remove the whole thing over here. Let's have a value like Python. So this time we have like one string value and we going to go and iterate it. Let's go and check that out. Now as you can see each letter going to be like one value and Python going to go and iterate through the whole string. And of course if you have here some spaces it going to be considered as well as a character. So you're going to execute it. you will get for the first round a space. So with that you can see our string value is not only one value. It is actually a sequence of characters that has a start and an end and you can use it in the for loop as a sequence. Okay, moving on to the next one. What else we could use in the sequence is that we can use a built-in function called range. So let's say that you would like to iterate like 100 times. It's going to be really annoying to make a tpple with 1 2 3 4 and like you define 100 values, right?
Instead of that, we could use this built-in function range to generate a sequence of numbers. Now, in this function, we can define many things.
Let's start with the one that is very common. We specify a stop. So, we use the stop to tell when it must stop generating numbers. For example, if you go and say range five, the sequence will looks like this. It start with zero 1 2 3 4. So that the total generated numbers are five. And you can see we don't have the number five in the output because otherwise it's going to be six numbers.
And this is might be confusing for many where they think if you add five you will see it as well in the output. Well actually the stop will not be included because it starts from zero. So let's try this out. So now we have here again our sequence of numbers using a tpple.
Now let's go get rid of this and instead of items we will use the function range and we have to pass for it a value.
Let's pass the five and try this out.
Now as you can see the output we start with around zero 1 2 3 4. So this is really nicer instead of writing like five numbers right now if you go and say 100 and execute it's going to be very simple it's going to go and generate 100 numbers and with that you iterate our code 100 times. So now let's go back to our five example here and execute. Now let's say that this is not what exactly I need. I would like to start from the round one not from zero. So how we can fix this? Now we could use in the range another argument. We could use the start argument. This is optional. If you don't specify anything, it's going to be always the zero, right? But now we want our start. That's why we going go and specify the start by for example one.
And with that we are forcing Python to start generating from one. So now if you execute it, we will get a sequence that starts from 1 2 3 4 and then it stops.
it will not include the five in the sequence because it doesn't include the stop. So by looking to this the start will be always inside your sequence but the stop will not be inside. So let's go and try this out. Now if you go over here and say okay start from one and execute. Now in the output it looks nicer. We are starting from the round one as we want. But we have the issue.
We don't have any more the round five.
Now in order to have it in the output we can go and increase the stop to six. So if you go and execute it, you will get the round five and we don't have the six of course. Okay. Now moving on to the final argument that we can pass to this function. It is the step. It is as well optional and we use it in order to define or specify the incrementation. If you don't include it, it's going to be always the one. So we always increment our sequence with one. So let's say that I would like to go and generate all the even numbers between 0 and 10. And now what we can do, we can specify the step two. So now the incremental going to be two. Let's see what's going to happen.
So it start from zero and then the next generated number it will not be one because the incrementation here is not one it is two. That's why the next number going to be two. Then the next in the sequence going to be four. Then six and eight. And now of course we will not have the 10 because the 10 is our stop.
Now let's say that I would like to go and generate all the odd numbers between 1 and 10. So we're going to have the same setup but this time we start counting from one. So now we start from one then the next one 1 + 2 going to be three and then 3 + 2 going to be 5 7 and then nine and with that we are generating odd numbers. So it looks nice. So let's try that out. I'm going to go and add the stop 10 over here and execute. You see we have all the numbers between 1 and 10. Then let's go and add the step two and execute. You will have only the odd numbers. So as you can see the iteration is less than before because we are incrementing with two.
Now if you go and say let's for example four and execute you will get only three numbers. So the step is bigger in the sequence. So far we are generating only odd numbers. Now let's say that I would like to start from two and end at 10 and the step should be two. So if you go and execute it you will get now even numbers. So it is very simple with those three arguments we can define exactly how the number is going to be generated.
So this is how the arrange function works. So those are the famous sequences that we usually use. But you can as well use any other sequences like for example the dictionary or maybe files. So any object in Python that is iteratable you could use it in the for loop.
Okay. So with that we have learned how to build a loop. how to build iterations inside our code. And now of course always the question is why do we need those stuff? How to use the for loop in real projects. So now in the data world usually we have like files or tables that are in one place and we would like to go and move all those tables from one place to another place. Now imagine that you have to write a code for each table.
So if you have like 100 tables, you're going to write like the same code 100 times. Now instead of that we can go and build a loop in order to go through all the tables one by one and start loading it to the target place. So we write the code once and we iterate through all tables and of course it could be files as well. You might have like 100 files and you would like to move them from A to B and for that we usually build a loop. Now if you are working with data we might go and let's say clean up those files those tables and if it is always the same action that we would like to use what we can do we can use as well a loop. So we go again and iterate through all tables and do the same data cleansing. So with that we are preparing the data before maybe analyzing it. Now of course not only tables and files we can go and iterate through columns. So let's say that we have in our table multiple columns and there are bad quality in the values of the columns like for example we have leading or trailing spaces. So of course in Python we could use the strip function in order to clean up the data. So now instead of applying this function for each column inside our code you might have really big file or table with hundreds of columns. Instead we make a loop for all columns and we iterate through all the columns of our table where we apply the strip function. So with that instead of having hundred of lines, you have only two lines of code, one line for the for loop and the second one for the function. So as you can see this can be like foundations for you as you are working with data. You use it in order to load a data. We use it in order to do data preparations, data cleansing and many other stuff. So the for loops are really amazing. Okay. So now I'm going to show you a few use cases where I use for loop. So we usually use it in order to do aggregations like summarizations finding the average. So let's say that we have here scores a list of scores and here we have few values like 80 50 60 and 75. Now I would like to go and find the total of those scores. So we can go and create new variables in order to store the total score. And now in order to do the summarizations of all those values we can go and use a loop. So we can say four and then I need a variable.
So it's going to be score in the sequence scores. And of course, don't forget the double points. And now what we going to do? We're going to say total is equal to the current total plus the scores. So we are accumulating the values we are getting from the loop and we are storing it to the total. And of course, as we learned in the operations, we have some shortcut in order to do this. We could add a plus before the equal and then just remove this. So I am adding the scores to the total. And now we can go and print few stuff like for example we can say the current total and then we say the total and outside of the loop I would like to see the final total. So final total and as well here the variable total. So let's go and try that out. We got an issue because this is wrong. You cannot use the scores the sequence. You have to use the variable.
So the score not the scores. We use the scores only for the iteration. So let's try again. And now as you can see it is working. So first we have the current total 80 then plus 50 130 190 and the final total going to be 265. So as you can see we can use it in order to do some aggregations. Okay to the next one.
As we learned we can do data cleanup data preparations using for loop. So let's say that we have a list of files like this. So the first one going to be reports dot CSV and the next going to be data dot CSV and the third one final txt. And let's go and add some evil white spaces on lefts on rights in order to have some bad data quality. So now here we have two issues. We have the leading and trailing spaces and as well you can see we have sometimes lowerase and uppercase. Now I am a data engineer or analyst and I would like to go and clean up the data before I do something about it. So here I have to do two things. First removing the spaces.
Second making everything either lowerase or uppercase. So depends on the rule that I follow in my project. I have to change the cases. So in order to do that we could go and create a for loop. Going to say for file in files double points.
Now what are the action? We're going to say file is equal to file dot and we have now to start clean up the data. So we can start with the strip function to remove the white spaces. And now in order to test it we're going to go and print the results. So we're going to say processing and then the variable name file. So now let's go and test this out.
So now by looking to the output you can see we don't have any more any leading or trailing spaces. So everything looks fine. And with that, we have cleaned up the names of our files. Now, we are not done yet. We still have this lower and upper cases. Now, we can go and add the second transformation. And I would like to say I'm going to go and make everything lowerase. Now, if you check the output, it looks way nicer.
Everything is lowered and everything is following our rule. And another thing, let's say that I would like to go and get rid of the txt. Actually, we only allow CSVs. So in order to do that we add another transformation and we say replace. Now the old value going to be txt and the new value our standard is csv. Now it is very important you don't write it capitalized because we are doing this the replace after we made everything lower. So it's always like this. First clean up the data and then do data transformations where you adjust and manipulate the data. The order is very important. first clean up and then manipulate. So now if you go and execute it with that the final is as well CSV.
So this is really amazing. We write our data preparations only once and then we build a for loop in order to iterate through all our data. And this is exactly the power of the for loops and as a data engineer analyst scientist you have to do a lot of data preparations in your pipelines and you will end up using those for loops many times. So make sure to understand those concepts. They're going to be very important for you. And my friends, it is challenge time. So you have to go and print seven times table from 1 to 10. But you must use a loop and it must look exactly like this output. Don't miss one character.
Okay. So if you are done with this, I have for you a second challenge. You have to print the following left aligned star pyramids. So the first line is one star. The second is two, three, and the last one should include six stars. And of course, my friend, you must use for loop for this. So go and solve that.
Okay friends, now so far we have been looping through items using a simple way, very simple design. But now we're going to level it up and we're going to build and design an advanced loop. In Python, they are like tools or add-ons that we can put in our loop in order to have more control like when to stop, when to skip. And here I'm talking about the special loop statements. We have break, continue, pass. So let's start with the first one, the break [music] statements.
The break statement [music] is used in order to immediately stop the loop. No matter where you are currently in the loop, you will exit. You will stop everything and go out. And once Python sees it, the loop ends right there and go to the next line of the code. So let's understand how this looks like. So now by looking back to our simple design of the for loop, you can see that Python can execute all the items inside our sequence without stubbing at all from the start until the end. There will be no break. There will be nothing. Unless of course your PC crashes. So everything going to be executed. But now sometimes we would like to have a check. If a certain condition is met, we have to do something. So that means we have to build a condition using the if statement. And we have of course to do it inside the for loop. So let's say that if the item is equal equal to two, something should happen. So now by looking back to our flowchart that means we are building a second condition inside our loop. So again the first condition comes from the for loop. it is something built in and we didn't specify it. But the second condition comes from the if statements that we defined inside the for loop. And of course the answer could be false or true. So now we have to tell Python what can happen if the condition is fulfilled. Of course you could like do something like print or call a function. But if you want to affect the iterations of the loop we could use one of the three statements.
So we could use break, continue or pass.
So now since we are talking about the break if you go and use it what going to happen Python going to put a break if the condition is true and if this happens everything going to stop and Python going to go immediately to the end but if the condition is not fulfilled everything is like before Python has to go and print the item. Now let's see how Python going to execute it behind the scenes. So it's going to start from the beginning and ask for the next item. So that means we are not at the end of the loop and Python going to continue. Now Python going to ask the second question. Is I equal to two?
Well, it's not because 1 is not equal to two. That's why it's going to go to false and Python going to print one in the output. Then jump back to the top and again ask the same question. Are we done? Well, the iterator going to answer with a value. We are not done yet. So that's why it stays inside the loop and go and ask the second question. Is I equal equal to two? Well, this time yes, this is true. 2 is equal to two. And that's why Python going to go to the break statement. Now what's going to happen once Python sees break? It's going to destroy everything and stop everything. So there will be no more iterators and Python jump immediately to the end and it can ignore everything else. So with that we didn't iterate through the whole sequence. So the iterator is not done and we have only one successful round one iteration. But because we have fulfilled the condition and the break statement everything stops and in the output you will get only one.
So as you look to this, it's like we are building an emergency exit right from the loop. If something catastrophic or emergency happens, we have to stop everything that we are doing and get out. This is exactly how the break statement works in Python. Okay. So now let's have an example where we have list of names. So we could have something like John and Mariam. And now we're going to have something empty. So we don't have any value inside it. And then let's say Kumar. Now you can see we have four values but one of them has an issue we have here empty. Now let's just make a normal loop in order to print the names. So we're going to say for name in names and now let's go and print. So we're going to say f name equal to our variable. So name let's go and execute it. Now as you can see we have all the four values and the empty one even. Now let's say that if there is something empty in my data I don't want to continue. I want to break the loop and to stop the program. So now in order to do that we have to make a check. So first we're going to say if name is equal equal to empty. So with that we are checking whether then it doesn't have any value double point. And now we hit enter and print a message. Now as you can see we are now at the second level of the intendation. Print is inside if and if is inside for. You have to be careful that. So we can say empty value detected.
And of course this is not enough. We want to stop everything. So we're going to write a break. And now very important to understand the break must be inside an if otherwise you will break from the first iteration. So now let's go and try this out. So now in the output you can see we processed two values John Maria and then once we processed the empty this condition was true and we went inside it. So we printed the message empty value detected and then the break is executed. So that means we stopped over here and we didn't process those two values. So they didn't have the chance to be printed over here. So this is exactly what's going to happen if you use a break inside your full loop.
Now let's go to the next one. The continue statements. We use it in order to skip one loop cycle. So that means once Python sees the continue, it going to stop right there. But instead of ending the whole thing, the whole loop, what going to happen? It just going to jump back to the top and start the next round. So that means it is not that catastrophic like the break where it's going to stop everything. We are just skipping the current round and go to the next one. So let's have a very simple example to understand it. Okay. So now in our code after the condition this time instead of using break, we will use continue. So now what going to happen behind the scenes is that if the condition is true of the if statements we will have continue instead of break.
But here comes the important points after continue we will not go to the end we will just go jump back to the top. So we're going to have an arrow from continue back to the start of the loop.
Of course with that we are not saying we are going to the beginning of the iterator. So we are not resetting anything. It is just simply we are skipping the current round. So let's try this out and see what can happen if you do that. Now Python starts from the top and ask the iterator for the next item.
It's going to be the first one. So the value number one and then go and ask the second condition is one equal to two.
Well, it is false. That's why Python going to go to the right side and execute the loop as normal. So in the output you will get one and then jump back to the top to repeat and ask the same question. What is the next item?
The iterator answer with two and then to the second question. Is 2 equal to two this time? Yes, we have fulfilled the condition and Python goes to the left side and execute continue. Continue means skip the current one and just go to the top and go to the next round. So with that we didn't do the normal iteration. So Python will not execute the print and you will not see the value two in the outputs. So we skipped the two because it is fulfilling the condition and Python has to start again from the top and ask for the next item.
It's going to be three and then ask the second question is three equal to two.
Well, it is false and then go to the right side to the normal loop. It's going to print the value three and then go to the top and ask for the next item.
Well, we are at the end. The iterator ask Python to stop. So that the answer going to be true and jump to the end of the loop. So as you can see now in the output we have one and three but we skipped one round. So it's not like the break where we stopped the whole iteration and we stopped everything and went to the end. We just skipped one single iteration because it was fulfilling the condition. This is exactly what continue means. Okay. So now back to our example where we have the four names and one of them is empty.
It was too much you know that once we founded the empty over here we stopped the whole loop because I would like to process as well commar over here. So what we can do instead of having break we can say you know what just continue on continue and with that we will skip only this one bad value but we will continue the loop. So let's try this out and execute. As you can see in the output we have now name is equal to kumar. So we processed this value and we still see our message empty value detected and this comes from here. So that means yes we found the bad data but we skipped it. We didn't process it inside the branch and with that we guarantee we are processing only the good data. And in this scenario, I'm going to say continue here is more suitable than break because if I'm using here break, it is like an overreacting.
I would like to process all other values. It is only bad value inside my data and I would like to process all others. And since this is not really critical, I could go with the continue instead of [music] break. Okay friends, now we come to a little break and I would like to give you a little tip. If you want to master something like let's say mastering the functions or the data structure, I really recommend you to try to teach it. So try to explain it to a friend to maybe a colleague at your work because once you start teaching something, you're going to understand the gaps in your knowledge and you start filling those gaps by maybe researching or watching one of my videos. So if you want to master something, teach it. Now that's it. Let's go back to the course.
So that's all for the continue statement. [music] Now let's go to the third one. We have the pass statement.
Pass is just like placeholder where nothing happens. So this sounds a little bit strange but all what we are saying here that I will put here code later but for now just keep going. Don't bother with the past statement means for now do nothing. So let's understand what exactly this means. All right. Now let's go and put pass inside the if statement.
So that means instead of continue we're going to have pass and this going to be executed if the condition is true. Now of course what is very important is where the arrow going to go after the pass. Well this time it will not go to the end. It will not go to the start of the loop. Well actually it's going to go to our block of code. So it's going to go back to the normal way of our loop.
Now if you look to this do you see something strange? Well it doesn't matter if the answer is false and true.
Both of them are leading back to our block of code. Right? So that means we are not stopping anything like the break. We are not skipping anything like the continue. Everything going to stay as it is. So the big question is why we need it. Well, we don't use pass very often in our coding. But we use it as a placeholder as we are planning the next steps. So for now in my code, I am sure that I have to check for this situation where I is equal to two. But I still don't know what I'm going to do if the condition is true. So it's like I'm planning something inside my code. I don't want to forget about it and I'm building something temporary and the final solution should not of course include pass. Now let's have a quick example to see how this can work for the first value one. Well is one equal to two? The answer is false. Python going to go and execute the print normally and we will see one in the output and then go back to the top and ask for the next item. You will get two. Well, is 2 is equal to two. This time we will go to the left side and execute the pass and nothing going to happen. Python has to go back to the print and print in the output two. So you see we are not skipping, we are not ending anything.
Then go back to the top and ask for the third item. You will get three in the output like usual. And the next one the iterator will give you no value and ask for stop. That's why button going to end and go outside. So as you can see it's like you are iterating without having the condition. You iterated through all the values 1 2 3. So again pass means just keep doing what you are doing. Keep doing the iterations and maybe later we're going to add something in this place. So it is like placeholder. So now back to our example where we have the four names and one of them is empty. The thing is this is yes an empty value but I don't know yet what to do with it. I could go and replace it with dummy values like unavailable, unknown, maybe some flags, but still I don't know what to do with this. And in the other hand, I don't want to skip it. I don't want now to break the whole program or skip this step. I would like to decide about this later. So what we can do, we can go over here and say, you know what, we're going to make a pass and then I'm going to go and write a note for myself so that I don't forget about it. So I'm going to say to-do handle empty value.
And to be honest, I don't want to print anything in the output because we don't want to see this yet. So it is just something for me that I would like to plan for the future. But at least I know about [snorts] this scenario. I know there is like an issue like this and I have already like a condition for it to check it. But I don't know yet the action. So now if you go and execute it, you can see we're going to go and process all the names that we have in our list. So we are not skipping anything. Maybe after a week I meet my team and then we discussed about this issue. I told them yeah we have like some empty values in our data and I would like to know how to handle it and then we agreed all to have dummy value like unknown. So now I come back to my code and say you know what let's go and do it like this name is equal to name and replace and instead of empty we're going to have unknown. So it is like this and I can go and remove now the pass we don't need it. I know the action and execute it. You can see now in the output I'm getting the unknown. So we didn't skip, we didn't stop anything. We just prepare it for the next steps. And this is exactly how we use this statement pass in our code.
So now let's have a real examples on how to use continue and break. So the first task it says loop through a list of days and print only the working days skipping the weekends. So now let's go and create a list of days. So for example let's have a Monday, Sunday, Wednesday and let's have it Tuesday just randomly some days. So now we want to print in the output only the working days. So we don't want to see any weekends and for that we can go and create a for loop. So we're going to say for day and days and then double points and now we're going to go and print the work day double points and then our variable day. That's it. Let's go and execute. And now in the output you can see we are printing everything even the weekends which is not really what we want. So we have to check whether the day over here is a weekend. Now for that we have to go and create an if statement and we can say if day in and then we're going to have our two values Saturday and Sunday like this. Now if this is the scenario what can happen? Should we break or should we continue? Well as the task says we have to skip the weekend. We don't have to stop everything. Yeah. So it's not that catastrophic. That's why we're going to go with the continue. We want to skip those days otherwise go and print the day. So let's try this out and execute.
You can see we have Monday, Wednesday and Tuesday but the Sunday is not in the output and that's because it is fulfilling our condition here. So as you can see it's very simple why I decided for the continue because I just want to skip I don't want to stop. So now one thing quickly about the styling as you can see again I'm always using the same name. So day in days and one more thing try to not put a list in conditions or maybe in the for loops. So as you can see for the days I always define a variable outside and for this as well here I'm going to go and take it out and make a variable for it. So we're going to say weekends and we're going to put it outside and then over here we're going to say weekends. So now as you can see it is now nicer and shorter and easy to understand for day and days if day in weekends. So it is easy to understand and you can hear extended outside easily than extending stuff inside the for or the condition. then things going to be easy to write and easy to read. So with that we have solved the task. Okay. So now let's have the following task and it says scan through a list of email addresses and prevent unsafe data from entering your system. So now as usual we're going to go and create some simple data. So emails like for example [email protected] and another one bar at Outlook D. And then we're going to have something strange like drop table users and semicolon. And then we're going to have something like mariagmail.com.
So now let's say that I would like to process my data and I'm going to say for email in emails and we're going to print processing email and then double points and our variable like this. So now let's go and execute it. So now in the outut you can see we have processed all the emails but actually we are not doing the job. The task is to check whether everything is safe and my friend this is not safe at all. We have here an SQL injection. SQL injections it's one of the most famous hacker attacks on your applications that could happen if you have any field where the user is entering data. So maybe in the registration form or in the sign in you have a field to enter the emails and with that the hacker is going to use it in order to add an SQL injection to destroy your database or maybe to read important informations and actually it is one of the easiest way in order to hack your system. That's why most of the systems must protect their data and their applications from those SQL injections. Now we can go and check whether the email includes a semicolon.
Semicolon is indicator that there is an SQL statement, there is a command. So we can go and check for that for say if we have a semicolon in email. So what's going to happen? We're going to print and we can say something like this. SQL injection and hacker attack. So with that we get some panic. Let's go and execute it now.
Now you can see in the output we are getting the message right but still we are processing it which is really bad.
We have to add here a control flow statement. So again continue or break.
Well I would say I will not trust the data at all that I'm getting from this source. That's why I'm going to stab everything and I'm going to go and use a break in order to protect my system. So with that as you can see we are processing only the first two emails and everything else going to be stopped. As you can see in this example we have high critical risk in our system and we use [music] break in this scenario.
Now let's have a quick recap and compare them side by sides. The first statement is the break where we add a condition and if this condition is fulfilled we execute the break and we exit the loop.
So you are building like an emergency exit if something goes really wrong.
That means all other values will be totally ignored. We have to exit immediately.
>> OH MY GOD. OKAY, IT'S HAPPENING.
EVERYBODY STAY CALM.
>> WHAT'S THE PROCEDURE EVERYONE? WHAT'S THE PROCEDURE?
CAR.
>> We usually use this only for high critical risks like for example security issues as we learned about the SQL injections. If you encounter this then you have to stop immediately otherwise you might destroy your system. Now moving on to the next one to the continue. This one is not that dramatic.
If the condition is fulfilled we're going to say just skip this iteration and go to the next one. We will not stop the loop. And of course we use this for medium risks cases like empty data, bad rows, empty files and any other weird special cases that you might encounter and you say okay just skip it. Now finally to the chilliest one of the three we have the pass. So for this one if the condition is fulfilled it's going to say you know what just keep doing what you are doing just execute the codes and do your loop do your thing. So it is like a placeholder and you say you know what I have planned for this but for now you know what just keep walking don't skip don't stop anything. So again break if you have something critical go out now the continue if you have a medium risk and you say skip this one and the pass it is just for the planning and you say do nothing.
So we use the else statement in order to run a block of code only if the for loop finishes naturally normally without hitting a break. So all what we are seeing here okay I went through the whole loop without any interruptions and at the end I'm going to do something extra. So let's understand what this exactly means. Now as you remember we used already an else statement but together with the if statement as we are building the conditional logic. So we use the if statement together with the condition and if it is true then we print for example okay and then after that we said okay if it is not true we could use the else statement in order to define what can happen if the condition is false and for example we say print not okay. So with that we use else in order to define what can happen if the condition is not true and then based on the execution either we go to the right side or to the left side. But now if you looked at the force statements, we said, okay, we have a sequence and we're going to repeat the same code multiple times until we don't have any value. And once the loop completed and we went through all the items, then we go and exit to the end. Now, if you look to this, you can see after the loop ended successfully, we don't have anything to do. It will just go and exit. But now, instead of that, what we can do, we can add an else statement. So, we put it outside the for loop. It's like you are doing if else and then you define what going to happen like for example print end and with that we have defined an action that's going to be executed once the loop is completed. So as you can see it is not exactly like the if else statements in the if else we went either to the right or to the left with the four else we're going to do everything we're going to iterate through the whole loop and as well at the end we will be executing the else as we are exiting to the end. So now let's go deeper in order to understand how this exactly works. So again we have this example where we have the three values and now we add an else statements where we print an end. So this block of code going to be executed if we reach the last item. So now if you go and execute it the usual going to happen. So Python going to ask okay what is the next item? we will get one and then we're going to go and print it then ask again to the next item we will get two then print it again and then to the last iteration and then it's going to go and ask again for the next item we will get three and print it the outputs as you can see so far we didn't execute yet the else statement because we are looping now going to go back to the top and ask again what is the next item iterator going to say stop because we don't have anything left and now the condition is true that means Python going to exit the loop but Before going to the end, we still have an else statement. So, Python has to execute now the else and we will get in the output an end and after that it can go to the end and stop everything. So, as you can see, Python executed the else statement at the end after it went through the whole loop and iterated through all the values in the sequence. So, this is exactly how the else statement works with the for loop and okay. So, now let's have a very simple example. We're going to go and create a classical for loop. So let's go and create few items like numbers 1 3 4 and 7 and then a very simple loop. So for i in items and in the output we will print the i. So let's go and execute it. As you can see we have printed all the values in our sequence. So nothing fancy so far. But now what we can do we can go and add an else statement. So now let's go to a new line and be careful. you have to place the else at the same level as the for not inside the for loop. So we're going to say over here else double point and let's print something like loop is completed. Now let's go and execute it.
Now as you can see at the end of our iteration we have this nice message saying loop is completed. So this is actually how you add an else statement to your loop. But here we have an issue.
Our code now makes no sense at all because what we are doing here we are looping through the items and at the end we are printing a message. Well actually the else here is totally useless because what I can do actually I can go and remove it and it is just simply like an instruction after the for loop. So after Python is done with the loop just go and print the loop is completed. So if you go and execute it you will get exact same results. So actually having an else like this is totally pointless and has no sense. And now of course the question is if this is useless then why we have an else statements in the for loop. Well my friends there is only one scenario where the else statement makes sense if you combine it with the break statements. You have to combine the break and the else in order to have a real usage for the else statements. So let's understand [music] what this means.
Okay. So now again back to our example as we have a normal for loop with a break. Now if you go and add an else statement at the same level with the for loop and we say for example print end.
So with that we are adding a block of code. If we reached the last item in the loop. So if this is true then print the end and then exit. So now let's see how Python going to execute it. As usual Python going to ask for the first item from the iterator. We will get one. Then it's going to check the condition is 1 equal to two. Well, it is false. That's why it's going to go to the print and print one in the output. Then it's going to go back to the top and ask again, give me the next item. It's going to be two. Is 2 equal to two? Well, this time my friend going to be true. And Python going to go to the break statement and exit the loop. Exit everything. And with that, my friend, you will get in the output only one value, the one. So, Python didn't execute the next value in the iterator, the three. And very important, Python didn't execute the else statements. And that's because my friend, the condition last item was never fulfilled. We didn't get true.
That's why Python didn't has the chance to go and execute the else statements.
And this is exactly why we need the L statements. We use it as an indicator to understand whether we iterated through all the items or we encountered an interruption. So as you can see the break and the else statements they work together in order to define this logic.
So actually it looks like this. Now we have a for loop and we want to repeat something over and over until the loop is finished and once the loop is completely successful we do something else using the else statements and then go straight to the end. So this is the normal flow if everything goes okay.
Otherwise, we can go and build an if statement that we're going to check in each iteration. And as long as this condition is not fulfilled, we stay in the loop. But if this condition is fulfilled, then we make a break in order to exit the loop and then go straight to the end. So now as I am drawing this, the break statements really work hand inhand [clears throat] with the else statements and they look like exactly the L statements. So either the loop is completed and we execute the else statement or the loop is actually broken and we are exiting using the break. So now really I understand now exactly why we are saying else cuz either it's broken or else it is completed. And this is exactly my friends why I love doing visuals because I understand then the logic how things works and this is exactly what I do usually. I draw the logic and then start coding the solution. Okay, so now back to our example where the else statement is totally useless. Now we can make a usage of it. I would like to go now and build a check to understand whether in my list we have an even number and if we don't have any even number then we have to print in the output that all the numbers are odd. So we have to go and make a very simple if statements and we're going to say if the remainder when we divide the item by two is equal equal to zero then actually we found an even number. So we're going to go and print something like this. Instead of the I, we're going to say even number found.
And then we're going to go and print this item. Now after that, we're going to go and break the for loop. And if we didn't find any even numbers, that means all the numbers are odd. So we're going to say all numbers are odd. So as you can see, it sounds like an L statement.
Now let's try this out and execute. You can see even number found and we have the four. Let me just add here a double point. So what happens here that Python did check the first number one. Well, it is not fulfilling the condition and nothing happened. Same thing for three.
But for the four we have a true. That's why Python did execute this pluck of code. So it printed the message and then executed the break. That means everything stops. And in this scenario, Python will not execute the else statement. Again the else statement is going to be executed if Python did manage to iterate through all the items completely without hitting the break. So that's why in the output you don't find this message over here. Now let's go and test something else where we going to have all numbers are odd like this. So we don't have an even number and then execute. You can see in the output we get all numbers are odd. So that means Python did try all those numbers and none of them fulfilled this condition over here. So it didn't manage to execute the break. That's why the loop was completed and Python didn't execute the else statement. So now as you can see the else statement here is totally useful. We use it in order to understand that Python did iterate through all the items and Python didn't execute the break and we didn't have any interruptions. And as you can see it only works if you combine it with the break [music] statements.
And now we come to the question that why do we need those stuff and when we going to use it in real projects? Well, the main use case is that we use it in order to search and validate our data. As we are getting new data, we have to search for bad data and report at the end whether everything is clean or whether we found some bad data and bad quality.
So let me show you now some real use cases. Okay. So now let's have an example. Let's say that I have a list of names and I would like to check whether we have any missing values. So let's have a very simple list of names like for example Kamar and then Tuba and the third one is actually missing. We have a none and then the last name going to be Monica. Okay. So now I would like to go and check whether we have any missing values. For that we have to go and iterate through the list. So we have to go and create a for loop for name and names double points. Now we're going to go and build a condition in order to check whether the name is empty. And for that we're going to use an if statement.
So if the name is none then what we're going to do we're going to go and print a message. We're going to say found a missing name. And after that we don't want to continue. We have already found an issue in the datam and that's enough for now. So I want to stop and exit the loop. Now let's say that we have checked all the names and we didn't fulfill this condition. So we didn't find any missing values. Now I would like to print a message saying that everything is okay.
And for that my friends, we use an else statement. We're going to say all names are available. Okay, let's try this out and execute. And now in the output, you can see found a missing name. That means it is working because we have here the third value as a none. And this made our condition fulfilled and activated the print found missing name and as well the break. Now let's try another thing like for example we go and add a name here like Mariam and then let's go and execute. You can see in the output we are getting all names are available because all names are there and this condition is never fulfilled and since we iterated through all the values of our list successfully we got at the end this print message from the else statements. So as you can see we are using the break together with the else statement in order to search for something and validate our data and this is something that is very common as you are doing data quality checks and quality assurance. Okay. So now let's have another example where we use this.
Let's say that I would like to check whether all my files are CSV. So that means we have to validate the type of the file and this is something that happens a lot as you process files.
Okay. So now as usual we start with an example. Let's have few data like for example we have a file name called data 1 dot CSV and another one we have report dot maybe PDF and the last one let's say that report 2 CSV so now as you can see not all my files are CSV we have one of them as PDF which is not really good now we have to check that so we're going to go and create a for loop for file in files so now we're going to go and create our checkpoints and we are checking the files what we are searching for is actually the last characters of each string. So that means we're going to go and use the methods ends with and we have to pass for it our expectation.
So it's going to be dot CSV but actually we are searching for bad data. So that means we have to search whether the file does not end with CSV. So we have to add the not operator. So what happens if your file is not CSV? First, we're going to print a nice message and we're going to say for example, let's print the file name. So, the file is not a CSV. After that, we want to break. So, we found something that is not correct and we end the loop. Now, otherwise, if everything is fine, we have to just print successful message. So, else print all files are CSV. So, now let's try this out. As you can see, we are getting report. PDF is not a CSV and that's because it is fulfilling this condition and this block of code is executed. Now let's try the other one. So if you remove the PDF to CSV and execute, you can see all the files are CSV. So as you can see again we use the break together with the else in order to check the quality of our data. And now there is like one thing that I would like to show you. If we say okay this is a PDF and we have another one. Let's say it is data 2.xt.
So we have two cases where it is bad.
Now if you go and execute it as you can see we are getting in the output only one. So report PDF is not a CSV. So once we cached the first issue the Python going to break the loop. So actually we are not getting the second file over here and that's because the loop stops.
But now if you say you know what we can get as well the second one if you remove the break and you say you know what let's go and use the continue. Well let's see what going to happen if you do that. And now if you're looking to the output now we are getting the two files reported PDF and data2.txt but the issue we are getting this as well all files are CSV which is not correct. So actually this makes no sense and that's because you cannot combine the continue with the else. It's going to work only if you use a break. So the continue what's going to happen it will just skip one iteration. It will not stop the whole for loop. So actually you cannot do that. It makes for me no sense. You have to go and use a break over here and with that you will get only one file. If you are not happy about it you can just go and say here not all files are CSV and then execute it. So that we are not reporting as well where we have the issue. So this is how we work with the break and the else statements with the for loops.
And now my friends as usual I will not leave you without a challenge and we have a tricky one. So the challenge says check the following list of file names and find out whether we have duplicates or not. So if you found duplicates you have to tell us about it. Otherwise if everything is fine just say all files are unique. Now I'm really interested to see how you're going to solve it. So let's go. All right.
All right friends. So now we're going to talk about something called nested for loop. A nested for loop it is very simple. we have a loop inside another loop. So that means we have a big loop.
We call it the outer loop. And for each run of this big loop, we going to run completely smaller loop. And we call this one an inner loop. And once this small loop is completed, we go to the next iteration of the outer loop. And the same thing going to happen. We going to run again completely this small inner loop. And again once the smaller one is completed we run one more iteration for the bigger loop. And of course we can keep nesting loops inside another loop.
So we could have like very small loop inside the middle one. So this is exactly what we mean with the loop inside another loop. So now let's see how we going to build it. Now we have this classical for loop. Nothing fancy.
We are just iterating through a sequence. Now in order to make a nested loop we define a new for loop but inside the first one. So for example for y in a sequence like one two and let's call the first one an x so it sounds better and with that we are iterating through two loop variables the x and the y and we are printing both of them the x and the y now about the chart python has like two loops so they are connected like this but we are not finished yet once we reach the end of the second loop it will go back to the start of the first loop and of course once everything is completed we will go to the end. So as you can see the start and the end of the second for loop is actually completely connected to the first loop and again we call the whole block as the outer loop and the block of code that is inside it we call it an inner loop. So the second loop. Okay. So now once you execute it Python has to go and create two iterators one for the outer loop and another one for the inner loop. So now Python start with the outer loop and ask for the next item and iterator going to answer with one since we are getting a value we are not at the end that's why Python now has to go and start the inner loop. So here Python going to ask for the next item in the second iterator in the inner [clears throat] loop and here as well we will get one. So since we are getting a value we are not at the ends and now Python has to go and finally execute the block of code print. So now in the print the x is assigned to one the y is assigned to one as well that's why in the output you will see one one and now python will go and start from the top of the inner loop and it's going to ask for the second item of the inner iterator we will get two and since we are getting value we are not done yet with the inner loop so it's going to go and print so the x is equal to one but now y is equal to two that's why you will see in the output one two so now it's going to go to the top of the inner loop and ask give me the next item. Now the inner iterator going to answer with a stop because we are at the end. So with that Python going to go and exit the inner loop and go to the end. But of course we are not at the end. Actually we will go to the top of the first loop.
So everything that we have done so far that was only for the first item of the outer loop. Now Python going to go and ask the outer iterator for the next item. It's going to be two. And guess what? two is now the last item of the outer loop. That's why it's going to go and start again completely the inner loop. So the inner iterator going to answer with one and you will have in the print now x is equal to two because the outer loop has the value two and the y is one. So you will have two and one and then the next value to two and the inner loop going to be done. then exit again the inner loop but you have to go again to the top of the first loop and the same thing happen you will go to the third value do the whole inner iteration and then go back to the top of the loop so as you can see this is the third time we are executing the inner loop and now Python going to ask for the outer loop give me the next item the iterator going to say stop I don't have anything left and finally you will exit the outer loop and go to the end and now we are at the end of the outer loop and everything stops. So this is exactly how Python executes the nested loops. Okay. So let's have a very simple example. We're going to start with the outer loop for X and range. Let's generate like for example three numbers. This is our outer loop, our big loop. Now inside it, we're going to add another loop. So for Y in range and this time for the smaller one, let's go with two iterations. So two numbers. This is our inner loop. And now we're going to go inside the inner loop and print. So I'm going to print for example between two parentheses. We're going to have the first variable the x and then separated with a comma and then the second variable. So something like this. Let's go and execute it. Now as you can see for each value from the outer loop we have two rows. So 0 0 1 then 1 0 1 1. And for the last one 2 0 to 1. So that's it. As you can see, very simple. This is how you can nest two loops inside each others. And of course, you can go and add a third loop. Like for example, for zed in range. Let's add as well two. So now we have to be careful. This print should be inside the third for loop. And here we're going to go and add our third variable, the zed.
So now let's go and execute it. Now as you can see we are getting like matrix of numbers. So as you can see we have a loop inside the loop inside the loop.
[music] This is really easy.
Okay. So now we come to the real talk.
Why do we need those stuff? When we going to use nested loops in our data projects and for that we have actually two major use cases. The first use case is for crossing and combining our data or we call it pairing the data. So it's all about we have two different lists and I would like to see all possible combinations of those two lists. So that means I'm going to go and combine and pair each value from one list with the other values of the other list. Okay. So now let's have an example where we have two completely different lists. The first one going to be the colors. So we have reds, we have blue and green. And another list it is about the sizes. So we have stuff like maybe L, M, and S.
Now let's say that I would like to go and generate maybe a product catalog with all combinations of colors and sizes. So I want to pair each color with each size. Now in order to do that, we can use the nested loops. And here it doesn't matter where you start. Like for example, let's start with the colors. So for color in colors. Next one going to be about the size as an inner loop. So for size and sizes. Now all what we have to do is to go and just print those two variables. So print where we're going to have something maybe like this maybe the first variable going to be the color and then after that I'm going to say size and we have the variable here size. So now let's go and execute it. As you can see for each color I'm getting all the sizes. So for the red, the blue and the green. So this is what we call sometimes cartasian or cross or combining the data where you pair all values with all values. So this is a nice use case for the nested loops. Okay. So now moving on to the second use case and the most important one. We use it in order to go through layers or we call it drilling into hierarchy. Let's have this sketch to understand this. Okay. The first example that we have hierarchy in the dates like for example we start with the years 2026 and 2027. Now for this we could make a for loop in order to iterate through all the years and this we consider as the first level and now we could drill down into more details if we go to the months. So that means we add another loop inside the year for month in months. So we are at the second level. Now we can go deeper where we go to the days. We are now at the lowest level at the level three where we add a third loop where we say for day in days and after that we have to go and do something about all those three informations. So as you can see we are using nested for loop in order to go through different layers in order to drill down into hierarchy. Let's have an example now. Let's have an example.
Let's say that I have to go and create multiple reports for each year for each month and as well for each day. And now instead of doing it manually, we can go and create four loops. So as usual, let's go and create our data. We're going to start with the years. So let's say 2026 and 2027. And the next level going to be the month where let's say that we have only two for example January and February. And the last level the days I will go and generate it using the range. So it start with one and ends with yeah 29. So now I have all my data and all what you have to do is now just to go and create the for loops. So the first one going to be for the years for Y in years. The next one for M in months and the last one for the days for D and days. Now we have our nested loops the three loops and all what you have to do for example let's go and just print the file name. So it's going to be f and the naming convention start with report underscore and then the year. So I'm going to go and get the variable of the year then the month. So the M and the last one going to be the day D. And as I said we are creating CSV files. So actually that's it. Let's go and try this out and execute. So now in the output we're going to get a huge list because we have here a lot of days. So we going to have all those file names that combines the year, month and days.
And of course we're going to learn later how to create files. But for now we are just printing it. So look at this. With just few lines of codes you can generate massive amount of data and as well you going to save a lot of manual work. And this is exactly why we are learning about the loops. And another example for this use case and this one is very common as you are working with data we navigate through tables and columns. So if you know some database concepts you know that we store our data inside tables columns and rows. So the table going to be the highest in the hierarchy and if you drill down to the next level you go to the columns. So each table contains multiple columns and now if you want to go deeper you go to the rows to the actual data and in order to navigate through this hierarchy we could use nested loops. So for the first level we could have like for table in tables.
Then we drill down to the second level for column in columns. And then to the last level where we say for row and rows and at the end as usual we have to do something like maybe cleaning up the data doing different preparations and manipulations to the data depends on what you are doing. And this setup you see now those three different nested for loops. It's something that you're going to do in each data projects. So if I open now my project, I'm always going to see those combinations where I'm iterating through the tables and then the columns and then the rows and then doing something. So now I'm going to show you an example that I just used in my project last week. And I was running an SQL query on multiple tables and columns just to check whether we have nulls in the tables. And now of course we will not talk about the SQL query itself. It's all about that I had huge list of tables and columns. And if I'm going to go and write those queries manually, it's going to take really a lot of time. Now, instead of that, I just wrote few line of codes in Python and it solves a task. So, let me show you how I solved it. It is very simple.
First, we're going to go and create a list of all tables that we are interested in. So, let's say we have the customers, we have orders and products.
So, here we make a list of all tables that I would like to query. And here, for example, the prices. Then next I need the columns that actually I am checking. So we're going to make another list for the columns. And here I was checking only for the ids and the creation dates. So those two columns are actually part of all my tables. And then I made really nice two listed loops. So I said for TN tables for CN columns. And now what we're going to do, we're going to take the query over here and add for it the variables. So I just made a print and then we start writing the query. So select counts star from but now instead of saying customers I would like to get the variable from the list. So instead of this one we're going to have the dynamic table name it's going to be the t and after that we have the static parts we have the where and then we have the column name and again I will not make it static. I'm going to go and get it actually from my list. The last part is actually static. So is null and then the semicolon. So as you can see I have converted my query into a dynamic query.
Now let's go and execute it. As you can see in the output I have automated everything. I got all my queries and for each table we have two queries. One for the ID and another one for the create dates. And with that I don't have to write anything anymore manually. I could just make few lines in Python in order to automate the whole job. And if I got in the future more tables and columns, I'm just going to go and extend it over here and then automatically generate my SQL query. And I know this sounds really simple, but this is what advanced data engineers actually do, especially if they are building systems like data warehouse, data lake, data lake house.
We will be dealing with huge amount of tables and columns and you cannot go and hardcode everything. So you have to load the tables from A to B. You cannot go and script something for each table.
Instead, we build something called metadatriven pipelines. We do exactly what we have done in this example where we write the metadata, table names, the columns and as well the data types and so on in variables and key values. And then we make a script like this over here where we iterate through the metadata and we do something like maybe copying the tables, changing the schema, doing few preparations and with that you will be saving as a data engineer or maybe as a data analyst if you are doing something huge in many tables. You save my friends a lot of time and I use it many times in my projects. So I know we are talking about the nested for loops but this is something very advanced and they're going to make your work highly automated. And another example about hierarchy in real data projects is that if you are working with data links like for example if you are storing data in Azure you might have like some containers and inside each container there will be different folders and inside those folders you're going to find different files and of course my friends we use nested for loops in order to navigate through this hierarchy so that we reach those files and then we load them maybe to somewhere else or we do some transformations. This is something that you're going to do a lot if you are analyzing those files or you are doing some data engineering about it. Of course, there are many other examples that you're going to encounter.
But the rule is very simple. If you have different layers, different levels and you have like hierarchy, you will end up my friend using nested loop in order to navigate through the structure.
So now we're going to talk about the second [music] type of looping in Python. We're going to talk about the while loop. So what is a while loop? We use it in order to repeat the same code over and over as long as a condition is true. So this sounds like the for loop, right? Now what are the differences between for loop and while loop? Here's the deal with the for loop. We have understood that Python going to go and create internally an iterator and as well a condition that keep checking whether we are at the last item of the iterator and as long we have values from the iterator we keep looping through the for loop and once we don't have any more values we go to the end. Now there is like two issues with this setup. The first one is that we are always predefining the sequence. So before we start iterating, we know exactly that we have here three iterations. So it start with a one and ends with a three and we know we're going to iterate three times.
So we know exactly how many times we going to iterate. The second issue is that this condition in the middle is as well something predefined from Python and we cannot change it. So it's always about whether we are at the end of the sequence and this is exactly why we have the second type of looping. We have the while loop. So this time we're going to define our own condition for the iteration and if our condition is true then we're going to go and do something and go back to the top. So that means as long as our condition is true we will keep looping and iterating. But the moment our condition is not anymore true it is false then we're going to exit the loop. The big advantage is that we define our own loop. So with that we have more freedom and flexibility. So we don't know up front how many times it will go. It is all about our condition and this sounds of course risky because we might end up easily in an infinite loop. So now by looking to those two side by side the difference is very simple. With the for loop we are iterating over a sequence like a range a list. So we are looping over something known but with the while loop we are iterating and looping over a condition that could change any time. So now let's dig deeper into the while loop. Now for the while loop we have as well like the for loop different designs and types and I'm going to put them into two categories. The first one we call it the while condition. So you define a condition and the loop going to keep going until the condition becomes false.
So this is the classical type but we have another design. We call it the while true. So this loop going to run forever. It is infinite. That's why we add to it together with a break statement in order to force the loop to stop. And of course, there is a reason for that. So those are the two main categories. And we're going to start with the first one, the classical one, the while condition. [music] So now we're going to start with a very famous use case where we build a counter using the while loop. We start with the keyword while and then we define a condition like for example I should be smaller than four and once you do that Python going to go and create for you a condition and as usual for each condition we have either true or false now since this is a while loop false means we're going to go to the end but if it is true we need to do something could be like for example print the I now if you do it like this after the action is done it's going to go back to the top of the loop so It is like two lines similar to the for loop. If you go and execute it, you will get an error because Python don't know what I is. So we have always to assign it first to a value before the loop. And of course we call this initialization of the while loop. Now once you do that it going to put it of course before the loop starts because we are outside of the loop. So now let's see how Python going to execute it. First it going to go and execute I is equal to 1. So it's going to create a variable and assign for it the value one and then enters the loop. So now it's going to go and check okay is I smaller than four. Well, it is true. That's why it's going to go and print the i in the output and then go back to the top of the loop and then ask again is i smaller than four. Well, nothing changed my friend. the i still equal to one and it going to fulfill the condition and then going to go print it again and go again to the top of the loop and going to ask again is i still smaller than four. Well, of course. So, as you can see, it going to keep looping. We are stuck in infinite loop because we never changed the value of i and the condition is always true. That's why it will never stop until it kills your PC.
That's why we have to add a new line inside our while loop in order to give a chance for the i to change. Like for example, we could update it by adding a one to the value. So that's the value get incremented by each iteration and we have lucky counter. So now let's see how Python going to execute it. First it's going to start with the initialization.
So it's going to assign one to the i and then it's going to go and enter the loop. It's going to go and ask is I smaller than four? Well, it is true.
That's right. Going to go to the print and print the current value is one. Now, next Python going to go to the second step and add the value 1 to the variable. So, 1 + 1 we will get two and then assign it to the variable I. Now, going to go to the top, but this time we have a new value for the I. So, is two smaller than four? Well, it is true.
That's why it's going to go to the print, print it in the output two and then update the value again. We're going to have now three in the variables and then go back to the top is three smaller than four. Yes. Go print it in the output and update it. So now our variable is equal to four and then go back to the top. Now it's going to ask is four smaller than four? Well, this is false. And finally, now Python going to break the loop and go to the end and exit. So as you can see, Python will keep looping and iterating as long as our condition is true until the condition change and turns to false. And this is the only way to exit our loop.
And this is how we build a counter in Python using the Y loop. So now if you look again to the code, we have here three sections. The first one outside of the Y loop, we have something called initialization where you define the initial value of the loop variable. And then after the while you define your condition and now inside the loop you do the thing that you want but the third component that is very important you have to build an update mechanism. So you have to change the value that we are checking in your condition. So if you don't do those three steps either the loop will not work or you will be stuck in infinite loop. And by the way, my friends, if you are enjoying this type of free tutorials where I'm sketching the concepts behind the scenes and showing the codes and you would like to see more like this, then support the channel by subscribing, liking, commenting. This really helps a lot. So now, let's go back. So now let's build a counter that's going to count from 1 to 5. Let's start first with the variable, the initialization. So we're going to assign for it the one. And after that, we're going to go and build our while loop. And now we're going to build our condition. So as long as the counter is smaller or equal to five, we have to iterate and loop. So we're going to say okay, print the count. And now the last step, we don't have to forget about making the update mechanism. So we're just going to go and add a one for it.
And actually that's it. Let's go and execute it. Now if you look to the output, you can see we have a counter that counts from 1 to 5. So with that, we have a nice counter. And of course we can control now with those values the whole counter we could make the step of two. So now if you go and execute it you will get in the output 1 135. So we have now bigger steps or we can say we're going to count until 10 not to five. So with those values we could control the whole counter and this is what we call counterbased loop. Okay. Now let's take things to the next level. Now look to our loop variable over here. This variable is getting the value from our code. It is something hardcoded and it start with the one. So there is no surprise how things can work over here.
But we can make things more dynamic where we ask the user for a value and then we check it and based on the user's input, we decide whether we continue in the loop or we exit. Like for example, we are building a system that ask a very simple question to the user. Do you agree? And the system going to expect from the user only yes, nothing else. So if the user answers like no maybe or I will think about it it will not accept it. We accept only yes only if you agree with the system. So it sounds a little bit like the system is intergating a user and it only accept yes. So now how we going to do it? First let's go and build a variable like the answer and at the start it's going to be empty. So now let's go and build a condition like this with a while loop where you're going to say if the answer from the user is not equal to what I want to hear the yes then ask again and in order to ask actually we're going to go and use the function input in order to get a value from the user and you're going to say do you agree and then we're going to give the answers yes or no then double point actually that's it that's all about the loop we will not make anything incremental So all what we are doing is we're going to ask the user for a value.
Then we're going to evaluate it. If it is not equal to yes, we keep asking. So the system going to end up in infinite loop until it hears yes. Now let's go and maybe print as well outside of the loop something like thank you. So now let's try this out and execute. So now the system is asking very politely do you agree yes or no. The user might say you know what I don't agree. Now the system can ask again do you agree yes or no and maybe the user says you know what maybe I don't know and as you can see the system is keep asking because the condition over here is fulfilled the answer is not equal to yes and we will stay in this loop until we hear yes until the condition is not anymore fulfilled. So now the system is asking for third time do you agree? Well, this time maybe the user going to be scared a little bit and say yes. And with that you will get the thank you and that's because we broke the loop. The condition is not anymore fulfilled and we went outside and printed the thank you. So as you can see it is very simple. All what you have to do is to have a looping variable like here the answer. Then you build a condition on top of it and then you make sure inside each iteration there is possibility to change the value. If you don't do that otherwise you will be stucking in an infinite loop.
All right. So that's all about the while condition. The first type. Now we're going to go to the second type the while true. This one going to be the most powerful but yet the most risky. So let's dive in. We're going to build it like this. We start with the keyword while and then after that we use the value the boolean value true. So there is no condition, no counter, no nothing.
It's just the boolean expression true.
And inside the loop, we're going to do something. So now in the chart, it's going to be like this. Python going to say okay while true. And this time we don't have like two answers. We are not checking anything. So we don't have a false and true. It will be always true.
So there is no escape from this to the end. Now if you just leave it like this and execute it, what's going to happen?
You will be stuck in infinite loop. So that's why we have to build somehow an exit in order to give it a chance to stop the loop. And in order to do that, we're going to go and add a second condition using the if statements. For example, x is equal to the word stop.
And if this condition is false, it's going to go to the top of the loop. But if it is true, we have to do something.
So think about it. What we could add here in order to exit a loop. Well, as we learned, we have the three statements in order to control the loops. And here we're going to go and use the break. It is our only chance to stop the loop. So we're going to go and include it inside the if statement. If it is true, the value is equal to stop. Then we're going to go to the break and stop the whole thing. Now, of course, there are multiple ways on how to get a value for this variable. But we could ask the user for a value using the input function. So we ask the user for a value and then we check this value using the if statements. So now let's see how Python going to execute it. Python going to start by the true and here there is nothing to be decided. So it's going to go immediately and enter the loop. So here it's going to go and ask the user for a value. Let's say our user entered high. So Python going to assign high for the X and then go to the next step and ask about it. So is high equal equal to stop? Well, no. So we didn't get a stop from the user. That's why Python going to go to the top of the loop and repeat the whole thing again. Maybe on the way we are printing or doing anything. So again Python going to see okay it is true so I can keep going in the loop and then ask the user for another value and here the user going to give us the value go and in the next step Python going to check it is go equal to stop well it is not that's why it is false that's why we're going to stay in the loop and then go back to the top after doing something and here again we have true and then ask the user for again a new value now finally let's say the user says okay I am done and give us the stop. So now Python going to go and check did we get stop from the user. Well yes finally we got the flag from the user that we are done and with that Python going to go to the break and execute it. And this is the point where Python going to say oh we have break we have to exit we have to stop everything immediately and end our loop. So as you can see this is another way on how to build a loop. So you build an infinite loop and then you add to it an if statement together with a break because this is your only chance to stop and exit the infinite loop. Otherwise, you're going to stuck in a black hole.
So my friends, this is very risky. But if you do it correctly, everything going to be fine. Oh, I have to fix this first. So something like this. Okay, I know you want to try the infinite loop, right? But first, a disclaimer. Only run the following statements if your machine can handle it. So only if you have strong CPU, you have saved your work and you are ready to restart your PC. So now let's go and build our infinite loop. So we're going to say while true and we're going to print something like I am unstoppable.
So something like this. And again here warning if you execute it your things might crash. If you stuck with it just eliminate it using the Ctrl C. Now I'm going to try this out and I hope I will not break the recording. So um we'll go.
Now as you can see it is executing. So Ctrl C and stop it. And now it is stops but I still hear the PC was not happy about it. Okay. So this is how we do the infinite loop. I don't know if you are still up there here but I hope you are happy. Okay. Now back to our task where we keep asking the user to type yes to agree with us until they do. Previously we have solved it using the classical while with the condition. But this time I'm going to go and solve it using the while true. So we're going to start with a while and after that we don't need condition. We will just say true double points. And by the way make sure the first character is capitalized. And then after that we go immediately and say we have a variable called answer. So that means it's not like before where we have like initialization at the start. We don't need that with the while loop because previously we have the answer in that condition and if you don't give it like any value you will get an error but here we are not checking anything yet.
So that's why we don't have to define anything outside and the same thing we're going to ask for an input. I'm just going to go and copy this from here. So with that we will get a value from our user but we have to check it right otherwise it will not work. So in order to check it, we're going to use the if statement and we're going to say if the answer is equal equal to yes and then double points. So now as you can see this time we are checking whether the value is yes. Not like here right now if the answer is yes what can happen? We don't have to ask anymore. We got what we want and we break. So we break the loop and get out. And of course we can put at the end like print.
So we exit and get out of our while loop. And of course at the ends completely outside the loop we can say thank you. So now let's try this out. I will just go and comment this out and let's go and execute it. So as you can see we are getting the same question. Do you agree? If you say no you're still going to get the same question maybe. I don't know. So with that as long as we are not fulfilling this condition Python will keep asking. So currently we are inside an infinite loop and we will not exit and break until we give the yes. So now if I go and give it a yes, it's going to be happy and say thank you. So as you can see now the system is behaving exactly like the while condition. And for this scenario, I'm going to go with the while true because it is easier to read. So we are building an infinite loop and we keep repeating that until we get the right answer.
So now if you compare the two types side by side you can see with the while condition we know when the loop should stop we have like something limited that going to end the loop naturally but with the while loop it going to run forever it is infinite until something happens until an event trigger the stop. So that means with a while condition things can end naturally but with the while true you have always to add extra stuff like a condition using the if together with the break. This is your only possibility to stop the loop. And if you are talking about the risk of having infinite loop the while condition it is safer.
Eventually it going to exit unless you made like a big mistake. But with the while row it is more risky because you are defining an infinite loop and you have to be careful designing the if and the break. And as we learned the best use cases for the while condition if you are building like a counter or you are building a limited retries or you are validating the output from the user. But on the other side, we use the while true if you have an open-end scenario and you are trying to get a trigger from something external like you are trying to connect to database to stream to APIs. So at the end I'm going to say always try to build things using the while condition but if you want something more advanced more flexible something that is open-ended then you have to use the while true. But be careful how you build it.
Now we're going to compare the two types of loops the for loop and the while loop side by side. With the for loop you need a sequence where Python going to go and create an object called iterator. But with the Y loop you don't need anything like that. And for the for loop Python going to build like predefined condition. It's going to ask always are we done with the sequence. It's not something that you change. But with the while loop you are totally flexible. You can add your own condition for the iterations. Now about when to use what.
We use the for loop if we know already how many times we want to loop. And as we learned, we use the for loop in order to go through our data to do something like we loop over the rows, the tables, the columns, the files in order to clean up or prepare the data. But now in the other hand with the while loop, we use it if we don't know how many times we want to iterate. We are waiting for an event, a trigger, a condition. So we use it if you are building like retries in the login or you are trying to connect yourself to a database or APIs. Now the advantage of using the for loop is that it is simple, clear and it is really hard to make an infinite loop with the for loop. So it is really safe but of course it is limited. You have always to predefine how many times you want to iterate. But on the other hand with a Y loop it is more advanced, more flexible.
It adds dynamic to your logic, but it is complex and you have a high risk of building an infinite loop. So at the end, think of the for loop as like you have a playlist. You know exactly how many songs you have and the Y loop, you are waiting for a reply. You keep checking until the condition is met. So that's all about when to use what.
Okay. So now time to challenge you a little bit. Now I want you to extend the current loop with new requirements. The first one is to limit the user to three attempts in total. So if you don't get yes from three attempts, you don't ask anymore. If the user type yes within the three attempts, then you have to print the following message. Glad we are on the same page. But if the user didn't give us a yes within the three attempts, you have to print three strikes. You are out. So now pause the video, solve those requirements, and then we're going to do it together. Okay. So now how we going to solve it? Now as you can see the first requirement is that we have to add like only three attempts. So now currently it is open-ended. We keep repeating the same question until we get what we want. But now with the new requirements we have to make it limited.
And with that we can rethink about maybe we go back to the classical while condition. I know that we're going to iterate maximum three times. And for this I'm going to say we go back to the normal while condition. So that means we're going to go and replace the true with a condition and we're going to say as long as the attempts less than three times you have to keep asking the same question. But of course we have to do the step before the while where we do the initialization. So we're going to assign attempts to a zero. So this is the initial value. This is our condition. And of course what is missing my friends? We have to make the updates.
So for each iteration we have to increase the attempts. And usually we're going to put it at the end before the next iteration. So we're going to say attempts plus equal to one. So that I increase the attempts with one. So with that we made the three attempts. Now to the next requirement we're going to say if the user did answer with yes within the three attempts we have to print this message. So again in our logic what we are saying get me a value from the user and if the value is yes then break. So that means this is a perfect place in order to print this message. So not only breaking out the loop we would like to give this nice message where we can print gludge we are on the same page.
After that we break and then we increase the attempts. Okay. So now let's go and try this out. So now first I would like to test whether the attempts are working the new logic. That means the system should not ask me more than three times not more than three iterations. Now I'm going to say no. Then ask me again.
Maybe I don't know. And now should exit.
Exactly. This is what happens. We exit because we are not anymore fulfilling this condition. So we are exiting the loop normally without using the break.
We had three iterations because the attempts were smaller than three. But for the fourth iteration, the attempts were equal to three and three is not less than three. That's why it is not fulfilling anymore the condition and we exited the while loop normally. Now let's go and test whether this going to work over here. If we get yes, it going to break and give us this message. So let's say for the first one it was no, but for the second one we have yes. As you can see the system didn't ask us for a third time because we fulfilled this condition inside the while loop. The answer from the user is yes. And and we fulfilled this condition. That's why we entered to this block where Python printed the message and then executed the break. So that means the while loop stops because of the break not because we are not fulfilling the condition anymore. So we didn't exit normally. We exited using the emergency break. Okay.
So now let's talk about the last requirement. Otherwise we have to print this message. Three strikes you are out.
That means if I'm not getting yes we should print this message. Well, you might say, you know what, let's go and get this one instead of the thank you.
We don't need the thank you anymore. And if we didn't exit with the break, we will get this message. Okay, so now let's try this out. I'm going to say no, no, and no. Now, as you can see, it is working. So, we are getting okay, three strikes, you are out. We didn't get a yes from you. But my friends, this will not work if we have a yes. So, let me show you what I mean. Let's say okay first time no but the second time we say yes. Now we have an issue. We have the two options together. So it is saying glad we are on the same page and this came from here. But at the same time it says yeah three strikes you are out.
Well this is not exactly what we want.
It is either we are happy we are at the same page or you are out not both of them. And that's because we have this print completely outside the while loop.
So after we broke the loop, the next instruction in Python going to be the print. So this going to be executed whatever happened in our while loop. But my friends, I would like this to be executed only if we didn't break. Now think about it. What we have used in the for loop in order to print something didn't break. Well, we have to go and use the else statements. And yes, we can use else together with the while, not only for the for loop. So let's go and add that. We're going to say else double point and then put this print inside it.
So that means if the loop ended normally without a break, this message can be printed but if Python executed the break then Python will not execute the else.
So either we exit normally and execute the else otherwise we exit with emergency using the break. So now let's try this out. The first one going to be no. Then the second one we're going to say yes. So now as you can see we are getting now only one message. Glad we are on the same page and this came from the break. That means we fulfilled this condition and Python printed the message executed the break and with that we exit immediately from the loop without executing the else statement. And we can do the last test where we have never given a yes. As you can see now we are getting the three strikes you are out and this came from the else statement.
So as you can see we can use the other control statements like the break pass continue the else statements in order to build our logic and control exactly how our loop going to behave. All right my friends. So now by looking back to our road map with that we have learned how to loop in Python and with that we have closed actually this category on how to control the execution of your code using those three chapters. Now moving on to the next chapter we have the data structures. This is going to be the biggest and the most important chapter in the whole course. Now the thing is previously you have learned how to work with data but with only using one single value. But the thing is actually in companies in real projects data come in collections in groups. We call them data structures. So we have advanced types like the list double set dictionaries.
So that means this time you're going to learn how to work with these advanced data types. We're going to learn all the tools that we're going to need in order to work with these advanced data types.
And again, this is really important for job interviews and as well for real projects. So, let's deep dive into this chapter. I wanted to be full focused because this can be amazing. So, what is data structure? And of course, if you want in the future more content like this for free, then support the channel by subscribing, liking, and commenting.
This is going to really help pushing on that channel to reach nice people like you and as well it's gonna push on me to make more content for you. If you would like to support more then you can go to the data academy and as well you can purchase my courses there. In the data academy it going to be easier to follow the structure because it is designed for courses and YouTube is actually not designed for that. So you're going to follow a clean structure and you can follow the progress and at the end you're going to get the certificate of completion and as well I have their quizzes and tasks to challenge your new skills. So now let's go back to the course.
So what is a data structure? It is a way of organizing, managing, storing your data in Python so that you can use it efficiently. That means you can access it, read it and modify it very easily.
And in Python we have four built-in data structures like the list, tpple, set and dictionary. And now of course the big question is why do we need it? Why do we need event to organize our data? What this means? Now so far we have been playing with simple data types in Python like numbers, strings, boolean. They are great stuff of course but the thing is they can hold only one single value. So that's why we call those stuff like the primitive data types. But in real world things get more complicated where you don't have only one value you have multiple values and for that we need more modern data types like the one that we're going to use in the data structure. If you just work with the primitive data types things going to get messy and chaotic. So let's have a quick example. Now usually we have one variable like the cost and inside it we store a value like for example 10. So this is totally fine because we have only one single value but in reality you're going to end up having multiple costs. So you have another value like 15 and then you go and store it in another variable called cost two and then another one 20 25 30. So you have already five variables for five values.
And now if you want to say you know what I would like to go and find the total cost then you have to go and summarize all those costs. So that means if we're going to get another cost that means we have to create a new variable and we're going to go and extend our formula and imagine now you have like hundred of values then you're going to end up having really big scripts. So this is really bad way on how to handle the data and this can turn immediately to cows.
So now instead of having five variables we're going to go and create a structure called list. So we're going to have one variable called costs and then in one container in one line we going to put all the values of the costs and then separated using a comma. So that means we have one container for all the costs.
And now if you want to calculate the total cost it's going to be very simple.
All what you have to do is to use a function called sum and then pass to it our structure. So the variable name costs. If you do that you're going to get the exact same result as the previous one. And now the best part. If tomorrow you got a new cost, you don't have to go and create anything else. So no new variable, no changing of the formula. All you have to do is to go and drop those values inside the list. So now just look at it. You understand exactly why we need data structure. Our data is nicely organized. Our code is shorter and it is very easy to operate.
So the rule is very simple. If you have multiple values for the same topic like the costs then you have to use a container a data structure in order to handle those values. And again in Python there is like not only one way on how to store multiple values. We have four different built-in data structures to do that. The most famous one is a list. It is a collection of items like for example a shopping list. Another type we have the tubble. It is very similar to the list but it is locked. So you cannot change any items inside it. A third one called sets. It is a collection of items as well but the items must be unique. So there will be no duplicates inside them.
And the last one we have the dictionaries where we have the key value pairs. And this one is very important especially if you are a data engineer, data analyst or data scientist. It is fundamental for you. So as you can see each type has its own features. Now you might say wait a second what are all those weird symbols? Like we have a lot of different types of brackets, right?
Well, we use them in order to define and tell Python which type of data structure we want. So if you want a list, you use the square brackets. For the double, you're going to use the parenthesis. And if you want set or dictionary, you're going to use the curly brackets. So the brackets going to tell Python which type you want to deal with. So now let's say that we have a data structure, for example, list. And the question is what we can do with it? Well, you can do a lot of things like for example, you can access specific value and read it or you can do some checks. You are searching for bad data and another thing you can go and change it and many other transformations, manipulations and advanced operators that we could use on top of our data. And now the question is what are the tools that we could use in order to do all those stuff. So now remember in Python we have standard library and inside it we have multiple modules and the one that we are working all the time is the built-in and if you go inside it you will find functions we call them the built-in functions like the print type length summarize maxin and many other functions that really gives you a lot of tools to manipulate the data structure. And at the same time we have something called classes. And for each type we have a dedicated class like we have class for list class for double and for set and dictionary. And now each class has its own methods. It's as well something like the function it is a tool in order to do something. But again those methods are allowed only for specific type. And once you create a data structure like for example a double then Python going to go and assign it for the correct class and then you are allowed only to use those methods in this class like for example the count the index. But now if you go and use an append or insert then you will get an error because this is not part of your class. So this is really interesting to understand in case you get an error you will know why you are getting it. Maybe because you are using something not allowed. So with that we have enough tools in order to manipulate the data inside our data structure. So now I would like you to understand one big difference between functions and methods. Not only we have a difference in the syntax where we start with the function name and then we pass to it like the variable but with the methods we start with the variable then dot the method name. But the big difference is what can happen if you use it. Usually the function is very simple. You give it an input and then something can happen and in the output you will get something out. Like for example the function length going to give you at the end how many items do you have inside your list and now in the methods they as well accept an input but we have many methods that's going to go and directly manipulate the input value. So the original value going to be changed without returning anything in the output. So you will got none in the output. So by looking to this the functions will not manipulate the original value the input it will just return a new value. At the methods they are more risky where they going to change directly the [music] original value quickly some coffee.
All right we're going to start with the first type of data structure in Python.
We have the list. So what is a list? A list is just an ordered collection of items that is very flexible where you can do anything with it. Like you can add and change stuff, you can order it.
it can maintain a lot of duplicates. So there are like no rules and you have a lot of freedom to do whatever you want and that's why the list are widely used in Python and usually the first thing that you're going to learn. So now we're going to deep dive into all the operations and things that you can do with the list. First we're going to start with the basics like how to create our data, how to create the list and then after that we're going to learn how to read and access our data and maybe how to unpack our data in different variables. Then the next step is that we have to go and learn how to explore and analyze the content of our data in order to have some basic understanding about the content and naturally after that you find some issues and as well actually your data evolves over the time. So, you have to learn how to change the original data by adding new items, removing some bad data or old data. Or maybe you say, I'm going to give it another chance by correcting, fixing, updating the data.
And after that, we're going to learn how to be more organized and not chaotic by learning how to organize, reorder, and sort the data inside the list. And after that, we're going to learn how to be professional where we don't go directly and change the content of the raw original data. But instead, we're going to go and make an extra copy so that we don't lose the raw data. But we have to learn how to do it correctly so that you don't get nasty surprises. And after that, you will get the reality check where you understand that actually not everything always in one simple list.
But in real projects, your data going to be spreaded in different lists. And your job is to go and maybe combine them back to one big list. And with that, we will be covering all the basics. And then we're going to go to advanced topics where we're going to learn the different methods on how to iterate through your data and items inside your list. And then we're going to learn how to do data transformation, how to do data filtering because you don't want to deal with all the items of your data. And then we're going to go to the last advanced technique. This going to be the most powerful and the coolest feature in Python where you combine the three power of looping through the data, filtering the data and transforming it everything in one simple line. So this is the plan.
This is our road map. We're going to go and deep dive into each one of them and we will start with the first one. How to create a new list. So let's dive in. So now of course always the first question is how to create things. how to create a collection of data using list because without it we cannot do anything right so let's learn how we can do that now I'm going to show you step by step how to create a list and we're going to start with the easiest one we're going to create an empty list so in order to create one we're going to go and use the square brackets and if you don't enter anything in between them then actually you are creating an empty list now in order to see the output as usual we're going to use the print and then we print the variable empty. So let's see what we're going to have. Look at the output.
We just have again the square brackets and nothing inside it. So this we call an empty list. Now of course we could check as well the data type of our variable in order to understand that we created actually a list not something else. And we're going to use the built-in function type and then we're going to pass for it our variable. So let's try this out. Now look to the output. We are getting the class list.
So with that you are sure I just created a list. Now behind the scenes what's going to happen is that Python going to create an object in the memory and our variable going to be pointing to this object and the type of this object going to be a list but still we don't have anything inside it. So it is still an empty. Now of course we don't want to leave it empty. We want to have our data inside it. So there are multiple ways on how to put data inside a list. The simplest one is to enter the data manually. Let me show you what I mean.
So let's go and create a list of letters. So I'm going to call the variables letters and then I'm going to make a new list and in between I will start adding values like for example the letter A then B and a third one C. So as you can see I'm entering the values manually inside the list and we can try and print this to the output and as well check the data type. So let's go and execute it. Now as you can see in the output we are getting a list full of data and as well since we are using the square brackets it is the class list.
Now by looking back to this behind the scenes Python going to go and create an object in the memory for each value and the type of the object is going to be of course a string. So we're going to have one for A another one for B and third one for C. Now by looking to this we have now two types of objects right? We have the values the string values and we have the object list. Now by looking to this you can see Python is not creating the string objects directly inside the list. It is really outside. So actually what is missing now is the connection between the list and the string objects.
That's why Python going to create internally in the list something called array and inside it it going to store like an address. So that means those addresses going to be used in order to point or to reference to the string objects. Now and of course the address values are not that really important. So now by looking to this the list object is actually just a box of pointers. It does not hold the real informations and the pointer is going to be pointing to another object like for example the string objects where inside it the real values are stored. And this is exactly how Python deals with the lists. And of course we could make a list of numbers like for example numbers and we could have 1 2 and three. So let's print it in the output as well. And you can see we have now a nice list of numbers. And now by looking back to this we're going to have again a list with pointers and those pointers going to point now to a new object. It's going to be the integer objects where they store the numbers one two three. So it is the same concept.
Now of course it is very important to understand that you can go and mix stuff. So inside the same list you could store different data types. So we could go and make a mixed list and we could store stuff like for example one a boolean true and as well we could use the none. So we could have a nice mix of different data types. And if you go and of course print it or check as well the data type. So let's go and execute. You could see I'm still using a list but with different data types and it going to look like this. It's going to be again the same thing. You're going to have an object list with different pointers and this time they are pointing to different types of objects. It's going to point to an object integer, an object string, a boolean and a non type.
So, Python is very flexible and you could have a mix of different data types in the same list. Okay. So now there is another way to create a list in Python by using the built-in function list. So how this going to work? Let's say that we have a string value data. So the data type here is a string. And now if you go and use the list function, what going to happen? Python going to take each character of your string and put it in a separate item in complete new list. So the D going to be one item, the A going to be another and so on for each letter of your string. So now by looking to this you can see we can reuse the data that we have in different shape and convert it to a data structure a list.
So this is another way on how to create a brand new list from already existing data. Okay. So now let's try this out.
So we're going to go and create an empty list by using the function list. So if you do that and print the empty. So let's go and execute it. You can see in the output we are getting an empty list.
So as you can see using this function we can create a list. Now of course if you want to put items inside it you have to use something inside the function. So let's go and create another variable and currently it is holding the value Python. So it is a string value. But now I would like to have all those values as a list. So let's go and print this out letters and execute. So as you can see it is very simple string value. But now the thing is I would like to have a list. So I don't want to have it as a string value. It is very simple. If you go and say a list and then put the string value inside it like this. Now if you go and execute it. Now look at this.
Python did create a list and converted each letter of our string as an item of this list. So that I have just created a list out of another value another data type. And of course for the list function we can pass any data type that actually a sequence. So for example, let's go and create something called numbers and then a list. Now remember with the for loop, we could use a function to generate numbers like for example the range. Now if you go and say okay go and generate five numbers. So with that it's going to be like a sequence of numbers from 0 to 4 and then you say okay after you generate it go convert it to a list. So put it in a list with that you will have a list of numbers. So let's try this out and say numbers and let's go and execute it. Look at this. We have now a list of numbers. So 0 1 2 3 4. Those items came actually from that generator range. So as you can see all what you need is actually a sequence. So this is another way on how to create a list by converting the data from one data type to a list.
All right. So [music] far everything is easy. Right now we're going to go to something very interesting. So far all what we have is actually a very simple list with a straight line of items. This is actually one row of data or we can call it one dimension. But now sometimes I don't want to have only one row. I would like to have multiple rows. It looks like something like a table. And we can do that using something called nested list. It is very simple. If you put a list inside another list. If you do that then you will have multiple rows and you will have the shape of 2D. Now how Python going to store this? We still have our variable that is pointing to an object called list and inside this object we're going to have as well some pointers but this time instead of pointing directly to the values they will point to another object list. So in this example our list going to be pointing to another two object lists.
And of course since they are an object list they as well going to be pointing to something else. And in this example they will be pointing to three different string values. So by looking to this my friends the object list could point to the real data but as well they could point to another list. So it is very simple. It is a list inside another list. I know that many of you are asking how I do those animated sketches. Right?
So the thing is I don't use any AI generated tools. I draw them myself using my iPad Pro. So I'm using here an app called Procreate. Everything that I draw is actually going to get recorded and at the end the whole history going to be like animated video and the last step in the process I use the venture resolve in order to make cuts and zoom in zoom out something like that. So this is very manual process and takes a lot of time to do that's why my content takes [music] time to be built. If you enjoyed it, you can like share it with others and support this kind of tutorials. Now let's go back to the course. So now let's go and create a nested list or a matrix. It is very simple. We're going to start with the first list. And now inside it we will not directly add the values. We will go and create another list. So now as you can see square brackets inside another square brackets and only inside the second one I will be creating our values. So for example A, B and C. So this is the first list, the first row and then I'm going to go and separate it with a comma and then create the second list. So now we're going to go and add the other values like D E and F. Okay.
So now let's go and print it and check how it's going to look like in the output and check as well the data type.
Let's go and execute it. Now as you can see the class going still a list and with that I'm getting my matrix. Now in order to make it easier to read and to write usually each time you are starting a new list just make it in new line. It is just now easier to understand and to read and of course it can still works.
Python going to print it but together in the same line. So this is very simple.
This is how you can create a matrix 2D list or a nested list. And as usual you could make mixed stuff. So let me show you another example. So I'm going to call it mixed matrix. So now I could start for example with string values like a and b and then start a new matrix where actually they are numbers so 1 2 and three. Python is totally flexible.
You don't have to commit actually with the same number of items. So in the first list we have two items. In the second one we have three items and maybe a third list where we have boolean value like for example true. So if you go and print it matrix so we are not getting any errors. As you can see Python is totally flexible but it is your job to make sure this makes sense. So now let's do a quick recap. We have different ways on how to create a list. The first one we could create an empty list if you use a square brackets and nothing inside it or you can use the function list without specifying anything inside it. This is really nice in order to create it and plan later to add the items. Another way is by manually adding the items. So you can use it if you know already what are the items in the list. And another way you could build a matrix if you put different list inside a list. So it's like we are nesting. And it is great in order to build a data structure like tables where you have rows and columns.
So if you have the data already but in different data type or an object, you can go and just simply convert it to a list. So you will get the same data but in different objects. So with that you have learned everything on how to create a list from the scratch and now you can go and apply a lot of methods and things on the list.
Okay. So now let's say that you have created a list. Of course the next question is how to use it, how to read information from it and how to access specific value in my list. So now we're going to talk about a very important two concepts, the indexing and the slicing.
So let's go. Okay. So let's start step by step. I'm going to go and create again a list and this time I'm going to go and include four letters A, B, C and D. Now we start with the first step where I say you know what I would like to access and read the whole thing the whole list. So I need all the items. We have done this already. So all you have to do is to use the print function and just specify the variable name without anything. So if you do that and execute actually you are reading and accessing all the items inside the list. But of course this is boring. What is more interesting is that to go and access only one item something very specific.
So we are saying I don't need the whole thing I just need one item. And for that we use something called indexing. What is indexing and why we need it? Now look at our list. So now if you say I want to access something specific like for example the first letter or the last letter or maybe to say the third letter.
So that means I'm interested with the position number of each item. So it's going to start from the left to the right. The first one has the slot zero.
And here be careful we are not starting from one. So A has the position number zero index zero. B has 1, C 2 and D three. Now if you say I would like to get the first item all what you have to tell Python is the index number and for that we use as well again the brackets together with the variable name. So now if you specify a zero between the square brackets Python understands uhhuh we need the item inside the slot zero. So with that you are accessing exactly one single value and the one that you need.
And if you say you know what I would like to get the last item then you specify the index three and you will get D. And if you say I need the second item here, be careful if you are saying second. That doesn't mean the index two.
The second item here has the index number one since we are starting from zero. And for that you will get a B. Now what is cool in Python we can go backwards if you use the negative numbers. So now if you are counting from the right side going to the left side the first item can has the position number minus one. So it doesn't start from zero then minus2 - 3 and four. This is really nice if you have a long list and you are interested only on the last items. So here you can say quickly give me the last item by specifying the index minus one and with that you will get the D. So that means if you are using in the index the position number three or the minus one at the end you will get the same results. So this is really nice.
You get exactly what you need but again only one single item. Okay. So let's practice again. I don't want the whole thing. I just want one item. And let's say that I would like to get the first item. All what you have to do is to say for example print then the variable name. And now side by side you add the square brackets. And now you have to define the index. So again we need the first item. The first item on the left side it's going to be zero. So something like that. Let's go and execute. Now as you can see in the output I'm getting the item a. Let's say that I would like to get the last item. So we can go and say print again the variable and the brackets. Now of course you can start counting 0 1 2 3 but this is annoying since we are saying I would like the last item then it's way better to use the negative numbers and it's going to be really easy. The last item going to be always minus one. So let's execute.
You can see I'm getting D. Now let's say I would like to go and get the C. So print again our variable name. Now here again you could go and say 012 but I would like to get it with a negative number since it's closer to the end.
Then I'm going to say minus1 minus 2 and I will get my C. Let's try this out. And with that I'm getting C. So this is exactly how you can get and access exactly one single value using the index. [music] Okay. So now let's take things to the next level where we talk about how to access and read a matrix. So let's say that we have three list inside one big list. And of course, here things are a little bit more complicated, but I always think about it like going to the cinema and finding your seat. So, as you know, in your card, you're going to have like two very important numbers, the row number and the seat number. So, how you find your seat? First, you have to find your row number. And once you find your row, then you check the second number, your seat. And this is exactly how you navigate a matrix in Python. So, for the first row, we have the row number zero.
Then the second one going to be one and the third going to be two. So this is your first number. The second number in order to find the seat you need the numbers for the columns. And as well here it's like the simple list. For each item we have a number. So 0 1 2. So now once we have the numbers we can access anything that we want in the matrix. And here you have to be careful what you are specifying. So if you don't specify anything for Python and you say matrix so you are just giving the variable name so that you are targeting the whole list with all rows and all items. But now if you want to target for example a complete row so I don't want the whole thing I just want one row but I want all the values of this row. In order to do that we have to specify for Python the row number and we use the variable name together with the square brackets and you give the row number. For example here one. So you are now at the second level of your matrix and now we go to the last level where you say you know what I need specific value or values from one row. So for example you say I just want the E from the second row.
Again it is exactly like you are finding the seat in the cinema. You have to specify the second value for Python. So not only the row number but as well the index number of the value. So in this example we are targeting the index one in the second row. And now you are at the lowest level in our matrix. So again if you just give the variable name you are accessing the whole list. If you give only one number then you are accessing one row a whole row. But if you give a second number then you are accessing a specific value in specific row. This is very important to understand in order to target exactly which information you want to work with.
Okay. So let's try this out with the matrix. Again we have our nice matrix with three rows and each row has three items. So we have three lists inside one big list. Now let's go and practice by reading and selecting. The first thing I would like to go and select everything.
So the whole matrix it's going to be very simple. You're going to say print and you just need the variable name. So if you do that look at the output you are retrieving everything from our matrix. So all rows all items. Now let's say that I don't need the whole thing. I just need the last row. So the last list that we have inside the matrix or what you have to do is to say okay we're going to have the matrix but this time we're going to be selecting specific row. So the last row going to be the row number two. So 0 1 2 and in order to select it we have to use the square brackets and then say two. I just want to comment this out and then let's go and execute. Now look at this. We have the last row in the output. And as you can see I'm not specifying which item in the list. I'm just saying give me all the items of the last row. And now since we are seeing the last row actually you don't have to go and count stuff just go and use the minus one with that you will get always the last row of any matrix.
So look at this we are getting the same results. So it doesn't matter whether we have 100 of rows if you say minus one you will get the last one. Now let's say that I don't need the whole row I just need one thing like for example I would like to get the eye. So the last item from the last row. So we are going deeper. Now we're going to say okay print matrix. So now the first thing is that we have to specify which row we are talking about. Well, we said the last item in the last row. So that means it's going to be the minus one for the last row. Now Python is actually focusing on this one here. So now forget about the other rows. Just focus on one simple array. We need to tell Python where you can find the item. So 012. So this is the index number and we're going to go and use another square bracket in order to select the item. So it's going to be two. Now if you go and execute it, look at the output. We are getting the I. And again the same thing since it is the last item. Actually all what you have to do is to go and say minus one. And if I run this again, I will get the same result. And again here if you want the last item from the last row, just go and use minus one minus one. And you will get that item. whatever your matrix looks like. Now I'm going to ask you how to get the first item of the first row without counting. Just think about it.
Well, we're going to say matrix and just blindly. I'm going to go with zero and zero. I will not count anything. If you go and execute it, you will get the A.
This going to be the first item of the first row. And you can use this as well in any matrix. So that's we have the first item, the last item. Now let's go and try to get the one in the middle. So the E we're going to say again matrix now in which row we can find the E? Well 0 1 that means in the row number one and now inside the row number one where we find the E. So 01 as well the same thing. So it's going to be one as well.
So let's go and execute it. So as you can see we are getting now the E. This is not a rule. You cannot say okay each time going to use the one one I will get the item in the middle of the matrix because this really depends on the size of the matrix. Okay. So this is how the indexing works with the matrix. It is not that hard right?
All right. So so far we have learned how to retrieve everything or retrieve one single item. But how about to retrieve multiple items? For example, in my list, I would like to get access to only two characters, the B and C. So, for this, we cannot use the index. Instead, we're going to use something called slicing.
So, now let's look at this and see how this works. We don't need the whole list. We would like to slice it to cut it. And for that, you have like two boundaries, the start and the end. So, you have to tell Python actually two things. And for the syntax, we're going to still using the square brackets. Now, the first thing that we have to specify is the starting index. In this example, it's going to be one. And the second thing you need the stop index. So for that, first you have to use double points and then you specify the index.
Here in Python, the stop index is not inclusive. So that means you don't specify here two. You have to specify three. So we stop at three and three is not included. If you do that, you will retrieve now two items, the B and C. Now another example. Let's say that I would like to get the first three characters.
So again, what is the starting index? It is zero. and the end index going to stay as three. If you do that you will get a b c. Now there is something really cool in python. The starting index the zero it is actually a default. So that means if you are start slicing from the first item in the list you don't actually have to specify it in the syntax. So you can skip it and you start immediately with the double points and you will get of course the same results. By the way this is the same thing for the last item. So let's say I would like to extract C and D. So the start position going to be two but the stop position going to be four.
You cannot have three because the stop is exclusive. And of course you will get the result but this is not really nice.
Instead you're going to say two double points and then empty nothing. So you say I want to start slicing from the index two and then I want to get everything after it. And now think about it. If I just specify the double points without starting position and end position, what's going to happen? Well, it's going to work. you will not get an error. If you do that, you will get the whole list. So you see, I want everything from the start until the end.
And if you want that, actually don't use any brackets or double points. Just use the variable name. Okay. So let's try this out. I would like to get the first two characters.
So we're going to print our variable name and then our square brackets. Now since I want the first two characters, that means I don't have to go and specify the first character. And since the first two characters 0 1 2 I stop at two. So now let's try this out. As you can see I'm getting now a and b. Now let's go and get the last two characters.
So print lst. Now I have to specify the starting position. It's going to be 0 1 2. So we start from two double points.
So now since I want everything after this position, I'm going to leave it empty. And if you go and execute it, you will get cd. And now let's try this fun one. and I say we are slicing but actually without defining anything and execute it you will get the whole list exactly like the first print. So slicing is simple right and of course my friends we can slice our matrix. So let's say that I would like to get only the first two list the first two rows. So we can do it like this. We say print and then matrix. So now it's time to slice. We start with the first row that means with the zero and then double points and we stop at the row number two. Again the stop is exclusive. So we're going to say two. But since we are starting from the beginning we don't have to mention the zero. So if you go and execute it you will get in the output the first two lists. As you can see it is very simple.
Now let's say that I would like to get the opposite the last two lists. So we're going to say print matrix and then specify the start. So the start going to be the row number one and then double points. And now since everything else in the list, we don't have to specify any ends because I want the rest. Let me just remove this one over here and then reexecute. Look at this. We are getting the last two lists from my matrix. Now you might say, okay, nice. We are slicing the rows. But how about to slice actually inside one specific row. For example, I would like to get the G and H from the row number two. So how we can do that? Now we are moving to another level, right? So we're going to say matrix. So now we don't want to target the whole matrix. We want to target one specific list. So we are focusing actually on the row number two. So we have to tell Python about it. Now focus and target the row number two. And after that you forget everything else. So forget the first two lists. Now we are at the normal list. How to get the first two items from a list? We do it like this. we start another brackets and since we start from the beginning we don't have to specify the zero we start immediately with the double points and now I need the G and H that means I stop at two so let's try this out and see what can happen exactly we are getting now the G and H so we think about this cinema example first we have to specify which row we want to slice from and then after that we tell Python what to do inside this row once you understand it you can do anything with the 2D lists okay friends so With that we have learned everything on how to read and access anything in a list. So if you want to go and access only one single item we use the indexing. But if you want to go and slice multiple items from your list we can go and use the slicing.
And of course if you want to get items from the start of the list then go and use the positive numbers. But if you want to get data and items at the end of the list you can go and use the negative numbers. And if you are working with matrix always you have to think about first we have to specify the row and then go and get the exact value that I'm searching for. So with that we have learned how to create a list how to read and access a list. And to be honest this is basically 80% of the things that we do with the data structure in real projects.
All right friends, now we're going to talk about something really interesting on how to deal with the data structure.
We have the concept of unpacking. So far what you have done is that we have created a list where we have packed multiple items together in one container in one box. So it's like you are putting your stuff in a traveling bag. So we are packing and of course we can do now the opposite where we start unpacking stuff.
So we go and open the bag and start taking the items out of it and maybe put it somewhere else. This is exactly what we can do in Python with our list. As you create your list, you are packing your items inside a container and we can use unpacking in order to take the items out of the list and put it in different variables. This is exactly what we mean with the unpacking. And now of course the big question is why do we need to unpack the items of a list? So what is exactly the use case? Like for example, we have a list of personal informations like you have the name, the age, the role and the country. As you can see, each piece of item inside the list has like different meaning. And for each of those informations, we could use different operations. Like with the name, I can go and calculate the length of the names. And with the age, I can go and find the average or maybe compare it with another number. With the role and the city, I could use them for example to build a category. So we could use it in order to do some aggregations or maybe filter based on a category. So now in order to work with those informations, it makes sense to put them first in different variables and then apply the operations. And this is exactly why we need unpacking. We're going to put those informations in different variables so that it's going to be later easier to do the different operations. So this is exactly why we need unpacking. Okay. So now in order to understand it, let's have the following example. So we have a list of personal informations like for example the first name let's take Maria and then we have her age it's going to be 29 then maybe her role she is a data engineer and maybe as well a country like maybe Spain. Now maybe I would like to go and grab each of those informations individually and put it in separate variables. So for example I would like to have like for the name a separate variable and in order to do that we could use of course the index. So we're going to say it's going to be the index zero. And then we're going to go and grab the age. It's going to be the index number one. Then the role. The next one, the last one going to be the country equal to person. Then three. Now, of course, it's going to be really annoying if you have like 10 informations about the person. That means you have to go and write 10 lines of code. Well, for this kind of scenario where you are extracting from a list, we have better solution where we're going to have smarter and cleaner solution by using the unpacking. Now, how we going to do it? Now, you remember as we built our list, we just made like a list of values after each others and we separated them using the comma. Now, we're going to do the same thing, but this time we're going to make a list of variable name.
So, we're going to do it like this. So we're going to have a name comma then age comma ro comma and country and of course we don't use the square brackets and after that we're going to say equal and then we're going to assign the values from our list. So now look at this again we have a list of variable names and here it is very important the order of the variable names must be matching your list. So since here we have a name we're going to start with the name and if you switch that you will get an issue. So that means those four variables are getting their value from the list person. So of course we can go and test it. For example, let's go and print the name. So if you go and execute it, you will get Maria. And if you go and test for example the role and execute, you will get data engineering.
And now if you compare the unpacking with the old methods on how to assign values using the index you can see everything in one line and it is clean easy to read and you don't have to deal with all those indexes where exactly is the age and the name and as well it is very easy to extend like for example if you get like fifth value like the city all what you have to do is to just extend the list of variables like to add the city like this. So this is very powerful.
Now this is only [music] the basics.
With the unpacking we have a lot of cool things like for example we have our list and again we have our four values. Now what could happen is that you might say you know what I'm only interested with the first item and the last item. So only with the name and the country and everything in between is actually something that I'm not interested about.
So I really don't care about the data in the middle. So now I don't want to go and create a variable for each item. I just want to have variables for the first and the last. So now instead of having a lot of variables, we could have only three. So now we could put the first item in one variable and the last item in another variable. And now everything in between you can put it in one variable. And you can do that with the magic of the operator asterisk. with that Python going to understand uhhuh the first item going to be for the name.
The last item going to be for the country and everything in between Python going to go and put it in details. So with the star you are telling Python go and collect the leftovers and go put it in a new list. So now again let's try this out. I am interested with the first item so I'm going to leave the name. I am interested with the last item so I need the country but everything in between is actually don't matter. So I'm going to go and get rid of those two variables and then I'm going to say asterisk and my new variable going to be details. So now let's try out I want to print the name. Then of course we want the details without the star. You just use it as you are unpacking and the last one going to be the country. So let's go and execute. Look at this. The first variable is Maria. The second one is going to be the middle one where we collected all unnecessary stuff and the last one Spain for the country. So it is amazing. Now if I go and add any value in the middle like for example the city it's going to be considered as unnecessary information and if I go and execute it it will go and join the middle variable the details together with the other values. It is clearly I'm interested only with the first and the last. And of course if I go and add an item at the start and then execute it.
Now as you can see the ID is now the first item and Maria did join the middle list. So it is again I'm not interested about this information and Python packed it in the middle. Now what else we can do is that if you say you know what I'm interested only with the first item and everything else is actually not interesting for me even the last item.
So what we can do we can say okay for the first item we're going to stay with one variable but everything else the rest will be included in the asterisk so the details and we will not be specifying a variable for the last item.
So now with this we have only two variables one without the asterisk in order to get the first item and the rest going to be with the asterisk. So let's try this out. I'm going to go and get rid of the ID. Now we're going to say okay the country is actually details. I don't want to have like dedicated variable for it. What is very important for me is only the first item and the rest is actually not interesting. So everything else going to be in the details. And now if you go and print it you can see the name is holding only one value Maria and the details is now holding the rest everything even the last item. So this is really nice if you want to just focus on one information and put the rest in another list. Now of course exactly we can do the opposite where we say you know what the last item is the most important one and everything else the rest is actually not interesting even the first item. So now I think it is pretty easy right what we're going to do we're going to go and remove the first item. So we start with the star asterisk and then after the details we're going to say the country information is very important. So now what we're going to do we're going to say print country and let's go and try this out.
Now look at this. The first four items is actually in one list and only the last item Spain is actually living outside of this list. So as you can see it is really simple. Always the order of the variables is very important. And of course you can try things like this. You say you know what let's get the city and the country and everything else in details. Well if you go and of course print it and execute. So with that I'm putting the city in one item, the country in one item and the rest going to be in a list. So with that you decide exactly which items going to be in separate variable and what are the items going to be in the list. So with that you can extract exactly what you need and be careful you are allowed only to use one asterisk. So here we used it with the details but now if you go and put it as well for the country this will make no sense at all. So you will get an error. We are allowed to use only one asterisk one time.
Now I'm going to show you quickly the rules of using unpacking in Python. So now the first one is that the number of variables must be matching. So now let's say that we have a list of variables first, second, third and maybe last and from numbers. Now the first rule is that the number of variables must be matching exactly the number of values if you are not using the asterisk. So now if I go and execute it, I will not get any error. But now if you go and remove one of them with that you have three variables and execute you will get an error because you have too few variables to fit your list. Python going to say okay I don't know where to put actually the four you gave me only three variables I need four and the same thing if you have actually too many so third fourth now if you go and execute it I'm going to get as well an error because here I have too many variables for too few values so the number of variables must be exactly matching if you are not using asterisk but now of course you could have here less variables if you say you know what I'm going to have a middle and put it with the asterisk So if you're going to execute it, you will not get any issues. Now one more cool thing. Let's say that I have only one item. But actually I have two variables like this. So I have the first and let's say here the rest. Now if you go and execute it, you will not get an error because Python going to go and let me show you the output in order to understand what is going on here. So print first, print rest and then execute. Look at this. Python did put the first item in the first and the rest going to be empty because we don't have a rest which is totally fine. But of course this only works if you are using the asterisk. If you remove it then you must have two items otherwise you will get an error. So if you don't have a rest that is totally fine you will get empty list. Now one more thing actually it works with anything that has a sequence like for example I can have high. So I have here a string value and I can use the unpacking to extract actually the letters of this string. So if you go and execute it, it's going to put the h in the first and the rest the i in a new list. So not only lists, you can use anything that is iterable or has sequence. So those are quickly the rules on how to use unpacking.
All right, moving on to the next one. We have another trick with the unpacking.
we could go and use a special character underscore. So what this means now let's say that we have three values name age and the country and now you might say you know what I'm just interested about the name and the country about the age I don't want it now previously we have created like three variables for it name age and country but the thing is I have created the age and I'm not using it at all so it is something that I have to create I have to name it it's going to take space as well in my expensive memory and I'm not using it at all. So it is totally wasting of resources. I just want the first and the last name.
Well actually there is solution from Python. You can go and use an underscore in the places where you don't need the values. So now instead of saying age you can go and use underscore and with that you are not creating any variable and the data will not be stored anywhere. So you are saying for Python okay for the second position I don't need anything just throw it away. Of course, we are not removing anything from the original list. The only thing that we are not extracting the second position, the age.
So by looking to this, it is really as well easy to read. You say I need a variable name. Then after that, I don't care. Then the third one, I need the country. So this is really nice in order to skip things and to put any value in the trash that you are not interested in. So now let's try [clears throat] this out. We have again our four values and our four variables. And now I'm going to say, you know what, I'm interested with the name. I'm not interested with the age. So you're going to go and use an underscore. Now maybe for the role I want it, but I don't want to see the country. So I just want the name and the role. Everything else is not interesting. And of course, you cannot go and print anything about the age and the country because we don't have variables for it. Now, if you go and execute it, you can see I have Maria and data engineering and everything else is actually skipped. So this is really nice. If you are interested only with few information from your list. So you don't have to go and come up with a variable name for each thing. So as you can see if you compare it with the asterisk you can go and use multiple underscores. So we are not limited to only one underscore. Now one more thing we can go and combine the power of the asterisk and the underscore together in one. Let me show you this example. So now imagine we have a lot of values in one list and you say you know what I'm just interested with the first and the last value and everything in between I really don't care about it. So now before with the asterisk we have puted everything in one variable. So with that you extracted the first and the last and everything else is stored in the middle variable. Now the thing is I really don't care about all those values in the middle. I will never use it. So it is totally waste of space if I do that. So now how we going to fix it? If you go and use only the underscore, you have to add an underscore for each variable in the middle. So it's going to be really annoying to go and add. You might miss one and so on. You have to go and count and make sure you have exactly the same number of the underscores. Instead, you can combine the power. So you put the asterisk at the start and then underscore. So treat the underscore like any variable. So with that you are saying okay the rest everything in the middle go and throw it in the trash I don't care about it don't store it in my memory. So again the asterisk is always for the rest and think about the underscore it's like I don't want to define a variable and use underscore in the places where you don't need a variable. So this is awesome. Okay. So now let's go and try this out again. I need the first item and the last item.
So I'm going to say here the country.
And now for country. So that I got the first and the last and we have to go and fix the middle. So we're going to say okay go grab the rest but don't put it in a variable just throw it away with the underscore. So now if you go and print the last one the country and then execute you will get Maria you will get Spain and everything in between will actually will be wasted and will not be stored inside the variable. So this is really nice. The same thing if you say you know what I just want the first item. So I just want the name. So I go and execute it. You say just give me the first name and everything else don't put it anywhere. I just need the last item.
Everything else the rest is not really needed. So let's go and execute it. You will get only Spain. So once you understand how it works, you can control exactly which values you want to extract from your list. So it is very flexible, quick and easy. All right, friends. Now, let's do a quick recap. Unpacking, it is exactly the opposite of creating a list.
Instead of storing everything in one box, we use it in order to start unpacking the item. So, we take the item out of the list and put it in different variables. And when to use it? Usually in our list, we have different types of informations. And we use unpacking in order to make it for us easier to work with each piece separately. And to do unpacking, make sure that you have the exact number of variables that is matching the number of values and items.
And we use two special characters. The asterisk, the star, we use it in order to put the rest of the items into new variable, new list. And the underscore, we use it in order to skip items if you don't care about the item and the values. So unpacking is fun, flexible, and going to make your life easier working with the items of the list.
All right guys, now we're going to talk about something really interesting in Python. How to analyze and check items in the list. And now the thing is if you have data then you have to analyze your data, right? So that means now we have to think like a data analyst and we have to start asking quick questions like how big the list is, what are the largest and the smallest items, does the list actually meeting the conditions and the rules in our projects and where do we find specific value and how we can summarize the values. So now let's go explore and analyze the data of our list. Let's go. Now in Python we have a lot of functions like for example you could use the max function to find the extreme high or you could use the opposite to find the extreme low.
Another function we have the sum in order to find the total. So it's going to go and summarize all the values and if you want to find how many items are inside the list we can use the length.
So those analyzing functions going to give you a quick snapshot to find high low total and count. And now next we have two cool functions all and any. So the all going to ask the question did everything pass. So are all the values true? If yes then you will get true otherwise false. And the second function is any. It is like you are asking did something pass. So if there is at least one true you will get true. So those two are your yes and no questions for completeness and uniqueness. All good or at least something good. Now moving on we have as well methods. Like for example, we could use the count in order to tell how many times something shows up or we could use the method index to tell you where it first shows up. So those two functions going to help you to search and count. And next we could use as well the operators to check something like for example the n in order to check the existence of a value inside a list or you could use the is operator to check whether they are the same object in the memory. So that we are checking the membership and the identity.
Finally, we could go and use the comparison operators like the equal to check whether the two list have the same content. So by looking to this, we have a lot of tools to explore and analyze our list. And inside the toolbox, we have a lot of functions, methods and operators. So now we're going to go and cover all of them one by one. And we will start with the functions. Okay. So let's have a very simple list of numbers like 1 5 2 4 3. So let's start with the first one. Let's say that I would like to go and find out what is the highest number in my list. So in order to do that, we have very nice built-in function called max. It's going to find you the highest value. So let's try this out. We're going to say print. And now since we are saying it is built-in function, so it's like the print, you're going to write the function name and pass for it your list. So our list going to be numbers. Let's try this out. Now as you can see it is saying the highest number is actually five and this is true. So it is very simple. Now let's say that I would like exactly the opposite. I would like to see what is the lowest value in my list. And for that we have the built-in function min.
So we're going to say min double point call the function min and pass for it our list. So let's try this out. And as you can see the lowest value in our list is one. Okay. So now to the next one. We have an aggregate function called sum.
You can use it in order to summarize all the values of your list. So this is really nice for data aggregations. We're going to say sum double points and we're going to use the function sum. The same thing we're going to pass for it our list. So let's try this out. Now you can see the total of our values is 15. So it is really nice one. By the way, this will not work if you have like string values. So if you go and execute it, you will see this will not work. This works only with the numbers. Moving on to the next one which is really important and nice one. You can measure the length of your list. And for that we have the built-in function length. So let's try this out. We're going to say length then double point and the function going to be len and pass for it the numbers. So let's go and execute it. You will get the total length of five. So we have five items inside our list. And of course this function works for any data type. So if you have here strings or like boolean you will still be able to calculate the length of your string.
Okay. Now moving on to two really interesting functions in Python. We have the all and any. The all function going to check if all the items are true. That means you have a real value in each item of your list. If that's the case you will get true. Otherwise you will get false. So let's try this out. And we're going to say print. And here we can say all double point and then all and our list. So let's go and execute it. Now you can see it is giving us true because we have a real value for each item. But now let's go and change that. I will just copy and paste. But I'm going to go and create a new list. So I'm going to create three items one 0 and two. Now if you go and execute it, you will get false. And that's because this function going to consider the zero as missing value. So that means not all the items are true. That's why you are getting false. And the same thing for the string values. So let's say that we have here a and then empty string and then b. So if you go and execute it, you will get as well false because empty considered to be a missing value. So you will get false for it. It will work only if you have everywhere a real value like for example here C. So we have a real value in each item. And if I go and execute it, you will get true. So all the items must be true to get a true. Now the exactly opposite is the any. It is more relaxed. You will get true if at least you have one true value in your list. So let's try this out. I will go and copy paste stuff. Now let's go and test first our original list and I'm going to say here any and the output as well any. So if I go and execute it, you will see I'm getting true because I have an item in each value. Now let's go and test the second list here where we have a zero which is not really good and I have to change this to any and then execute it.
So now in the output we now we are getting true before with all we got false because we have one missing value but as the word any says it is at least to have one value and here it is more than enough we have the one and the two that's why you are getting true and the same thing if you go and get the string values over here and say any and here as well any and execute you will get true the only way you will get false with the any if all the items are actually missing. So you have zero and here as well zero. And if you execute this you will get false because the minimum is not fulfilled. You have to have at least one value. So as you can see those are really nice functions in order to validate and check the quality of your list. So it's going to help you to find quickly any issues or any gaps in your lists. So those are the Python built-in functions that you could use in order to validate and analyze something in your list. But as well in Python we have two extra methods that we could use in order to analyze our list. So we use counts as the word says in order to calculate how many times a specific value shows up in your list. So you are calculating the appearance of specific value. So now for example let's go and count how many times the number five appears in my list. So we're going to go and print count double points. And now since we are talking about methods, first you have to specify the name of the variable. So we're going to say numbers then dot then the method name. So we're going to say count. And now we have to specify for Python the value that should be counted. And here I want to check how many fives I have in my list. And now if you go and execute it, you will get count one. So we have the five only once. So now let's try something else where I add another five in my list. And if you go and execute it, you will get count two. So five appears twice in my list. So this is really nice in order to find out whether your values are unique or you have duplicates in your list. Now I said we have two methods. The second one we have the method index. So this function is really amazing because it's going to help you to find the position number of the first time a value appears in your list. So far you remember in order to find the index number or the position number we always counted like 0 1 2 3. Now of course what you can do in Python you can give a value and Python going to return the index number or the position number. So for example I would like to know the index number of the value five. So let's go and print it and say index double points. This is a method. So numbers dot index and now you have to pass the value. So I'm searching for the index of the value five. Now if you go and execute it, you will get index one which is correct. Look again to the list. So we have zero for the first item and for our five, it is position number one. And yeah, you might say, but how about the second five? This function going to return the position for the first appearance of your value.
So the second appearance will not count.
And by the way, the index going to return only the positive numbers. So you cannot retrieve the negative numbers. So they are easy and fun, right? Okay, moving on. Not only functions and methods, we could use as well operators in order to check something. Like for example, we could use the n and the not in operators to check the membership.
And this is really nice in order to double check whether our value is inside our list or actually it is missing. So let's try this out. Let's say that I would like to check whether in my list I have the number four. Now in order to do that, you're going to say is four in our list. So this is very simple. We used it a lot in the for loops. Now of course we have to go and print this out and let's see what can happen. So as you can see it is true. The output of the in operator is either true or false and Python did find it. Now let's try something else like for example eight.
So let's execute. You can see false. 8 is actually not in our list. And if you say not in so is 8 not in the list. If you go and execute it you will get true.
So as you can see it is very simple. We are just checking whether our value is a member of our list. Of course you can do that as well with the string values. Now another check that we could do that let's say that we have two lists. So this is list one and let's have another list. So let's go and create something like 1 2 3 and they're going to hold the same informations.
Now if you want to say you know what I would like to check whether those two lists are actually identical. So they are holding the same values. What you can do, you can go and use the equals to operator. So this is very simple. All what you have to do is to say list one equal to list two. So let's try this. As you can see it is saying true because those lists are identical. If you go and make here like new item and you execute, you will get false. So that means we can go and use the comparison operators in order to check two lists. And now let's have another example where we are comparing two lists like let's make here in the second list we have five. Now if you go and use the comparison operator the smaller and execute you will get true. So now how Python actually saying one list is smaller than the other one.
Well actually Python will not go and check all the values. It is lazy. It's going to go and check only the first value between the two lists. So now we're going to go and compare is one smaller than five. Well it is true. And that's why Python going to stop and say yes it is true. Now of course if you say what happens if they are equal and here we have something like eight. So if you go and execute it going to say false.
Now what happens at that Python going to check the first item. Well they are equal. Okay let's go and check the second item. Well the first list is higher than the second list. That's why it is false and it's going to stop. So this is what can happen in Python if you compare two lists. Now moving on to the next one. What can happen if you use the is operator? So now let's just make those two lists identical and then execute you will get false. So what is going on? They have the same values. If I go and use equal equal and execute, I'm getting true. But now if I use an s operator, why it says it's false?
Because those values are actually identical. Right? Now the is operator going to check the identity of your items. Now how Python going to store those two lists? We're going to have two separate lists in the memory and the s operator don't care about the values of your list. It's going to check whether they have the same memory address. So now since we have physically two different lists then the memory addresses are not equal. So it's like you are asking are the two list actually pointing to the same address? Well, if the variables are pointing to the same data then you will get true otherwise you will get false if you have two separate objects. And later you're going to understand exactly why do we need this operator as we make copies of the lists guys. So as you can see we have a lot of functions, methods and operators that you could use in order to do data analyszis on top of your data. So with that we can learn from the data and we can answer quickly analytical questions like what is the size, the total, the extremes, how to find specific item, how to compare them and of course doing exploration and data analyszis. It is a very important step before doing anything complex. So before you do anything hard, you have to understand the content of your list.
All right. So now so far everything that you have done is actually passive things. So how to read, how to check, how to analyze, we are not changing anything in the original list. But the thing is in real projects the data going to change over the time. So you cannot maintain the original data in the list.
You have to go and do changes and usually we have three types of changes.
You might get a new data and you have to go and add it to the list or maybe after analyzing the data you find some old data or let's say bad quality and you would like to go and remove it from the list or you say you know what let's go and fix it actually let's go and correct it. So you're going to go and update the content and the data of your list. So again either adding, removing or updating your list. So now we're going to learn how to add new items to the list. And this is very important in real data projects. You will be having always a new data like new customers, new products. And over the time you will be adding new items and expanding your list with new data. [music] So let's go.
Okay. So now we have our new value and we would like to add it to the list. The easiest way or let's say the lazy way to add it to the list is to say you know what just throw it to the end. So for that we use the method append. If you use it Python going to go and add this new item to the end of the list and maybe later you want to add another item and you use append then Python going to do the same thing. It's going to go and add it to the end. So it's like you are sticking and stacking items always at the end. Okay let's try this out again.
We have the list of the letters and I would like to add new values at the end.
So for that we have to use the methods and as usual we start with the variable name then dots and our method is a bench and here all you have to specify is actually the value. So let's say I'm going to add an x at the end. So if you go and execute it you can see we have now a new member in our list and exactly at the end. And now we can go and add another one append. This time I would like to go and add y. So let's check this out. Now in the output you see we have y at the end of the list. So this is really easy. Now with the append actually that was really lazy one where you just throw things at the end but sometimes you would like to add specific value at specific position. Maybe you want to sneak a value in the middle or at the start and for that we have another method called insert. Now let's say that I would like to have it between the A and the B. For that you have to specify for Python actually two things.
The first one is the position number and this is totally normal. If you want to change anything at specific position, you have to tell Python where. So now in this example, we want to put it at the position number one. And the second thing, you have to tell Python which value you want to add. So if you use this insert, Python going to put your value between the A and B exactly at the position number one. Now another one, let's say that you would like to add a value at the start of the list. So again, we need the two informations. The position number going to be the zero and the value itself. If you do that, you will be adding new value at the start of the list. So with that, as you can see, you have to do more stuff now. It's not lazy like the append, but you have now more control on where exactly you are adding your items. Okay, so let's try this out. Let's start with adding a value at the start. So letters dot and here the method is insert. We need two things. The position number, so at the start it's going to be zero. And then the value itself. So let's say X. Now let's execute it. And as you can see we are having now the X at the start. Let's say I would like to add something between the B and C. So again letters dots and now insert. So now we have to find out the index number. But we have to be careful. Now we have a new list because we've changed it with the previous insert. So now by looking to the output X is 0 1 2 and then actually it is three. So I have to add a three here. And this is really important to understand those kind of methods are changing the structure of our list. So it is directly affected and changed. So let's go and add here a new value like Y and execute. Now as you can see in the output we have Y between exactly B and C. Okay. So this is how you deal with the insert and of course we don't have to forget about our 2D matrix. So how we can add stuff here? Let's say that I would like to add one more list inside our list. So that means I need one more row. In order to do that, we could use append. So what we can do, we can say matrix dot append. And all what you have to do is to give your new list. So let's say I'm going to say X, Y, and Z. So let's check this out. As you can see, it is appending completely new row, new list inside our matrix. And if you don't want to add it at the end, you want to add it maybe at the start, then you could use as well the insert methods. So we want it at the start. That means we give it the position number or the row number. And then you give your list. So this time maybe a another A and third A.
So let's try this out. Now look at this.
Our new list, new row is actually at the start. So it is very simple. This is how you can add a new list and rows to your 2D matrix. Now this is easy. But now let's go to something little bit more complex. Let's say that I would like to extend this list here with a new member.
I would like to add at the end an X. So how we can do that? Let me just remove this here and I say you know what I want X and I can still use the append because I want to add it at the end of the list.
So now if I leave it like this and execute look here in the output you can see X is actually a new row or a new member of the whole list. But this is not really what I want. I wanted actually a member of the second list. So what happened here now by looking back how Python stores the data with this syntax currently we are targeting the first list and we said a bend to it an X. So Python did create a new pointer to a new object called X. So that means Python is actually extending the whole list the first objects. But what we want is to extend actually the child the sublist. And if you want to do that then you have to target the correct list because Python works with one level at a time. So in order to fix it, you have to specify which list by adding the brackets and saying the row number one.
And if you do that, then Python going to target the correct list and extend it exactly where you want. So let's try this out. I'm going to go over here and say, you know what, go to the index number one so that Python is targeting this row and then we are saying append to this row and X. So if you go and execute it, you can see in the output it is correct. Now X is member of the second list. Now let's try one more thing. Let's say that I would like to target the first list. So let's say I would like to add a Z at the start. We can do that. So matrix and now we are targeting the first row. So that means the index going to be zero. And now we cannot use append because I want to add it to the start. So we're going to go and use the insert. And now we have to give the index position where the new data going to be inserted. So I want it as well to be inserted at the start. And my value going to be zed. So let's try this out. Now look at this. We have a new value at the start of the first list. So as you can see it is very simple. Just do it step by step and be careful what you are targeting in which level you are doing the changes. All right. So with that we have learned how to add a new items either using the append. This is very simple. You want to add an item at the end of the list so you are stacking new things to the list or you use the inserts if you want to have more control. you want to sneak an item into a specific spot. So this is really important. In each real data projects, you will be having new data over the time like new customers, new products, new orders, and you have to keep expanding and adding new items to your list.
All right, friends. So now we're going to keep talking about how to change the data of our list. So now instead of adding something new to our list, we're going to talk about how to remove items from the list. And this is something very common that's going to happen after you analyze and explore your data. You might find out some old data or maybe duplicates that makes no sense. Or in worst scenario, you might find some bad data that is corrupting actually your list. So you want to go and clean up your data by removing those bad data.
And of course there are different ways and methods on how to remove things from your list. So now let's dive in. Okay, the first option and the easiest one if you say you know what I would like to get rid of everything. So I would like to delete and clear all the items and I would like to get back to an empty list.
So for that you have the method clear.
If you use it, it's like you are hitting the reset button and in one shot you would like to remove all the items. So sounds fun, right? Okay, let's try this out again. We have our nice list and I would like to go and remove everything.
I would like to have it as an empty. So all what you have to do is to use the methods clear and of course we don't have to pass anything for it because we are just now destroying. So let's go and execute it. As you can see in the output we are getting back our empty list. So this is really straightforward. It is always easy to destroy things compared to building them. But now sometimes you don't want to go and throw everything away. You want to pick one specific value and remove it from the list. For that you have two methods. The first one is removing by value. So all what you have to give Python is the value that you want to remove. And if you do that, Python going to start searching for this value. And once it finds the first match, it's going to go and remove it.
And after that it's going to stop. It will not keep searching for a match. So in this example, it will not go and remove the second B only the first match. So this is how you remove by value using remove. Okay. So let's try this out. I'm going to go and change something here. We're going to have a list like this A, B, and again A in order to have duplicates. Now I would like to remove by value. So I would like to remove the A. So all what you have to do is to use the methods remove and of course we need the value that you are searching for. And in this example, we want to remove the A. So let's try and execute it. Now, as you can see, Python did remove the first A because this is the first match. So, Python found it and removed it from the list. And look at this. We still have an A at the end because Python is removing only the first match. And if you say, yeah, I would like to remove as well the second A. Then you have to repeat the instruction. So, you have to say twice remove A. So, let's try this out. So, look at this. We have only B. The first instruction removed the first A and the second one removed the second A. So this is how the remove function works. Okay, moving on to the last way. If you say, you know what, I really don't care about the value. I want to remove something that is sitting on specific spot. So I'm more interested on the position number like I would like to remove the first item or the last item or exactly something in the middle. And for that we have a method called pop. So now for example, let's say that I would like to go and remove the first item. So again here we have to tell Python exactly the position number. So it's going to be the index zero. So if you give pop zero, Python going to go to the first position and remove it from the list. So as you can see, Python is not anymore searching for specific value. It is going directly to the index number and removing it. And if you want to remove something from the middle or what you have to do is to specify the index number. And there's something here really cool about the pop. If you don't specify any index number, the default going to be always the last item. So if you say pop without giving any number bison going to go to the last item and remove it and one more thing that is really interesting about the pop is that it will not just remove the item but as well it going to return the removed item to you. So with that you have the chance to understand what is removed and as well maybe to do something about it. So this nice method the pop is actually doing two things. It is changing your list and as well it is returning something back to you. Okay.
Okay, so now let's play with this method. I would like to go and remove now the last item. So as usual, letters dot and our method going to be pop and since it is the last item, I don't have to specify anything. It's going to be the default. So let's try this out and execute. As you can see, we are getting now A and B and we don't have the C. But as we learned, it is not only removing, but as well it is returning the removed item. So let's try this out. I would like to go and create a new variable to store the returned value. So I'm going to say removed and then I would like to print it. So print removed item double point and then our new nice variable removed. So let's try this out. Now as you can see the removed item is C. This is really cool. So it is changing my list and as well returning.
Now let's say that I would like to go and remove actually the first item. So this time we cannot leave it empty.
That's why we're going to go and specify the first index going to be zero. And if you go and execute it, you can see we don't have any more A and the removed item is A. Now let's try something else like something in the middle. Let's remove the B. It's going to be the index one. And if you go and execute it, look at this. We don't have B. And the removed item here is B. So it is easy.
And of course, we have to talk about how to remove stuff from our matrix. So that means I want to remove by value. So we're going to use the method remove and then specify the whole list. So A, B and C. Let's try this out. Now look at the output. We don't have this list anymore. So we are removing by value.
Okay. Let's take another example where I want to say, you know what, let's go and remove the last list. So now since we don't care about the values, we are removing by the position. So I want to remove the last one. We're going to go and use the pop. And since it is the last one, we're going to use the default. So I don't have to specify anything. So let's try this out. And with that, the only list that is inside my list going to be this one here. So we removed the last list. Okay. So this is how you remove a complete row, a complete list. Now let's go to the next level where I want to remove one specific value. Like for example, I would like to remove this one value, the E. How we going to do it? We're going to say matrix. Now, of course, we have to tell Python where to search. Now if you say remove e and then you execute you will get an error. So what happens here?
So now if you look to this we are trying to remove a string value from the big list. But here there's an issue. The big list has pointers only to an object that is list. So we are not pointing to any string value and that's why you are getting an error. Python is telling you you cannot remove string from this list.
But now if you want to do it correctly, you have to go to the child and then there you tell Python to remove the E because it is pointing to string values.
So let's see how we can correct this.
The issue is over here we are trying to remove E from the whole matrix. Well, this is wrong. I have to specify for Python exactly in which row it should search. So now it should search from the row one. Right? So we're going to go and say one. And now we are saying from the row one go and search for E. And if you find it, remove it. of course for the first match. So let's execute it. Now look at this. In the second list, we don't have anymore the value E. Okay. So let's try another thing. I would like to remove the first item from the last row.
So I'm not removing by values. I'm removing by position. So let's say matrix. Since I'm saying the last row, I can go and use the minus one. So it is the last list in the matrix. Or you can go and use the two of course. And then I will not go and use the remove. This time we're going to use the pop since we are interested with the position number.
So I want to remove the first item. That means the zero. So I am actually over here at the G. Now look at this. This is our last list. And we have removed the first item of the last list. So it is nice. Let's go and remove the last item of the first list. So now in order to do it, I want to target the first list. It is always the index zero. And now I want to remove the last item. So that means I'm going to use pop since we are removing by position and we are removing the last item. So that means I don't have to specify anything. As you can see in the first list we don't have C. So we removed the last item from the first list. You just need little practice and then you're going to get it. All right.
So with that you have learned everything on how to remove things from your list.
Either by hitting the reset button where you wipe out the list and you remove everything from the list or you say this is very extreme. I just want to remove one specific thing. And here there are two ways. Either removing by value using the method remove or you say I don't care about the value. I know exactly where to remove. So I'm going to remove by position using the pop methods. And of course by default you're going to remove the last item. But if you use an index you could go for example and remove the first item. So that now you have enough tools to clean up your list by removing old data or maybe duplicate data and in the worst scenario removing bad data from your list.
[music] All right friends, now we're going to go to the last operation that you could do on your list. We can go and update our data. So now again after you analyze and check your data, you might find something wrong. Maybe duplicates or let's say corrupt data. But this time you say you know what I don't want to go and throw it away from the list.
Actually there is a chance to fix it. So all what you have to do is to go and update the content of my data and then everything going to be fine. Or maybe the data itself changed over the time like for example the customer has new email or maybe the price of the products got updated to something higher or maybe your employee just moved from one city to another city. So that means data always get changed over time and we have to learn how to update it. So that means my friends, we're going to learn now how to keep our list up to date. So let's dive in. Okay. So now the question is how we can update the value of an item in a list in Python. Well, we don't have a dedicated methods to update values.
Instead, we can use the assign operator.
So all what you have to do is to specify for Python what should be changed using the brackets and the indexing. So here for example the index 2 and then use the equal operator and then tell Python what is the new value. So once you do that Python going to go to the position number two remove the old value and add the new value. So with that you have updated the content of the index number two. So that's it all what you are doing here is actually overriding the value.
Okay. So let's try this out with our nice example here. I would like to update the value of the first item with a new value. So all what you have to do is first specify the index number. The first value going to be the zero. And now in order to assign the new value, we use the equal. And then specify whatever you want. Like for example an x. Let's try this out. Now look at the output.
Instead of a, we have x. So we didn't add, we didn't remove anything. We just updated the content of the list. Same thing. If you want to update the next one, you're going to say letters one equal for example y. So let's try this out. With that we updated the second item. And of course be careful what you are updating. So for example if you go and say letters are equal to zed without specifying an index at all. And if you run it you will see in the output we will get zed. So we don't have anymore any list. And if you go and print the type now of the letters. So let's execute. You can see now the variable is actually a string value and we don't have anymore a list. So we have updated the content of the whole variable. So letters is not anymore a list. It is now a string value. So be careful what you are updating. If you just want one value, you have to specify an index.
Otherwise you are updating the whole list.
So now as usual we have to practice always with the matrix. Now let's say that I would like to go and update actually the last list. So all the values are wrong. And I would like to get a new list instead of those values.
So we're going to say matrix. And now we have to be careful. If I leave it like this, what can happen? We're going to update the whole matrix. We don't need that. I want to update the last row. So we [snorts] have to tell Python about that. Now the last row either you say two or I like to have the minus one. And now what going to happen for the last row? We are assigning completely new values. So we're going to say X, Y, and Z. So that's it. Let's try this out.
Now, as you can see in the output, the last list has now new values. So, Python did remove this list and replaced it with a new one that we assigned. Easy, right? Now, let's go to the next level.
I would like to update one specific value in specific row. So, for example, let's update the first value of the first list. So, let's go and do that.
And since we are talking about the first item, it's going to be as well zero. So now we are targeting the first item in the first list and we're going to go and assign for example I'm going to go with like special character minus. So let's try this out. Now look at this. The first item of the first row is now changed. Now let's go and target this middle one over here. It's going to be simple matrix. We are speaking about the row number one. And we are speaking about the value at the position number one as well. So let's go and give it another minus and execute. As you can see the middle value as well changed.
Now let's go to the last one. I would like to update the last value in the last list. Can be matrix minus one and we give it a minus. So let's execute.
With that we change as well the last item with the last list. So as you can see it is fun. It is easy. You can easily update a matrix in Python. All right friends. So that's it. This is how you can update and correct your data.
And with that my friends, we have learned everything on how to modify and change the original data of your list by adding, removing and updating. And this is of course very important as the data change over the time.
All right friends, so now we come to something very essential in any data project that you have to learn how to order your data. So the thing is now you might say okay my data inside the list is actually up to date there is no change needed but the issue is that the data is chaotic it is not organized so I have to go and organize it rearrange it change the order of the items inside my list and of course this is very important in real applications like for example if you want to create ranking where you are sorting the products by the most expensive price so you want to sort it from the highest to the lowest or maybe you have a list of customer names and you would like to sort it and organize it where you sort everything alphabetically and it's going to be easier to read and navigate. So that means ordering the data is something very essential in any real data projects. And now we're going to learn how to do that in Python for [music] your list. So now let's dive in. All right. Now let's say that we have the following list. So 4251. Of course you might say this looks not really good. So we can reorganize it by changing the order in specific way. And for that we can use the method sort. If you use it the items going to be sorted from the smallest to the biggest. So 1 2 4 5. And of course if you have letters it's going to sorted from A to Z. And we call this way of sorting data ascending. Okay. So let's try this out. We're going to have the following. Let's say letters. I'm going to start with C then A and then the last one going to be B. Now of course this is chaotic and I would like to sort it. Now the first methods we could use the sort methods. And if you leave it empty, then we're going to sort it ascending from A to Zed, from lowest to highest. So now let's try this out.
So now look at this. The data is already sorted in a nicer way. But now sometimes you might need exactly the opposite. So you want to sort the data from the biggest to the smallest. So for that, we're still going to use the same method sort, but inside it we're going to give it another arguments. So we're going to use the keyword reverse and then we're going to say it is equal to true. So if you do it like this you will get first 5 4 2 1 and if you have letters it's going to be from Z to A. So it is the same idea but we just change the direction.
Okay back to our example here again we have our unsorted list the chaotic one and I would like to go and sort it but this time from the highest to the lowest. So we can still use the methods sort but now since we want it descending we have to go and use the keywords reverse equal to true. So let's go and try this out. Now as you can see the list is sorted from the highest the C then the lowest B and then the last one going to be the A. So from Z to A. So actually that's it. And of course we have always to practice with our matrix.
So we have our matrix here. And I'm just going to go and change the order of the lists. Maybe something like this. Now if you say you know what let's go and sort our matrix. What can happen? So dot sort and let's just run it. So now look at this. Our matrix actually got sorted. So now of course the question is how Python is sorting a list inside another list.
Well the thing is Python will not go and check all the values of each list.
Instead Python going to go and compare only the first item of each list. So actually Python is comparing the D with the G and the A and based on the first element it's going to go and sort the data. So since A is the lowest it's going to be the first list and after that comes the D and the last one going to be the G. So, Python is sorting actually by the first item. But now, let's play around and say, you know what, we have two lists that start with the same character, but the second one is actually Z and one here we have a.
So, does Python going to sort it based on the second item? Let's see what going to happen. Well, look at this. If they both start with the same item, then Python going to go and check the second item. But again, we'll not keep checking the rest. It's only if the items are actually equal. So this is how Python sorts multiple lists. So now if you say I don't want to sort the lists, I would like to sort the items in specific row like for example I would like to sort the data in the second row. So in order to do that remember you have to target the correct list. So you have to go to the row number one and then say sort. So I'm telling Python sort only the row number one. Let's go and execute it. Now as you can see the lists are actually not sorted. It's only the items of the second list is sorted. So it is like anything else, right? Okay. So now so far all what we have done is actually we have changed the order of the items of the original list. But now if you don't want to touch the original list, you want a clean temporary copy of the list where there the data is sorted correctly. And for that we don't use methods. We use a function called sorted. So it sounds similar but it is a function. So you can use it in order to leave the original list as it is but you create a new copy where everything is organized. Okay. So back to our example.
This time I don't want to change anything in the original list but I would like to make an extra copy that is more clean and sorted. So in order to do that we have to create a new variable.
So let's call it new list. Something like that. And then we can use this time the function sorted. So it's not a methods and we have to pass for it now the variable the original list the letters. So you see already the difference the previous one called sort the function called sorted and it is a function not a methods. So now let's go and execute it. As you can see in the output we still have the letters unorganized. So nothing actually changed. Now in order to see the organized list you have to print the new list. So let's go and say I'm going to call the first one actually original list. double points and then the next one going to be sorted list and this going to be our new list. So let's go and execute it. So now look at this the original list is actually unchanged. So nothing is sorted here but the new list the sorted list you can see the same items but they are sorted from the lowest to the highest. And of course if you want to sort it from the highest to the lowest in order to do that we're going to use the same arguments and we're going to say reverse equal to true. So let's go and execute it. Now you can see in the output the sorted list is actually sorted descending from the highest to the lowest. So it is very similar to the method sort but the only difference here is that you are making an extra copy and you are not changing the original list. [music] So now we come to something that might confuse at the start. Let's say that you don't care about the value itself of the list. So you don't want to look at the data itself. All what you want to do is just to flip the list around. For that we have a nice method called reverse. So here be careful it is something else than the argument that we used in the sort. Now we have a dedicated method called reverse. If you use it, it's going to flip everything around. So the first becomes last, the last becomes first. So actually there is like no sorting logic involved. You are just reversing the list. Okay. So let's try it out again. We have our list and this time we're going to go and flip it around using the method reverse. So letters dot reverse and actually that's it. Let's go and execute it. Look at this. Everything is flipped around. The first item C is now the last item and the last item B is now the first item.
So it is fine, right? This is exactly how it works. And now here again we have the same scenario as the sort. we can do the reverse on an extra copy so that we don't change the original list. So let me show you how this is going to look like. Since it is a function, we're going to go and create a new list because it's going to return a new list.
So we're going to call the function reverse and then we're going to pass for it our original list, the letters. So now we have two lists and we're going to go and print them. So the first one going to be the original list and the second one going to be our reversed list. So let's add double points and here's going to be from the new list. So let's try this out. Wow, look at the output. We are getting some cryptical values over here. Well, this is not anymore our original list. It says it is an iterator. Well, it is not exactly like the sorted. Python will not create a new list. It's going to create another type of object called iterator.
And of course, we're going to learn about this type of object in the advanced topics. But now, of course, we can get back our list. If you go and convert the data type from iterator to list by using the function list. So if you do it like this and then execute, you will see in the output our list, but everything is flipped around. So it is not anymore this scary iterator. It is a normal list. So again, you use the method if you want to change directly the original value. But if you don't want that, you want to do it in an extra copy, then use the function reversed.
All right, friends. So with that, you have learned everything on how to organize and order the items of your list. And here we have a lot of methods and functions. So you could use sort without anything in order to sort from the smallest to the highest. Or you can use the keyword reverse equal to true if you want the opposite from the highest to the lowest. Or you can go and use the function sorted in order to create a new sorted version without touching the original list. And we have learned as well we could use the method reverse in order to flip the list around. And as well we have for this a function called reverse in order to create an extra list but backward without changing the original one. So those stuff are really important to organize your data and as well maybe to create some nice ranking in your data.
All right friends. So now in real projects actually we don't go directly and change the data and the content of our list of the original data because maybe you are just experimenting and testing stuff. You are not yet sure and you don't want to lose the original data. So you don't go directly and change stuff. Instead what we usually do with that we make our own separate version. So that means we go and copy the data from the original list and we put it in a new list. In the new copy you can do whatever you want. So you can do changes, you manipulate the data and if things goes wrong it's fine because you still have your original data. And this is as well very cool if you have both of them. So you have the modified data and you have the raw data. You can go and compare them in order to check whether you didn't lose any informations. You didn't lose any items after the modifications. So there are a lot of benefits and a lot of reasons why we have to go and create our own copy and my friend creating a new copy in Python especially for the data structure like a list it is something really tricky especially for beginners and there are a lot of methods and ways on how to do that and you have always to understand the effect are you really copying the data or are you making a reference so that's why we're going to deep dive now [music] into the different methods step by step so don't worry about it let's Okay, the first method is to [music] use assignment with equals. So again we have our original list over here. And now what we can do, we can go and create a new variable and then assign it to the original list using the equals. Now of course it is very important to understand what going to happen. Am I getting a real copy or just a reference?
Now if you do the assignment like this, what's going to happen? Python will not go and create a new copy. So there is no new list. All what can happen is that the new variable going to be referencing to the same list. So that we have now two variables pointing to the same list to the same data. So of course if you go and add an item to the first list variable then what's going to happen it's going to go and change the same list and this means the other variable the copy variable going to be as well affected because they have the same source of data. So actually if you are using assignment you are making a reference. You are not making any extra copy. Okay. So let's try this out again.
We have our original list and I would like to make a copy out of it. So in order to do that we're going to go and create a new variable letters copy. And now for this new variable I will not go and create something from the scratch.
I'm going to say you're going to get your value from the letters. So I'm assigning the original list to the new variable. Now let's go and print this out. We're going to say letters copy and maybe add a text. So copy double points and same thing here original.
Okay. So let's go and execute it. So that we have copied one list to another list. So now what is really interesting to see what can happen if I change something on the copy. So let's go and add new item to the letters copy and we're going to say append for example the character zed. So let's go and execute it. Now look at this. We see now the new item in both of the lists. Well, as we learned it is only one list and any change that we are doing to it, it going to be reflected in both variables.
So the same thing if I go to the original one and say letters and let's say I'm going to go and remove the last item. So we are removing the C. Let's go and execute it. Look at this. We don't have C in both of the variables. So as you can see if you want to copy one list to another using the assignment, it is risky. It is not safe because if you change anything anywhere it's going to go and affect all other variables. So be careful with assignments. [music] Okay. So now how we going to fix this?
[music] I don't need a reference. I need a real extra copy because I would like to go and play with the data in the copy without affecting the original list. So in order to do that we're going to use the method copy. If you do it what going to happen? Python going to really create a new container, a new list that is independent from the original list. And of course, each variable going to be pointing to its own list. So now, if you go and remove an item from the copy, it will not affect the original list because they are independent from each others. So with that, it is way safer and you can do whatever you want with the copy without affecting the original data. All right, let's try this out again. We have our original list and we are trying to copy it to another new variable. Now, previously we just done a simple assignment, but it is risky.
That's why we're going to go and try with the copy methods. So, we are saying create an extra copy for the new variable. So, let's try this out. In the output, we're going to see the exact same list in the copy. Now, what is interesting is to modify things. So, first I'm going to go and modify the copy itself by adding a new character.
So, we are appending the zed. So let's try this out. Now as you can see it is changing only the copy because we have a real extra copy. We don't have any more reference. So that means the change is going to happen only on the copy not on the original one. Now let's go and change something in the original by removing the last character the C. So let's try this out. As you can see the C disappeared from the original list but not from the copy. And this is way more safier than just doing assignments.
>> [music] >> And of course, as usual, we have to check the matrix whether everything is fine there. So again, we have this matrix, but I'm going to go and make it a little bit smaller. So I'm going to go and remove one row and one level of the items. So A, B, C, and D. So this is our original matrix. Now, let's go and copy it to a new variable. So we're going to say matrix copy equal to the matrix. But of course, we're going to go and use the copy since it is more safe. And we're going to go and print both of them. So original then we're going to go and print as well the copy. So copy then matrix copy. So let's try this out and execute. Look at this. We have a new copy of our matrix.
So now it's time to test whether everything is fine. We're going to go and remove the last row from the original matrix. So let's try this out.
Perfect. As you can see the row removed from the original but not from our copy.
Now let's try something else. We're going to go and add a new value to the copy for the first row, the first list.
So remember how to do it. First, we're going to go and call the variable. And then since we are talking about the first row, it's going to be the zero.
And we'd like to add. So we're going to go with the append the new value zed. So let's try this out. Uh-oh, we have an issue. Why do we have a zed as well in the original? Didn't we say they are like isolated lists? So what is going on?
Well my friend in Python the method copy considered to be a shallow copy. So what is that? So it looks like this. Remember the matrix you have the top level where you have a list pointing to another lists. So this is the top level the parent and then you go to the next level deeper to the childs where you have lists that actually pointing to the real data. So with that we have two levels.
Now if you go and use the method copy in order to copy the whole nested list to a new variable. Now what's going to happen? Python going to take the top level and copy it completely to the new variable. So that you have like two variables pointing to different lists.
So you have a real copy but the issue is that they will be pointing to the same children. So for the next level the deeper level you will not have a real copy. So that means you don't have a complete copy for all the levels. you're going to get only the copy for the top level, the first one. And this is what we call a shallow copy. It is not that deep, right? And now, of course, if you go and do any change to the deeper level, of course, it going to go and affect both of the copies because they are referencing to the same data. And if you go and change anything to the top level, it will not be reflected in the other list because only the top level is actually independent. So this is the issue with this method. the copy. It is a shallow copy. Now, how we going to fix it? Well, actually, we don't have any function and methods in the built-in module that can help us with that. We need to bring in some extra help and that's going to come from completely different model called the copy model.
And inside it, we have two nice functions. The first one is called copy and the second one which is really interesting. We have the deep copy. So now if you use this function what's going to happen is that unlike the shallow copy the deep copy going to go layer by layer copying everything no matter how deep is your nested list. So that you are 100% sure you have completely independent copies from the lists. There is no pointers there are no references and you don't have to worry anymore about your original data. So that means a deep copy is the safest way in order to create a copy. So it is completely independent. You don't have to worry. All right. So let's try out our new module. So first we have to go and import it. Import, copy. So with that we are installing this module only for this script. And now we are allowed to use anything inside it. So let me just remove all those stuff here. And as well let's go and remove the built-in copy. So now how we going to use something from our new module? First you have to write the module name. So copy dot. So with that Python understandhuh anything comes after it is actually belongs to the module copy. Now after the module name we're going to call the function name that is inside the copy.
So we're going to use the deep copy function. And since it's function we're going to go and pass for it the list the original list. So let's try this out.
Now look at this. The copy is working but of course we cannot trust this without testing. So let's try again by removing one item from the original list and execute. Perfect. As you can see, we changed something in the original list, but it didn't affect the copy. So that means they are not referencing each other. They are really independent lists. Now, of course, we're going to go one more deeper in order to check the childs. So now what we're going to do, we're going to test as well by adding a new member to the first row, but only to the copy. So that means I expect to see the new value zed only in the copy and not in the original. So let's try this out. Perfect. So as you can see it is working. We have changed a child without affecting the original list. So that means the function deep copy going to make real independent physical new copy of the original one. So everything all the levels no matter how nested is your list. So it is completely safe and there is no connection between the copies and the original values. So it is the safest methods. Okay, one last quick thing about the copy that inside this module we have another function called just copy. So it is not a deep one. Let's go and try this out over here. Copy. So the first one is actually the module name.
The second one is the function name. Now this function is exactly like the method copy. It is a shallow copy. That means it is only copying the top level but the same issue it going to be referencing to the same childs. So if you use it for the list you're going to get exactly the same results. Let me just show you quickly. So just remove those stuff and execute. So we are getting the same results. Now if you go deep and change something on the child what going to happen? It's going to affect as well the original copy. So the child are referenced. Now of course you're going to say what is then the difference between the method copy and the function copy. For the data structure list there is no differences at all. It is just something that going to give you flexibility with other types. As we learned methods like the copy belongs always to specific class. But the functions are always like allpurpose like generic. So that's why Python offers as well the copy as a function.
If you have a struggle with other data types where for example the double class doesn't have the methods copy. So for this case you're going to go and use the function copy from the module copy. So that means if you have a method called copy just go and use it. It is quicker but if you don't have it then you have to import the copy and use the function copy. So this is why we [music] have it.
By the way friends if you would like to have an access to all those sketches and my notes for Python you can find my Python handbook. There you're going to see around 200 pages only sketches and notes about the course. You can check the link in the description. Now, let's go back to the course.
All right. Now, I'm going to show you something cool on how to check whether you have like two independent lists or they are sharing something. Previously, we checked by adding and removing stuff in order to test. But there is a smarter way on how to do it using the is operator. Okay, so let's see how we can do that. Currently, we have our original nested list. And let's start with the first way assignment. So it is very simple. We're going to say copy one equal to original. Okay. So that we have two variables pointing to the same object. Now we want to check that right.
In order to do that what we can do we can say very simply is the original is copy one. So I'm asking are they like pointing and referencing to the same object in the memory. So if you go and execute it you will get true. Well yes both of them are referencing to the same object. So this is the first level. I'm going to go and add here something same object and then we have the check over here and at the end I'm going to go and print a new line. So if you go and execute it you will get true. Well yes both of them are referencing to the same object. So let's go to the next one and we're going to test the shallow copy.
So very simple copy 2 equal original but this time with the method copy and we're going to print the same thing but using the copy 2. So again we are asking the same question. Are those two list identical? Are they sharing the same objects? So let's go and test that. Well this time you are getting false. So that means each variable is pointing to a different object in the memory. They are not identical. And as you can see this is really nice right? But now what we can check as well we can check the children. Right? So let's go deeper.
We're going to say print shared lists.
So are they sharing anything? This time we're going to say original but we have to check the row. So for example you can pick any row like the first row over here and then you say is copy to and as well the same row are they sharing the same child and we print a new line and actually I don't need this one over here. So let's try this out. Now as you can see they are sharing the same childs the same lists. So again they are not sharing the same objects but deeply they are sharing same lists which is not really cool. So let's go to the last level deep copy. So copy three equal this time from the module copy. I would like to use deep copy and then pass to it our original list. So we're going to go and do the same prints over here. I want to check whether they are sharing the same object and the same childs but here we have to target the copy three.
Same thing here. So look at this for the first one is false. They are not sharing the same objects and as well deeply they are not sharing the same list. So actually Python is storing the first rows in different objects. So my tip for you after you copy anything go and check use the is operator to understand whether they are referencing to the same object or not. So this is really smart thing to do. All right friends. So now let's do a quick recap. I'm going to say don't just go use the assignment. It is very confusing and risky. I try to avoid it. Now about the methods, I usually go with the method copy because only if I have a simple list, one dimension, there are no nesting. So for that, it is more than enough. I don't have to go and import anything. It is quick. And don't worry about it. It's going to be safe as long as you don't have nesting. But of course, I'm going to go and use the deep copy. If I have nesting, I have a matrix. I'm just want to make sure that I'm really creating completely independent list with the parent list and with the all childs. So I don't need any shared references. I don't want any surprises. So with that you have learned all the methods on how to make an extra copy of the original data in order to experiments and to do your tests and without having the feeling and the risk of losing the raw original data.
All right, my dear friends. Now we're going to move to the next advanced thing that we usually do with our data that we can go and combine our data. Well, in real projects, we dream about it where we have one simple list where all your data is packed perfectly together. In reality, your data going to be spreaded around in different data structures and maybe lists like the customer information in one list, the products in another, the orders in third. And now, my friend, your job is to go and bring them all together and combine them in one [music] perfect list. So, it's like you are stitching around your lists in order to get your dream one list. And of course, in Python, we have different ways on how to combine your data. So, now let's dive in. Okay. So, let's see how we can do that. In order to combine things, we need two different lists. So, the first one going to be the letters.
And we're going to have a P and C. And let's have another one, a list of numbers. So, we're going to have 1 2 and three. Now, the first and easiest option is to put everything together in one big list. So, that means we want to create one long list with everything, all the items. Let's go and create another variable. So, the combination and this can be very simple. we could use the operator plus. It's like you are adding numbers or combining string values. So we're going to say letters plus numbers.
So if you do that you are just combining lists. So now let's see how it going to looks like. So we're going to go and print the combination. Let's go and execute it. As you can see we have a list with everything all the items. Now of course the order of the items depends on how you are like combining them. So if you start with the numbers then you will get numbers at the start. As you can see it is simple. Now just quickly since we are talking about the operators what you can do you actually can go and multiply a list. So if you say let's go and copy the letters and then multiply it with two. So what can happen in the output you will get in the output your list but this time you're going to get it twice. So we are not combining list here we are just multiplying it. Okay moving on. Next. Now you might say you know what yes I want them to be combined but at the same time I want them to stay in separate groups. So again we are talking about two lists inside one big list. So let's see how we can do that it is very simple. All what you have to do is to create a new list. So this is going to be the top level the main list and inside it we're going to specify the variable name and split them with the comma. So it's like you are creating a list but out of variables. So all what we are doing here is just building a nested list. So if you go and execute it, look at this. Yes, we have combined the two list in one big list, but still the inner groups are actually isolated.
So with that, you are not mixing everything in one big flat list. Okay, let's talk about another scenario. Let's say that you still want to have this one flat list, but you don't want to combine the two list in a third new list. All what you want to do is to change the list itself by adding the second list inside it. And for that, we have the method extend. It's going to go and stretch one list with another list.
Think about it like the append. We always add something at the end. So the extend you can use it in order to change one of the lists by combining it from the other one. Okay. So now we have our two lists. Now we have to decide which one you want to change. Now I'm going to say let's go and change the numbers. So we're going to say the numbers should be extended with the list from the letters.
So now let's go and print the results.
So let's first print the letters and then we're going to go and print the numbers. So let's see what going to happen. As you can see the first list will not be changed at all because we didn't modify it. But now look to the other list the numbers. As you can see it is extended. It is stretched. We have added to it the items from the first list. So that we didn't create a third new list. we just changed and extended one of the lists.
Okay. So now let's have another scenario. Let's say that I don't want to have like one flat list with all the items and at the same time I don't want to have everything isolated from each others. So both of them are for me really extreme. Now how about to go and pair them up? So we take the first item from the first list put it together with the first item of the second list and then we go and take the second item from the first list together with the second item of the second list. So if you keep doing this you are pairing the items from the two lists and for that we use the function zip. Now there are like few things that you have to pay attention here. The output as you can see the pairs are actually between parenthesis and this is what we call tpples. It's as well a data structure. So that means the output of the zip going to be a list of toupples. So it is something nested. And one more thing currently we have a perfect example a perfect match where the length of both of the list are equal. So let's say that the first list is longer than the first one. So we have four items there. So now what's going to happen is Python going to go and create a tubble with only one item. Well no.
Python going to stop at the shortest list. So that means all the extra letters and items will not be paired and it going to gets left out. Sad, right?
[screaming] One last thing that you have to understand about the output is that you will not get it directly like this. The output of the zip function actually going to be an iterator and object. So you will get in the output a weird number. But don't worry about it. You can always convert it to the list using the function list. So don't be scared of the output. Okay. So let's try this out.
We have our two lists. And now I'm going to go and create a new variable in order to combine them. And then we're going to use the function zip. Now we're going to go and specify the first list. And then we're going to say combine it with the second list. So actually that's it.
Let's try this out and print it. And if you go and execute, you will get these weird numbers. The special object zip.
And this is an iterator. We're going to learn about it later. Don't worry about it. And now I don't want to see this. I would like to see a real list. In order to do that, we're going to use the function list to convert the data type.
So if you do that now, we got our pairs.
You see the A with the one, the B with a two, C with three. So this is perfect.
Now let's make one of the list bigger than the other one by adding a fourth item. So we have here the number four.
If you go and execute it, sadly Python will not be able to bear it. So it going to stop at the shortest list. And now quick cool thing that we can do is that we can pair them with a string value. So if you say at the end for example let's go and add like hi and then execute it.
As you can see Python now is pairing everything together. So look at this.
The first item from the letters together with the first item of the numbers together with the first letter of the string. Don't forget string is a sequence. And the next double going to come from the second item from the letter. the second item from the numbers and the second item from the string. And now that's it. Python gonna stop because of the string. We have here only two letters. So that's why Python going to stop at the shortest. So this is really nice and cool function in order to pair things together. Now I would like to show you one real example about the zip function that I used last week. I had the list of employee IDs. So for example 101, 102 and 103. And I had another list where I have the employee names. So I have the names like this. So for example, Ali, Sara and John. So that was actually ids. And now those two lists were actually separated and I wanted to pair them together. So I would like to know which employee ID belongs to which name. Now if I just want and say do you know what IDs plus names and then execute. This really makes no sense because I still don't have the relationship between the ids and the names. So I don't know which id belongs to which name. Instead of that I used the zip function. So I used it here where I paired the ids with the names. And of course we have to convert it to a list like this. So let's go and execute it.
So now look at the output. It looks way nicer. Now I have the relationship between the ID and the names. All what I have to do is to make sure the order of the list is correct. Of course, if it is not matching, then I going to have the wrong ID for the wrong person. So, this is really nice. It's like you're building relationship between two different lists and it has a lot of use cases. All right, friends. So, that you have learned all the different ways on how to combine your data. So, the easiest and simplest way is to use the operator plus in order to combine a different list in a brand new complete list. So, this is really straightforward and easy. And another way we could use a nested list if you say you know what I don't want to put everything in one list instead I want to combine them but they still should be separated from each other and for that you have to create a nested lists and another way you could use the method extends if you say you know what I don't want to create completely new list this is wasting of resources I don't want to create a new variable I just want to extend one of my list from another list so it's like you are stretching one of your list with the content of other one. And another way you could use the function zip if you would like to pair elements by positions like as we learned the customer ID with the customer names. So I'm not mixing everything together and I'm not creating a nested list [music] so that you are creating something called tpple in a list. All right. So those are the different ways on how to combine multiple data structure in Python.
All right my friends. So now so far I can say we have covered the basics like creating, reading, analyzing, changing, sorting, copying and even combining list in Python. And that was actually only the foundations. And of course we don't want to stop at the basics. We want to push more to learn advanced techniques on how to deal with the data structure with the list. So we have to learn how to iterate through our list and we're going to learn the concept of iterable and iterator. We're going to learn how to do real data transformations. How to filter our data. So let's start with the first topic how we going to iterate through our data our lists.
All right. Right. [music] So now so far we have learned we can go and create like a container of items like a list and behind the scenes Python going to go and create an object with the type list and we could have of course other objects like the tupil set dictionaries and the primitive objects like the string integer boolean and so on. Now once you build such a container we have learned how to deal with it like how to read how to change it how to order it.
So everything is fine and we are happy.
So where is the issue? Well, the thing is everything, all the values, all the items will be stored inside the memory.
And this my friend going to be a big issue if you have a lot of data. That means you're going to be taking a lot of space in the memory. And actually most of the cases we don't need all the data that we have in the containers. We just need like a subset or few data. So now in Python in order to overcome this issue, we have another type of object called iterators. So the iterator on the other hand will not store everything in the memory. So it's not like container or box. It's more like a machine that going to produce values. So it's going to produce items one by one as long as you are asking for it. So now the question is how we going to work with it. Each time we have to ask the iterator for the next item and the iterator going to answer with the next item and move to the next one. So that means we cannot jump around. We have to keep pressing next until it's completely empty. It's exactly like the same iterator that you have in the for loop.
And now of course the question is how we can create this type of object. The iterator. Well, we have a lot of functions in Python to create them like the enumerate, the zip, reversed, map, chain, all those functions and stuff can help us to create iterator objects. And of course, you can change the type of the objects. So if you have iterator, you can make it any other type like a normal list. So now if you ask why do we need iterators? Well, mainly we have three reasons. The first one, we need it in order to build a loop. So for a for loop, we always need iterator to go through all the items to repeat specific block of code in order to do something.
The second reason is we use it in order to save memory. So if you have a lot of data, you don't want to store everything inside the memory. We use iterators.
They're going to stream the data one by one. And this is gold for big data streams. You don't want to put millions of row in the memory. You just want to process them one by one. as they come in. And the third reason is actually for speed and flexibility. So you can build real pipelines that going to go and transform the data on the fly without having them to be stored somewhere. So iterators are amazing and modern and they going to help you in a lot of big data use cases and as well they save you memory. All right. Now there are like two words that you might hear a lot in Python. Iterable and iterator. They might sound almost the same but they are not. As we learned an iterator, it is an object that's going to help us in order to do the iteration. So it is the process, it is the engine, the machine that's going to help us to do the iteration. But in the other hand, the iterable is the thing that you can loop over. Like for example, the list we call a list is iterable because it has items and we can loop through that. Add as well the string values. All of those stuff we call iterable because it is something that we can loop over. But if you have an integer even a big number or let's say true and false the boolean values they are not iterable. So they don't have like independent items that we can go through. So again iterator is the thing that's going to help us to iterate. And iterable is anything that has a sequence of items. It's like a container that we could use it in order to loop over. All right. Now enough theory. Let's go and practice. Again we have our list of letters. Now we want to loop over the items and it is very simple. So we're going to say for L in letters then double points. So that means I want to loop over our list and then we're going to say simply for example let's go and print the item. Now if you go and execute it. So look at this Python iterated through all the items of our list and printed it. So again this is possible because the list is iterable. So that means I can loop over our list. Now of course if you go and make any like big number stand of it and execute it you will get an error where Python going to say well integers are object that is not iterable. So you can do that only for lists. Now of course you might say why do we need to do those stuff? Why I have to loop over my list? Well actually we use it in order to do some transformations like for example I don't want to have a lowercase values. I would like to have it as an uppercase. So I'm going to go and transform each string value using the methods upper. So if you do that and execute you will see now my items are actually in uppercase. So we use it in order to do some nice transformations.
And now you might say okay but I would like to store this new result in a list.
Well for that we're going to go and create a new list at the start is going to be empty. And now what we're going to do each new value going to be appended to this new list. So what we're going to do, we're going to go inside the loop and we're going to say new list dot append. So I want to add something to my empty list. And I would like to go and add the items. But actually I don't want to add only the L. I would like it after it gets transformed. So we're going to add the upper. And now what we can do, we can go and print actually the new list. So let's try this out. Now you can see each step how our list is getting bigger and bigger. So at the first iteration we have only the first item, the A. Then we append to it the B and all of them are uppercase. And in the last iteration we appended the last item the C. So now you have nice understanding why we iterate right? We iterate through the original list. We do some transformations and maybe at the end we store it in another list. So this is why we loop through a list.
Okay. Moving on to the next step. We have an amazing built-in function in Python called enumerate. It going to take any iterable like a list or string value and it going to give you back both of the value and the index. Normally as we loop through the for loops we always go through the values. But there are scenarios where you need actually not only the value but as well its index. So if you use enumerate it's going to return two things. The position number of the item the index and by the way as a default is start by zero but you could specify when it should start. So you could choose where it starts and the other thing that it's going to return is the value itself. So let's try this out.
We're going to say enumerate and then we're going to pass to it our iterable value. It's going to be our list the letters. And now of course nothing going to happen if you don't print. So let's go and do that. Now as you can see in the output we are getting an object enumerate. So once you see those weird numbers that means this function is actually returning iterator. So again this function accept an iterable like the list that we have here and it returns in the output an object iterator that we could use in order to loop through. Now of course if you want to see the content of this iterator what you can do you can convert the data type to a list. So now look at this our list has now more information. We have a number for each item and this is the index number. And if you say you know what I don't want to start from zero I would like to start from one. So you can say start and now you define your own number. If you go and execute now look at this we start from one this time not from zero. So this is really nice but the thing is we don't use it like this.
We use the iterator in order to build a for loop. So we're going to say for and now this time since we have two informations two values we have to assign two variables for this loop. The first one going to be the index and the other one going to be the value. So before we used to have only one variable because we just loop through the values but now we have two informations the index and the value and then the same thing we're going to use the operator in and then we're going to say enumerate and we're going to give it the letters and I'm going to stay with the defaults where we start from zero. So now double point and now the question is what we going to do with it? Well, I'm just going to go and print it. So I'm going to say print index and then the value.
So I'm printing both of the variables.
Let's go and execute it. Now, as you can see, we have three iterations because we have three items and we don't have only the values, we have as well the indexes.
So that's why we say enumerate is an iterator because we can use it in our for loop in order to iterate through the values. Now you might say why do we need this actually? Why do we need the index?
Well, the thing is if you have a long list with a lot of values and let's say that you are iterating and you found a bad value like for example let's go and remove this value here. So we have now an empty string which is not really good and for the others we have a value. So if you go and execute you can see in the output immediately okay we have here an issue and I know exactly where this issue is. So it is at the position number one. So I use it a lot if I'm analyzing my data to find any issues and it's going to help me to find exactly where this issue is in my data. All right, moving on to the next one. We done this already. We have the reverse.
So if you use this built-in function, the reversed and you pass to it an iterable like the letters and then if you go and print it. By the way, as you can see those functions that could be used for the iteration has for me like different color. So the theme here showing like light blue. It's not green like the print, right? Because this one could be used for the iteration. So now if you go and execute it now look at this at the output we have again those weird numbers and stuff. That means this function is returning an object iterator and I can use it in my for loop. And of course if you want to see the content of this you're going to use the list. So we're going to change the data type. And now we can see in the output our reversed list. Now again we can go and use it in a for loop. So reversed and then the letters double point and we can print our items L. So if you go and execute it we're going to have three iterations but it starts from the end and ends with the start. So now finally we have now understanding why the reverse gives us those weird numbers because it is actually an iterator that could be used in our for loop. So now moving on to another function that we encountered that as well give us weird numbers. So let's go and make another list the numbers 1 2 3. Now remember the zip function. So we use it in order to pair two lists. So letters and numbers.
And as you can see the color of this function is again the light blue. Again depends on the theme that you are using in your visual codes. So if you go and print it you're going to get the weird number. But if you go and change the data type to a list then you will see the content. So now we can see we have combined the two list in one but we have paired all the values. Now of course we can go and iterate through those items like the one that we have in the enumerates because here we have pairs.
So in order to loop through two items you need two variables. So for L and N in zip letters and numbers let's go and remove this over here. So I'm combining both of the lists and now I'm going to go and iterate through them. So we're going to say print L and N. So the letter and the number. Now let's go and execute it. Now look at this. We are iterating at the same time in two lists after using the zip function. So A comes from the first list and one comes from the second list. B is the second item from the first list. Two is the second item from the second list and so on. So this function is doing actually two things for us. It is combining two list and at the same time it is returning a really nice iterator that we could use in order to iterate.
Okay, again we have a list of letters and as you can see the values are lowerase. Now sometimes you want to go and do some data transformations like for example I would like to go and change the case from lowerase to uppercase. Okay. So how we going to do it? We need to apply this function for each item. One solution could be like let's go and build a for loop in order to repeat the same task for each item.
But we have in Python way smarter solution. We could use the help of the function map where you give it two things. The function that you want to repeat for each item and as well the iterable which is the list of letters.
So now if you pass those two things for the function map in the output you will get your list but after transforming it well the output will not be immediately a list it going to be an object called map. So it is like an iterator that you could use it in your for loops but of course you can change the data type to a list anytime. It is very simple. The map going to take your list and apply that transformation that you define for each item. All right. So we have a list of lowercase letters and I would like to make it an uppercase. Now in order to transform all the values we're going to go and use the function map and here we need two things the transformation how we going to transform the data and as well our data. So here the transformation going to be like this.
I'm going to say go and use the method upper. So in the class string there is a method called upper. You have to go and use this method for that transformation.
Then after that you have to give it our data. So our data going to be our letters. So now of course to see something in the output let's go and print it and execute. Look at this. We have again this weird numbers. So that means we have an object an iterator. In order to see the data you have to change the data type list and then execute again. Now perfect. Look at this. We have the list of letters but everything in uppercase. So with that I have transformed my data in one very simple line. So it is not that hard. Let me give you another example. Let's take like a list of numbers. And the thing is my numbers are actually in a string values. Well, this is not really cool. I would like to go and transform each of those items to a number. So we have to go and change the data type. And for that we could go and use the map function. So now what is the function?
All what we have to do is to say ent. So I want to see the data as a class integer. And then we're going to give it our data. So let's try this out. But of course we have to cast it as a list. And as well we have to print it. Now let's go and execute. So now we have a list of numbers and not anymore a list of string values. So as you can see in this example I didn't even use any method or function. I just specified the class type and I said I would like to see the numbers in the class type integer. But if you need of course a specific transformation like here the upper then you have to mention it in the function.
Let's have one more example. Let's say that I have a list of names like for example Maria and I'm going to go and add few bad spaces left and right. The next one could be John and maybe at the end we have space and the last one going to be Kumar where we have on the left side a white space. Now this looks really bad. We have to clean up the data by removing those unnecessary spaces. So for that remember we have to use the method strip. So now again in order to transform our data structure we're going to use map. So here we have to specify the method it's going to be the strip and of course the question is where do you find the strip? Well the same thing we find this method inside the class string. So it is like the upper I'm telling here again use the strip methods that you can find inside the string class. So with that Python knows what to do and of course we have to apply it on the data structure names. same thing and as well our print. So let's try this out. Perfect. Look at this. We just cleaned up our data and we don't have any more those unwanted spaces. So now one last thing. Let's say that I would like to use the map function in the for loop. So I don't want to print it actually. I would like to go and iterate through the values. So we don't need to cast it actually to a list. All what you have to do is to say forn in and then the map function since the output is in iterator we can use it for the for loop and we can say print and then an n. So let me just highlight it and execute.
Now look at this we have now again all our values but after we transformed it using the method strip together with the function map which gives us at the end an iterator that we can use in order to loop through. So this is how we do data transformation on the data structure using the help of the map function.
Okay. So now [music] again we have our list of letters and we are happy about it. But now sometimes what might happen that maybe our users start entering stuff accidentally like maybe in number maybe boolean values. Now the thing is I don't want that. I want only to have in my data letters. Now, of course, you might say, "Okay, let's start reusing the method remove." In order to start removing those unwanted characters, but this is not really something that you want to do because it is wasting time each time you have to go and clean your data. Instead, we can build like a filter. And we specify the rule on how to filter the data. Like we say, we're going to keep only alphabetical characters, so only letters. And for that, we use the amazing function, the filter, where it is very similar to the map function. It needs two things. The first thing is the function and in this scenario we're going to use the is alpha and the second thing is the iterable which is our list of letters. So if you give it those two for the filter function in the output you will get the same list but after applying the filter function and of course you will not get immediately your list you will get like an objects a filter object so that it is not taking a lot of space in the memory you can use it in order maybe to iterate using the loops. So this is really amazing in order to filter out any bad data from your data structure. So all what you have to do is define your rule and give it your data and in the output you will get your clean data. Let's try this out again. We have here our clean nice data but with the time let's say that we are getting some weird data in between like for example an empty list or maybe we have a none so there is no real value and at the end we have a boolean value like false. Now those informations none false and empty string they are not true values they are just falsy values and I would like to go and remove them by building a filter on top of the list. So in order to do that we're going to use the help of the filter function and now we have to specify how to remove the data and we're going to say you know what go and remove the none. So if you use the none in the filter you are telling Python just go and remove the falsy data not only the none but as well the empty string and the false. It is little bit confusing but this is how it works. If you use the none in the filter and then you pass to it the data the letters then you will get only clean data. Now let's print it out. And of course in the output you will get the objects. We don't need that. I'm going to go and do the list.
In order to change the data type to normal data let's try this out. Now look at this. We have again our nice list of letters. We don't have empty none false.
So that means we have cleaned up our data from the falsy values using the none in the filter. Now there is another method that you could use in the filter where you're going to get the exact same results. If you go and use instead of none you say pool. So with that I'm saying just keep in the list the true values. So the a b c they are true. The empty none and false they are false. So if you go and execute it you will get the same results. You will get your clean data. So you could go with the bool or the none. Okay, let's go with another example. Let's say that we have a list of stuff like string values like the SQL and we have here a number inside a string. Then we have Python and the last one going to be as well string of numbers. Now let's say that I would like to make a rule that I accept only letters. So actually other data types like digits and numbers are not allowed.
It is very simple. In order to do that, we're going to use the help of the filters. So we're going to use a method inside the class string. It's exactly like the map function. You define the class and then the method name. So we're going to say is alpha. So this is our rule and then we're going to give our data. And in order to see the data, what we're going to do, we're going to have a list and print as usual. So let's check this out. Perfect. As you can see, we build a filter to our list. And what is left is only the items that are alphabetical. So we get rid of all those numbers from the list. And of course my friend we can build a for loop on top of the output of this function. So for I and filter I'm just going to go and copy the whole thing and then double point and we can say print our I. So let's go and try this out. Now look at this. We are iterating through the output of the filter function and we are getting only the alphabetical values. So after the data got filtered. So as you can see it is really nice in order to filter bad data from your data structure. All right my friends. So with that you have learned how to iterate through our data structure the list. What is the difference between iterable and iterator? Again iterable is actually the container the data structure itself like the list and iterator is actually the process the machine that going to produce the next items. So with that you have learned how to do data transformations in your data structure by using the help of the function map and it is very smart and simple. All what you have to give it is actually two things. the data transformation, the function that should be applied on each item of your list and you have to give it your list, your data. So once you give it those two things, it's going to go and do the rest. It's going to do the magic by transforming and changing all the items and data inside your list. In order to filter your data, you have only one function called filter. And it is very simple and similar to the map. It needs only two things. The rule, the expectation, and your data that should be filtered. And we usually use it in order to clean up the data inside the list or we want to focus only on specific subset of the data that fulfills a certain condition. So filtering data is something as well very essential in any data project.
Okay my friends. So now we're going to talk about another advanced concepts on how to work with data structure the list in Python. We're going to talk about the lambda. The thing is in Python there are a lot of built-in functions that we could use but sometimes you want to create your own custom logic and we can use lambda to do that. So now let's deep dive and understand this technique. The lambda function is actually a tiny function without a name and we also call it anonymous function. So instead of building a whole function we have like a short way or a shortcut with one line you can define a whole function and you will see lambda is used a lot together with the other data structure functions like the map filter sorts in order to do extra complicated logic on top of your data structure like the list. So how actually lambda works? So once you say lambda then you are like defining a function. It has an input like for example x and then you define in the function an expression and it could be anything like maybe you are doing some calculations where you say x multiplied with two or you are checking specific things like for example is the letter a exist in this input in this x and any other expressions and of course at the end you will get the result of this expression. So actually that's it you need only two things the input variable or variables and the expression. So what can happen with the input and that's it.
Okay. So let's try this out. All what you have to do is to use the keyword lambda like this. And after that you define any variable name for the input.
So for example I need only one variable.
So x and afterward double point in order to define now the expression. So what's going to happen with the x? We're going to go and multiply it with two. It is very simple. Now the question is how to use this? In order to do that, you have to define at the start for example a variable like for example multiply and then you say equal to the lambda. So that means actually the variable multiply is holding like a formula. We don't know yet exactly what is the value. So now in order to use it let's go and print and we're going to say multiply and now all what you have to do is to specify a value. So you are treating now the multiply as like any other function like the print you define the function name the multiply and then you pass for it a value. So here since we said okay we need an x that means one item I can go and have a number like two. So now if you go and execute it now look at this we have four this is exactly the result of our expression. So two multiply with two we have four.
Okay. So now let's take it to the next level where we have actually two inputs.
So we have two variables. So in order to do that it is very simple. We start with the first input. It's going to be the X and then comma and the second input. So now the rest is going to be the same.
We're going to have a double point. And now we're going to tell Python what going to happen with the two input.
Well, we're going to say X + Y. So I'm just adding the two inputs. So this is our formula. We're going to put the whole thing in variable name like add.
And then after that we have to go and use it. Now we're going to use the variable name and then we're going to pass this time two values because we have two inputs. So we're going to say one for the first value and two for the second value. So let's try this out. Now look at this. We are getting three. So 1 + 2 3. Mega easy, right? So now so far as you can see we are using some kind of like calculations on top of our input.
But now what we can do we can make like a check in order to check the input. So we don't want to calculate anything. We are just like analyzing and checking. So our expression will not be manipulating the data. It will be just checking. So now for example, let's say that I'm going to go and check whether a character or a letter is part of another string. So for example, we're going to say lambda. We are checking only one value. So it is only one parame. So the input going to be only one character. So it's going to be only one input. We could use any variable. So we don't have always to be an x. So we're going to say i double point. And then the expression this time going to be a normal check. So we're going to say I in and then let's have any like string value for example Python. So I'm checking whether the input is actually character in our string. So that's it. Let's go and give it a name and then we're going to print the variable name. And now we have to pass any value like for example let's start with like an n. So is n actually a member of this string value? Let's try this out. Well, it is true. So as you can see we are not changing the value.
We are just checking. Let's go and pick another value like for example zed is zed member of the python. Well let's check out. As you can see we are getting false. So an expression could be anything.
All right. Now with this you have the basic understanding on how lambda works.
So for example if you look at the map function we said you need two things.
The first one is how to transform your data the function and the second thing going to be your data like the list. So now we said we could use some built-in functions and method from Python like the upper lower or is alpha and those stuff. But if you say you know what I need to do something specific so some extra complex logic we cannot go and use the bu-in stuff we could go and use the help of the lambda in order to specify what we want to do. So with the lambda we're going to define exactly how we going to transform our data structure our list. Okay. So now let's practice.
I'm going to show you an example that I had in my project. So I had a list of prices but the issue is that the prices were in string values and as well each price has a dollar sign at the start. So we have 1250 maybe another price like as well with a dollar sign 9.99 and the last one as well with a dollar sign 100.0.
Now the thing is with those values you cannot do a lot of aggregations and calculations like the average min max.
You have to convert it from a string to a float. So that means I have to transform my list from a list of strings to a list of floats. But the challenge is that you cannot go immediately from a string to a float because of those string values. So that means first you have to do some manipulations to remove it and then change the data type to a float. Now I'm going to show you how I usually do it. I don't jump immediately and start writing lambda and stuff. I try to build the transformation like with one value and if it works then I go to the lambda. So for example I'm going to say let's have a variable called p and I'm going to give it the first value 1250. So now I'm going to start like playing with this value. So the first thing that I'm going to go and get rid of the dollar sign. So how to remove a dollar sign from a string value. Think about it which string methods we have to use. Well, we can go and use their place. And what we're going to do, we're going to replace the dollar sign with actually nothing, an empty string. So let's go and try this out. Perfect. Now I don't have the dollar sign, but still my value is actually a string value. The next step of that transformation, I'm going to go and change the data type. So it's going to be very simple floats and then the result of the replace going to be the input for the float. And with that, we will be changing the data type.
So look at this. We have 12.5. And of course if you want to be very sure we can go and check the type. So as you can see now we have a float and this is exactly what I need in order to do some aggregations. So I have actually the formula and now I have to put it in lambda. Now the input going to be only one price. So for example p double point. Now what we're going to do with the prices is actually this whole transformation. I'm just go and copy and paste over here. So now I know the input. I know what going to happen with my input. Now the next step is to go and apply the whole thing in our list. So in order to do the transformations, we're going to use the map function. And then we're going to pass for it our list. It is the prices. Let me just get rid of this. And of course the output of this going to be an iterator. That's why you have to go and change the data type to a list in order to see the values. And the last thing of course I would like to see the output. So let's try this out. Now look at this. I have a list of floats.
And this is exactly was my data transformation that I wanted to do. I change some weird string values to a float. So again step by step always think about how to do the transformation then put it in a lambda and then use a map function in order to apply the whole thing to your data. It is not that hard right? Just do it step by step like this and you can do amazing custom transformations on top of your data structures.
Now the same thing for [music] the filtering we need two things the rule on how to filter the data and the data itself. Now about the rule the function we could use the built-in methods and functions. But if you want some custom specific filters we're going to use the help of the lambda again. So that means with the help of the lambda we going to specify exactly how to filter our data in the list. Okay. So let's try this out. We're going to have a simple example where we have a list of prices.
Let's say I have 120, 30, maybe 300 and 80. Now my goal is I just want to see the high prices in my list. So everything equal or higher than 100 should stay in the list. All other prices that is lower than that I don't want to see it in the list. So that means we have to build a filter. And for that we need some custom logic. Now the first step is to think about how you're going to write the condition the logic for the filter and it going to be very simple. The price going to be higher or equal to 100. So this is the logic. Now in order to use it for this data structure, we have to go and use the help of the lambda. So we're going to say lambda and we're going to pass for it of course one price. Now the question is what we going to do with this price?
Well, actually we're going to check whether it is equal or higher than 100.
Okay, so we have the logic. The next step is that we're going to use the filter function. So we're going to say filter and this is the filter logic and we have to pass for it our data. So what should be filtered? It's going to be the list of prices. Now, of course, to see the results, we have to change the data type. And I would like to print the results. So, let's see what can happen.
Look at this. We have a list with only high prices. Everything that is higher or equal to 100. So, very easy, right?
We use the lambda in order to define the custom filter logic. Then, we go and use it in the filter function together with our data. So, it is easy. This time, we're going to go and use a nested list.
So we can have a list of students with their scores. So let's say in the first list we have the name and as well the score and let's have another student let's say Omar and we have 90 and the last one max and we have 60. So three students with three scores. So now let's say that I would like to have a list where the students has a score higher than 70. So anything below that should not be part of my list. Now, of course, this is very specific. You don't have a function for that or a methods. You have to build your own custom filter. Let's see how we can do that. So, the first thing is that we have to go and select exactly what we are checking, right? So, it's going to be the students and let's go for the row number zero for example. Now, what we want to check is actually the scores, right? So, it is the index number one.
So, now this score should be higher than 70, right? Now, we can go and test it, of course. So we're going to say print and then execute it. You can see it is true. So the first student is actually matching our rule. Let's go for the second student. It is true. The third student. So with that actually we have our filter rule. Now the next step we're going to go and put it in lambda. Now let's do that. We're going to say lambda. Now here it is interesting. What is the input for this function? It's going to be the whole row. So it's going to be row. So I'm going to send Maria and as well the value. And now what we're going to check in this row is actually the index number one, right? It is the second value. So we're going to say row and one. Again, I'm sending to the function the row and I'm checking the second value of the row. If it is higher than 70, then everything is okay.
So this is always how we're going to do it in the nested lists. You send the row to lambda and then you tell Python what to do with this row. Now the next step that we're going to put everything in a filter since we are building a filter.
So filter this is our rule and then of course we're going to send it our data.
So the students and now of course in order to see the results we have to convert the object to a list and as well of course to print. So now let's try this out. Now look at this at the output. We have Maria with a score 85 and Kumar with a 90. But we don't have max with 60. So actually our filtering is working. So as you can see with that we have built a custom logic using the lambda and we used it together with the filter in order to filter our nested list. Now another application for the lambda we have the method sort in order to sort your list. So if you use the default going to sort it from the lowest to the highest or if you say you know what let's sort it from the highest to the lowest using the reverse equal to true. But now how about to define our custom way on how to order our list. And again we could use the help of the lambda in order to exactly explain for Python how to sort the list. Okay. So now another challenge maybe to you if you want to pause the video. Now I would like to keep only the students where the first character of their names start with M. So that means by looking to the data I expect you to have only two students in the outputs Maria and Max because both of them starts with M. So pause the video otherwise I'm going to show you how it works. Now again I start just with the logic. So let's get any students like for example the second one. Now what we are interesting is with the first information with the first name. So it's going to be the index zero. And now in order to check the first character of any string we're going to use the method starts with and then we're going to tell Python what we are looking for. So we are checking whether the value start with M. And actually that's it. So let's go and print it out and execute. So as you can see for max it is working. Let's go for kumar and try this out. We are getting false. So actually my logic is ready and I'm going to put it in lambda. So lambda as usual we're going to pass the whole row double points and then we have to specify which value in the row. Well it's going to be the first value the first name. So the index number zero and then we're going to say starts with m. So I have the whole logic. The next step I'm going to go and put it in a filter in order to filter my data students and same thing list and the last one print. Now I'm just going to go and comment this out. So let's try this.
Look at this. We have now the students where their first name begins with M. So as you can see it is not that hard, right? Do it step by step. The first one is to define the filter logic using lambda and after that go use the function filter together with your data in order to filter your list. And that's it. All right my friends. So that's all about the lambda. It is an amazing technique in order to create your custom logic in very short and easy way and it's going to be great assistance to use it together with other functions and methods like the map filter and the method sorts. So this opens the door for many flexibility in order to define your logic.
All right my friends. Now we have reached the final advanced technique, the last advanced tool that we have in the toolbox in order to work with the data structure in Python. And to be honest, this one is the coolest feature in Python. We have the list comprehensions. So what is this? Now we have learned in the advanced topics that we can iterate through the data. We can do data transformations and manipulations. We can filter our data.
But now how about to put them all together in one simple line? Sounds like a perfect plan, right? So now let's deep dive into the list comprehensions. Okay.
So now let's say that we have the following three prices. And I would like to let's say multiply the prices but only for the high prices only if the price is higher than 50. So the 80 going to be 160. The 20 it will not pass the test that's why it will not appear in the results and the 100 it going to be 200. So we are filtering and transforming the data. Now in order to build this in Python, we could use the way of comprehension. Well, we don't have a keyword like a method or function for that. You have to build three blocks in one line. The first block is going to be your data transformation. So it's going to be your expression, the price multiply by two. And then the next block is going to be the loop itself. It's going to be a very simple for loop in order to go and iterate through the items of your list. And now in the third and last block, you will define how to filter your data. And for that, we're going to use the if statement with a condition price is higher than 50. Now what going to happen behind the scenes if you execute this one line of code, the first thing Python going to go and create the iterator out of your list and after that it going to build a classical for loop where it's going to go through all the items one by one and once there are no more items to process, it's going to go and jump to the end. Now what is interesting what going to happen inside the loop? Well, Python first going to go and filter your data. So, we're going to have first the if condition. It's going to check whether the price is higher than 50. And only if this true, Python going to go and do your data transformation. So, the expression going to be price multiply by two and then go back to the start of the loop. And now here it is interesting. If it is not fulfilling the condition, it is false.
This time it going to skip everything and go back to the top of the loop. So that means if your item is not fulfilling the condition, it will not be transformed and as well it will not be appended in the new list. So it will not appear at all in the final results. So now let's go through it quickly with this example. Python going to go and grab the first value the 80. Then first it's going to try to filter it. But since it is fulfilling the condition 80 is higher than 50, why it's going to go and implement your expression. So it going to multiply it by two and append it to the new list. Then go back to the top of the loop and grab the next item.
It's going to be the 20. Now 20 going to go to the test and this time it going to fail it because 20 is not higher than 50. This time we're going to go to the false and it's going to skip everything and go back to the top. So for the 20 we will not have anything for it in the output. Now the last one we have the 100. It is passing the test but then going to go and multiply it with the two and then it's going to go and append it to our new list of course at the end and then go back to the top of the list.
Well, we don't have any more items to process. It's going to go and exit the loop and then return the new list. So, this is how the comprehension works and Python going to execute it like this.
First, it's going to loop through your items, try to filter your data and then if it pass the filter, it will run your expression to modify your data. And one last thing that you have to understand, filtering the data is optional. So, that means actually you can skip it. If you do that, Python has to iterate through all the items and apply the expression for all the items. So filtering the data is totally optional. Okay. So now let's practice. I'm going to show you an example that I have as well in a project. So I had like a list of domains. URLs for the domains could come in different formats. So for example, you could have like ww.google.com and maybe another one for example.com.
And a third one it's going to be like localhost. So you don't have com or anything. And last one let's make it uppercase data with parro.com.
Now you can see the data is not really clear. We have different formats different cases and as well we don't have like a domain here in the local host. So I would like to normalize and transform the data to standard format and at the same time removing things that are not actually domain. So that means I have to do at the same time transformation and filtering. And for that we're going to use the comprehension. First we start with a new list. So let's call it clean.
So now here we have to add the three blocks. The first one is data transformation.
The second going to be the for loop and the third going to be data filtering.
Now usually I start with the for loop.
Of course you can put everything in one line by the way. Now for the for loop it's going to be very simple. for D and domains. That's it without double points by the way. So with that I have the iteration and the loop. The next step I start working on the data transformation. So now what we're going to do in the data transformation we're going to clean up the data. The first thing that is really annoying is the lower and uppercase. So I'm going to say I want to skip the uppercase. I'm going to make everything lowered. So for each domain apply the method lower. So this is the first thing. After we made everything lower, we can go and get rid of the first part www do because it is unnecessary for domains. So I'm going to say replace remove for me the first part and replace it with nothing. So this is how you remove. Now I'm going to say if you do that you're going to clean up the data and I think the best example here is the openi.com. It is lowercased and as well without the first part. So with that we have covered the two blocks and now we come to the data filtering. Of course, this is optional, but now I need it in order to remove the local host.
So, it is actually very simple. We're going to start with an if. So, what is the condition? Well, actually the domain should contain a dot. If we don't have dot, then this is not valid. So, if dot in D, if that's true, then do the transformation. Well, actually, that's it. So, as you can see, between the blocks, there will be no commas, there will be no double points or whatever. Of course, if you want to put everything in one line, you can do that. So if I go and do this then after that the four I just have to separate using a space then of course it's going to work but to be honest I like it like this because it is easier to read right now the last step of course we have to go and print our new list so now let's go and try this out well I have an issue I think I have here one extra W right so let me just try it again now look at those domains they look really nice so we don't have the W's at the start and everything is lowercased so Everything is clean and normalized and we don't have as well our bad data the local hosts. So look at this how Python is amazing and very compacted. We have done many things in one statement. So we created a new list.
We iterated through the items. We have done cleanup and data transformations and we have built a filter on the data in just one line or let's say here three lines. So this is amazing. Now let me just show you it quickly. If you go and remove the filter and execute it's going to work. You will not get any syntax error. But with that you are getting of course the band data in the output. And by the way there is one more thing that I can show you with that I can get back my filter. But let's say that I just want to filter the data but I don't want to change actually the values. So I don't want to do any transformations.
But if you go and like remove it and execute you will get an error. Of course you have to have a first part. But now instead of writing a whole expression I just go and add the item. So only the D.
If you do that then Python going to accept it. And with that we are saying okay append the D but don't do any transformations on it. In this setup here we are just filtering the data and creating new list. Let's try this out.
Now look at this. We have only three items. So we don't have the local host but we have the exact same values like before without any normalizations. So this is as well one way in order only to filter the data without doing any manipulations if you want the same values as before and of course I think in this scenario I can go and put everything in one line because it is very quick and short. So if you go and execute it it will work. So again you can do transformation and filtering or only transformation without filtering and the last scenario like here only filtering the data. So once you understand the three blocks it's going to be easy to understand what is going on here because to be honest at the start as I saw the comprehensive list it was not really clear for me what is going on. So D for D in domain it was a little bit hard for me but as I understood the plot and how things works behind the scenes everything clicked and I understood how this works. All right, my friends. So let's recap. The list comprehensions is the coolest thing in Python. You combine actually three things. We go and loop through the list.
Then we do data filtering. And the last thing we do data transformation and everything is actually in one line. So this is very powerful for data analyszis and data engineering. And this is the most elegant feature that I know about data in Python.
The first thing we can do is how to access a list and you can go and pick directly one by using parenthesis and specifying the position number. So we call this indexing you are getting exactly one item. But if you want to get multiple items we have to use something called slicing. So if you specify the start and the end you can cut exactly how much you need. And another thing that we can do to our list is to unpack it. That means we're going to split all the items into multiple values. So in one line in one go you are assigning multiple variables to the values of your list. And in Python we have a lot of built-in functions that can help us to explore and analyze the data. Like for example if you want to find the highest value in a list then you can use max or you can do the opposite where you are searching for the lowest value using the function min. And another one if you want to find out how many items do we have inside the list and for that we use the function length or we can check our list by finding out whether there are any missing values by using the function all all values must be true in order to get true and the opposite where it is more relaxed we have the any going to checks if at least one true in our list and as well we have other methods that's going to help us to analyze our list like the count it's going to tell us how many times an item appear appears in our list. Another method called index in order to find out where exactly we can find our item the position number and as well we could use operators in order to analyze our list using the in operator.
So we are asking is this item member of our list or not? And we can as well change our list. Like for example, we can use the method append in order to add an item to the end of our list. Or you can use the method insert in order to put them exactly where you want by specifying the index. And now if you want to remove all the items you want to hit the reset button, you can use the method clear in order to remove everything. But now if you want to remove only one thing, you can use the method remove. So here we're going to remove by specific value or you can go and use the method pop in order to remove by position number. By default, it's going to remove the last item. But of course, you can specify exactly which position should be removed. Now if your list is chaotic and missy, we can sort it like using the method sort if you want to sort everything alphabetically or numerically from lowest to the highest. Or you can use the method reverse if you want to flip the list around. So the first item going to be the last one and the last item going to be the first one. Now we come to something confusing where you want to create a copy. The first option we can use assignments. If you want to point two different variables to the same list. So any change going to affect directly both of your variables. But if you want to have like a real copy then you can use the method copy so that you're going to end up having two variables pointing to two different lists. This is more safe especially if you have like very simple list. But if you have nested lists this will not work because this going to create a shallow copy. So only the first level is separated but all the childrens are shared. That's why we have another method called deep copy where everything all the levels going to be isolated in a real copy. Okay moving on. In real world usually we have a lot of lists and we would like to merge them all in one list. So you could use the plus operator in order to combine two lists and you generate brand new list in the output.
This is really nice if you want to keep the originals. But if you don't want a new list, you would like to extend one of them. We have methods called extend.
So it going to go and stretch and add one list with the items of another list.
So the only difference here is one create something new and the other just expand what is there. Now next things going to get more advanced. For example, we have a function called zip. It's like a zipper. It's going to go and connect items from multiple lists into pairs. So the first item with the first item, the second with the second, and so on. And another nice function, we have a numerator. It's going to go and automatically adds an index number for each item of your list. So it's pairing the values with their index. Now we come to something serious and very powerful in real projects. We have a function called map. It's going to go and apply a function, a data transformation for every single item inside our list. Like for example, changing all the letters to a lower case or to an uppercase or doing some casting. So we are applying a data transformation for all the items. And another function which is very similar called filter. You're going to define your condition, your rules, and Python going to keep only the items that fulfilling your condition and anything that is not meeting the requirements, it going to be filtered out. And now we come to the last one, the most elegant solution from Python. You can combine data filtering and data transformation into one line. So that means we're going to use two rules. You're going to define the filtering and then you're going to do the data transformation. So it's like both filter and map function in one. So that's it. Those are the 30 things, functions, method, operators that you can use in Python data structure lists and I think you can go and print this cheat sheet and each time you are dealing with data structure you can use it.
Hey friends, so now everything so far that you have learned all the methods the functions the operators they were applied only on one type of data structure the list. But the thing is in Python we have three more. We have the tubble, set [music] and dictionary. So in total we have four types of data structures. That's why we have to go and learn them all understand the differences between them so that at the end you know how to choose the right type for the right scenario. Now the first thing that I want you to understand that each data type structure behaves differently in Python. Each one has its own personality and usually we categorize them into four main characteristics. So we have ordered, allow duplicates, indexed and mutable.
So now let's understand those four types for the first data structure the list.
Let's go. Okay. So let's make very simple list of numbers. We're going to have 10, 30, and 20. Now the first characteristic is ordered. So that means Python going to remember the exact position of every value in your list. In other words, nothing going to change unless you change the order. So that means if I go and just print our list and execute, you can see the first item is the 10, the second 30 and 20 exactly like I defined it. So Python didn't change the order of my data. It kept it exactly like I defined it. So for example, if we put the 20 as a second item and the 30 as the last and execute, you can see Python is following my orders. That's why we call the list is ordered. So we're going to say here ordered. All right. Then second one it's all about duplicates and in Python lists allow duplicates that means you can store the same value more than once and Python going to keep it. So it will not remove it. So for example we have here 10. If I go at the end and add another value 10 it is totally fine. So if you go and execute it you see in the output we have the 10 twice. That means it allow duplicates. Now moving on to the third characteristic we say lists are indexed. So what this means we can access the values of any list directly using their position number their index number and by default we always start with a zero. So for example if I would like to go and access the second value the 30 it is very simple as we learned before we're going to use the variable name and then we specify the index number it's going to be the one. So now if I execute it you can see I'm accessing the value 30. That's why we say it is indexed. I can go and access any value of my list using the index or position number. So far everything is easy right now to the last one and to the most important characteristic. The lists are mutable. That means after I create a list I can go and change it like adding, removing, updating, rearranging the values. I can do whatever I want. So for example I would like to go and update the last value over here. we can say my list and it's going to be the position number three and let's go and assign it for the value 40 so that I'm updating my list. So let's print it out and check. So as you can see in the output the last item now is 40 that means the list is mutable. So you are mutable. So my friends this is what we mean with the four characteristics and with the list as we understood it is ordered it is allowed dlicates it is indexed and changes are allowed. So it is mutable. So if you are using list, everything is allowed and that's why lists are simple, flexible and used almost everywhere. It is my default option. Enough with the list.
Now we're going to deep dive into the second data type structure that let's go.
All right. Now my friends, if you are using a list in real projects, you're going to find out sometimes it is really risky. Like for example, if you are storing the database informations, the database server, the port, the username.
Now, if you use a list, what can happen?
You're going to be a little bit scared that anything inside your code might change those values. There is no guarantee. Maybe you or someone else that did something and changed the data.
And if this happens, you're going to lose that connection to the database and everything going to fail. So for this scenario, we don't go and use a list.
Instead, we use a tpple. So what is a tpple? A double is an ordered collection that cannot change after creation. So it is very similar to the list but with one big difference. It is locked. It is frozen for safety reasons. Once you create it, Python will not allow you or anyone else to change it. So it is exactly perfect for this scenario. And the syntax for this going to be very simple. We are going to use as well special characters, but this time we're going to use the parenthesis. And as usual, you separate your values using a comma. So now as we have done with the list, we're going to check now the four characteristics of the tpples. Let's go.
Okay. So let's go and create a very simple tpple using numbers. So again 10 30 and 20. Now the first thing that we're going to check whether the tpple is ordered. So let's go and just print our tpple. Now if you do that you can see it is ordered. So as you can see we have 10 30 20 exactly how I defined it.
So Python going to keep the order as I define the double and we say it is ordered. Now let's go and check whether it allow duplicates. So I have here 10.
I'm going to go and add another 10 and then execute as well like the list it allow duplicates. So I can have the same value more than once and Python will not drop it. Now the next thing whether it is indexed. So again I would like to access the second item the 30. Let's try this out. So my double and the index number going to be one. So let's try this out again. The same as list I can access any value using an index number.
So we say a double is indexed. So far everything exactly like the list. Now to the first characteristic can we change something after creating it? So is it mutable? So again for example let's go and update the last value. So the 3 to something like 40. Now let's go and execute it. Well, you're going to get an error because a tubble is immutable. It is totally frozen and Python will not allow you to change anything in the tubble after the creation. I can read it. I can access it. But I cannot go and modify it. So you cannot add anything.
You cannot remove or update. So if you go and try any method that we learned with the list like for example the remove as you can see it is white because we don't have such a method for that double. So it will not work or uh pop add all of those stuff are not allowed and that's why we say a touble is immutable you cannot change it and now you might say you know what okay we don't have a methods for the table but I can go and use the functions like for example the sorted it's going to go and sort the data like in the list so let's try this out to sort a double and of course we're going to go and print it so let's try this out and we add this comment here so let's go and execute you can see the data is sorted. So we were able to change the data. So we have 10 10 20 30 is ascending. So it is immutable. I can change it. But my friend, this is a list. This is not anymore a touble. So the output of this function going to always be a list. You can pass for it. Yes, a tpple. But in the output you're going to get a list.
So you cannot really change anything with a tubble. Even sorting the data differently using a function. So this is how the tpple behaves in Python. It is very similar to the list. It is ordered allow duplicates. It is indexed. But the only big difference here it is locked frozen. So you cannot change anything after you create it. So double is mutable. And for the use case you're going to use it each time you have the feeling that data must be protected. If I use a list it's going to be a big risk. That's why in this scenario you don't use a list and you protect your data using a tpple.
All right friends, now we're going to talk about the third type of data structures. We have the sets. So now what is the issue? Let's say that you have a list of customers ID and as you are checking the data you have found out that some ids are appearing twice. Those duplicates in my list is indicator that the data is not clean. So now what we can do we can start like removing the duplicates and cleaning the data by building a loop and then start removing duplicates and after a while we have to recheck again and maybe start removing again. So this is not really nice. We don't do that in Python. If you have this issue where you want to keep the data unique then we don't use list.
Instead we use the data structure called sets. So what is a set? It is unordered collection of unique items. So here again we have two things. It is unordered and at the same time it is unique. And in order to create a set the syntax for that is going to be very simple. Again we're going to use those special characters. So we're going to use the curly brackets and as usual the data inside it going to be separated with a comma. So now let's go and understand the behavior of the sets in Python because as we understood every data structure has its own personality and rules. So let's go. Okay. So let's go and create a set this time the curly brackets with same data. So 10 30 and 20. So now the first thing that we're going to check whether the set is ordered. So print my set and let's go and execute. Now look at this. We have 10 then 20 and 30. But I defined it like this. 10 30 and then 20. So Python did change the order that I have defined.
That means my friends the sets is unordered. It is not like the list or the tubble. But now you might say it looks nice. Python is like sorting the data ascending. Well, this is totally coincidence because this is totally random. Let me show you exactly how this works. As we create a list in Python, it will be stored in like an array. So each item is sitting next to each others in the memory and each one of them has the index number. So Python can go and access any value using the index number and with that as well, it maintains the same order. But as you create a set, Python can store it completely differently. So we have like a hash function that is generating critical values and each item going to be placed based on this hashed value and not by the position number not by the index. So now look at this after button create the set it has no chance at all to understand the order of the items. So it doesn't know which was the first the second the last and once you print it it's going to be depending on those hash values to print it in the output. So that's why the sets are unordered. And now you might say but this looks catic why do we have those hash values? Well the only reason for that is speed. So that means with the set Python can check if a value exist very fast using those hash values. So there is like a trade-off between losing the order and having fast performance for your lookups. Now let's check the second thing. Remember does it allow any duplicate? So I'm going to have the 10 twice. Let's go and execute it. Look at this. I have the 10 only once. So this is another restriction with the set. It doesn't allow duplicates but you will not get any error in the output if you have duplicates. Python just going to go and remove it which is sometimes a very nice thing if you want to keep unique list of data like as a start as I said the customer ids I would like to have it unique I don't want to deal with duplicates. Now let's keep moving. How about accessing specific value? Is it indexed? So can I access the second value the 30? Remember as we done with the list and the double we go and tell Python the index number going to be the one. So let's try and print it. Let's go and execute. Now look at this. It is not indexed. And this is of course will not work because look at this again with the list you have for each value the index number. But with the sets we don't have those numbers anymore. So we don't have anymore the possibility to access any individual item inside my set. And of course we cannot go and use those hash numbers because it is something internally for Python. It is not for us to access specific value. So that's why my friends we say a set is not indexed.
Okay. So no order, no duplicates, no index. How about the last one? Can I change anything after creating my set?
So let's try something. I cannot go and update because I don't have an index.
Let's go and remove by value. My sets dot remove. So I have a color which is really nice. Now I'm going to go and remove for example the 20 and after that I'm going to go and print my set and then execute. Finally something is allowed that means it is immutable. So this is how the sets behaves. It is unordered. It is unique. Doesn't allow any duplicates. It is not indexed. We don't have the position number anymore.
We have those hash values but it is mutable. So we can go and change it after we create our set. Okay friends.
So now since sets have their own personality they have as well their own methods. So now we're going to go and explore a new methods and operators that we didn't use with the lists. So let's go. [music] Okay. So now let's go and create a very simple set. So we're going to have 10, 20, 30, and 40. Now the first question is how to add an item to our set. Now we cannot go and use a pens or inserts because it is based on the position number of the item and we don't have those stuffs. That's why we have a new methods called adds. Very simple. And you're going to go and add for it the value. So it's going to go and insert the item, but only if it is not already there because we cannot have duplicates.
So let's try this out. And let's print it. Now look at this. We have 50 somewhere here in the middle. So it will not append it, right? Totally random.
Now, of course, if you go and add 10 and execute, well, nothing going to happen because we have it already here. You will not get an error. But Python going to go and ignore it. So you can go and add only new values. Okay. So now this going to be annoying of course if you want to add multiple values to my set.
So for each new value I'm going to go and add a new line. So for that we have a very nice method called updates. And now you can pass for it any iterable. So anything that has a sequence. It could be a set, a list, and even a string.
Like for example, let's go and add hi.
So if I go and update it, you can see both of the characters, the H and I is inside my set and you can go and use a list like one two. So if you go and execute it, you can do that. And as well, it's going to accept a set. So as you can see, it's going to accept any iterable that you add to the update. So this is really a nice method in order to add several values at once. And by the way, there is something nice with the sets. We can use like operators as a shortcut instead of using the method name. So if you want to do an update, we can do it like this. So we say the variable name, then a pipe and then equal. And then you can add the items that you want to add. So the one and two. Now if I go and execute it, I'm going to get the same effect. It is like the update allows me to add multiple values in one go in my set. So that I have like nice shortcut instead of writing uh the update. Now how about to remove values from my sets? So we learned with the lists that we have a pop and remove either you remove by value or index. Well you can go and remove actually by value like for example the 10 and then we can print this out. As you can see we just remove the 10. And one more thing that is annoying with the remove. If you use like a value that is not part of your sets and executes you will get an error.
But sometimes this is really annoying because it's going to break the whole code. So it is not really safe. You have to be careful using the remove. That's why we have in Python something like savior in order to remove values. And that is the discard. So discard. Let me just comment this out. So it can work like the remove. So I can go around remove for example the 10. As you can see we don't have a 10 inside my list.
But if I go and remove an unexisting value like the 100, nothing going to happen. and I will not get an error.
This card is really nice. If the item is there, it going to go and remove it. But if it is not, then simply nothing going to happen. And by the way, I just tried the pop. It actually works with the set, but it going to go and remove something totally random. So for example, here it removes the one. So I don't recommend you to use the pop at all. So as you can see with the sets we have some new methods the add update discard and we cannot use any method that is using the position or the index number with the sets.
Okay. So now we come to the interesting part where we're going to talk about the mathematical operators in sets. So now the sets in Python it's like the mathematical sets that you have learned in the school or the sets operators that we use in the SQL. So now sometimes you have like two sets or two groups of values and you want to ask questions like does the values appear in both of the sets? Which values are overlapping?
Which values are different? And the data structure sets is perfect for those questions. They are using this hash mechanism in order to make it very fast to give you the answers especially if you have large data sets. And exactly for this reason we have in Python some built-in methods in order to do comparisons. So now let's deep dive into those mathematical operations. Okay. So now let's go and create another set. So we have some overlapping values like the 30 40 50 and 60. So now the first thing that we could do with those two sets is how about to combine both groups together. And for that we have a method called union. So you start with one of the sets. Doesn't matter which one. So you say union and then you pass to it the other sets. And of course in order to see the whole thing you're going to go and print it. So let's go and do that. Now as you can see I'm getting all the items from both sets. And this is exactly what the union does. It gives you every unique value from both of the sets. So it is just mixing those two groups together. And if there's like duplicates it going to go and remove it.
So as you can see we have 30 twice but in the output you have it only once.
Same thing goes for the 40 you have it as well only once. And it is as well important to understand we are not changing the original set. So if you go and print the A, you still have the same data. The same goes for B. So we are not changing A and B. We are just merging them together into new set. And as well we have some nice shortcuts. So instead of saying union, you can say A pipe and then B. So if you use this operator, you're going to get the same exact results. All right, moving on to the next question. I would like now to find out the values that both of the sets are sharing. So only if the value exist in both of the sets. I wanted to see it in the output. And for that we have the methods intersection. So again it doesn't matter where you start. You can start from B. So A dot intersection and then the second set. So if you want to see the results we're going to go and print it. So let's try this out. Now we are getting only 40 and 30. So it is very simple. it is returning the values that appears in both of the sets. So you're going to get only the values that are overlapping or shared between the two sets and same thing we have as well some shortcuts. We can say A and B. So if you go and execute it you're going to get the same results. All right moving on to the next question. So I would like to have the items that belongs only to the first group. So that means I would like to see the items that only appears in the first list but doesn't overlap with the second list. And here we have another method called difference. But you have here to be very careful where you start. So I'm saying here show me the items in the first set that has a difference from the second set. So the sides here are important. Let's go and print it out and then execute. It going to gives us the values that are in A but not in B. And of course my friends we have some shortcuts for that as well using an operator. So it can be very simple if you use the minus. So a minus b you will get the same results. Now if you go and switch it where you say b minus a you will get different results.
So look at this we are getting 50 and 60 because now we are saying show me the items that are in the second set but does not belong to the first set. The a and in this example we have the 50 and 60. 40 will not work because we have it already in the first set. Same goes for the 30. So be careful with the directions as you are using this. So now let's go to the next question. How about that? I would like to find what are values that are not overlapping at all.
So the exact opposite of the intersection. Now of course you can go and do the difference twice for each side. But there is like better way to do that. For this we're going to use something called symmetric difference.
And again does it matter where you start? So it can return everything that is different between the two sets and everything that is shared or overlapped will be excluded. So let's try this out and execute. Now look at this. From the first sets we are getting the 10 and 20 because you cannot find it in B and in B we are getting the 50 and 60 because we cannot find it in A. And of course we have an operator for this. So we say A carrot and B. If you execute it you're going to get the same results. So now let's recap. If you want to get everything from both of the sets, you're going to use the union. But now if you want to [clears throat] return only the shared item, the overlapping in the middle, then you're going to use the intersection. But now if you want to get exactly the opposite, all the items that are not overlapping, they are unique in each set, then you're going to use the symmetric difference. But now if you want to get only one-sided thing like only the items that are in one set but not in the other then you're going to use the difference or the opposite if you want to get the items that are in the second but not in the first and as well you're going to use the difference.
So this is fun right? This is exactly the power of the sets in Python.
So now so far if [music] we have like two sets we can merge things we can find the differences and the overlapping but sometimes we want only to understand the relationship between two sets we are just checking we don't want to merge anything and for that we have as well methods so for example if I want to ask this question is everything in this set the a is also inside the other one and with that we have a nice methods it going to start with is as we are asking question so it's going to be the is subset. So is A a subset of B. Now let's go and print it and see what we're going to get. It's going to say false because is subset going to return true if all items from A exist in B. So it tells you if one group is fully contained in the other. And in this example, it is not.
So now in order to get true, let's try this out. We're going to have 30 and 40.
So the whole A exist in B. Let's go and execute it. You can see it is true. And with that A is a subset of A. Now sometimes you want to do the complete opposite. So does the group B contains every single item, every single value from the other one. So it's just the way around. And for that we have as well methods that start with is super set a.
So the name says everything, right?
Let's go and try this out. As you can see, we are getting true. And that's because B contains all the items of the set A. And now you might ask, okay, when do we use those stuff in real world?
Well, I've used this in this scenario.
You have a table that is a master table that contains all the customers ID. No customer should be in the business without having an ID in this table. And sometimes you have another table that holds as well the customer's information, but it is not the master table. it is like a subset of customers for some reason maybe the VIP users. So now the rule is even if you have like a subset of customers outside they must be part of the master table otherwise there's something wrong in the system.
You cannot have like customers outside this master table. So that's why in order to do quality checks you can use the is subset or is superers set to make sure it is clean your master table has everything. So this is how I use those two methods in my projects. Now moving on to another question that you might ask. Do those two sets shares any items?
For that we have a method called is disjoint. This is the last one in this group and a. So now let's try this out and print. Now as you can see we are getting false. So what is going on? It going to return true when there is no overlappinging between the two sets. The two sets are completely separated disjoint. So zero values in common. And of course in this example we have overlapping. We have the 30 and 40. Now you can make it working and get true if you have like values like 10 and 20. Now both of the sets they are not sharing anything at all. If you go and execute it, you're going to get true. But of course with the others you're going to get false. It's not anymore subsets. Now you might ask okay when do we need this?
I used this in this scenario where I had like one big data set and I wanted to split it into two separate sets to two tables and I wanted to make sure that everything is splitted correctly. So there is no one same record in both of the sets. In order to do that, we can use the is disjoint to make sure that there is no overlapping between them. So those are three nice methods in order to check the relationship between two sets.
I'm not creating anything new. I'm just asking question and getting yes or no, true or false. So my friends, as you can see sets in Python, they have their own personality. They have their own methods and it is very different from the list and the toubles. They focus on the contents whether the data is clean, unique and we use it in order to compare multiple data sets together. So those are the unique methods for the sets.
Hey friends. So now finally we're going to talk about the most powerful data [music] structure type in Python. My favorite one and the one that I keep using in my real data projects, the dictionary. [music] So now let's understand it and why do we need it? The thing is all the other types of data structure the list the double the sets we use them in order to store one piece of [music] information at a time like for example we have a list of names list of countries list of age but the thing is all those three informations are stored in different variables they are three informations but all of them are describing an entity a person a customer so that means we don't want them anymore to be disconnected and spreaded into multiple variables. We would like to put everything in one place and for that we're going to use the data type the data structure dictionary and since we have different type of informations we can have a special structure for this type. So that means a dictionary lets you store different type of informations in key value pairs where the key describes what the data means and the value holds your actual data. So with that we have everything that described the customer in one place the name, country and age instead of having three separate lists. And this is exactly the real power of the dictionary. And the syntax for that going to be very simple.
You're going to use the curly brackets and then we're going to have pairs of key values and they're going to be separated with double points or colon and you're going to separate the pairs using a comma. So nothing crazy, right?
It is easy. Okay. So now the first thing that we have to do is to understand the behavior of the dictionary. As we learned each type has its own personality and of course with the dictionary we're going to have completely new things. So now let's go and create very simple dictionary. So with the curly brackets and now we're going to go and create very simple pairs of key values. The first one going to be a and its value going to be 10. The next pairs b its value 20. And the last one c its value 30. So we have three pairs of key values. Now the first thing that we want to check is whether the dictionaries are actually ordered. So let's go and test that. As usual, we're just going to go and print it. So print my dictionary. Let's go and test. Look at this. I'm getting from Python exactly the same order as I defined it in my structure. So the first key A, B, and C.
So that means my friends dictionaries are ordered which is really nice thing.
So now let's go and check the second one. Can we have duplicates inside our dictionaries? Now, of course, we have to define what I mean with duplicate. Is it duplicate in the keys or the values? So, let's first test the keys. Can I have another like a where the value for example 40? So, with that, as you can see, we have duplicates. We have two keys with the A. Now, let's go and execute it. Look at this. Python will not allow duplicates for the keys. We have only one A. And now, it is interesting which value is actually Python going to show us. It's going to be the last one in our dictionary. So it's going to show the 40 for the A. So that means my friends, we cannot have duplicates in the keys. Now how about the values? Like for example, let's say I'm going to have here another 20. So we have 24 B and 24 C. Does Python going to allow that? Let's go and execute it.
Look at this. Python doesn't care about the values. We could have like duplicates because it is not that critical like the keys. So that means the keys are unique and the values allow duplicates. Okay. So that's all about the duplicates. Now let's talk about the third characteristic. Our dictionaries index. So can I call any items using an index? For example, let's say print and I would like to access the second item.
This one over here. Can I go and say my dict? And then the index number can be one for the second item. Let's go and try this out. As you can see we are getting an error. We are getting the key error. So that means a dictionary is not indexed. Now of course the question is is it like the sets where we cannot access any individual value? Well no. In dictionary we still can access any individual value but not using the index number or the position number. Instead we use keys in order to access values.
So now again in this example if I want to access the second value the 20 I don't use the index number like here instead I use the key and what is the key for the second value it's going to be the B so since it is string we're going to call it by the value string B so now let's try this out look at this we are getting now 20 so we say dictionary is not index it is keyed I can access any value using its key so that's all about the third one the index Now, how about the last one? Can I change something after creating the dictionary? So, is it mutable? Let's test this. For example, I would like to go and change the third value, the C.
Instead of 20, I would like to have it for example 80. Now, in order to update the value, we have to access it using the key. So, it's going to be my L. And we're going to specify the key value, the C. And then, it's very simple. We're going to go and assign for it the value 80. And after that we're going to go and just print our dictionary. So let's try this out. Now look at this. Our key the C has now the value 80. So we're going to say you are mutable. So this is how dictionary behaves. It is ordered. So that mean Python going to maintain the same order as you create it. Now about the duplicates, the keys are unique. You cannot have the same key more than once.
But it doesn't care about the values.
You could have duplicates. Third one, it is not indexed. we cannot access the values using position number or index number. Instead, we use the keys in order to access the values and it is mutable. So you can anytime go and change the values of your dictionary. So because of the key values we have here different things compared to the others.
[music] So now my friends since the dictionaries are very special very different from the others because of this key value structure we have in Python special methods functions operators specially for the dictionaries. So now let's go through them one by one. So now let's go and create a very simple dictionary of a user where we have for example an ID like one and let's go for age like 30 and then a city like Berlin.
So now the first thing that we have to learn is like how to access a dictionary and as we learned we access them using the keys. So you're going to call it like this very simple and let's go for example for a city of course it's going to be like a string value. So if you go and execute it you will get the value of this key. So it is very simple and easy but there is an issue. What happens if the key is missing? A key is not part of my dictionary. Like for example let's go for the name. So we don't have a name here. If you go and execute it, you will get an error. And this is not really nice because it could go and break the whole execution of your code. So it is not really safe. That's why if you are not really sure about your keys, we have another nice methods in Python called get. So it going to looks like this.
Print and then user and then dots. It is a methods. So we call get. And now we pass for it the key name, the name. So let me just comment this out and try this out. You can see we are getting the output. none and it is not breaking my code. So it is saying okay we have a missing value there is nothing and of course if you use the age it going to work so it's going to return the value.
So the get methods return value or none.
And if you say you know what I really don't like to have like a none in the output. I would like to have something like meaningful or easy to understand.
That's why you can pass to it as well a default value like an non. So if you go and execute it you're going to get something more friendlier. it says unknown. So this one is the safest way on how to access a dictionary's data.
Now moving on, we can do few checks in our dictionary. Like for example, you can check whether the key exist in your dictionary. Like for example, we can say print and I'm going to check whether the age is a part or a member of my dictionary. So age in user. Let's go and execute it. As you can see, we are getting true. Now let's go and check another one like the name. So if you go and execute it you will get false. So it is nice double check on your keys of the dictionary and of course you can go and use the not in and with that we are getting now true. Now we can move to another set of methods that are special for dictionaries. They are the view objects. So it gives you like insights about the structure of your dictionary.
Let's have an example. They are very simple. So we're going to say print user dot. And now the first one is to get all the keys of your dictionary using the methods keys. So if you go and execute it as you can see it is returning all the keys of your dictionary the ID age city without any values. So with that you understand okay this is the structure of my dictionary. Nice with that we are getting the keys but now how about to get only the values. So the opposite we can go and use the methods user do values. So it is very simple if you go and execute it this time instead of getting only the keys we are getting only the values without keys. And now if you say you know what I would like to get both of them the keys and the values in one go and for that we have a very important methods called items. So if you call user do items and execute you will get both of them. So you will get the pair of key and values. So ID 1 age 3 and so on. And now you might say okay but why should I use items if I just can go and print for example the user as it is and with that I'm going to get as well the pairs. So the thing is if you just want to display the whole dictionary then yes you can go with the print user but with the items as you can see we are getting like a list of doubles right it is not anymore dictionary which can makes it easier to loop to unpack to do transformations which is something that we're going to use a lot in order to deal with the dictionaries. So let's go and check the looping now as we learned before we can loop like this for you and user double points and we can go and print the item.
So if you go and execute it, as you can see, we are getting now only the keys of our dictionary. We are not getting the values in order for you to get the values. You're going to call again the dictionary and then you have to pass for it each item as a key in order to access the value. So if you go and execute it, yes, you're going to get the keys and the values. But this is not really the cleanest way on how like to iterate through a dictionary. So I don't really like this. Instead, we're going to use more modern way. So now since we need two things from our dictionary we're going to go and define two variables the key and the value and then we're going to say in and this time instead of just using the variable of the dictionary we're going to use the methods items. So we're going to say items and double points. So now what we're going to print it's just the key and value. So let's go and execute it. As you can see we are getting the exact same results. And if you just go and compare both of those stuff, you can see the second method. It is like way cleaner in order to read and as well to work on. So that's why if you want to work with the dictionaries, we don't just use directly the variable name. We use the amazing methods items.
All right. So now let's talk about how to change a dictionary. The first thing is how to add a new key value pairs.
Let's say that I would like to go and add a name to the user. So we call the variable name and then we define the new key. It's going to be the name. And then here very important we don't use the double point since it is now as a variable and I'm doing assignments. I'm going to go and add a value live for example John. So let's check our dictionary. Now as you can see at the end of our dictionary we have a new key value name John. So with that Python is adding something new to our dictionary at the end. But if you'd like to go and change the values of already existing key like for example the age we're still going to use the same method. So we're going to use the variable we're going to call it by the key and we're going to assign for it a new value like for example 35. So let me just print it again. Now as you can see Python is not adding something new. It is just now updating the already existing key. So as you can see assigning values in dictionaries you can do actually two things. You can add new pairs if the key is totally new to the dictionary or you can update an already existing key by assigning new values to it. So as you can see it is really easy. Now since we are talking about the updates there is another way on how to update multiple values because if I want to update a lot of values in the dictionary I'm going to end up like creating a new line for each key value. Instead we can use the methods update. Let me show you. So updates and now inside it we're going to put multiple key values in order to update the dictionary. So it's like you are defining a new dictionary. So we're going to start for example let's say I'm going to go and update the age and here be careful we are using the double points not the assignment. So here we're going to say 40. The second one let's say I'm going to change the city and update it to Paris. So let's try this out. Now look at this in one go. I'm updating multiple keys. So the age is 40 and the city is Paris. So this is really nice. Now let's talk about removing. As we use in the list, we can use the method pop. So let's try this out. Let's say that I would like to go and remove the age. So we can say user dop. And of course we cannot specify indexes. I'm going to specify the key. So remove the age. And if you go and print it, the age should be removed. So look at this. We don't have an age. And as you know, pop return actually the item that is removed. So we can create a variable called age and assign it to our methods.
And actually you can go and print it. So removed item then we're going to say the age. Let's go and execute it. So as you can see the age is removed and the item that or the value that is removed is 40.
Now let's say that you have done a mistake where you are targeting like a wrong key like for example a salary. We don't have a salary inside our dictionary. If you go and execute it you're going to get an error. And again this is not really safe because it is breaking the whole code the whole program. Now what you can do actually you can define a default value if the code breaks like for example we can say not found if you go and execute it. Now look at this the removed item is actually not found. So we are getting the default value if your key is missing. So this is more safe instead of like breaking the whole codes. So now how about to use the same thing where I don't define anything. I just say pop.
So in the list we're going to remove the last item. Now if I do this is the last item going to be removed. Let's try this out. Now as you can see we going to get an error. It says Python is expecting you to use an argument. So you cannot leave it empty like we have done for the list. You have always to specify something. But if you still want to remove the last item without specifying the key, there is another method called pop item. So let's try this out and we say user and then execute. Now our list don't have the last item. So we don't have name equal to join. So it is removing the last pairs of your dictionary. Okay. So now let's move to something very cool about the dictionaries. So let's say I'm going to talk about the creation of new dictionary. So for example I'm going to create a dictionary called user. But at the start I don't know the values. So the ID is equal to none. And as well I don't know the name so it's going to be none. Same thing goes for the age and then a city and the same thing none. So now as you can see I'm repeating the same thing. I'm just defining the dictionary and since I don't know the values I keep repeating none none none.
And imagine you have like 20 lists.
You're going to repeat the same thing like 20 times. So the issue here I have a list of keys and I want them all to start with the same value. And for that we have a solution from Python.
something very elegant called from keys.
So let me show you how it works. We can say user and then we're going to say dict dot and then the method from keys and you have to pass for it two things.
First the list of the keys. So you can create a list where you have all the keys like the ID, name, age and city. It is very simple list of all keys. And the second thing is actually we pass the value and you have to write it only once. So we say none. Let's go and print our variable user and see what going to happen. The first thing happened here is the method created brand new dictionary.
We have all the keys that we have defined in the list and all those keys has the value none. So that means Python took this value and assigned it to the ID to the name to the age to the city and everything in one line. How elegant is that? And of course for example if you go and say zero then you will get the output zeros for each key. So this is how I usually do it. If I create and brand a new dictionary where I still don't know the values and everything should be at the start is missing or none then I create my dictionary using this method and later through the program I start of course changing the values by using the key for example here the age going to be like 40. So we use this only initially in order to build the structure of our strct.
And as a data engineer, oh my god, I use alltime dictionaries in my scripts if I want to add manually data inside my scripts. The first and the most obvious use case is whenever I query a database or let's say an API and in return we retrieve something like a record and usually we use dictionaries in order to fill those informations. So very typical SQL stuff you got the columns that are the keys and the values inside those columns. And now another very common use case for the dictionaries we use it in order to do mapping like for example translating a technical weird values that comes from the sources to something more friendly. Now most of the systems stores data in very short way in order to make everything fast. But if you show those informations directly to a business or end user, they might not understand what are those values. That's why we use dictionaries in order to do the mapping between the technical hard values to the friendly easy values. Like here maybe the status of ordering something like in the database could be like 1 2 3. But I want to translate it for my business users. I'm going to call it open in progress and done. Another very famous example of mapping is actually mapping the abbreviations to the full text of the countries like for example the abbreviations of the countries the E fen that stands for Germany France and India. So again it is mapping between two things and here it is the abbreviation and the full name.
So those stuff we use it all time in Python projects. And now we come to something as well very common. We use dictionaries in order to store environment variables and configurations. If you go to any real projects, you're going to find this config python file where you're going to find a lot of dictionaries and it looks like this. Usually, we connect our system to multiple external systems and we need to store the connection informations like the server name, the port, the user, but we don't store passwords. So, usually we put all those connection informations in dictionaries like this. And as well if you are a data engineer usually you have a lot of parameters in order to control the run the patch of the ETL pipeline. So there are a lot of configurations to tell the ATL how to load the data from A to B.
And usually in real projects it going to looks like this. And we use dictionaries for it. And another use case that I use dictionaries in my real projects. I use it in order to store the metadata. It is one of the most important thing that you do in your data projects is to store the structure of your data like for example the SQL tables or the JSON files. Again the metadata is like data about data but here we don't see any like customers. We just see the structure. So in this table customers we have columns like the ID, the name, the age, country and then for each column there is like extra information. What is the data type of the ID? it is integer string whether it is nullable or not. So I'm just describing the structure of the customers and this what we call my friend metadata and almost all the time we use dictionaries to describe it in our scripts and usually we use this then later in order to process the data correctly in our data platforms. So this is one of the most important use case for dictionaries in data projects.
[music] And now next it's time to challenge you.
So now again we have our dictionary where we have ID, name, age and city.
And now I wanted to return a new dictionary that has only key value pairs where the value is a string and all those string values must be converted to uppercase. So in this example, it must return only the name and the city and the values must be uppercased. But you have to do the whole thing in an elegant way. So pause the video and try this out. Now if you look to this task, we have actually to do two things. We want to do filtering because we want to keep only string values and the other thing we want to do data transformations. We are converting them to an uppercase. And you remember with the list if you want to do those two things we use the comprehensions and comprehensions has three blocks. The expression, the for loop and the last one, the filter. So let's do it step by step. Let's start with the variable name. Let's say it's user string. something like this and it's going to be a dictionary but inside it we have three blocks. So now what do we need? We need an expression we need a loop and we need a filter. So now usually I start with the loop since it is the easiest one. So for and now we need two variables because we have always two things key and value and we will not loop directly from the variable. We're going to use the method items. So as we learn this is the best way on how to iterate through dictionary using the items. Now let's go talk about the filter. So I want to keep only the pairs where the value is a string. So we're going to make a condition if. So now in order to check the data type we have a really nice function called is instance and we're going to pass to it two things. The value and the data type that we are checking for. So we are checking for string str. If this is true the value is a data type string. It will be kept in our dictionary. Otherwise the numbers the one and the 30 they are not string. That's why they will not be kept inside our dictionary. So we have the loop, we have the filter and the last one we want to do the data transformation, right? So the expression going to be like this. What do we have?
We have always key values. So key double point value. So now if I leave it like this, actually nothing going to happen to the key or to the value. It can stay as it is. All what we are doing here is just looping and filtering. But actually I would like to change the value. So I would like to apply for it some method or function. And for that we're going to apply the method upper. So by looking to this the key should stay as it is but the value must be transformed to an uppercase. That's it. Actually we have transformation [snorts] loop and filter. Let's try this out and print it. So we want to print the new dictionary. Now look at this. We have done the task. We have only key pairs where the value is a string. So we don't have an ID. We don't have an age. And at the same time the values are now uppercased. So this is the dictionary comprehensions. It is very elegant way to do multiple stuff in one go. And if you do it step by step, it will not be hard. And of course you can put the whole thing in one line. Now if I look at this, I think the most confusing one going to be the first one. So now I'm going to show you a few stuff. If I just remove it. I just keep the key value and execute. You can see the key and the value. They have the exact same stuff from the original dictionary. But if I would like to manipulate something, the key or the value, I can add stuff here.
Like for example, I would like to have the keys as an uppercase. So this time I'm just transforming the keys. Now look at this. The keys are now uppercased.
And I can say, you know what, let's go and make the values as lowerased. So I'm transforming both the keys and the values. You can see Joan and Berlin are now lowercased. So it is really easy way to manipulate the keys and values in the dictionary. [music] It is really nice, right? Okay. So now let's have a full recap about the dictionaries. So dictionary is a data type structure that allows us to store multiple related informations in one place using key value pairs. The syntax is very simple.
We use the curly brackets. Then we define key value and in between we have the colon, the double points and we separate the pairs using comma. And dictionaries behave like this. They are ordered so they remember the order you inserted things. The keys must be unique. You cannot have the same key more than once. But you can have duplicates and repeat the values as you want. And dictionaries are not indexed.
So you cannot use the position number to access any value. Instead they are keyed. That means you use the key in order to access its value. And the last one they are mutable. So you can go and update, insert or delete anytime. Now since dictionaries are really special one we have for them as well special methods like the method keys it gives us all the keys the method values it shows us all the values and the important one the items it going to return pairs of key values this is really important in order to do iteration we have method update in order to change or insert multiple values in one go we have the pop items which removes the last added pair and now because of this special structure we use dictionaries in many scenarios in real projects. Like for example, we use it in order to do mapping between abbreviations and long names or between technical names to friendly values. And we use dictionaries in order to store configurations like environment variables, the API settings, the database connections. So anything you want to keep grouped together in one place and this is something we need a lot in projects. So that's it. [music] This is the full big picture of the dictionaries. I know I might be sounded like excited about this data type but I want you to understand as well this is really important topic because we use this data type almost everywhere in projects. [music] So practice and make sure you master this type.
All right. Now what we're going to do we're going to go and compare all those four types the list double sets and dictionary side by side to understand the differences between them. So, first lists are flexible ones. They keep their order. They let you have duplicates. You can grab things by the index number and you can change them whenever you want.
So, you can add, remove, update stuff.
Next, we have the tubble. It is very similar to the list. So, it is ordered.
It allow duplicates. You can use an index to access any value. But the only difference here, it is frozen. It is protected. Once you create them, they going to stay the way they are. you cannot change them. So it is perfect when you want something that should not be touched. The third type it is the sets and this one can be really restrictive. So they do not care about the order. They will not accept duplicates. You cannot use an index to access any value. I know there is a lot of negativity but they are great when you care about making sure that each item is unique and it is great tool in order to compare multiple sets of data.
This has a lot of use cases. And finally, we come to my favorite one, the dictionaries. So, they are the one with the key values. So, they're going to keep their order. And now, about the duplicates, the keys must be unique, but the values can be repeated. And you cannot access the values by using an index number. Instead, we use the key in order to access any value. And the last one, it is mutable. That means you can go and change anything you want in the dictionary. So as you can see each type has its own personality and characteristics and those are the major differences between them.
And now my friends I [music] remember the first time that I learned about the data structure the list double and those stuff it was to be honest confusing for me. Yes I learned what are the main differences between them but I still I didn't understand how to use them in real projects. So it was confusing at the start but after I have done some projects I got like a decision tree on how to choose the right one. So now I'm going to show you that. So now by default I end up using each time the list. So without any special reason to choose something else or a reason to avoid the list. Most of the time I work with the lists. So it's going to be fine. Don't worry about it. But now if we found actually a special reason like for example the data must be protected must not be changed. So no changes are allowed then I don't use the list I go with the tubble and another special reason could be if the data must be unique and you want some performance then I avoid using a list and start using a set and especially if I want to compare multiple data sets together and a third special reason not to use a list is if you want to do mapping. So once I have the feeling that I have multiple informations and I'm mapping stuff together, then I'm going to go and use the dictionary instead of a list. So it's like one thing is pointing to another thing where whenever your data has like a label has a value then I'm going to go I use dictionary. So it is very simple my friends. Default is to use a list until you have a special purpose and based on the special purpose then you can use either double set or dictionary based on the requirements that you have. If it's frozen then touble if it's unique then set. If you have mapping then dictionary. So this is my decision tree on when to use which type. And if I have a looked all my projects I can tell you there are like two types that I use all time. The most used one is lists inside my scripts and the second one is dictionary. that tpple and the set it was like only few times I used it compared to the list and the dictionary. So this is my experience about those four types. My friends I know that was a big chapter that was a big thing but with that you have learned how to work with the data structure.
This is very important step and now by looking back to our road map now we're going to go to the final layer. We have the functions. So [music] this time you're going to learn how to think like professional Python developer. We're going to learn how to design reusable logic, how to break complex problems into smaller pieces. So it's like you are learning how to build your own machine inside your Python code. So now let's deep dive into functions. And of course we're going to start with the fundamentals.
Hey friends. So now we're going to talk about the most important concepts that you will learn in programming. It is what separate casual coding from professional coding. I'm talking here about the functions. This is of course not a Python thing. You're going to find functions in every programming language.
And to be honest, learning functions can get you overwhelmed and confused at the start. That is totally normal. So don't rush it. But I need you here full focused because this is a skill that we need to get right. And my friends, don't worry. I'm going to take you step by step from the very basics about the functions and then I'm going to walk you through to the advanced topics where you're going to learn how we write functions in real projects. So now let's start with the basics and we're going to start with the first question. What is a function? A function is a small reusable block of code that does one specific job. It's like a machine that can take an input and it can return something back. So actually that's it. It's very simple but we have to understand now exactly what this [music] means and why do we have even functions.
So now the thing is as [music] you are coding you usually write a small logic or flow in order to solve a small problem. Now over the time you might face a very similar problem where the logic going to be almost the same as the previous one. So what you're going to do you're going to go and copy the code and paste it. At the start, this sounds really okay, but over the time you're going to find out that the same logic exist multiple times in your code, and this is the start of the chaos because imagine you found a bug or something wrong inside this logic or maybe over the time the requirement changed. So now things get really annoying because you have to remember all the places where you use this logic and then do the changes and believe me in a real project your code going to be really massive and of course I promise you at least you're going to miss one. So now some of the logic got updated the others using the old logic which going to make the result of your code inaccurate. So that means if you leave it like this it's going to be really hard to change and as well it's going to be really hard for you to read. It's going to be a really big script and you're going to get the feeling that it's going to be really risky to touch anything in your code. So the bigger your project gets the worse this becomes. And of course what is the solution for this? We're going to use functions. So instead of repeating the same logic over and over, you're going to take your logic and put it in one place inside a function. And whenever you need this logic, this behavior, you just call the function. So you don't rewrite it, you don't copy and paste, you are just reusing the same logic whenever you want. And by doing this actually all the problems going to disappear. So since everything in one place if you found for example a bug or something wrong in the logic or maybe the requirement changed all what you have to do is to just to go to the function and do the change only once and with that every part of your program that is using it automatically going to get the new behavior or the new updates.
That means my friends the change is going to be very fast cuz you are just changing one code. You are not changing like multiple things. And very important as well it is now more safe to change the codes. you are no longer touching many parts of your code. You are just changing one clear isolated piece. Now another big win is that your code going to be smaller because you don't have any more repetitions which going to make your code first of all readable and as well it's going to be easy for everyone to understand your code and by using functions as well it improves the collaborations in projects. For example, we can split the work. One person can work on the function and another one can work on the rest of the code. And another thing since we are talking about teams, it as well speed up the projects.
And this is what usually happen different developers going to work on similar problems. So now instead of each one of them writing the code from the scratch, we can start reusing stuff. So you can say for example, hey, I already done this issue. I have a function for it. So just go and reuse it. With that, we save a lot of time. We avoid reinventing the wheel and the whole project can be faster. And now we come to the last benefit of using function.
Functions going to introduce something called modularity. So this is a way of breaking big complex problem into smaller manageable pieces. So that means functions it is one tool in order to overcome complex problems where you divide and conquer. You divide the problems into smaller pieces and you put them in different functions. So that means as you learn functions you are actually learning how to think like a developer. So those are my top reasons why functions are one of the most important topics in Python.
All right my friends. So now we come to a topic where I totally understand if you tell me you know what bar we already learned about functions. So we have the string the number the data structure functions. Are we going to learn more functions now? Well the thing is all those functions are built in functions.
So something that already exist and we are just using it. Now we are talking about functions that you're going to create from the scratch. So that means my friends there are like different sources where functions come from. Let me show you what I mean. So the first source of functions and the simplest one is the built-in functions and that's comes directly with Python. They are always there. You don't need to install or import anything. You just go and use them. We already worked with many of them like the print, the length, type, changing the data types, int, string. we already use them a lot without thinking about it. The second source is the one that comes from libraries and here we have like two types of libraries. The first one is the Python standards library. This one is written by Python team. So the thing is those functions are very specific and advanced that they don't expect you to use it all time in your code. So that means first we have to import the library and only after that we can start using its function.
Now for example we have the module math.
If you want to do advanced calculations on numbers and another one we have the date, time and random. So this is one source. Another source called external libraries. These libraries are written by the community by other developers or maybe as well by companies and they were nice enough to share it with us. Now in order to use them we have to do an extra step. So first we have to install the external libraries because they don't come with Python. And then after that we have to import them and finally you can start using its function. Now there are a lot of famous libraries like the pandas to do data analyzes and to clean up the data. Another one is the mat lot li in order to do data visualizations.
We have the numpy to work with numbers and arrays. And my friends this one is the biggest strength of Python. They are outside thousands of people that are working on solutions and sharing it in libraries. And to be honest this is what we do in projects. All what you have to do is to just plug and play, install, import, and reuse works that already exist. So now for those two sources, the built-in and the libraries, you don't have to deal with the definition of the function. So you don't have to know exactly what is inside the function. You just use them. Other people get to deal with the definitions and work on it. But now still my friend, there are some scenarios in your projects things going to get very specific and you have to write your own definition, your own functions. we call them userdefined functions. So this time we will not install or import anything. We have to define the function and then of course use it. So we are doing things from the scratch. So those are the three different sources of functions. And of course the question is if I have a problem and I want to solve it which source should I use? There is a simple golden rule for that. If you follow it you are already thinking like a professional. Before you write any piece of code like a function from the scratch, always check if it already exists somewhere else. So just don't reinvent the wheel. So here how to do it. First check the built-in functions because they are already there and they are usually very safe and fast to use.
You are not finding anything then go and check the standard libraries because they are already installed and trusted.
So all that you have to do is install, import and use. Now if you don't find the solution in the libraries then it must be written from a scratch. But now even here before you start writing it on your own go and ask your team. So check your project and ask your colleagues because this is the nature of projects.
The same problems or let's say the same logic shows up again and again and maybe one of your colleagues already solved it and build a function for it. So all it have to do is to just reuse the already existing function. But now it could happen that that you didn't find anything. So your problem is very specific only in this case you're going to go and write it on your own from the scratch. So the idea is very simple always check whether it already exist and reuse it before you write anything or any function from the scratch. So this is how we decide on which source we should use. And now of course we're going to focus on the userdefined functions. So let's dive in. [music] Now the userdefined function has [music] two parts. the function definition and the function call. The function definition or sometime we call it declaration. It is where you describe the function. So what the function should do and the syntax for that starts with the keyword def. So that's Python understand. Haha. This is a definition of a function. And after that you specify the function name. So it is just any name that you give it. Then parenthesis and a colon. Now below that we're going to have the block of code.
Of course we're going to use the intendation in order for Python to understand. This is the body of the function. And here depend on the logic we can add some normal Python code like assigning values to variables or making a loop with conditional statement or maybe call another built-in function. So with that Python understand the definition of your function. It understand what it should do but nothing runs yet. That's why we have the second part. The function call is how you use the function and it is very simple. You just write the function name followed by parenthesis. So once Python sees it, it's going to go and execute the function like any other bu-in function that we used before. So very simple the definition you describe your function and the call it is where you execute your code. Now let's have a very simple example in order to understand how Python going to execute it behind the scenes. So we're going to add a print at the start and print at the end. And our function going to be very simple. It is a greet function where it has only one thing to do to print hello. So that's it. Now if you excuse this as usual Python going to start from the top. So the first line going to go and print the start and the output nothing fancy then it's going to go to the next line and now Python going to see a diff. So it has to define a function. Now how it's going to do it? Python will not go to the next line inside the function to the print hello and execute it. Instead Python going to go and create an object in the memory and then it's going to put inside it the function name the body of the function and some extra stuff. So the definition step means it is only saving the function inside the memory without executing whatever inside the function. So nothing happened so far on the screen. And now Python going to go to the next step which is not inside.
It's going to go to the call of the function. So Python going to see the function call greet. So it's going to go and check the memory and it's going to find it. So now what going to happen?
It's going to go inside the body of the function and start executing it line by line from the top to the bottom. Well here we have only one line. So it's going to be very simple. Python going to print in the output hello. After that, Python going to exit your function and goes back to your code where it was. So it's going to go to the next line print end. It is a normal line of code. So at the out we're going to see start, hello and end. So I just want you to understand this one thing. If you define a function that means Python is creating an object in the memory and that means it is not yet executing the function and the function called going to be the one that going to execute it. So this is how Python works internally with the userdefined functions. Now let's slow down. Let's have a break. I would like to let you know something that everything takes time. So if you are feeling everything is overwhelming and there are a lot of things and ideas and you might start forgetting things. This is totally normal because learning any new skill just need time. It's like everything else in life, right? Your body need time. If you want to make money as well, it need times. Same thing for [music] the skills like Python. It just need time. I hope this helps. Now let's go back.
Okay. So now let's practice a little bit. First I'm going to show you why we need functions. So let's have a very simple example where we are describing our morning routine. So the first thing that I do usually is to wake up. I am a morning guy. So I wake up at 5. After that the first thing I do is I need my coffee. So I'm going to start the coffee machine. So now I'm describing the process. After that, I'm going to make the coffee. And the last step is I'm going to drink it. So, I'm going to enjoy it. Now, after I have my first coffee, I will be working for a while.
So, I will do stuff. Previously, I worked on my full-time job. But now, I'm just making content. Now, after a while, usually I need my second cup of coffee.
So, I need to do the whole process again. So, I'm going to start the machine, make the coffee, enjoy it. So, what I'm going to do, I'm going to copy the whole thing and just put it over here and then keep working, having lunch, and etc. Now let's go and print this first in the output. Now I can see all my steps. Now as you can see we have a process. We have a logic that exist actually twice and this is not a nice thing because I might do changes to this process. Like for example maybe I start drinking some different type of coffee where I add milk to it. Now if I want to go and add this step I have to do it over here. Add milk. And then I have to make sure that the same step is as well added in the next workflow. So after making the coffee, I will go and add the milk. Now let's go and execute it. Now we have those steps. But as you can see, it was a little bit annoying where I have to make sure to make the same change twice. But this is not really nice. Instead, we're going to put the whole logic of making the cafe in one function in one place and call it whenever we want. So how we going to do it? Usually we're going to go to the top of our code at the start and we use the keyword def. So with that we are defining our function and now we have to come up with a function name like for example make cover and after that we have to put the parenthesis and the colon. So now we are ready after that hit enter. Python going to gives you the intendation the four spaces and now what you want is to put our logic the process inside it. It start from here until enjoying the cafe. So I'm going to cut it from here and then put it in the body of the function. But this will not work.
We have to go and give it a tab with that we have the intendation and Python can understand. Uhhuh. Those four lines belongs to the function make cafe. Okay.
So now let's go and just execute it. Now look to the daily routine. It is not correct anymore. So wake up working for a while. Where is my cafe in between?
And that's because Python will not execute those steps. We are just defining the function. So Python is just creating the objects without executing it. Now in order to execute it, it going to be very easy. we will just use our nice function name. So as you can see even the editor now is suggesting the function name. So make a cafe and then the double parenthesis and that's it. So now let's go and execute it. Look at this. We have again our daily routine.
And now I just notice that I have a typo with the machine and this is perfect because I can show you now the benefit of having function. Now everything in one place I just have to do the change and the fix only once. So I'm going to go over here and say machine and that's it. I don't have to go to the second place because I will remove those steps and just use the function call again. So make cafe and double parenthesis. So as you can see it was very easy to fix in one place. Now if I go and execute it with that we have everything correct. So as you can see the output going to stay the same. There is no extra stuff that's going to happen. It is just we developers how we work with the logic with our code. So we are organizing and making things more usable and we are reducing the risk of making mistakes.
And now if I decide to make a third cup of coffee, it's going to be just calling the function. I don't have to copy and paste anything. So just the function name and execute. And with that I'm going to get my third cup of coffee. So this is the magic of using functions in Python. So my code is simple. It is easy to read, easy to write, easy to understand, and everything is now more professional. All right. So now I'm going to show you quickly the different source of functions that we have in Python. The first one as we said it is the built-in function. So for example if I'm calculating the length of a word like Python. So actually that's it. This is a builtin function and we are just calling. So now let's go and try a function from library. For example let's have a number like 4.2. This is a decimal number but I would like to have a whole number. And in order to do that we have different ways. One of them is using ceiling. So now we don't have out of the box like a built-in function in order to do the ceiling. But we have a nice function inside the Python standard library from the module math. Now of course I cannot go and just use it. So it is like this seal and then we pass for it in the lumper. As you can see Python going to have it like as a white and if you execute it you're going to get an error. So Python does not understand what is seal and that's because it is not built in. We have to import it first. So what we're going to do we usually do it at the start. So we're going to go and import the module math and in order to tell Python that this functions comes from this module we have to put before it the math dot and now it is green and now Python can understand aha this comes from the standard library. If you go and execute it you will not get an error and we are getting now a whole number using a ceiling that means we are first importing and then we call. So now as you can see for those functions that we are using we don't know the definition.
So we don't know the block of code that is used in order to do the job. So we don't have to know the code behind those functions. But for the third type the userdefined function of course we have to define it and we have to tell Python exactly what are the steps. So for example we are just greeting greet and then colon and then the definition is simple. So hello and then a new line but make sure you are outside. We just go and call the function. So let's go and execute it.
With that we have hello. So what are the steps you define first and then call. So now my friends really depends on the editor and the theme that you are using but for me once I see a green that means it is a function. And those are quickly the three Python sources for functions.
So far we have covered all the basics about the Python function. Now we have to take things to the next level where we have to understand something really important. How our data actually move through a function. So we're going to learn things like the parameters, arguments, how to return an output of a function and we have to understand as well the different types, shapes and purposes [music] of functions. So let's dive in.
Now before we start talking about the syntax, [music] we have to understand one core idea. Not all functions work the same way. So some functions like we learned before, they don't take any data. So the data doesn't go inside or there is no data goes outside. It is just a block of code that's going to run and do something and then stop. That's it. But there is another shape of function that actually takes data in. So it has an input. It going to perform an action and then stops. That means it has no output. There will be no data going outside. And some other shape where you have a function that it accept an input.
So it takes data in then it transform it and then it return the result as an output. The data going to get out of the function. So data goes in and data goes out but with different shape after it get transformed. Of course some functions takes multiple inputs. So multiple data goes inside then something going to happen then they going to do calculations and transformations and then you could have as well multiple outputs multiple data are going outside.
So as you can see you can design the function the way that you need. You have different shapes. You could let data go in and as well go out. Now in Python we have different terminology about the input and the output. We call the data that goes in through an input. We call it parameters and arguments. And the data that comes out we call it return value. So from now on I will not say like an output and an input. We're going to use those terminology. So this is the big picture. Different shapes means different data flow. And now we're going to deep dive into the input the parameters [music] and the arguments. So let's go.
So now what are parameters and arguments? The parameters are names that you write in the function definition in order to describe what kind of data the function expects. So you are describing the input. The arguments are the real values that you pass when you call the function. So they are your actual data that is used when the function is executed. Now of course this might sounds a little bit confusing but in order to understand it I'm going to show you an example and how things works behind the scenes. Okay. So now first let's talk about the syntax as we learned before. If you want to build a function you need two blocks. You need the function definition and as well the function call. So now what we're going to do we're going to extend it in order to accept data in. So now between the parenthesis we're going to go and put the parameter name. class means in the function definition we define the parameters. So this is where it goes.
Now about the function call we put there something else we call it argument.
We're going to put an argument value in the function call. So now let's say that I would like to build a function that's going to go and multiply any value by two. So how we going to do it? We're going to call the function times two.
This is the function name. And now we have to specify a name for the parameter. So it could be any name like for example I'm going to go with the x and now we're going to write the functions. We're going to go and print the result of multiplying the parameter with two. So x multiply by two. And actually that's it for the definition.
Now in the function call we're going to use again the function name. And now we have to specify an argument value. So as you hear me I'm saying a value. So not a name. So it could be any value. Like for example let's say three. As you can see it is very simple. The syntax stays the same. But we just added a parameter in the definition and our value the argument in the call. So now once you execute it it's going to go line by line. So it's going to start with the diff but going to understand okay this is the definition of the function. So it will not go inside it and do anything.
Instead it's going to go and create an object in the memory where it's going to put the function name the definition of the function and a list of parameters.
So now since here we have only one parameter it's going to be the x. So it has no value and the parameter here is like a placeholder. Nothing inside it yet. So that's it. Once it's created, Python going to go to the next line which is the function call. Now Python going to see okay, I know this name. I have it in the memory and now it's time to execute the function. But first there is one step to be done which is Python going to go and take the argument value and it's going to go and assign it for the parameter. So if you are looking at this this looks exactly like variables where you have a variable name and a variable value. So the name is here the parameter the argument here is the value. So with that we just filled the placeholder with a value. Now after the assignment Python going to go and start executing the function. It going to see the function print and inside it it must do the calculation first. So it going to pick the value that is assigned to the local variable x. Here we have it at three and then we're going to go and multiply it by two. So we're going to have the value six and it's going to go and print it in the output. So now once it's done, it is very important to understand it's going to go and destroy the whole local variable with its value.
So everything going to be lost and again we're going to have an empty parameter as a placeholder. So all I want you to understand from here is that when the function is defined the parameter is going to be like a placeholder. It's going to be empty, no value inside it.
And only when the function is called and executed, Python going to take the argument value, assign it to the parameter. So it is filling the placeholder and then use the whole thing as local variable and once the function finishes, everything disappears. So this is what happens behind the scenes once you start using parameters and arguments. So just quick comparison between the parameters and the arguments. The parameters are used in the function definition. The arguments are used in the function call. The parameters are the placeholder and the arguments are the real value that going to fill the placeholder. So a parameter is only a definition on what a function expects. So it defines the input and the arguments provides your actual data that's going to go inside the function.
So it is very simple. All right. So now let's practice in Python. Let's say that I have the following name and the quality of the name is not really good.
So we have at the start some space and then we have Maria like this. So the cases are messed up and at the ends we have like two spaces. So that means the quality of the name is not really good.
I would like now to go and process the name in order to make it more clean. Of course in order to do that we're going to use the string methods. So the strip going to go and clean up all those white spaces. Of course in order to see the results let's go and print it. Now if you check the output you can see we don't have any more spaces. And the second step we want to deal with the cases. So I'm going to go with the lower case. So lower and then execute. Now look at this. The name is processed and clean with us. We are doing some data transformations and clean up. But now let's say that in our projects always the text comes like this. We have spaces. We have weird cases. And this rule I'm going to apply it over and over. So now instead of repeating the same steps over and over to say okay strip and then lower. What I can do I can put this rule this logic inside the function. I have it once and I can use it whenever I want. So now let's go and do it. I usually put the definition at the start. So I'm going to say diff. We have to define the function and then the function name. You can call it clean name. But if you want to make it more generic, maybe you want to clean as well the addresses or the countries and stuff like that. You can say okay clean text or string or whatever. But I'm going to leave it for now for clean name. And now of course we have to put the parenthesis and then at the end the double point. So now I'm going to take the whole thing and put it inside the function. And of course we have to add the intendations.
So the spaces and with that we have the definition of the function. So now what is missing is of course we have to call the function. So we're going to say clean name and actually that's it. So now if you go and execute it you're going to get the exact same result. So you're going to get Maria where the name is cleaned. But of course we have an issue. So now if I go and just execute the function multiple times. I'm always transforming the same value. So we have Maria three times. But in my project I have different values and different names not only one. So it will not work that each time I'm going to go and change the name inside the function.
Instead I want it to be more flexible where each time I'm going to give it my own value. So that means my friend we have to change the shape of this function. So that's data goes in. So how we going to do it? It is very simple.
First we have to change the definition.
So now inside the parenthesis we're going to give the parameter name and it's going to be just the name. Now look at this. Something changed already. And now the next step is that we're going to go and remove this assignment because the values will not come from the function itself. It will come from outside. So that means I'm going to take the value Maria from here and put it as an argument for the function call. So something like this and then just remove this over here. And with that we have only one line inside the definition. So you can see inside the function there are no values at all. It's just describing what going to happen to the parameter. So now let me just try this out. I'm going to remove those stuff and then let's go and execute. Look at this.
The result is the same but this time the value is coming from the outside as an argument not a static value inside the function. Now let me show you how cool is this. Now we can call the function but using different arguments. So for example let's say we're going to have kumar everything with capital letter and at the end we have one space. Now let's clean up this name and execute. Look at this again we have the same standards.
So the white spaces are removed and everything in lowerased. So as you can see now my function is more professional. It is more generic that I can accept any value. So it is very cool and you can keep giving it values as much as you need. But now one more thing of course if you execute it without any argument you're going to get an error because the function has a parameter and it expects an argument. So you cannot leave it like this. But of course if you don't want to pass any value you can pass for it like an empty this can still work. but you have to pass something. So this is how we use a parameter and an argument in order to build an input to our function which can make things more flexible and professional.
All right friends, so now once you start working with parameters and functions, you're going to get the feeling that they look like variables. So why do we have like parameters and what are the differences between them? Well, I totally understand this topic is very confusing for beginners because in Python we usually have like three kinds of variables. We have parameters, [music] local variables and global variables.
Well, at the end it comes actually to only two things. How long they live and who can access them. So now, of course, as usual, I'm going to show you in details using animated sketches the differences between them. So, let's dive in. Okay. Now by looking back to our very simple function where we are multiplying things by two. So it accept an input then multiply it by two and print it out. So now the x here the input it is our parameter. So nothing new so far. Now let's extend it by [music] adding a new variable completely outside the function. So for example the if it is equal to two. Now since this is outside of our function we call it a global variable. a global variable. It is a variable that is created outside the function and available everywhere to be accessed throughout the program. So that means actually inside the function I can access this variable and I can say okay multiply the parameter by the variable f instead of two. So that I am flexible on how to multiply and I can call the function multiply by factor. So this is a second type of variable. Now coming to the third one, we're going to go inside the function and create inside it a new variable. So we're going to say the variable y going to hold the final result of our calculation and then at the end we're going to print the y. So now since this variable is inside the function we call it a local variable. So a local variable it is created inside the function and here the big difference it is only available for this function not globally for everything else. So those are the three different types of variables. But of course in order to understand it let's see what going to happen behind the scenes once you execute it. So now as usual Python going to go step by step from the top to the bottom. So now the first line it's going to go and create in the memory a variable called if I'm going to assign for it a value to. So at [music] this point the variable exist and available to be used. Now we're going to go to the second line where going to see it there.
So Python going to understand okay I'm going to go and create in the memory an object called function. It has a name.
So multiply by factor and inside it it's going to go and put the body the function code and then list of parameters since we have only one then we're going to have only the x but without any value yet so we have only the name of the parameter and now comes something very important the local variable y is not created yet and that's because as we learned in the definition of the function python will not go inside the body and start executing its code and of course without executing Python will not know about the local variable Well, all what Python knows is we have a global variable the f and its value 2. We have a parameter called x but we don't know yet its value and it knows nothing about the local variables.
So now once it's created python going to go to the next line and here we have the function call with an argument value three. So now what's going to happen first python going to take the argument value the three and assign it to the parameter. Now Python knows its value x is equal to three and then it can go to the next step and start executing the body. So the next step is going to see the y and the calculation. So first it's going to execute the right side. So it going to go and get the value of the x.
This example is going to be the three and then it needs the value of the x. So it's going to go and access the global variable and get the two. So with that we have the three and the two. It's going to go and multiply it. And now python have y is equal to 6. [music] And only at this moment, Python going to go and create a new variable called Y. But this variable is a local one. And it's going to assign for it the value six. So with that is done, it's going to go to the next step where it's going to go and print the Y. So it going to access [music] the local variable, take its value and print it in the output. So that Python is done executing the body of the function. And now comes something very critical to understand. Python going to go and remove the argument from the parameter. So our parameter going to go again as an empty placeholder but of course it will not destroy the parameter itself. It is just an assigning the value and the second step is going to go and destroy the local variable y not only its value the whole thing the variable name and as well its value. So by the end this variable is totally unavailable and we has no idea about it.
So at this point we are back to the first point. Python knows there is a global variable. It knows there is a parameter called X and it has no idea about the local variable Y because it destroyed it. So now by looking to this you can understand exactly the lifetime of each of those variables. The global variable the if has the longest life. It going to be created at the start of the program and it going to lives as long as the program runs. Once the program ends, Python going to go and destroy the global variable. The local variable has the shortest life. So it's going to be created when the function runs and it's going to be destroyed after the function ends. So it is totally temporary. Same things for the parameter its value going to live as long as the function is running. The second point that it is very critical to understand is how those stuff are accessed. Now about the global parameter you can access it anywhere. In our example, inside the function, we accessed the global variable and as well outside the function. Like for example, if you say print f, you will not get any errors and you can access its value. But now for the parameters and the local variables, it is totally different. You can use them only inside the function and not outside. Like for example here inside it was totally okay. So we used the parameter x in the calculation. We printed the y. Everything is happening inside the function. But now once you go outside whether before or after you say print Y, Python will not understand what you are talking about because in the memory there is no object called Y. Same thing goes for the parameter. You're going to get an error if you do that outside the function because Python sees only the object f and the function object. Of course, you cannot use its parameter without calling it first. So let's recap. The global variable they have long life. So they will be created at the start and destroyed by the end of the whole program. And they can be accessed everywhere whether inside the function, outside, wherever you want.
But the local variables and the parameters they have short life. They only live during the execution of the function. And they can only accessed inside the function, not outside. And those are exactly the differences between them. All right. Now, back to our example in order to practice. And if you look to this, we have one thing. We have only the parameter. So now let's go and add more stuff like for example a local variable. So we're going to go inside the function and create something called clean. And now we're going to go and put the whole thing the whole modifications of the parameter. And the output of the methods going to be stored inside our local variable. So now actually this is as well best practices on how to do this function because here things are more clear for you like for example usually the parameter holds the row data. So this is the input. Then we are going to modify the raw data and then store the process data inside a new variable so that you can understand immediately. Okay, the raw data inside the name and the process data inside a new local variable called cleans. This is very logical and easier for you to understand the purpose of the function and with that actually you have now the two versions of the data the row data and the process data. So this is clearly a big win for you if you introduce the local variable. And now what we're going to do, we want of course to print the local variable, the new processed value cleaned. So again the parameter is defined here. And here we have a local variable. So now before I execute I will just remove this over here and then let's go and run it. As you can see we are going to get the same result. So this going to work as long as you are inside the function. So actually you can go and as well print the parameter name.
We're going to say row double points and then the name and and here we're going to say clean double points and then we can execute.
So by introducing the local variable now I am able to access the old value and as well the clean value. So now of course this is going to work as long as you are inside the function. So for example if I take the parameter and just print it outside and then execute but I'm going to say what are you talking about? There is nothing called name. Same thing goes for the cleaned. So if I go over here and execute it, it's going to say I don't know what you are talking about.
So whether you put it before or after, it will not work. So parameter local variables only inside the function. And now let's go and introduce a global variable. Now I'm going to make it a little bit interesting where you're going to say you know what I'm going to control whether we lower the case or not using a global variable. So in our project we're going to say you know what the case rule going to be lower. So it is just like a global variable. So this is global variable and only if it is lower then the function must lower the case. So that I'm creating like a logic I need to build like condition and for that of course we can go and use the if statement. So now we're going to go and access the global variable case ro if this is equal to lower only then go and apply the method lower. So I will not go and apply it right away. Okay, what we're going to do, we're going to say clean going to be clean dot lower.
Otherwise, if the function is not lower, then don't go and apply it. So, it is very simple. Now, we just go and remove this print over here. And of course, we can go and try this out. Now, look at this. We have Maria and everything is lowered. So, now let's say that the rule is now do nothing. So, na, you don't have to lower any case. So, now if you go and execute it, you can see the cases is like the original. So that means the method lower is not applied because the rule says something else. So my friends as you can see we are able to access the global variable inside the function and as well we can access it outside. So we can say the rule is double points and then the case rule. So this is very classical one you can see it can work.
So inside and outside but the parameter and the local variables this is something very specific and we can access it only inside the function. So my friends as you can see again it's very simple those are the three different types of variables.
All right friends so now so far we just talked about only one input one parameter and this is something rarely happen in real code instead we usually like send a lot of data to functions. So that means we need multiple parameters and multiple arguments and here is going to be tricky about the order of the arguments. So now let's see how we're going to do that. Okay, so now back to our example where we have only one input, one parameter and now I'm going to go an extended where we're going to say you know what I'm going to send both the first name and the last name to be cleaned and maybe at the end it should be merged in a full name. So there will be multiple steps that means we need two informations goes inside the function.
We have to change the definition not only the first name but also the last name. I'm just going to make the underscore. So now my function accepts two parameters, two values and of course we have to do something with those two values. So the first one I'm going to call it just first and then first name and I'm going to do the same steps for the last name. So last name dot strip and we're going to lower the cases as well. And then the last step we're going to say full name equal first plus and maybe we're going to add some space to look nicer and then last. And at the end we're going to go and print the full name. Right? So actually that's it. This is the definition. We accept two values, first name and last name. We're going to go and clean them up, then combine them together and print it in the output. So that's all about the definition. Now if you leave everything like this and execute, you're going to get an error because Python going to say, you know what, you have here two parameters, you have to pass for me as well two values.
So is Maria now for the first name, the last name. It will not work. That's why as well you have to pass multiple arguments. So in the function call you have to say okay Maria and then comma it's like the parameters. So you just make a list of values and maybe Smith and then space. So now let's go and try this out. Look at this. Now we have the full name Maria Smith everything lowercased. There is no spaces or whatever and they are combined together.
So you can keep extending your function and sending more data. Like for example I'm going to say here the country and maybe the country I will not edit it at all. I will just print it at the output.
I'm going to say from and then country.
And now in order to pass for this the value of course I have to extend it and maybe say D. So now if I go and execute it, it going to say Maria Smith from DE Germany. So as you can see it is very simple to extend. All right. So now the next step going to start talking about the arguments because in Python we have like two different ways on how to send the values to the function. Those ways has some fancy names like we have the positional arguments and the keyword arguments. So the positional arguments are values passed to the function based on their order and the keyword arguments are values that are passed to the function by the name of the parameter.
They might sounds very similar but actually they have a different behavior.
So now let's check the positional arguments. Actually we already using it.
So if you think about the syntax this is a positional arguments. It's all about how Python is mapping those values to the parameters. So we are saying the first value is actually for the first parameter. The second argument for the second parameter the third for the third. This is what we mean with the positioner arguments. So that means the order of the values over here is really important. And this is of course very scary because if you go and like make something wrong, we all do mistakes. So if you put the country first, then first name, last name, and then you go and execute it, you will not get an error, but your data will makes no sense. Di Maria from Smith. Well, maybe the first part makes sense, but the whole thing is messed up because Python does not care whether the data is correct or not. It's just going to go blindly and trust your order. So, the first for the first, the second for second, and so on. That's why it is risky. But there's another way on how to do this by using the name of the parameters in the call. So, let me show you what I mean. Now, we're going to use the name of the parameter first name, then equal, and then the first name.
It's like you are creating variables and assigning to it values. So you're going to keep repeating the same things. Last name equal then Smith. Let me just make it smaller. And then country equal to DE. Now if you go and execute it, you will get the exact same results Smith from DE. So actually that's it. This is the syntax of the keyword arguments. Now of course the interesting part if you go and do mistakes and you put the country at the start and then you go and execute then everything going to be fine because now Python knows which value belongs to which parameter even if the order makes no sense it knows okay de for the parameter country and it's going to go and assign it really to the country. So if you messed up with the order then it's going to be fine and this is a very big advantage for the keyword arguments.
As you can see this is another way on how to pass the values to the function.
Now if you think about the readability and understanding of course the keyword argument is going to win because it is really easier for us to understand which value belong to which parameter. It is really clear the e is a country first name Maria last name is miss. But if you go over here you have always to keep your eye on that definition. So you're going to say okay of course here it is wrong. Let me just make it correct. So you're going to keep mapping in your eye. Okay. So the first value is the first name, the second is last name. It is not that easy to read. So the keywords for the readability, it wins as well for the safety. So of course if you have a lot of parameters, we are all humans. We're going to make mistakes.
It's going to be more safe if you use the keyword arguments. But here we come to the annoying point about the keyword arguments. It is way more effort to write it. So I have to keep writing the parameter names each time I'm writing the call. And of course the maintaining going to be really bad if you go and change things like in the definition.
you say you know what I will just remove the name I will make it only first so I'm changing the name of the parameter I changed my mind so now not only I have to change things inside the function I have to go for each call and start as we're renaming the parameter this is a lot of efforts and now of course you might ask me which one should I use positional or keywords well this really depends on the number of the parameters the rule says like between two and three parameters it is totally fine to use the positional arguments because you can manage it. It's only three and I hope you will not make mistakes. But if you have more than two or three then you should go with the keyword arguments. It is just more safe because things can go out of your hands and you will not be able always to maintain the correct order for the arguments. So if you have one two parameters go with the positional more than that go with the keyword arguments.
And now to the next point actually we can go and mix things. So you can go and use both of them in one call. So we call it the mixed arguments.
Now how we going to do it? We can say clean name. Now the first one going to be the positional argument. So it's going to be Maria. But let's make it like with spaces and stuff. And then it is followed by a keyword argument. So here going to be the last name going to be Smith and then a country.
So now if you go and execute it, you will not get any issues and it's going to work. So actually you can go and mix it. But there is like rules to do that.
So the rule says you cannot start with the keyword arguments. So for example, I cannot go and say you know what first name equal and then followed by a positional arguments. So without any name for the parameter. If I go and execute it, I'm going to get an issue.
It says positional argument follows keyword arguments. So this is the rule.
You have always to start with a positional argument and then the keyword argument. And of course you cannot go and alternate between them. So you start with positional key and then the last one you say okay go back to the positional you will get an error. So the rule is very strict and of course you can go and start with two positional arguments and only the last one it is keywords. So this is of course totally fine. Now you might tell me but why we should do this? Why should mix the arguments? Well the main reason why you would do this it is just like styling thing. Another thing is if you have a lot of values sometimes we have like primary values the most important one and then we have like secondary values that are helping the context. If I look to this example I'm going to say you know what the first name and the last name those are the primary values. So this is the identity of the person and the most important thing in let's say the function but the country it is something like secondary it is like a context extra details about the person.
So for example if you have like a phone number or an email all those informations are secondary to the main important information the first and last name. So it's just like styling thing a luxury thing to understand what is primary and what is secondary but from my point of view I would not go with this mixing because it might confuse later as you like change things either go with the positional and the keywords and I told you about the rule two parameters go with the positional more than that go with the keywords. If you want to do things more fancy then maybe try the mixed but I don't recommend you.
So now my friends as you see all the parameters that we used so far they all required the value and if you don't specify value for it the code going to break. But the thing is in real projects this will not work because sometimes we have parameters that are not that important or let's say we don't have a value for it. That's why in Python we have something called a default parameter. A default parameter is a parameter that has only a value. If you don't pass for it an argument, a value, Python going to go and use this default value and this going to make our parameter optional. So it is totally okay if I don't pass for it any argument any value. So let's see how this works.
So now by looking into our example what could happen that you have some registration form and you have some optional fields like for example the country information. It's totally up for the user whether they enter their country or not. And if they don't enter the country, the code the program should not break. So that means we have to go and add a default value in case we don't have in the call the country. And we can do that in the function definition. So what we're going to do, we're going to go to the parameter over here and add for it a value not only the parameter name but as well a default value like for example in a not available. So thus means my friend it is very simple. If you see a value inside of the definition then this value is actually the default.
So now if I go and execute it as you can see we have always at the NDE because in all of those calls I'm specifying the value of the country. So that means it is unnecessary for Python to go and use the defaults. But now if I go and let's say I'm going to call it here default and I'm going to say here clean name.
specify the first value maybe Kumar and Surish. Now the last thing the country I will leave it empty. So I hope this is the correct order but now I will not give the value for the country. So I'm passing here two values but we have three arguments but again this is totally fine because we have a backup.
We have a default value for the country.
Now if I go and execute it as you can see we're going to get Kumar Surish from NA. So this time we didn't get any syntax error because Python has a backup. it has the default value to be used. So actually it is very simple. Now of course things going to get confusing if you do this like for example you go and remove the default from the country and you give a default for the last name. Like for example I'm going to say if we don't have a last name then na.
Now if you leave it like this and then you go and execute it will not work because it says parameter without a default follows a parameter with a default. So actually you have to pay attention for the parameters. First you have to list all the parameters without a default value and only after that you list the parameters with the defaults.
So that means you cannot mix it like this have as well to go and now add a default for the country otherwise of course Python will not understand how to map all those things. Now if I go and execute it I'm going to get the same results and now even if I don't have the last name and then execute commar na from NA. So this is totally okay. So that's it actually. This is what we mean with the default parameter.
All right. So now we're going to talk about something that is a little bit more advanced. So far we defined exactly the number of inputs for our function.
But there are some scenarios where we don't know in advance how many values we're going to pass to the function. And exactly for that in Python we have two tools. [music] We have the star arcs and the double star quarks keyword arcs. So what this means? So the star or the double star arcs they're going to allow our function to accept any number of arguments. That means it's going to make it totally flexible. We can send for it one value, two value, three values. So now since we have those two types positional and keyword arguments that's why we have the one star arcs for the positional arguments and the two star quarks for the keyword arguments. So now let's have a very simple example to understand how this works. So now let's say that I would like to make a function that calculates the total of multiple values. So we're going to make a function called total. And let's say that I want to pass for two values. So I'm going to go and define two parameters for that. And then I'm going to say print a + b. And actually that's it. So let's go and call it total. And let's pass the two values one and two.
So now if you go and execute it, it is very simple. It is just adding the values. But now let's say that I would like to go now and add three values 1 2 and three. Now since I have three values I have to go and extend my function and add for it another parameter C and then as well go over here and say plus C and then go and execute. Of course we're going to get an error because the first call will not work and that's because we have here only two values. And now in order to make this works we have to go and add a default value for each parameter. So if there is no any value it's going to be zero. Same things for the first one and let's go and put everything with the defaults. Now if I go and execute it going to work. So for the first call going to be three and the second one going to be six. Now guess what? We're going to go and add another value. 1 2 3 4. Now things will be really annoying because I'm going to go and keep adding for each value a parameter and as well a default value.
As you can see this is not really smart.
We don't know exactly what's going to happen next. Are we going to get like five values, 10 values? Are we going to go and guess the number of parameters here? So now in order to solve this scenario we have actually two options.
The first one we can go actually and pass a list. So instead of having like separate arguments we're going to send only one arguments one value a list and then we're going to do the sum operator inside the function. But of course this going to feel heavier because you have to go and create a list and stuff like that. That's why we have a second option where we're going to use the star or the double star arcs. Now let's see how we can do that. The syntax for that is very simple. Now, instead of having all those parameters in the definition, we're going to go and remove them. And this time, we're going to say star and then arcs. And with that, I'm telling Python, you need to accept any number of arguments. Of course, we have to talk about the definition of the function. We don't need all those stuff. The parameters, we don't have them. Now, we have only one thing called arcs. So, what is actually arcs? We can go and check that. So if you go and say type and they say arcs let's go and execute it and check the type. As you can see it is the class tpple. So that means my friends python is collecting the different values inside the data type.
And now we can use any function or methods on this tpple like we learned before. So what we're going to do actually we want to summarize all those values. We're going to use the function sum and we're going to pass for it our tpple. It is very easy right? Let's go and try this out and execute. Look at this. We are getting exactly the same results. So it looks clean and as well we can go and pass for it any number of arguments. So I can go and say 1 2 3 4 5 6 and if I go and execute it can work without me going and extending the parameters. So my friends it is totally flexible and as well clean. So now we have to understand one thing when to use the onestar args. If the values that we are passing to our function has always the same type of information like here we have a list of numbers. So all of them are numbers or maybe we have like multiple characters multiple string values then we can go and use the one star arcs but if things gets more complicated where we have multiple type of informations that we want to send to the function and for this scenario the one star arcs will not be enough we have to go and use the two star arcs to have an example to understand what this means. So now let's say that we are building a function that creates the user profile. So we're going to go and define for example a function called create user. And now of course here comes the magic. If you want to have things flexible and you accept any type of information to your function then we're going to go and use the double stars and then say quarks. Then of course double point and here we will not go and create anything. We're going to go and just print it. So we're going to go and print the quarks. So this is the definition of the function. I'm just recording this again because some audio issues. Now, of course, the first step is that we're going to go and call the function. So, it's going to be create user. And now, of course, we have to pass the arguments, but it should be a keyword argument. So, that means key values. Like, for example, this I'm going to say first name equal like for example, Mo. And let's have a last name like Salah. He's just the best. right now his age I believe 33 right of course depend on the recording time and the country going to be Egypt so now those are the information about our user as you can see we have different things we have names we have age with number we have the country and what's going to happen now we're going to go and send everything the whole thing to our function but now before we execute the whole thing I would like to see the data type of the quarks is it a tpple I don't think So, so let's go and print type the quarks. Now let's go and execute the whole thing. Now look at this. The data type of the quarks is not a double. It is a dictionary and this of course makes sense because we are using the keywords arguments and this is key value pairs right and it is the only data type that can hold those informations. And now as we are printing as you can see we are getting the whole dictionary. So now this is one user of course we can go and call it again but we can use completely different setup like for example instead of saying the first name we can go and use a name and this time we're going to say Ronaldo and then we will not use any last name we're going to go and have only the country so Portugal so we don't have the age we don't have the last name and it is only two key arguments so if you go and execute it it's going to be totally fine Python going to go and collect those to put it in a dictionary and then print the whole thing in the outwards and of course you can use the quarks only if you are using keyword arguments. So for example if I go over here and just remove the whole thing. So I have here positional argument and execute I'm going to get an error. So this will not work. So actually that's it. It's very simple. This is how the double star quarks works. So now let's go and recap and compare them side by side. The onear arcs accepts multiple positional arguments and the two star arcs accepts multiple keyword arguments.
So you have to include as well the name of the values. We use the one star arcs if you are passing the same kind of informations. So we either sending numbers or we are sending string values.
But if you want to mix them and send multiple piece of informations then we use the double star quarks. And now about how Python stores those informations. The one star arcs stores the data inside a tubble and the two star arcs are stored as a dictionary because it has to store the key value pairs. So actually that's it. This is for those two tools. It is not that hard.
Now so far all what we have talked about is how the data going to go in inside our function using the parameters and the arguments. So this is actually one shape of the functions but of course we can go and extend it where we build an output from the function. So the data goes out goes outside the function and of course we can do that using the keyword return in order to return something back to our program. So now by looking to this this is the most famous shape of functions. We call it the transformation function because it works like this. We send the row values to the function. So data goes in then something going to happen to the data. So the data going to be transformed to different shape and then this data going to comes out and return back to the program in order to do something else with it. So it sounds very simple and most of our functions looks like this. Now if you look back to our example you can see actually we are just printing the output of the function and of course printing it is something for us for human. We are seeing the text in the screen. But the thing is programs don't work with the printed outputs. They need the function to return the values backs to it internally. And for that we have the return. So that means if you want the result of your function to be reused later in the program, you cannot use print instead you have to use return.
And of course you can use both of them.
If you want to print something for you and return something back to the program. So now of course let's have a look to the syntax. Now we have to change both of the component the definition and the calls. So now for the definition we're going to go to the end of the function and we're going to add the keyword return. And after that very important you have to add something. You have to tell Python what to return. You cannot leave it empty. So it could be anything a value a local variable a parameter calculation logic or maybe calling another function. So anything after the return is going to be okay. So now by looking back to our example I'm going to go and remove the print and instead we're going to put return. And what we are returning here is the local variable the final result of the calculation return y. Now about the call we cannot just go and call the function and walk away. We have to store the returned value somehow right and of course for that we usually use variables then assign for it the function call. So now whatever the function returns back it going to be stored inside this variable. And of course this is amazing because now we can go and do something for this output like maybe go printing it or do something else. So now let's understand how this going to be executed behind the scenes. So as usual Python going to execute it from the start.
We're going to go and define the function without executing it by creating the object function in the memory where we have inside the name the body parameters. So that's it for the definition. Then Python going to go to the next line where we have the global variable and the call. Now since we don't have the value yet, Python going to go and execute the right side. So it's going to go and execute the call by first assigning the argument to the parameter and then it's going to go inside the body and start executing again step by step. Here we have a local variable and an expression. So it going to go and grab the parameter value multiply it by two. We're going to get six. And now Python going to go and create a local variable Y and assign for it the value six. Then Python going to go to the next line and here it's going to see a return. And after that we have the variable. So now Python going to go and grab the value of the local variable Y. So the six. And then it going to send it back. It going to return it back to the color. So that we have Z is equal to six. But of course before doing anything else it going to go and clean up. It's going to go and destroy the local variable and assign the parameter. Now we have everything. And with that Python going to go and create the global variable the Z and assigned for it the value six. And now we can reuse it as we want. So for example, we go and print it. It's going to grab the value from the memory and put it in the output. So now my friends, as you can see, you have now the full big picture on how everything works together. The input, the arguments, the local variables, the global variables, the output, the return, and it's pretty easy, right? So everything makes sense. But there is one small thing before we go to practice.
Actually, in Python, if you don't specify a return, still Python going to send something back to the program. by the way and you might say what what you are talking about well it going to send something called none and of course none it's not a real value it's something unknown missing it's not a useful information but still it is an indicator that the function is not returning a real value and this is something of course we can use so if you are not defining a return python going to send a none but if you use a return it going to send a useful information back okay so now of course let's go and practice Okay, so now by looking back to our example where we are sending a row data, the row name and then we are cleaning up the name by removing the spaces and as well making the case lower. But now the thing is I'm just printing the value in the output and that's it. And this is not something that we usually do in projects. Usually we do a step we cleaned up the name in order to use it in the next steps inside my codes. So by leaving it like this I'm losing this cleaned value in my program. So that means we have to return this value. So our example is more realistic. At the end we're going to go and say you know what go and return the local variable that holds the cleaned value. So I will go and remove the print. I don't need that. If I leave it like this and execute nothing going to happen.
Everything is lost. The program is not getting the returned value and we as a human I'm not seeing what is going on in the output. So of course in order to capture the return we have to go and add a global variable. So let's go and add a variable called clean name equal and then assign for it the whole call. Now let's go and execute it. Of course we are not seeing anything because we are not printing. But at least my program is getting something back. Now I'm going to go and print our new global variable. So the clean name as you can see we are getting Maria. So not only me I'm seeing the value and as well my program is storing the output of this function. So actually that's it. I'm really happy about the example. Now it looks perfect.
Data goes in. We do transformations and then we return the data back. Now I would like to test something like for example I will remove the return. So my function is not returning anything. But still I'm trying to catch something from our function. So if I go and execute it as you can see we are getting none. So if you are not using a return python going to send the none it means nothing back to the caller and then it's going to go and assign it of course for the variable so that you can see none. All right. Now next one more scenario about the return actually we can go and use multiple returns inside the function definition. So let's have an example.
You remember if I go and remove everything here from the arguments and say I would like to send an empty string. Now if I go and execute it what going to happen? I will not see anything in the output because it is an empty string. But now dealing with empty string it is something really annoying.
We usually convert the empty string to a none because it is easier to handle and it makes everything more clean. So what I'm going to say I'm going to extend the logic and say if the name contains an empty string then replace it and return a none. Otherwise if you have a real value then go and do all those steps.
That means there is like a logic there is a conditions right? Let me show you how we going to do it. So we're going to say if and then say not name. So if there is something wrong with the name it's not a real value like here what can happen don't do anything just return a none. So that means I'm not returning the value empty string I'm just returning none otherwise if we have a real value then actually do the whole steps. So I'm going to go over here and say else and of course we're going to put the whole logic inside it. So make sure you are working correctly with the inendations and with that look at this we have twice the return but since we have an if else Python can execute only one of them. So now let's try this out with this empty string and go and execute it. Look at this in the output we are getting none. So that means the if statement is activated. We fulfilled the condition of the if statement and it is returning the none. So we are not returning the name or the variable. Now if I go and say Maria like this and then execute it is not fulfilling the condition because it has a real string value and it is going to the else. So it is processing the name and returning the process name. So that's it about using more than one return. Now there is another thing here. We are returning only one value but actually we can go and return multiple values. So let's have the following example. I'm going to go and get rid of the conditional statements and then we will have this code. Now in this example we are always switching the names to lower case. Now what could happen that I would like to see my value in both lower and as well upperase. So that means my function should now return the same value twice.
One with the lower and one with the upper. So how we going to do it? I'm going to go over here and say okay this is the low. And now I'm going to go for the upper cleaned equal. Now either keep working on the original value and say you know what we're going to strip it again and say here an upper or you can go and take the lower and switch it to an upper. So this is the transformation and that I have two local variables. Of course I have to go and now return them.
So I'm going to say l clean. This is for the first value and then in order to put another value as usual we use the comma cleans. So that's it. I'm returning now multiple values back to the caller. Now the caller should be responsible and as well receive both of them. If I leave it like this and execute, I'm getting in the output now a touble of both of the values. Of course, you can go and check the data type of the returned value. So this is a double. But if you'd like to get like the returned values in two separate variables, all what you have to do is to make as well a list of global variables. So the first one I'm going to say hello name and the second going to be name. So with that I will capture both of them and of course I can go and print both of them as well. So low name and app name. So that's it. Let's go and execute. Look at this. I have two values. They are not in the double anymore. They are just normal string values inside my two variables. So this is how you return multiple values in one go. Back to the caller. [music] So my friends, as you can see, we have different shapes of functions for different purposes. And you can see how the data flows inside the functions. So data goes in, maybe goes out. And of course, this depends on the scenario. So again, by looking to this, we could have a function that is just a block of code.
Nothing goes in, nothing goes out. Or we could have a function that takes one input using the parameter and the argument. Or another shape where you have a function that as well has an output. So it is returning something back to the program. That means data goes out as well. We can have like multiple outputs. So we are returning multiple values back to the program. So my friends, you have the full control on how to design your function by using the parameters, arguments and return. So you have the full control on how the data going to flow inside your function.
So now so far we have learned all the different types of Python function but based on how the data flow through the function. But now in real projects we categorize and classify the functions by something else by the purpose. We classify them based on the job each function is responsible for. So now what we're going to do I'm going to show you some famous patterns and function types based on their purpose and the use case.
So now let's go.
All right. The first type is [music] the action functions. It is a function that does something in the system instead of returning a value. So action functions they are actually focused on a side effects. They change something outside the function. Like for a simple example a function that is printing something on the screen or very famous one in data engineering where it saves data to a file or to a database or you might have function that is actually sending emails and notifications if something goes wrong. And another one could be maybe calling an external API. So as you can see all those action functions they don't deal with values and data but their job is to do something outside the function as a side effect. But sometimes different developers and different projects use different names like side effect function command function handler service functions all are different names but for the same idea. So now let's have a very simple example. Let's say that we want to store the application log messages in a file so that every time an event or something happens, we're going to record it and store it inside the file. Of course, we didn't talk about how to deal with files inside Python, but this is very simple one. So, just follow me step by step.
It's going to be easy. Okay, so now let's see how we're going to solve it.
We start with the diff as usual. And then the function name, we're going to call it write log. And now for the input, the parameter going to be the message. So we need it in order to store it inside the file. Now in order to work with files we're going to start with the keyword with it going to help us to open a file use it and then save it automatically and safely. So with open and now we're going to specify the path of the file. Now of course depend where you store your files. I'm going to go and put it like this. So it's going to be in C and then back slash. I'm going to put it in main then python and then I'm going to call the file app.log. So now since we are using here a lot of backslashes, I'm going to go at the start and say an R to tell Python actually this is a string. This is not the special character backslash. So it doesn't mean starting a new line or do something like special. It is just part of my path. Now after that we're going to say comma and we're going to tell Python how it going to go and store the information inside our file. There are different modes. We're going to go with the A. So we are telling Python go and append my messages to the end of the file. So don't go and delete anything or update anything. We always go and add things to the end of the file. And then we're going to say as file. So that we can write to the file. Then double points. So a colon and then another enter. Make sure the intendations are correct. Now we are inside this statement. So now what we're going to do we're going to go and write something to the file. Right? So what we're going to write inside the file it's going to be the messages. So it's going to be our parameter. And now since I would like to see each message in a new line I'm going to go and add a plus and then a new line. So backlash n. So again what's going to happen? Python going to go and create a file called app.log inside this path. The mode is append. So it will not delete the data inside the file. And then what is the action? We are writing inside the file by adding the parameter message into a new line. So actually it is very simple. That's it. Now let's say our program runs at the start. So we're going to say the app started. So this is the first event. Now what we're going to do, we're going to go and execute it. So now if you get it like me without any errors, that means it was successful. Of course I'm not seeing anything because we are not printing anything in the output. And in order to check our results, we're going to go to our file, right? So I am inside this folder and I can see the app.log. Now let's go and open it using any editor that you have.
So now look at this. Inside it, we have our message stored inside the file. And this is exactly what we wanted. Now if you go back and then over the time something else happens. So we're going to write inside our log user logged in.
So this is the second message. I will just comment this out. Let's go and execute it. Now let's recheck our file.
Look at this. We have the second message appended. So it is something added to the end of our file. User logged in.
Let's go and add a third one. Of course, I don't want this to be repeated. Let's write log app stopped. So we are at the end of our program. And I go and execute it and then reopen the file. You can see the last message app stopped. So now as you can see my friends it is really easy now to deal with the logs using this nice function where it does one action it go to my system open files and write the message at the end that's why we call it an action function [music] moving on to another type by purpose we have the transformation functions we already saw that previously it is a function that takes row data as an input then it does some transformations and data manipulations on it and at the end it returns a new value with a new shape.
And now of course about the transformations we could do a lot of things like calculating new values, transforming a text or structure, converting from one format to another, extracting or reshaping the data. So our logic could be anything that's manipulate our data. So it's not about like changing the system or like interacting with something outside the function. It's all about changing the shape of your data. And of course, in any data projects, engineering or analytics, this is the most important shape of functions that you have to learn because it's going to contain the core business logic that can manipulate the data. And of course, developers in different projects use different names like the data functions, calculation, utility, mapper. So again, different names for the same purpose. Okay. So now let's have the following task in order to practice. Our task is to clean the email addresses and split it into a structured data. So that means we have to return the username and the domain out of the email. So let's see how we going to do it. Of course as usual we start with the definition the name going to be clean and split since we are doing two things the email. Now about the inputs of course we just need only one thing the email. So this is our parameter. Now let's go inside. I'm going to say the first thing that's to go and clean the email. So I'm going to put it in a local variable and usually we clean it by removing the extra spaces and maybe as well by normalizing the string value to a lower case. So just standard things we already done before.
Now of course our task going to say split it into username and domain. Just to have an example let's say that we have sara atgmail.com.
So that means Sara is the username and the gmail.com is the domain. That means we need two informations and for that we're going to have two variables username and the domain. Now how we going to extract those two informations from one string? Do you remember we used the method split?
So it is very easy and of course we have to tell Python where exactly it going to be splitted. it's going to be at the at.
So that's Python going to go and extract everything before and after the at and return them as two values. Now actually that's it. All what's left is to go and return them. So of course you can go and return the username and the domain separately. But our task says we have to return it as a data structure. So that means we have to put those two variables into one data structure and you have to decide on which type. Of course, since we have here two different type of informations. We have the username and domain. We're going to go with that dictionary. So, it's going to be very simple. We're going to use the parenthesis and we need key values. So, at the start, we're going to say username double points going to be the username and I would like to start a new line. The domain going to be the value from the domain. Actually, that's it.
Are taking the email address, we are cleaning it up. We are splitting it into two informations and then returning it as a data structure. Of course, we have to go and test. So, we're going to call the function and let's have sara gmail.com. And now, of course, if I run this, I will not get anything in the output. I would like to see the results.
So, what we can do, we can either assign it to a global variable or just go and print it actually. And that's it. Let's try this out and execute. Now, look at this. We are getting our dictionary.
Everything is splitted. But I would like to go and test like for example I'm going to go and add here space and at the end as well space and maybe write everything in capital letters and here as well. So let's try this out. As you can see even if I have bad data I'm still getting a clean data and the output. So it is actually working. So as you can see my friend this is a typical transformation function. It takes data in. So the email is the raw data. Then some transformation going to happen and data clean up. So we are manipulating the data and we are returning the result the new shape of the data back to our program.
Okay. Now we have a third type of function. We call it the validation function. It is a very simple function that's going to checks whether something is valid or not. And typically this type of function going to return a boolean.
So either true or false. So my friends the validation functions are actually focused on the rules and the correctness more than changing anything like it will not change your data or your system. So there is actually no action. You have a very simple question and this function can answer your question by either yes or no. And this is very useful for many scenarios like for example validating the user input, checking whether the business rules are correct, verifying the data quality and something that I use a lot. We're going to use it in order to check whether the user has permissions or not. And my friends, in real projects, the validation functions are really important because usually the data has bad data quality and you're going to use those functions to protect your system from bad data. And as a data engineer, as you build your data pipeline, you're going to as well write a lot of validation functions to protect the data before inserting it. So this is something really important and we use a lot in real projects. Now about the names, mainly we call them validation functions, but you might hear of maybe a checker function, but they are all the same. Okay, so now let's have this very simple task to practice. Check whether the passwords meets the minimum length of eight characters. So of course this going to be easy as well. We're going to start with def as usual. And now since this is a question that we're going to ask our function, we usually write the name as a question as well. Valid password. It sounds like question and going to be really easy as well to understand this is a validation function by the name. And of course about the input we want to send our password. And now we're going to go inside the function. So now we have to calculate the length of our password. And for that we have our nice function the length. It is a built-in function. and we pass for it the password as well. So with that it's going to return the total number of characters of our password. And now of course we have to check the length.
That's why we're going to go and say it should be longer or equal to eight. This is the minimum length. Now of course this expression like this it going to return either true or false. So this is a condition and actually we can return the result of this condition like this.
So it is very simple. we are returning the result of our check. So actually that's it. So we're going to go and test it. So is valid password. Now just go and throw any password. So yeah 1 2 3 4 5 6. And of course in order to see the results we're going to go and print the whole call. So let's go and execute it.
Now look at this. We are getting false.
We are not fulfilling the requirement because this is only six characters. So now of course let's try a valid one. I'm going to go and add 78 and then execute.
Look at this. I'm getting a true. So as you can see my friends this function is not transforming anything. My password going to stay as it is. It is just answering a very simple question. Is the password valid or not? So the answer going to be always yes or no. True or false? Okay. So now let's have another task. It says check if an email address has a basic valid format before we use it. So this is a very typical thing that might happen. The user is just registering and giving us the email.
Sometimes they make mistakes and we're going to get bad data. So now before we start using the emails maybe to store it or to analyze something we have to check is the email valid or not and for that we can write a very nice function like this. So now since we are checking something we're going to call it as well as a question is valid email and of course what is the input? It's going to be the email address. Now one of the typical things that might happen is that the user might forget to add the at.
This is very critical of course. So we're going to say is the at in the email. So this is the first condition and as well very important structure is the dot. So com.net and stuff like that.
So I'm going to say as well whether the dot is inside the email. Now of course there could be a lot of scenarios but this is the basic checks. So now we are saying is at in the email is the dot as well in the email. If both are true then of course we're going to get true. But if one of them is false we will get false because we are using the and operator in between. So now this is our expression and what is left of course we have to return the result of our check.
So actually that's it. Let's go and try this out. So is valid. Now I'm going to write a bad email like soro gmail.com without an ad. And of course we have to print. So let's try this out. As you can see we are getting false. This is not a valid email. But if I go and add for it an add in between. Look at this. I'm getting true. So this is a valid email.
We can check another scenario where we don't have a dot. We are getting false.
So both of the conditions should be fulfilled. The dot and the at. And of course if we don't have add and dot we should get as well false. So as you can see I'm just asking the function over and over. And all what I'm getting is yes no yes no. So this is something that you're going to do a lot in real projects where you're going to check the data before doing anything.
All right, my friends. So now we're going to talk about my favorite and the last type of functions based on the purpose or the use case. We have the orchestrator functions. An orchestrator function is a function that controls the flow of your program by calling other mini functions in the correct order. And that's why we call it an orchestrator function because it is focused on the coordination and not about the logic or calculating the data or changing anything. Their main job is to connect everything together and to decide what is the next step to be executed. That's why in real projects you're going to find mostly in those projects like a little logic that going to decide based on some condition what is the next step to be executed what is the next function to be called and this is why I love those type of functions because they represent a process a workflow and it makes it really easy to understand your whole program in a very high level. So those are amazing type of functions and I can tell you once I enter a new Python project I always go and check the orchestrator functions to understand the behavior and the steps of the whole project. Now of course about the names it could be orchestrator or workflow functions controller pipeline coordinator again different names for the same purpose. All right. Now in order to practice this type we can have a mini project. So we have the following requirements. We have to build an application. We are going to receive an email from a user. We must check if it is valid email. If it is not valid, we have to log the problem in a file. But if it is valid, then we have to clean it and store it in a data structure. And of course, we have to log each step of the program. Okay. So now let's see how we can do this project. We have already solved a lot of parts like for example logging the messages, we have the validation, we have the cleaning and splitting. So that means we have already some random function solve part of the puzzle but we have now to put everything together in a process in a workflow. So now those are the steps and the first thing that we have to do is actually we want to receive an email from a user. So let's say that I'm going to ask the user in an input to give me the email. So I'm going to say the email going to be equal to the function input and we're going to say please enter your email. So that's we're going to get the email from the input and we're going to store it inside the global variable email. Now let's go to the second step. We must check if it is a valid email or not. So we already have a function for that is valid email.
So what we're going to do? So that means we're going to call the function is valid and we're going to pass for it our email. Okay. So now this is the first step. Now the second step it says if it is not valid we have to log the problem.
So that means there is some kind of condition that I have to do first and if it is fulfilled then I have to write a log. Of course we have already a function in order to write a log in a file. But first we have to build the condition. So we're going to say if is valid. So the same thing if it is not valid that means I have to go and use the not operator. So if it is not true what's going to happen? We're going to log the problem. So we're going to call the function write log and what it needs actually just a message. So I'm going to put the whole message with the email address as well. So I'm going to say an if in order to compile the message with the variable email going to say invalid email received then double points and then after that double points and of course our variable so the email. Okay. So now let's go to the next step. It says if it is valid we clean it and store the structured information. We're going to say else and now we're going to call the transformation function. So clean and split the email. So we're going to pass for it of course our email address and actually that's it. So this going to happen only if the email is valid. So as you can see this is how the two types of functions works together. First we validate the email. So we are using validation function and if everything is okay then we use the transformation function. Very very typical. Now the last step it says we have to log everything that is happening. So that means we have to log the start at the end whether it was successful or not.
Now if you look to this we just logged if something goes wrong right but still I would like to log as well if [clears throat] it went successful. So I'm going to say after cleaning go and write in the log something like processed email and then actually I would like to print the result of the cleanup. So that means I have to store the returned value from the function in a global variable like for example clean email equal. So I'm assigning the return into this variable and then I'm going to print it in the logs. So I'm going to say clean email. So that we have as well a nice information inside our log about the processed email. Now of course we can go and add some extra informations like for example at the starts we're going to write in the log like application started and completely at the end we're going to say application is stopped or ended and it looks now like a workflow like a process. So our application going to start after that we're going to get the email from the user. Well actually this is unnecessary so let's remove it.
We're going to validate it over here. If it is not valid, then we're going to write an issue inside our logs. But if it is a valid email, so everything is correct, then we're going to process the email where we're going to clean and split. And then we're going to print it as well inside our log. And at the end, we have a nice message that everything stops. Now, let's go and try this out.
So, I'm going to say let's start with a valid email. So, sgmail.com.
Everything is correct. Let's go and execute it. Now, look to our log.
Everything looks nice. So, the app is started. We processed the email. So we have the structure. We have the username and the domain. Everything was okay. And then the app stops. So that I have a nice protocol of how things worked.
Actually, it looks nice. Now let's go and try the invalid email. I'm going to say just remove all those informations and save again because it's going to be appended. Now let's go and run it again.
Andsgmail.com.
So we don't have an ad. And then execute. So if you look to this we can see the app started stopped but we received an invalid email. So actually that's it. Our program is ready and everything is correct. But now you might say but wait a second where is the orchestration function? Of course you are totally right. All those steps are actually the preparations for the orchestration function. Now look to the whole code over here. You can see we have our functions and now we have the whole workflow outside the functions in the body of our program. Right? And this is a process calling the other functions. Now what we can do actually we can put the whole thing inside a fourth function. So here we're going to have our orchestrator function and as usual we're going to start with a diff and then we're going to say process user email.
And now about the input here we have like two options. Either we're going to leave it empty because we are getting the email from the input or we take only this step outside and we send to this orchestrator the email. So I'm going to say I'm going to go with the second option. I'm going to take this tip completely outside and just put it here at the end. So now the orchestrator going to get an email and everything else going to go inside the body of the orchestrator. Let me just clean up little bit those comments. I'm going to put it over here. So look at this. This is exactly the purpose of the orchestrator. It is calling all the other functions. So we have here the right log is valid email the transformation and it is in the correct order. So that means first we have to validate and then we're going to clean up if everything is correct and as well it is controlling the flow of the program. So it has its own like if else condition right and this is exactly the purpose of the orchestrator function to call other things. Now all what we have to do in the body of our program is just calling this one function and it going to do the rest. So we collected already the email and then we're going to say process user email and I'm going to pass for it the input from the user and that's it. So now as you can see the body of our program has only two line of codes get the data and process it and everything else are actually inside functions. So now of course you can go and test those stuff. So if I provide clean data it's going to work. So again app started processed email and app stops. So my friends this simple program is a real world scenario that we usually do at projects. And as you can see this is the mindsets. We split the functions actually by the purpose by the use case.
Once I start writing a new function, I will think is it like transformation, is it validation, it is an action and then after writing many different functions, we put all the puzzles together inside one orchestrator. And with that now we have a feeling, a real understanding how we use functions in real [music] projects.
All right friends, [music] so with us you have learned the different types of functions based on the purpose on the use case perspective and now you have more understanding on how we use functions in real projects. So again the first type the action functions they make things happen. [music] So they interact with things outside your program your system printing saving files calling an API or another very famous form the transformation function.
They work with your data. So they take input in, they change it and then they return a new value. Another use case we have the validation function. It checks the rules. So it going to answer your questions with either yes or no. You're going to use them a lot in order to protect your system and to check the quality of your bad data. And the last one, my favorite one, the orchestrator function. It's going to go and connect everything together by calling other mini functions in the correct order. And once you understand those patterns, it's going to be really easy for you to design your program to write the code and to maintain it. So those are the different types of functions by the purpose.
So now we're going to start with the first step where we're going to learn simple but yet very powerful rules that every Python developer should follow when they are writing functions. The goal is very simple. We have to write code that is easy to read and easy to understand not only for you but as well for your colleagues. Many of those rules are actually come from the pip 8. This is the official style guide for Python.
Just Google it and you're going to find there like a lot of rules that are considered as standards on how to write clean Python code. Of course, we will not deep dive into all of them, but I'm going to show you the most critical one that if you ignore it, your pull request in the review going to be rejected. But at the same time, there are rules that are not written in the official document, but still it is like common standards between professional Python developers. So now let's go and dive in.
All right friends, so now here we have a very simple example of a function that is actually working but not following any standards. So this is really bad.
The function accepts two parameters, the price and the rate, and it does some calculations in order to find the price after applying the discount. So now what we're going to do, I'm going to show you how I usually review the code in my projects and we're going to refactor this bad function so that we're going to apply all the standards and the rules that we usually follow in our professional projects. So now usually the first thing that I look at is the function name. And here we have few rules. The first rule says follow the naming convention the snake case for the function names where we use lowercase letters and we separate them using underscore. This is the official style from Python if you are following the piph. So now by looking to the function name as you can see it is not following our standards. The d is uppercased and as well the p is uppercased and the two words actually connected to each others.
There is no separation. So there is no underscore. Now in order to fix it, we're going to do the following. So we're going to say diff. And now everything is lowercased. So disk underscore. And then after that we have the print as well lowercase. So actually this is the function name following the first rule. Now the second rule is as well about the naming of the function.
So it says a function name should clearly describe what the function does.
So we usually start with a verb in the function name because usually our functions are actions and it does something and always use clear names and avoid abbreviations. So now by looking back to our name disk print it doesn't say anything right it is not an action I have to check the function or maybe the description to understand what is going on right so now we have to give it a better meaningful name so first we're going to start with a verb by looking to this we are calculating the discount right so the verb here is actually we are calculating so calculate this is the action name and then we have to describe what we are calculating it is actually the discounts so This is the function name. It start with a verb and all the names are clear. So now by just looking to the name you're going to understand immediately we are calculating the discount. So if you go and compare it to the old one it is way more clear. Right now the next step I usually go and check the parameters. And here we come to the third rule. Parameter names should explain what kind of value they hold. So it is very similar as well to the function name. Don't use abbreviations or like here single letters for the parameters. Just use a full name. It's going to make your life and your colleague's life easier. So now instead of having P, we're going to say price as well. Of course, everything lowerased.
And instead of R, we're going to say rates. By just renaming everything is clear. Calculating the discount going to need the price and the rates. But now if you read this, you're going to ask yourself, what is P? What is R? I need some catalog to understand how this works. All right, moving on to another thing. After reviewing the name and the parameters, I usually go and check the description of the function. So, where is the documentation here? This is really bad. And here we come to the rule number four. Always use a dock string to describe what a function does. I know this is a topic at the start might sound annoying for many developers, but as you do multiple projects, you can understand why we document our functions. Of course, in projects, you will not be soloing Python. You're going to work in a team. And it is always nice to let others know what the function actually does. And not only for them, actually for you as well. Because believe me, after a week writing your function, you're going to forget about what you have written. So by having a nice clean description, it is as well good for you to remember what you have done in case you want to refactor or maybe to fix your function later. So now I said we use dock string. So what is that? It is a string that is placed at the first line inside the function. So this sounds a little bit weird. Let me show you what I mean. So this is our function here.
And now I'm going to go inside it. The first thing that we do inside the function is actually not writing the logic and the calculations, we write a description. So in order to write a dock string so that it is easier for you to write like multiple lines of descriptions. But for now I'm going to have it only like one line describing the function. So it is very simple.
calculate the final price after applying discounts. So actually that's it. This is only one line. And now you might ask the first question and you say why we are putting this in a string? Why we are not just using a comment like we used before. So we start with the hash and then we put the whole thing. Well the first thing is that once Python sees the hash, it understand it is a comment and Python will going to ignore the whole thing. So it will be lost. it will be ignored and the program the code don't know about this comment at all. But if you are using a dock string, Python going to use this as a description for the function. So it's going to go and store it inside the function as documentation. So it is not lost. It is something that is attached to the function. And of course you might still asking why we need actually the description. We are not using this value, this string value inside our calculation, right? You are totally right. We are not using this as a value for our function but it is there inside the program and I can go and call it for example if you go and use the function help and then use the function name. So something like this Python going to return the documentation of the function. But of course in order to try this I need something inside the function. So maybe return now one something like this just in order to not get any error. So now if you go and execute it now look to the output I'm going to get a full description of my function including the dock string. So this is a really nice information and it lives and exist inside Python now but if I don't do it so let me just go and remove it and I stick with the comments look at this as I'm asking for help for the function there is no documentations for the function that means I have to open the code to open the function in order to read the comments. So this is something that we check whether the function has a description or not. Then we go inside the function and start checking the logic and how the function is written. And here immediately you're going to jump to the next rule where we're going to say don't print inside functions. Instead use returned value.
So now by looking to our function actually it is printing a lot. We have the first print and the second print and it is not returning anything back to our program and this is something that we have to avoid. So avoid using prints. So that means if I take the logic over here and start like refactoring, I'm going to get rid of the first print as well. I understand it is nothing to print in the output, but we usually don't do that. If I need to print, I always print in the main code, not inside the functions. So I will get rid of this and instead of the last print, I'm going to go and replace it with a return of course. And of course here we have to go and start using our parameter. So it's going to be the price price price and here the rate.
So that's it actually don't use prints use return instead. Okay. Moving on to the next rule. Don't modify the values of the parameters. So leave it as it is.
Use local variables instead. So now by looking to this example, you can see the parameter P is actually getting modified by assigning it to the new calculation.
So with that at this stage actually you lost the row value. So we don't do this.
Instead, we introduced a new local variable. So now instead of saying price is equal, I'm going to say final price.
So that means the original price inside the parameter and the modified one is inside local variable. So with that I don't lose my values. And now I'm going to go and return of course the final price. Of course at the end I'm going to get exactly the same result. is just like clean way on how we work inside the function because as things gets more complicated you might go and start like adding more steps and it will not be any more clear are we using now the original price from the input or is it like modified so just make it clean and introduce a new variable it will not be bad so now the next one it is not a rule but if you have like one line calculation like this and you don't have any extra steps inside it you can actually skip using any assign assignment and using a local variable you can put the whole thing as an expression inside the return. Our function is just one line together with the return. So I'm going to say this is not like let's say hard rule. It's going to be okay if you use it like this or introduce like a local variable for it.
Now I'm going to say if you follow those rules now you are 80% professional at your codes but you have followed all the must rules. Now we're going to go and add some extra. Let's say add-ons modern rules. So they are not described in the pip 8 but they are rule that are actually followed by professionals at companies. So now the first luxury rule says use data type hints for the function parameters and the return values. Now the thing is in real projects most of the bugs comes actually from wrong data types. So what actually happens is that if I am like calling the function and I understand okay it accepts a price and rate but I don't know exactly which data type I should follow like maybe for the price it's going to be clear I'm going to use maybe 100 but for the rate I might go and add like maybe string value like 20%. Now if I go and execute it I will get an error because this is a string value and it will not work in the calculation. it need a numeric value but it's something that I really don't know by just reading the function description or the function name of course it is now very clear that I should not use it but if the function is little bit complex you're going to end up using a wrong data type and this happens a lot in projects really so now instead of me try and error which data type I should use usually the one that is building this function should give me a hint so how we going to do it we're going to go to the price and we're going to a double point after it. So not equal. I'm not assigning anything double point. I'm going to say float. So now this is very important thing to understand. This is a hint. This is for us for humans in order to understand how to do the call. It is not for Python. So Python will not go and change the data type of the price to floats. The data type going to be depending on the values that you are passing in the function call. So Python going to see this is 100 and then it going to use for it maybe a number. So I'm not assigning here or doing a data type casting it is just hint for us and the same thing I'm going to go and add it for the rate. So use a float. So that's for me as a caller for this function. I'm going to understand immediately okay the rate should not be let's say string value. It should be float or a number of course it's fine.
So now with this we gave hints of course for the parameters the input of the function but as well we can go and give a hint for the returned value so that I understand okay I'm returning here a float number but now in order to do it we will not go over here and add a double point instead we're going to go over here just before the double points we're going to go and add a nice arrow and then we're going to say float so with that we are saying this input going to return a float like it looks nice right so as I said this is a luxury thing But professional Python developers use those hints usually in the functions. Now another luxury thing but still it is professional. We add as well description for the input and the output inside the dock string. You're going to find this everywhere from professional Python coders. So how we going to do it?
We're going to go and add a new lines inside our dock string. So now this is just a description. Usually the first one going to be the description of the function and then we're going to say args for arguments. Now we are just describing the input of the function. So just organize it nice. We're going to say okay the first input is the price and the data type going to be floats.
And here you just describe the input. So maybe you can say original products price and then the second input going to be the rate as well floats. So we're going to say discount rate as numbers.
Maybe I'm going to go and add an example 24 20%. Something like that. So I'm just describing the inputs. Now of course we can go and describe the return value. So we can say returns double points. We have here only one value. So the final price. So it's going to be as well float final price after applying discounts. So with this I can say you have now professional dock string for the function. One line for the function description for the inputs so the parameters and for the returns. So my friends those two things the hints for the data types and the extended dock string it is something nice if you want to work as an expert you can go and add those small details. So now just have a look go and compare the new design of the function with the old one by adding those few rules which is really simple.
Our function looks way more professional than the old one. And in just few seconds you're going to understand exactly what is going on and you're going to look professional at your project. All right my friends. So now by looking back to our road map with that we have completed the last chapter the functions. Now you can think like a real Python developer. So you learned how to break complex problems into small clean pieces using functions. And now if you sit back and look to the whole road map with that you have completed the whole course. You learned the foundations of Python. You learned how to work with data. How to control the execution of your Python code using logic conditional statements. how to automate things using loops instead of repeating the same thing over and over and as well how to work with advanced data types the data structure list double set dictionaries and of course how to build functions in Python. Now my friend you made it congrats if you are watching this right now then you really should be proud of yourself. Seriously you just finished Python course. This is not something small. You didn't quit. You didn't stop halfway. you made it until the end. That already puts you ahead of so many people who starts and never finish. And now I would say it's really nice time to share your new achievement. So tell your friends, tell your family, post it in the LinkedIn, be proud of your new skill. If this course helps you, leave a review, share it with others. And of course, I'm not saying here this is a goodbye because this is just the beginning. If you enjoy coding, then this is a perfect time because you can consider AI and data engineering. They are currently booming in the industry.
The demand is really high. And of course, Python is the core skill, the foundation for those career. And if you like my style, then follow me because I'm going to go deeper into those careers. And of course, if you want to support more free content like this, then subscribe, like, and comment. This is going to really help with the algorithm. And as well it's going to help reach nice people like you. And now nothing left to say but thank you so much for taking this course and I will see you in the next journey.
Up Next

Relational Databases Explained: Structure, Queries, and Benefits (2024 Guide) -
@IBMTechnology
268.7K views•2021-10-22

BitTorrent Protocol Explained: Piece Selection & Peer Choking
@StevenGordonAU
481 views•2013-02-22

HTTP Requests Explained: GET, POST, PUT, DELETE
@codecademy
103.1K views•2021-10-07

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science


































