Skip to content

The Ultimate Python Course For Beginners (Learn How To Code in 2026)

Comprehensive Python course for beginners covering basics to advanced topics with practical projects and AI applications.

Ask about this video. Answers come from its transcript only — with the timestamp, so you can check them.

Generated from the transcript and can be wrong — check the timestamp.

Key Takeaways

  • Python is an ideal first programming language due to its simplicity and readability.
  • Learning Python builds a strong foundation that eases learning other programming languages.
  • Practical coding and project-based learning are essential for mastering programming concepts.
  • Setting up a proper development environment like PyCharm enhances coding efficiency.
  • Python’s wide applicability makes it a valuable skill for various programming paths including AI and web development.

What the video covers

  • Introduction to Python programming for absolute beginners with no prior coding experience.
  • Step-by-step explanation of core Python concepts like variables, data types, functions, conditions, and classes.
  • Hands-on practical examples and projects to reinforce learning and problem-solving skills.
  • Guidance on setting up Python development environment using PyCharm and online IDEs.
  • Coverage of advanced Python topics including automation, AI application development, and basic web app creation.
  • Emphasis on Python’s versatility in AI, web development, automation, data analysis, and application building.
  • Encouragement to engage with the course via comments and timestamps to enhance learning and community interaction.
  • Availability of audio dubs in five popular languages to support a wider audience.
  • Supplementary learning materials such as lesson summaries, code snippets, memes, homework, and resources available on python.click.
  • Tips on making Python code readable and best practices for beginners.

Answers

Questions about this video

Do I need prior programming experience to take this course?

No, this course is designed for absolute beginners with no prior programming knowledge, covering all concepts step-by-step.

Which Python version should I use for this course?

Any Python 3 version will work fine; the instructor uses version 3.10.6 but minor version differences won't affect learning.

Can I follow this course without installing any software?

Yes, you can use online Python IDEs to write and execute code without installation, though PyCharm is recommended for extra features.

Full Transcript — Download SRT & Markdown

00:00
Speaker A
Hello world. If you don't know Python and you want to learn, then continue watching this course. During this training, I'm going to cover every Python concept that you need to know to begin your programming journey. Even if you've never coded before and you know absolutely nothing about programming. We're going to cover all of these concepts one by one, and I'm going to simplify it for you so you can better understand it and add it to your coding skills. And by the end, you will be
00:22
Speaker A
surprised with a new Python skill and how much you'll be able to code on your own. And as you can see, Python code can be self-explanatory and read like English. Once you know the basic rules, and that's exactly what you're going to learn today with me. And don't worry if this code was a little bit overwhelming. There were too many concepts at the same time, and we're going to cover all of them one by one during this course, and it won't just be the theory. The best
00:44
Speaker A
way to learn programming is to actually start using it to solve real problems. So there will be a lot of practical examples, and we are going to build some cool Python projects together. So during this course, you're going to learn all the Python basics that you need. We're going to cover even advanced Python concepts. We're going to automate a few simple tasks. I'm going to show you how to create AI applications with Python. We're going to do a little bit of web
01:03
Speaker A
coding in the end to create a simple web app. And this is just a few examples. Python can be used for anything. It can be used for AI, web development, automations, data analysis, building applications, and a lot more. It's incredibly versatile, and that's why it has become the most popular language in the world. It's quick and easy to learn. It's forgiving for beginners, and there are free libraries available for almost any task you can imagine. And the best part, the core concepts of programming
01:27
Speaker A
and computational logic you're about to learn, such as variables, data types, functions, conditions, classes, and so on, they all transfer to other programming languages as well. That's why learning the second language is always 10 times easier than learning the first. And that's why Python is the best programming language that will provide you a solid foundation for any programming path you choose in the future. And also keep this in mind, this is going to be a very comprehensive crash course for
01:49
Speaker A
absolute beginners. There's no need to rush through everything in one go. I recommend you to leave timestamps where you stop in the comments and write your biggest takeaways. This way it will help you to quickly refresh your memory. You can compare with others, and you continue from the same place without missing anything. It will also tremendously help the course in YouTube algorithm. So I consider it's a win-win situation for all of us. So comment as much as you want. On top of that, these
02:14
Speaker A
course come with audio dubs in five most popular languages to help more people to learn Python. And you can choose the right language right there in the corner settings in the audio tab. And a last quick note, I've wrote summaries for each lesson in this course with explanations, code snippets, memes, homework, and additional resources to better help you learn Python. You can access all of that absolutely for free on www.python.click website. And now I want to wish you
02:39
Speaker A
happy coding. Get yourself a cup of tea or coffee, hit that like button to show your commitment, and let's begin this epic Python course.
02:52
Speaker A
All right, lesson number one. How to execute Python code. To begin learning about Python, we need a code editor where we can execute Python code. And there are many free options to choose from, including some online as well. You can literally type online Python IDE in Google or whatever search engine you use. And then in here you will find some website, for example this program. And in here you will be able to execute Python code and follow along the course without
03:15
Speaker A
installing anything. It will be more than enough to follow along. However, I will take it a step further and I will actually download PyCharm IDE so I can have a few extra features while I code, and you can follow along with any code editor you want. But if you decide to go with PyCharm, here's how to install it. First of all, type download PyCharm in your browser. Then in here you will see a web page from JetBrains, and this is exactly what we need. It's going to take us to this website
03:40
Speaker A
jetbrains.com/pycharm/download and in here we can click on download. You can see it's unified. It's free forever plus one month of pro included. That's exactly what we want. Let's click on that and download. Once you're going to download it, there's going to be a PyCharm installer. Just double click and run the installation. Then click on next. Here's where it's going to be installed. Click again on next. On this menu, you can select all of these tick boxes. It's going to
04:07
Speaker A
create associations with Python files, add you option to open folder as a project and so on. It's going to be very useful. Now let's click on next and lastly install. Just going to take a while. So let's wait until installation is complete. All right, installation is complete. And it suggested to reboot our computer, but we're going to do this later. So select I want to reboot later and click on finish. And after that, you'll be able to open PyCharm. And it's right here, 2025.1
04:35
Speaker A
version. Let's click on that one. And it's going to open up the PyCharm. All right. And once you're going to open PyCharm, this is the menu you're going to see. And here it's going to welcome to PyCharm. What you want to do, you want to create a new project. Right here is the button for the new project. Then we need to select a location where we're going to save this project. I'm going to click right here on location and then select anywhere. I'm going to go to my documents.
05:00
Speaker A
Then create their folder EF Python course. And in here I'm going to create my project. Select folder. That's fine. Then you don't need to create your Git repository. You don't really need welcome script. Then in the interpreter type you can select project VF. It stands for virtual environment and it's going to kind of save the settings of your Python environment. All the packages, all the settings that you're going to use. Just don't overthink it. Just select that one. It's the easiest.
05:22
Speaker A
And lastly, we need to select Python version. If you don't have any Python version selected, you can easily click here. And you can see there's download and install any version. During the course, I'm going to use 3.10.6. I think the version. And to be honest, it doesn't matter which version you're going to use because all of them pretty much the same and they have the same functionality. There's just minor changes here and there, and it's not going to affect anything during this
05:44
Speaker A
course. So, select any version you want. You can select 3.10.1. I'm going to use this one because this was installed on my machine by default. Now, let's click on create. All right. And here is our PyCharm project. At the moment, it's quite empty, but don't worry. On the left side, right here, you can see there's the project and you will see here the folder structure of your project. Then, ignore external libraries and scratches. You don't need to know anything about this. Just
06:07
Speaker A
pretend they are not there. But in here you can right click then you can new. You can either create a directory if you want to create folders or we can straight create Python file. And I'm going to call mine hello world. And now we are inside the file. The first thing I want you to do also it's really useful setting is go here click on settings. Then in the search we're going to write here something like scroll. And I think it's going to be in editor in general. And it's right here.
06:37
Speaker A
Change font size with control mouse wheel. Make sure this is selected. Then you can hold control and kind
07:02
Speaker A
works and you can execute Python code correctly. This is really important. So I want you to write print then open parenthesis and you can see it already autoc closes this. Then I want you to make double or single quotes and type hello world. But I want you to type it exactly like this. And then we can execute it. To execute a Python file in PyCharm, we can either click on this button right here to run the file. Just make sure that you have the current file selected. Or you can rightclick and run
07:29
Speaker A
hello world. Click on that one and have a look right here. and you will see that you displayed hello world text in the console. Congratulations. You've just written your first hello world statement and it means that you began your programming journey. There's the Chinese proverb that says a journey of thousand miles begins with a single step and you've just made your very first step into the programming world. Oh, make sure that you install your code editor,
07:50
Speaker A
you try print hello world, and that it works. And then you will be ready to start learning more about Python basics in the next lessons. See you soon.
08:04
Speaker A
All right, it's time to get your hands dirty and dive into Python code. But in order to start writing any code, you need to understand that there are certain rules that you have to follow. And these rules in programming are called syntax. Syntax is like a grammar of programming languages. And if you don't follow it, well, Python won't understand you. And you're going to get an error message. But also, don't worry. Errors are actually your best teachers. They tell you exactly what went wrong
08:27
Speaker A
and where. So you you know where to look and you can fix it and try again. And to be fair, that's how all of us learn programming by trying and fixing lots of errors and learning from them. And in this lesson, I'm going to focus on absolute basics of Python. So open your code editors, then right click here to create a new Python file. We're going to call it something like 01 basic data types. And now we are ready to begin with the basic data types. And we're going to start with strings.
08:55
Speaker A
So text in Python is called a string and it has to be inside single or double quotes. We can also use double quotes inside single quotes and vice versa. But we cannot write text in Python without quotes. It will break the syntax rules. And you can also see that there are certain keywords like not in, break that are highlighted because they have a special meaning inside Python syntax. And we're going to talk about them more later. But for now, just know that you cannot really write text like this
09:21
Speaker A
because Python doesn't know what it is and it's going to break. So to avoid this error, let's also comment it out. To make comments in Python, we can put hashtag symbol in the beginning like this. This way it becomes a text more like for us developers. So we can read inside the code and Python is just going to ignore it. We can also write it behind other statements. For example, here this is a string. And you can see this will not cause any issues because Python sees this symbol which means
09:47
Speaker A
comments and it's going to ignore it completely. And also to be fair, we cannot really write strings like this in Python because we have to assign our data to a container and it's called variables in programming. Think of variables as a box where you can put your data like text, number, even list of data. So you can grab this box later and use it in other parts of your code. To create a variable in Python, we need to define any name and use an equal sign to assign data to it. For example, right
10:14
Speaker A
here, we're going to write text equals like this. And in this case, text is going to be our variable that's going to hold this data because we assign it with the equal sign. Also, in Python, it's very common to name your variables with multiple words by using underscores. And usually in Python, you keep everything lower case. This is called snake case. In some other languages like C#, it's more common to write, for example, in the capital letters, but in Python, this
10:39
Speaker A
is the way we prefer to do this. And also keep in mind that you cannot begin your variable names with a number in the beginning. You can see it's going to be highlighted. It even has another color indicating that it's a number and it's not going to work. So instead, let's call it text free like this. This way, it's going to work just fine. And the general rule is to keep your variable names as short and simple as possible while keeping them readable. And also
11:02
Speaker A
avoid using any special symbols because if you're going to put them, you can see it's going to be highlighted as an error and it's not going to work. But don't worry, if you're going to rightclick and try to execute it like this, you're going to get an error message. And again, don't be afraid of error messages. They just tell you exactly what went wrong. In this case, it shows you that on line seven, which is this line right here, and this piece of code, it's going to give you an error, syntax
11:26
Speaker A
error, invalid syntax. And it's very clear. It means that something is not right. And it's even highlighted. So, if I'm going to remove it and try to run it, it's going to work just fine. Obviously, we're not printing anything. So, it's going to be empty console. But you can see for now everything works. And also some error messages going to be a bit more confusing and you would not understand what they mean. Just ask judge and it will explain you what it means and over time you will see this
11:48
Speaker A
error messages over and over and you will get used to them. Now we assign this data to variables so we can reuse it in our code. Let's make a few print statements to display this data in the console. As you remember previously we wrote a print statement and we wrote here something like hello world. And when we execute it, it's going to write here exactly the value we provide. And the beauty of variables also let me reduce some space here is that uh we can reuse the value assigned
12:18
Speaker A
to this data. So we can come here and instead of writing the text, we're going to provide the variable name. So you can see we're taking this variable name and we can put it right here like this without any quotes because this is a variable. Also with Ctrl + D, I'm going to duplicate it a few times. And let's print text two, text three. Let's print text free twice. Now right click, click on run. And you can see all the messages appear here. And the text free is also
12:45
Speaker A
printed double because that's what we say right here. We say print text free two times in a row. And this is a key concept in Python that you assign your data to a variable. So later you can reuse in other places. You can print it, you can provide it as argument and so on. And don't worry, you will see during the course how we use them. Oh, and lastly, when you name your variables, you have to avoid using special Python keywords or built-in function names. At
13:06
Speaker A
the moment, you don't know many of them, but over time, you will know exactly what they are, and you will know that you should not use them because if you use them, you're going to override internal meaning, and that's not good. So, let me just paste in here a bunch of different words that you should not use. You will also notice that they highlighted in different colors. For example, these blue ones, they represents built-in functions. You already know print. There are a few
13:27
Speaker A
other ones, very simple. Then these are the special keywords in Python syntax. They have special meaning to use different concepts and you're going to learn all of them during this course. And also there are other other functions which kind of define the data types. This refers to string, float, list, boolean, integer. And later I'm going to explain more in detail about them. And let's say that somewhere here you decided to make something like you know what my sum is going to be 2 + 2. And
13:54
Speaker A
it's going to work but you're going to override the internal meaning of the sum built-in function. And you can see right here, it's even highlighted. And if you're going to hover over it, you're going to get this message says shadows built-in name sum. So it tells you that you should not use it. So use something else. And if you really want to use sum, you can at least add the underscore. Then later on, if you try to use the built-in function like a sum, for
14:15
Speaker A
example, it's still going to work. All right, our code gets a little bit messy. So let's clean it up with comments. So firstly, I want to clean up all of this. And the way I'm going to do this, I'm going to select everything. I'm going to hold control and click on forward slash or division key. And you can see it's going to comment all these lines. I can click it again to uncomment. And it's very useful when you write a piece of code. It doesn't work, but you don't
14:39
Speaker A
want to delete it yet. So, you're going to uncomment it. So, Python ignores it. And you still have access to this piece of code. Also, it's a great idea to use comments to organize your code into different sections. And as a beginner, I love to explaining everything to myself that I did inside the code with comments. Also, I like to separate it in different section. So, let's do that as well. For example, in here, we're going to write text variables. We're going to
15:02
Speaker A
also add here a bunch of different dashes. So, it kind of creates this kind of visual uh text separations. Then, this one we can remove. We can leave some comments. Then, let's copy that part to here. We're going to write our print statements. Then, we're going to copy it again. Right here we're going to write special keywords in Python do not use. You see this way your code becomes more readable because now you can see this is the first section. This is the second one.
15:31
Speaker A
You can also see here is the comment. Another thing when I create comments in my tutorials I love to use uh emojis and for that I'm going to hold window key click on dot going to give me this emoji picker in Windows. For this one let's use a box. Then in here in print statements, I'm going to look for something like ice. And then here we're going to write something like stop, so don't use it. Many Python developers going to look at this and they're going to cringe, but I
15:56
Speaker A
really like it and I think it makes it a bit easier to follow along my code and read it. But that's up to you. You can decide what you do in your own code. All right, by now you know the string data type and how variables work. These are already the very basics of Python. But let's look in other basic data types and there are not so many of them. So I'm going to select that with control slash and going to comment it out. Then let's go all the way down. Let's make another one. And right here
16:22
Speaker A
basic data types in Python. Let's make some space so we can code. And now you already familiar that we can create strings and it has to be text inside of single or double quotes. There are also number integer which is a whole number like 10, five, 100. There also a number float which can be like 34 and this float refers to the floating decimal points. So it means that it's a number with decimal and values. Then another one is boolean and this one is very simple. It's true or it also can be
16:56
Speaker A
false. Think of it as a checkbox which you click on off on off. That's exactly what it is. And last but not least, there's also a none type and it's just called none. And it it means that there is no data and surprisingly you will use it more often than you think right now. Also another thing in Python syntax is that you can create multiple spacing. It doesn't always have to be just one space between your variable name, equal sign and so on. You can create any kind of
17:21
Speaker A
spacing you want. What's important is that it's all stays on the same line. Also workarounds about this but we're going to talk about this later. And these are our basic data types. String, integers, float, booleans, num type. They're very simple, very rudimental, but you absolutely need them in nearly all of your scripts. Also, let's make some print statements. I'm going to write print like this. Then with Ctrl D, I'm going to duplicate it five times. And I'm going to print here
17:47
Speaker A
the values of all of these variables like this, this, and this. Now, with a right click, run. I can see we're printing here text 314, true, and none. And the thing in programming is that for example, this number, it could also be a string to be honest. For example, let's write here string number. And we're going to make it like this. Then we're also going to create a new print statement. Put it there. And now when we're going to execute it, you can see in here it says 10 and 10.
18:22
Speaker A
But you don't know which one is a string, which one is an integer. And sometimes it's really important. For example, if going to try to make some math operation with that one, you're going to get an error. And it's very common to double check your kind of variable types inside your Python code. The way to do this is we can use type built-in function. The way it works use the type function name and in parenthesis we need to provide an object we are testing. For example, let's test
18:46
Speaker A
the string number. This way it's going to return what is the type of object. But also if we want to display it in the console, we also need to print it. And for that we're going to write here print in the beginning. And also notice that in here we're putting a function inside of a function because this function returns you something. You're going to see it in a moment, but it's going to return you a kind of value and you can display it. You could first of all define a variable
19:11
Speaker A
name. Let's say x equal this and then print x. Or it's also very common if you're not going to reuse it anymore. You can just print print it directly like this. And let's do it for all of these values. I'm going to remove that one. And we're actually going to write here type like this. like this. Just make sure you have exactly the same number of parenthesis open and closed. And if you have more, well, first of all, it's going to be highlighted. And second of all, if you're going to try to
19:37
Speaker A
execute, you're going to see an error message. And let's leave that one so you see the message. But first, I'm just going to kind of paste it everywhere. So, it's going to be here, here, here, and here. Then, let's close it everywhere like this. Now, here we have one extra one because I want to show you an error message. So, let's execute it. And you get an error message here. You can see the code. Here is the line 55 which is that one. And it tells you syntax unmatched. And then there's closing
20:06
Speaker A
parenthesy. And it's very clear that something is wrong right here. So we're going to remove that one. And now everything is fine. So lastly, let's execute it. And now have a look. And in here you can see the class of all these objects. It means that for example the first one has a type of string, integer, integer, float, boolean, nontype. Great. All right. And you're going to learn what we can do with all of that during the course as we need to learn a little
20:30
Speaker A
bit more concepts first to actually make it somewhat usable. But for now, I want to actually make a simple exercise to show you why do we need to store all data in variables and what's the benefit of using it. So let me just clean up here a little with controll slash. I'm going to comment it out. Then again I'm going to copy this piece right here so I can create a kind of new section. And I'm going to write here simple exercise. Maybe let's add here an emoji. And we're
20:59
Speaker A
going to flex the biceps. So for this one, I need to paste here a little story. So I'm going to paste it here. And you can see it's just a bunch of print statements. Let me print this. You can see once upon a time there was a Revit user named Eric. Eric wasted month and yada yada yada. So here we have this story and you'll notice that Eric kind of mentions multiple times here. Then also the app name mentioned multiple times here. And let's say that somewhere
21:24
Speaker A
along the story you decide to change the narrative. Instead of Eric, I want to call it maybe Josh, Bob, whatever. So one way would be kind of go through and say, okay, the Bob here, then Bob here, here. And this is very inefficient way to change anything in your code because you can miss one of the instances and that can lead to errors. So let's Ctrl + Z a few times. Instead of that, we can replace it with a variable. To do that, let's call here variable user called
21:52
Speaker A
Eric. And also we're going to call app called Revit because that's what I use, but it can be anything, Blender or Photoshop, whatever you want. Now the task now is we need to take this variable user and we need to insert it into our string. There are multiple ways to do that. Let's start with very simple one. One way would be is that we can join two strings together. We just take two pieces of text, put a plus sign, and then it's going to join. So we're going
22:17
Speaker A
to write here plus and user. Obviously we're going to remove that one. Let's make one here as well. User plus and then remove that one. So this is going to print as one single statement where we join both of them. Let's try that. Run and see the story stays the same. Now I'm going to change it to Bob. And let's change to maybe something like Blender. Right click. Run. You can see now it says Bob and Bob. So now we need to go and change it everywhere and it's
22:47
Speaker A
best to kind of do it in the beginning when you know something is reusable but that's okay to fix it later as well. Now the next one you can see it's in the middle it might be tricky but actually we just make string here string here and we add user in the middle and obviously plus and plus because we can add as many strings as we want. Let's print it until one day Bob said enough. And notice that there are no spaces because you have to include space here and here because
23:13
Speaker A
space is a character and Python treats it like one. So click on run. Now it's looking better. But also this is not the most efficient way to reuse your variables inside of strings like this. So I want to introduce you to another really important concept in Python called string formatting. And it's just like it sounds. We're going to format our strings. So let me go back a bit. Listen, that's also fine to use it this way, but that's not very efficient. And there are two ways to kind of reuse your
23:39
Speaker A
variables inside of strings. Let me just add here a little description because first I want to explain with this example, then we're going to come back to the story. So there are two different ways to do that because before Python 3.6.6, we had to use format method and it's available in newer versions as well. But this is how we had to do this. We write our string. We create sort of like a placeholder inside the string and then we apply the format method. If you're going to learn more
24:07
Speaker A
about methods, don't worry for now. Just follow along as you see it on the screen. What it does, here's the placeholder. Here's the value. And it's just going to replace it right here. In your version of Python, and all of you are going to use it, unless you're going to go in Ravit, for example, we can use this kind of F strings because there's F in the beginning and it stands for formatted string. This way we can make this placeholder and we can already kind
24:31
Speaker A
of insert a variable inside. You can see it's in white. It's not green anymore because green is text and white is variable. And this is exactly what I'm going to use. However, if you follow my tutorials for Revit, then this is the way you have to use because inside of certain softwares, we use all the versions of Python for different reasons. I'm not going to go in details why. Just know that's if you're using older Python version, this is the way to do it. But
24:56
Speaker A
now let's use the newer version. So first of all, let's go and make it f strings everywhere. And I'm going to replace. So this one is supposed to be user. Let's copy that one. And here, here, here, and here. And then this rivet. This one is supposed to be app. So copy that one. And let's see where else was it used here. Here. And I think that's it. Yeah, I don't see anymore. So that's fine. And now let's test our story. So right click run. You can see once upon a time there was a Blender
25:31
Speaker A
user named Bob. This time about Claus working in Photoshop. So right click run. Now you can see in the story it says once upon a time there was a Photoshop user named Claus and yada yada yada. You get the point. Now we can reuse this variable and we have a single control for these values and it's very easy to change the story narrative. And I know this is just six print statements, but imagine if you're writing a book and you need to keep these variables. I doubt you're going to
25:55
Speaker A
write book in Python, but just something to keep in mind. All right. And these are the first things that you need to understand about syntax rules in Python. We have a basic data types like strings, integers, floats, booleans, and nype. And to define our data, we need to store it inside variables so we can reuse them across our code. And now it's time for you to practice. It's not enough for you to just sit there and watch me writing all the code for you. you actually need
26:19
Speaker A
to start writing the code yourself, even if it's as simple as it is. This way, you're going to make a few errors and you're going to learn from them along the way as well, or at least you're going to kind of build it into your fingers how to write the code. So, for now, I want you to create a few variables, assign different data types, try to print it, maybe use type function, and as a bonus point, try to use formatted strings to reuse your variables. We're going to use them
26:42
Speaker A
during the course quite a lot, so it's really good skill to have. And now I want to wish you happy coding and I'm going to see you very soon in the next lesson.
26:54
Speaker A
Hi, welcome back. You've just learned about basic data types in Python and how to store them inside of variables. And now let's practice by creating a simple calculator. We're going to ask user for two numbers. Then we're going to add them together. And I know this sounds ridiculously simple, but it can actually be tricky because we need to look into data types and how to work with them. But don't worry, I'm going to explain everything as we code. Let's just dive
27:13
Speaker A
in. So to begin, we need to ask for the input and luckily there's a built-in function called input and we need to use parenthesis because this is a built-in function meaning that we need to call the name of the function and inside we need to provide an argument. In this case the argument is going to be the question which we're going to show to the user. We're going to write here enter first number. So this function is going to return you a string. Therefore we need to provide
27:39
Speaker A
here a variable name. So I'm going to call my number A and this is going to be enter the first number. Let's also make like this. Now with Ctrl D I'm going to duplicate it and I'm going to make here number B. We're going to write enter second number. So with this we're going to get number A and B and I want to make a total by taking number A and adding it to number B. All right. And then we're just going to print this total. Great. So let's right click and actually
28:10
Speaker A
see what's going to happen. So when you use your input and you run it, you'll notice that inside your console right here, it's kind of stopped here. Enter the first number. And we can click right here. We can type something. For example, the first number is going to be two. Now it proceeds to the next line of code. Now it asks enter second number. Second number, let's give it three. And obviously the answer is 23. Wait, what? Hold on a second. We took 2 + 3 = 23.
28:38
Speaker A
Something is not right here. So let's try to understand what's going on. Obviously it took this one and this one and it just added them not like in math but literally just put them together. This is not what we want. And the reason might be is because this one is not a number. Let's see what it is. I'm going to write here print. Then I'm going to use type. So notice in here I put a function inside another function. That's also okay. And in type I'm going to write number A.
29:06
Speaker A
Then with Ctrl D going to duplicate and put here number B. Now let's again right click click here on run. Now we need to provide numbers. Again I'm going to provide two. Then I'm going to provide three. And now look at that. I can see right here that the first one is a string. Second one is a string. So in this case we providing two strings together. So I could run it again and it means that I can also write here AB then maybe C D and it's going to return me AB CD. So it's just literally
29:35
Speaker A
adding to text. So we need to do something about this. First of all, it's really good to mark it as this is a string of number A and number B. And now to do the math correctly, we need it to be a number. Here's how to do this. So let's select this print statements with control and division slash. I'm going to comment it out. I can remove it completely. All right. And now in here I'm going to write the following. Number A is going to be integer and I'm going
30:00
Speaker A
to provide string of number a. So what's happening here is I'm taking this string right here and I provide it inside of my function and the function is the integer. It's kind of the built-in function which creates this integers. So I'm going to convert string to an integer. Now let's do the same for the B. Duplicate it. Rename this one and this one to B. And now we're going to try to use them. So we're going to try to create total with these numbers. So I'm going to right click and click on
30:28
Speaker A
run. This time we're going to enter. The first number is two. The second number is three and now we get five. So this time we actually work with numbers and therefore Python knows okay 2 + 3 is equals 5 not 23. Now let's run it again and try something different. What if this time I'm going to provide here 2 and 1/2 + 2 and 1/2 right? Oh I get an error and don't worry it's okay to see errors in programming. tells you here it's a value error and then there is like description invalid
30:58
Speaker A
literal for integer which is the function with the base 10 and you can see here the value we provided. So obviously integer it needs integer values right 1 2 3 2 and a half cannot become really an integer. Obviously you could floor it but at the moment it's still text. It's not a number to floor it. So instead instead of integer we're going to use here float as remember this is a floating decimal point. This is like numbers with decimals. Now let's try it again. This time I'm
31:26
Speaker A
going to try 2 and a half plus I don't know 325. Okay, now it understands it because now we're telling this take this string and actually convert it into a floating number. Now let's run it one more time and you'll notice what if we're going to write a + 5. This time it tells you could not convert string to float a still have to be very logical. If you take in a text, try to make a number out of it. You need to make sure that your text is actually a number. It's more like a logic inside
31:56
Speaker A
your code and not like in general. But there are also ways to avoid it with error handling. But I think you're not ready for that. We're going to talk about this later. For now, just know that you need to provide correct numbers which can be converted. And the next point here I want to make, let me just make a quick comment, is that there are the following data type functions. We could use string, we could use float, we could use integer, boolean. These are the base ones. Then
32:23
Speaker A
there's also list tpple. Uh what was it? Set dictionary. Right? These are your default data types that you need to know about. And you can also use it to convert other numbers between each other as long as it makes sense. So in this case, I just want to give you the names. So I'm going to select everything and comment it out because I don't want to get any errors by using it like this. And now I just want to briefly give you a few more examples and quickly explain them.
32:48
Speaker A
So let's create here some space. And then with window and dot key I can select one of the emojis. I'm going to make this number three. And just to save a bit of time I'm just going to be pasting here examples that I already prepared. And now first of all numbers you can define a number like for example 10. Then you convert this number into a string. You can also convert it into a float. Then it's going to become 10.0 zero instead of 10. You could also make a boolean. And in boolean when you
33:15
Speaker A
use boolean if you have any number positive or negative that's true. If it's a zero then it's false. Right? You can see it here. This one becomes false. All right. Then with strings again we can define a string and if it's possible we can convert it into integer into float. Also we can convert it into boolean if the string has anything. And in if it's an empty string meaning there's not even a space then it's going to return you false saying that this string is empty
33:45
Speaker A
then let's convert here quickly a boolean you can see that we can get the boolean true if you get for example one zero is going to give you false and the same also can be applied to collections if we provide empty collection like a list or dictionary or set it's going to return you false because it's empty there is nothing however if we're going to provide boolean to an object that has some data like strings like uh some number maybe something inside the list,
34:09
Speaker A
then it's going to return you true. And also, funny enough, if your empty list contains other empty lists, it's going to return you true because it contains something. It contains other elements even though they are empty, right? But let's not go that far ahead. That might confuse you a bit. All right. And now you know how to kind of take your data and convert it between different types. Can get it as a string, then convert it into an integer of float, vice versa,
34:34
Speaker A
and so on. And it's really really important to know so that we can convert the data so we can actually do the math correctly because otherwise we adding numbers like text and it g gives us completely wrong answers. And now I want you to follow along this lesson and do it as a homework and try to experiment yourself. You see in programming you're going to learn the most by practicing. You're not going to learn a lot of programming by just watching others code for you. You need to code yourself
34:58
Speaker A
because you're going to misspell something. You're going to run. you're going to get an error and you're going to learn how to solve these errors because remember programming is mostly about problem solving not about writing the code. Code is just a tool we use to solve problems. So I'm going to leave you to this homework and once you're done I'll be waiting for you in the next lesson where we're going to talk about the next Python concept. So happy coding. See you soon.
35:27
Speaker A
So you know how to create variables and how to store simple data types in Python. But what if you want to store more than a single text or numeric value? Well, then you have to use collection data types. We're going to talk about lists, tpples, sets, and dictionaries. And these are the main ones. We're going to cover all of them during this lesson, but keep in mind that you're going to work with lists in 90% of all cases, if not 99. So we will focus the most attention on them during
35:50
Speaker A
this lesson. We'll also cover the other types so you understand how and when to use them. But again, we're going to focus the most on lists because this is the most important you need to understand. Right now, let's actually jump in and begin this lesson. So, I created here a simple table to explain you the main differences because you can see there are lists, tpples, sets and dictionaries. And just going to briefly mention that for example lists they are mutable ordered collections. Mutable
36:11
Speaker A
means that you can make changes and immutable means that you cannot make changes. So, list are mutable ordered collection. Tpple is immutable ordered collection. set is unique mutable or unordered collection adding dictionary is mapped mutable ordered collection and also notice here's an asterisk because uh before Python 3.7 dictionaries were not ordered but it's not so relevant because it's first of all mapped collection but we're going to cover all of them don't worry let's start with our
36:37
Speaker A
like absolute champion lists list is mutable ordered collection we're going to start with absolute basics so let's write here syntax and first of all to create an empty list we can either put the square brackets or we could also Write list like this. This will also create an empty list. Let's keep it like this for now. And to create a list with actual data. For example, I'm going to call it my list. We need to create our square brackets. And then inside we can
37:00
Speaker A
write all the items. For example, I'm going to write wall, floor, uh roof, maybe also ceiling. And notice that I put there four different items. And all of them have to be separated with a comma. It's really, really important. It's okay to put comma in the end. It's not going to hurt, but it's not necessary. And also keep in mind that it's not only strings that we can store them. We can also put their different numbers. We can put their floats, can put their true or false booleans, none
37:25
Speaker A
type, and we can even put other lists with other data. For this lesson, let's keep it simple. Just know that any kind of type of data can go into list, but we're going to just keep it just a few strings so it's a bit easier. Now, also, if you're going to print your list in the console, you'll notice that it's also going to be shown here with the square brackets. Then you know it's a list. Now, since it's a collection of data, we can also get data out of it. And now let's have a look how to get
37:47
Speaker A
single list item. I'm going to zoom a little so we can see it better. And here's the thing. To get any items from the list, you need to put here another square brackets. This is going to indicate the index at which you want to get the item. And let's say two. And here's another thing. If I'm going to run it, you can notice that I'm getting the roof. And you might get a little bit confused because you can see that well 1 2 3 the roof is the third one. However,
38:09
Speaker A
in programming, we start counting from zero. It's always going to be the first item is going to be zero 1 2 3. Therefore, when you look here for the tool, you're getting actually the roof and not the floor. And so this is how pretty much all programming languages works. And you will see it all the time. So get used to counting from zero instead of one. Then there is also another thing that we can get uh items from the end. We can write here minus one or maybe minus2. And then in this
38:34
Speaker A
case we're going to count here from minus1 -2 -3 -4. So this one is going to get us ceiling. This one is going to get us roof. Now let's just run and test it. Ceiling, roof, everything exactly correct. Before go any further, let's add some comments and maybe also let's print the first one. So it's going to be the first item. Now we run and we get wall, roof, ceiling, roof exactly like we supposed to. Another thing you need to know is that sometimes you might try
38:58
Speaker A
to get items outside of the list scope. Let's try to get the hundreds item which is obviously not there. Run it and you're going to get an error. Index error. List index is out of range. And this is okay to see errors. Don't worry. It just tells you exactly what's going on. This list doesn't have so many items. So probably you're doing something wrong. And in future I'm going to show you how to work with logic so we can avoid any errors like this. But just know that well you're going to get an
39:20
Speaker A
error if you try to do something like this. And this is in general how to get single items from a list. Let's select everything and with control and a forward slash going to comment it total all out. And now I want to show you how to get multiple items a slice right a piece of the list. I'm also going to copy that one and move it down. Now, to get a slice of a list, we're also going to use here the square brackets. But this time, we're going to put here a colon, a number two like this, or maybe
39:47
Speaker A
like this. Or we can also do it like this. And I'm going to cover exactly what's going on in just a second. Let's just run it. So, we see results. And I'm going to explain how it works. So, you can notice that on this that one we're getting wall and floor. This is because we are telling we want to get all items until the second one. So, 0 1 2 3. If we say until the second one, it's going to go until here excluding the second item. So, we're only going to get wall and
40:11
Speaker A
floor. In the second case, we say we want to get items from the second one including. So, it's going to start from here and then take all the items in the list starting from this point. Therefore, we get roof and ceiling. And in the last one, we combine it. We say that we want to start getting items from number one until items number two excluding it. So, two is excluding and one is including. Therefore, we only get floors. So if I'm going to put here for example three and run it, you can see
40:37
Speaker A
that now I'm getting floor and roof and so on. And it's actually very commonly used when you work with lists. So this one is going to get you items until the second one excluding. Then this one is going to get you items from second one including and then this one is going to get you items from the first including until the third excluding. Right? So far so simple. Now let's continue. That's not all we can do. We can also specify that we want to get items with a certain
41:00
Speaker A
step. But that we're going to put two uh columns and right here too. We could also write here that I want to get from first until the third one. Ideally, I would need more items. Let me just copy that. And I'm going to just going to kind of increase the list. I'm just going to add the same items. That's okay. Just so I can show you the last one how it actually works. Going to get items. Let's get it from two until six. And it we're going to do in the step of two. So now when we run
41:28
Speaker A
it, let's see what's going on. In the first one, we just say that we want to change the step of two. So we take wall, roof, wall, roof. Every second item is going to be taken. And here, first of all, we say we want to take from the second item. So 0 1 2 including until six item. So three, four, five, six excluding. And then we want to take every second item. So it's going to be roof and wall. That's therefore we only get two items. If we would extend it to seven items, we would also get another
41:59
Speaker A
roof. So in here, we're going to get every second item. Let me just comment down these ones. And there's a really cool trick as well that we can take our list. I can use this kind of step order with minus one. And this is a trick to reverse your list because now have a look. I'm going to run it. It's going to be ceiling, roof, and so on. But we start from wall actually. So it reverse the order from back to forward. It's really cool. And that's pretty much how you get
42:22
Speaker A
different slices. To be honest, it's not very commonly used. maybe just the reversal thing, but these ones are quite often used. And I actually want to show you a quick example as well. And imagine that this list represents one column in your Excel. And the first item here is going to be like categories, right? This is the header of your uh column, which is usually bold and bigger text. And these are all the data items. You might want to separate your header by getting
42:45
Speaker A
your list first item, which is going to be these categories. And then you can get the rest of the data by using my list. And then I want to get from the first item onwards. This one is zero, one and so on. So zero is going to go right here. And then first item and all of these are going to go inside of data. So we separate the beginning and the end of the list and then we can print our header and then we can print our data. Obviously I'm keeping it very simple but in Excel
43:12
Speaker A
case for example you could make your header bold and you know how it's going, right? And here's the categories, the first item and then the list of the rest of the data. This is just one of the examples to illustrate you why slicing is important and that you actually will use it more often than you think. Now let's move to another example and this time I want to talk a little bit about membership operators. I'm not going to go in depth on them because this is
43:32
Speaker A
necessary for logic and we're going to cover them more in operators lesson and in logic lesson but nevertheless when you have a list of data you can verify if something is inside or not. For example, I can write print if wall uh in my list or maybe I want to write if I don't know uh door not in my list. I can print these two statements and it's just logically if wall in my list which is correct I get true. If door not in my list also true it's correct statement. And also keep in
44:02
Speaker A
mind that lowerase and uppercase letters they're not the same. And if you run it, you'll notice here is false because there is no such item with a lower case. In this case, you would first need to modify your list to make all items lowerase and then you would kind of check it. But again, this is something for another lesson. I'm just briefly letting you know that we can check logically if there is something inside. Now the next I want to have a look at popular functions with list and function
44:25
Speaker A
as you know is just some kind of action in Python. For example, we can use print to print the message in the console. We can use type to kind of display the type of different objects. Can put here like my list. And I just want to show you a few popular functions for working with lists. For example, first of all, we can print the length of our lists. And this is just going to return how many items there. There is one, two, three, four. The answer is four correctly. We can
44:50
Speaker A
also create a new sorted list. And if we print it, you'll see that all items going to go in alphabetical order. So ceiling, floor, roof, wall, right? very simple. Then another thing is more for the numbers. So let's create here a new list for the numbers. I'm just going to put there 10, 20, 30, 40, 50. And we could also kind of sum all the numbers together. We can also get what's the minimum numbers and we can also find what is the maximum uh number in this list. So if I'm going to print it and
45:18
Speaker A
then you can see the sum of numbers is going to be 150. Then the minimum number is going to be 10 and the maximum number is going to be 50. And obviously these are very important when you work with numbers. And listen, there are many more functions and we're going to cover absolutely all of them in another lesson. I just want to show you the most used for lists. And before going to continue, I'm also going to add here the comment so you can see what is going on
45:38
Speaker A
where. And again, we're going to select it and comment it out. And now let's go to the next point. And now we're going to talk about list methods which is built-in functionality. And you see functions they stand on itself. There is like first you're going to write print, then you do something, maybe you make sum, you do something, and so on. Methods on the other hand is something related to your data types. So you have a list and inside this list if you're going to write my list do that you're
46:00
Speaker A
going to get all these auto suggestions and these are the methods these are the actions that are coming from inside your object you can see for example lists they have clear count index copy pop remove extend sort reverse append insert methods there are I think 12 of them in this list you will also notice this dunder methods and dunder stands for this double underscore therefore dunder double underscore dunder and this is a way in kind of python to define that you
46:25
Speaker A
should not touch it this is for internal meaning and all of them have some special meaning related to list. You don't have to worry about them. Just imagine they are not there. All you care about are these ones on the top. Now, we're going to go quickly through all these methods. There are not so many of them. So, I think it's worth just quickly covering them. But again, there's going to be a whole lesson of the methods during this course as well. So, don't worry if something is a bit
46:45
Speaker A
confusing. Going to cover it again. For example, here we have our list. The first absolute thing you need to know is the append. This is how you add other items to your list. For example, in here I want to add door and duplicate window. By the way, you duplicate with Ctrl D in case I haven't mentioned. Now let's print uh my list and you can see now we have door and window in the end. Another important thing notice that I'm just taking the existing variable of a list and I
47:12
Speaker A
modified by applying this append. I don't have to write here my list equals my list because then it would kind of break your code. It's it's just going to return you none. So let's go back and this time it works. You could also write here my list plus equals another list for example door and window that also works. This is joining two lists. So if I'm going to do that can see that well door window door window because I'm adding it two times very similar. Since we're talking about
47:41
Speaker A
joining and all of that we also have to cover my list uh extent. This is exactly the same thing that's happening here. This is add another list. If I'm going to let's just create here my list two. So to keep it simple I'm going to put it here and here. So this is exactly the same thing. It joins two lists together. But this one it just adds one single item. So let's write here add single item. Then let's remove this one. We already have it. And then uh join two lists.
48:08
Speaker A
And obviously now if we're going to run it can see door window door window because we adding it two times. So far it's clear. Let's comment it out and go to the next example. And next we're going to look at the sort. We can provide their arguments but we can only run like this then it's going to sort itself. You can see now ceiling floor floor roof and also at any case if you need any help with any methods in PyCharm you can kind of click on this method anywhere with control q
48:34
Speaker A
you can get this dock string bend object to the end of the list takes objects extend extends list by appending elements from the iterable. Terrible means like a collection. Then sort you can see inside it takes a few arguments and it gives you description what's going on. You can get a additional help like this. So let's comment it out. Add here a little comment and go to the next one. So next is going to be my list. And the next how do we count different items. Let's count
48:59
Speaker A
how many times door is mentioned in the list. This is going to return you a number obviously. So we're going to print this number. Now let's run it and can see zero. There is not a single instance of door inside of here. However, if I'm going to kind of add these two and see that now inside I have door two times. Therefore, we get number two. So, we can count how many times certain things inside your list. Very often used. Another thing you can also check where what is the location where this
49:25
Speaker A
item is located. I'm going to write my list index then say where is the I don't know uh wall. Let's run it. You can see zero because it's at the index of zero. Remember we count from zero. Now let's try roof. Try to guess it. 3 2 1 and it's number two because again 0 one two therefore we get roof and then index two and one of the reasons you might need to get the index is so you know how to insert items in your list and then there is insert so with control q we can verify first we
49:56
Speaker A
need provide index then the object let's say that the index two I want to provide here I don't know sofa now notice that it's zero one then at index to its new item sofa and then everything rest was going to move to the next index. So, it's three, four, five, and so on. All right. Now, let me add a few comments and we're going to go to the next one. So, the next one is going to be let's look at the remove. Since we know how to add items, we can also remove items. For example, we're going
50:23
Speaker A
to try to remove I don't know floor. Now, let's run it. And you can see now we still have wall, roof, ceiling, floor because actually here floor is two times. But if I'm going to remove that one, can see now it's only one time. Or actually I could just leave it here twice but remove it two times like this. But if you remove way too many times you will get an error saying that list remove X not in the list referring that X is whatever item you provided here. So
50:50
Speaker A
be aware of that is when you kind of try to do something and it's not possible you might get an error. This is why you also will need to learn how to use logic. So you can check if something is not inside don't do this. If something is inside do this but we cover it another lesson. All right. Now let's go to the next one and we can cover the pop which is an interesting one. This way it's going to uh it's going to remove and return the item at the index. All default is the last one. So it means
51:15
Speaker A
that it's going to remove an item. So I can write here item equals and I'm going to print it. So if I do that and what's happening here is we're removing the last item floor and it's going to be added to the new variable. I'm going to say two. I'm supposed to remove roof and add it to another variable. So let's try it. Now you can see there is no roof in this list. And now roof is added right here to this item here. This way you can get items out of your list and do something with
51:44
Speaker A
them because when you do that using uh my list tool, you're just getting a copy of the item. The item still stays inside of there. Now the next method is uh reverse. You can just write it like this. Reverse. And it's going to reverse your list. And this is pretty much the same as if you would uh make it like this. This way you have to also print it here because you can see you're going to reverse it but then it's going to still uh leave the first one intact. So it's
52:11
Speaker A
not going to change the original. So here's the difference. Now again I'm forgetting the comments. So let me quickly add it. And we are almost done with methods and list in general. But let's look at the next one. Next one is going to be clear. This way you're going to remove everything inside your list. You can see it becomes empty to be honest. Instead of using the clear, I like to just say my list equals a new empty list. Exactly the same result, way cleaner syntax in my opinion. This is
52:35
Speaker A
going to clear all items from list. This is way better approach to do that. And I think there is the last one we need to talk about copy. And this one is actually quite important because remember we have our list right here. So we could say, you know what, x equals my list. And you might think that's a copy as well, but there is going to be a difference. So I'm going to write here copy. So to beginners, this looks exactly the same here. I'm just kind of taking the copy of the list. This one is
53:03
Speaker A
creating a copy, but it's not the case. Let's take my list append. And I'm going to append there some new item. Doesn't matter what it is. And I'm going to print X and Y. So I'm not even printing my list. Just want to show you something here. I'm saying that X equals my list. And it's not a copy. It's a reference. I'm creating X as a reference to my list. So they are pretty much referring to the same collection of data. So you can see here wall and so on. And
53:28
Speaker A
therefore when I make changes to the my list X also got this changes because they are related. My list is equal to X. But Y is independent copy and it's not going to change no matter what you do with my list because you have to modify Y to make changes. Just know that Y is going to create independent copy of a list. Very important. All right guys, I think we covered all the methods. This is the built-in functionality of the lists which comes with it and you can use anything
53:57
Speaker A
whenever you need it. Now we are almost done with the lists. Let me just see if I mentioned no I have not. This is how to replace items. If you want to replace items, uh let's just get the list here. I'm going to make it a bit smaller. Let's say that instead of floor here, I want to say something else like new item or whatever. I can say here my list. Then specify the item index. Let's for example two. So it's 0 1 2. I'm going to say it should actually equal new item.
54:28
Speaker A
And then if I'm going to print my list, you're going to see that I'm going to get the list and I'm just going to replace the item. So this way I say that index two is actually supposed to be that one. Or maybe my list the last item should be last item. Now if I'm going to print it, you can see this is the last item. We could also replace slices. So we could say from one to three, one including, three excluding should be a list of AB and see it works, but it's
54:57
Speaker A
not something that you would actually use. It's just possible. But I never used it like this, trust me. But this one sometimes I had to use it. Okay, so just wanted to show you that you can also replace items in your list. Completely forgot this part. Now we are almost done with list. Let's just scroll down and I want to talk a little bit about nested list because as I told you you can put the list inside the list. I'm just going to explain how it works. So you can see we created a
55:21
Speaker A
list and we put the list inside. We don't have to put it on new line all the time. We can put whatever here. That's because we have start and end here. So theoretically if you do something like this it's not an error but it's horrible way to write your code. And usually when it's very short that's fine to do it like this. However, when you work with nested lists, it's very common to separate it on new lines like this. Now, let's say that you want to get one of the points.
55:46
Speaker A
This just represents XYZ. Just very simple. Now, let's say that I want to get the second item from the points. And as you remember, we count from zero. So, I'm going to write 2 equals one because this is index one. Now, we can print points and we can print our point two. You can see we're getting here the full list of lists and here we're getting exact list which is this one right here. This is good. But you can also print inside that list. For example, you can
56:10
Speaker A
say I want to the first item is going to be X, which is two. And maybe we just want to make X, Y, and Z. It's going to be the first, the second, and the third item. And I know it's confusing because we come from zero. Just get used to that. So we're going to get it like this. 2 0. We kind of get all the values outside of it. But we could also say, you know what, I want to print points. Give me the second item. And I want to read the xcoordinate. This also going to
56:35
Speaker A
work. This is going to be two. I'm going to put here two. This is going to be uh so this is going to be this value right here because one is going to be first of all we're getting this list and then right here two is because we're getting this item inside that list. So this is how nested list works. Again don't overthink it. You will get used to that as you kind of start working more with list. For now I just want to mention that this is how it works. All right and we are this close to finishing
57:01
Speaker A
with list. There's just one more concept and I'm going to go ahead of myself a little bit because I think it's worth mentioning loops. We'll cover loops in more depth in another lesson because it's very important and we need to talk about this, right? I just want to mention that when you work with list, you definitely want to try loops because here is your list which is just list of items. Let's say you want to do something with each item individually. You would create a loop like for I in my
57:23
Speaker A
list print I and here is what's going on. I'm just going to go very briefly. We're going to have the whole lesson on loops later on. For is going to start the loop. This is a special keyword. Then you say I. This is the name of the variable which going to take each uh item one by one on that in a moment. And then we have to point to a list or other iterable object something we can iterate over. So in this case it just we point we want to kind of make a loop for this list and
57:50
Speaker A
this I it's going to be a variable name for each item. At first round it's going to be a wall. On the second round of cycle it's going to be floor. Then on third one it's going to be roof and ceiling. And then it's going to end the loop. So whatever you write here is going to be executed on each item one by one. So if I'm going to do something like this and run it, I'm going to print each item in the list three times. Wall wall floor for floor and so on. This is
58:13
Speaker A
the idea of the loop. Again, not going to go into loops yet. We're not ready. We still need to cover a lot, right? We need to learn about operators, logic, and then eventually we're going to go in loops, and you're going to absolutely love it. And that's it about lists. I know this is a lot, but I really covered pretty much everything you can do with a list. And you don't need to know everything at once, but you will get used to all of that. Remember, you're going to use it in 90% of cases. So,
58:36
Speaker A
list are the most important. Spend the most time on that one. But we also going to cover all the other data types starting with tpples. And tpple is immutable ordered collection. And tpples are pretty much the same as lists. The biggest difference between them, you can make changes with lists. You cannot make changes to tpples. That's the only thing. Because of that, tpples are more limited in functionality, but they also much faster. And usually it doesn't
58:58
Speaker A
matter. But when you work with huge data sets of like coordinates, financial data where you work like with millions or billions of different rows, well, tpples make a lot of sense. But until then, just use list and don't care about tpples. That's at least my approach when I started and I still follow that. Now, to create an empty tpple, we're going to create it with a parenthesis or we can also write here just like this. That also works. So, let's make it a comment and leave it like this. Let me
59:26
Speaker A
actually insert here a few examples. And again here's the list of data. Then there's the same one of tpple of data. You can also make nested tpples just for you to see the examples because when you're going to uh print data or list data you'll notice that in the console it's going to be very obvious. Square brackets means list. This one means tpples. That's it. Overall they work very very similarly. Now we're going to go very briefly through tpples because they have a lot of functionality of the
59:52
Speaker A
list. If you want to get a single item it's exactly the same. Also start from zero and you get items like this. you run it. You can see here a bunch of items. I don't think we have to cover it anymore. However, remember that you cannot change values in your tpples. If I'm going to try to execute this piece, you can see I'm going to get an error. Tpple object does not support item assignment because this is kind of assignment of the item inside the tpple. But we cannot change anything in tpples.
60:16
Speaker A
Just know that it's not possible. You can also get multiple items from a tpple. So if you're going to run like this, you can see you're going to get different uh slices. Let's see. So this is our list of data. Let's actually put it down so it's easier to kind of see what's going on. Now here we're getting until the second item. So it's 01. That's correct. Then here we're going to get from the second item including. So it's going to be like this. Then we say from the first one
60:42
Speaker A
until the third one excluding like this. So it's one two. Pretty much the same as before. Then let's briefly look at popular functions with tpples. Again I'm going to put here the data. We can also get the length of the data, find the minimum, the maximum value, sum it up. We can also create sorted. However, when you use sort it, it's going to create a new sorted list. It's not going to be tpple anymore. It's going to convert it into a list and it's going to be sorted.
61:08
Speaker A
But if you want to bring it back into a tpple, you can sort it into a list and then create a tpple out of it. You can kind of change your data types from back and forth from list to tpple to set to dictionary. Well, not dictionary, but you will see how it works in a moment. And you can see here, this is the length. There are 12 items. minimum item zero, maximum item five, and sum of number 30. This is the new created sorted list. And this is converted back into a tpple. Notice parenthesis and
61:32
Speaker A
square brackets. That's the functions of the tpples pretty much all the same things. And now for the methods, you want to write here data. Notice there are only two methods, count and index. And they work exactly the same like uh in lists. Count how many times number three and data index where is the three? I also need to print it so I can see it. And now if I'm going to run like this, you can see that uh there are two times number three here and here. And three is
62:00
Speaker A
at the index 0 1 2 3. It's actually at the same index as the number. And that's it about methods. There is nothing special about them. Again, you're not going to use tpples really for any functionality. It's just to store data and use it later. You can always convert tpple into list, but just stick with the list. That's much simpler. And you can also iterate, create membership operators. Again, I'm not going to cover it. Tapples can do pretty much all of these things that list does, but except
62:24
Speaker A
this additional functionality in methods. That's it. So now we can go to the next one. We're going to talk about sets which is an ordered unique collection and focus on unique. This is what sets it apart from anything else. So to create an empty set need to write set like this. And if you want to make a set of items for example, then you would put curly braces and then you put your items like 10, 20, 30. And the thing is that you cannot really define a empty set
62:51
Speaker A
like this because this is going to be a dictionary. So dictionaries and set they use the same symbols. But I'm going to show you why this different happens in a moment. But now just know this is how you create set with data. This is how you create empty set. And later you can add their items. And now we're going to cover the most important part about sets. And we just need to print that set. And notice what happens. Do you see any differences? And you'll notice that in here not a
63:16
Speaker A
single item is repeated. Everything is unique. Here I have for example 10 10 ab true true true. So I repeated 2020 20. So I repeated a lot of things but now here it's only everything once. This is the point about sets. It's the way to kind of compress your list and taples into a single uh container of unique items. This is why pretty much why you use set to find unique items or compare two different sets of items. More on that in a moment. Now let's have a little bit more practical example. I'm
63:46
Speaker A
going to show you how to convert lists and tpples between each other. So let's say you have a list of data and you know that there might be some duplicates and you want to get rid of them. So you're going to just create a set from existing list of data. This is the thing when you work with list tpples and sets you can interchange like their data types. You take your list and put it in here in the constructor of set. You could also bring it back to the list of data or maybe
64:09
Speaker A
unique list of data. That will be list and it's going to be set data. What's happening here? This one goes in here to become set. And then since this is a set, it goes here to become a list. What's important is if you want to print list data, uh set data and unique set data. When I run it, have a look. This is our original list. Here we convert it into a set. Then we convert it back into a list. Now we lost all the duplicates. This is one of the ways of using sets.
64:38
Speaker A
And another important thing, I don't know if you noticed or not, but here when we print our sets, it's always going to have different order. Not always, but it's often going to have different order. So false true, then ba, then it's AB, then here it's 20. You see, it just kind of randomly changes its position. It's unpredictable. So just don't rely on the order in your sets. Now, let's speed up a little and have a look at the set methods. And this is, as I mentioned, built-in
65:04
Speaker A
functionality. And this time I'm just going to paste all the kind of built-in functionality that you need to know about so we can quickly cover it. Here's the set of data. And now we can obviously create a copy of the set. Very clear. We can also remove and return random element. Remember how we kind of use the pop on the lists. It works exactly the same. Can also clear it to remove everything. Again, I would create just a new set. Can remove certain element and you're going to get an error
65:30
Speaker A
if it's missing. However, there's also discard. It's also going to remove an element, but there is no errors if it's missing. And this one is quite useful. You can also add single item to your set. This one is very very straightforward. But then I want to show you something another really important because if I'm going to take items, I'm going to have a look. There are way more methods and you can see there is a is subset difference intersection and so on. This a comparison methods to find
65:55
Speaker A
difference between two different sets. We covered all the regular ones quickly and now I'm just going to again paste it so we can quickly cover and I'm going to explain what's going on. Let's imagine you have two sets. 1 2 3 4 3 4 5 6. There are few items repeating in both of them and there are unique ones. And now let's just print and go over each one of them so you understand what's going on. The first one we're going to join two different sets. Now we're going to get
66:19
Speaker A
one two three four five six. Obviously we're going to get rid of duplicates. Second one is intersection. This is kind of this boolean things. We're going to find items in both sets. So three and four exist in both of them. Can also create a difference. In this case, we're going to run it on a set. So we're going to set a items that are missing in the set B. So one two is not present in the B set. But if you want to get overall difference, you need to use symmetric difference. This way you're
66:44
Speaker A
going to get everything that's not in both of them. So 1 2 and 56 is going to be right here. And then there is a is subset and supererset. And what it says like is set A completely inside set B and does set A contain all elements from set B. So if I would make it something like 34 can see this one is going to be true because this one completely inside of B. So this is subset and if I would do it otherwise this would be the supererset because this one has all the items in the B plus
67:16
Speaker A
something else. Therefore it's kind of bigger version of that set if that makes any sense. All right. And these are the main methods. There are a few more, but I never used them myself. There are this kind of difference and update, intersection, and update. So, it's at the same time you're adding an item and you're also comparing. It's I never used it. All right guys, the last point and it's going to be popular functions can also get length, minimum, maximum, sum,
67:41
Speaker A
and sorted. Exactly the same as tpples and lists. I forgot my dictionary to test it. Now if you execute it, so there are four items, minimum one, maximum four, sum is going to be 10. Here's like sorted list. Remember list sorted creates a new list. Not so useful, but still sometimes you might run it. And again, you can create loops. You can also create different logical checks and so on. We're not going to cover it. Just know it's possible. And that's it about sets. Well, this
68:10
Speaker A
lesson is growing really big, but I decided that I'm going to cover everything I can about list set tpples and dictionaries because well, that's an important topic and you should definitely know really a lot about this. And lastly, let's talk about dictionaries. The last collection data type and it's a mapped ordered mutable collection. So, we can make changes to it. It's also remembers the order after Python 3.6 or 7 something like this. Before that, it was unordered. It's also
68:36
Speaker A
mapped. And mapped is the thing that I need to explain. First of all to create an empty dictionary can either create like this looks like set but I'm going to explain why it's like this or just going to write dictionary for dictionaries you're definitely going to use curly braces this is the common way or just write this now I'm going to create here a dictionary example and this is really important it's a mapped because we need to provide here key then with the column we provide here value
69:03
Speaker A
then on the same line I can either create key two and then value or it's much more common to always keep new pair on a new line. Here's the thing. You can think of dictionaries as the actual dictionary. Think about that. You heard a new wordistration. You would pull up some dictionary, find where it is, then ah, here's the key is going to beration. The value is going to be definition. It's the act of throwing someone out of the window. That's a funny word. I wonder who made that one
69:32
Speaker A
up. But still, this is the thing about dictionaries. you specify the keys related to your values and the best way I can explain it probably is if I'm going to create here a example list there's value one and two but in list you use indexes to kind of count or get items right but in dictionary you don't have these values there's no 0 one instead of that there is key one key2 and it can be anything it can also be here for example uh 01 this way we're pretty much making it a list and then
70:03
Speaker A
later you'll also get the items the same way. So get items you'll write here dictionary example give me the first and second item rerun you're going to get this values only because I specified here the indexes but we have an option instead of using this numbers going to say it's a key one and it's going to be key two this way 01 not going to work because it's not inside there is key error there's no such key as zero but we can look inside and get our key one and key
70:32
Speaker A
two it's like a dictionary we looking for definition of this word, this is the word, this is definition, this is the key, this is the value, this is the administration, this is the act of throwing someone out of the window. This is the thing about dictionaries. It's not that complicated. It's just for a little bit different structure of your data. Not relying on the order, but relying on the names related to your values. Again, this is just a simple syntax, but let me add here another
70:57
Speaker A
example. This is going to make probably more sense than before. Can go also uncomment that one. Let's have a look. Here's a dictionary of data types. I specified here all the keys like integer float string boolean list double blah blah blah blah. And here are all the values, the definitions of these keys. So integer is a whole number like 1 2 3. Float is this and so on so on. Also, it doesn't have to be really a string. It can be anything number. It can be a list of data. It can be another
71:25
Speaker A
dictionary if you really want to and so on. I just keep it strings because it's very simple to understand. Now as I mentioned already if I want to get any of the values I need to take my uh data types and with square brackets I need to specify the name uh of the key. So I want to read what is a dictionary what is maybe a set. There's also alternative syntax instead of using the square brackets you can use get and let's get a I don't know non type. I can see we're going to get all the
71:57
Speaker A
values. So we are reading the definition. So non type then here's the dictionary then there's tpple or whatever we chose and this is just a simple way how to get values but keep in mind that if you're going to try to uh get value that is not existing like missing data you're going to get an error key error which is very common just tell you that something is not existing in the dictionary normally you need to verify if something is inside there however there is a way to
72:23
Speaker A
avoid it by using one of the methods again going to go ahead of myself there is set default It's very stupid name. I absolutely hate it. But it's to get certain key and if it doesn't exist, give me I don't know another one like my value. So this way you're going to try to get the missing and if it doesn't exist, you're going to get my value. But if it exists like integer then you're going to get definition of an integer. Very stupid name set default but just know
72:50
Speaker A
that it's possible. Now next since it's a mutable we can add or modify data. And to for example modify data, we're going to take our data types again with the square brackets. We're going to define what we want to work on this way. We're going to get it. But if you want to change it, we're going to put equal. And what would we set to it? For example, integer is going to be I don't know uh just a number. So print data types run integer is just a number. Also, dictionaries look like this in single
73:20
Speaker A
line. It can be really confusing, but well, it's just a lot of data in one line. Nothing special there. Can also add items. Let's add here a new one. Complex. So if it exists, it's going to override it. If it doesn't exist, it's going to be a new one. And it's going to be these two complex, you know. Now, if I'm going to run it and we're just going to go to the end of the dictionary. Right there is our last pair. Key is going to be complex and the value is going to be it's too complex. Here's
73:50
Speaker A
another value. Here's another value. This is another key. Another key. So we can see how to read it. It's like this. Separated key value. Key value. Okay. We can comment that one out. Let's move to the next one. Quickly going to mention membership operators. You can check if something is inside. But you're going to check keys. So if I'm going to run that one, it's going to be true. True because list is inside dictionary and test is not inside. Right? So this is opposite.
74:17
Speaker A
If I would say is test inside, it would be false because it's not inside. Again, you're going to learn everything you need very soon. Just please be patient. For popular functions, well, there are not so many of them. I pretty much only use length probably with to find how many pairs, right? It's how many keys or like pairs key values. It's not like keys plus values. It's just one or the other. And lastly, I want to show you some methods. These are quite important. There are not so many of
74:44
Speaker A
them. If you're going to uh take data types and have a look like this, you're going to get their values. Pop item keys get from pop, copy, update, clear items, set default. Not so many of them, and we already covered the first one. Most important you need to know is the keys, values, and items. So the first one is going to get you a list of keys, list of values. And this one going to create a list of pairs of key and value as tpples. Now let's print all of that. And it's
75:14
Speaker A
going to look a bit messy, but don't worry. It's also going to say dictionary keys. You can think of it just as a list. It just remembers different data type. But that's okay. We can also add here list so it's less disturbing. But normally you would not convert it into a list. Just treat it as a list. At least that's what I do. Now we can see here is going to be a list of all the keys. Right? If you're going to compare you will see all these items right here.
75:37
Speaker A
Then in the second one it's going to be a list of all the values. So it's going to be these ones. And the last one is going to be uh sort of taking key value and making it a tpple then key value making it a tpple. So you can see uh this is key value number one then key value number two then key value number three and so on. So it just takes keys and values and together. And it's actually very very important when you work with loops. Let me let's just go one step ahead but more on that in a
76:07
Speaker A
second. Let's check a few more methods. So let's see what else we have inside. We already covered quite a lot. We know items, clear is obvious, copy obvious. Update is to merge two dictionaries together. Uh for example, if I have this dictionary and I want to add maybe another one. Let's make it maybe split like this. In here in the update, I'm going to say that I want to add this dictionary inside. So pretty much merging two dictionaries together. Now if I'm going to print it,
76:41
Speaker A
you can see now it's going to have these items in the end. Key one, value one, key two, value two. So it took these items and we just added them here by using that update. And then again, if you're going to use pop, we're just going to say here, for example, I don't know, boolean and then print data types. You'll see that we're going to get this item outside. We're going to get only the value and then boolean is not going to be inside. It's a bit messy, but just
77:05
Speaker A
trust me, boolean is definitely not there. That's the point of the pop. If you want to work with loops and you write for for example I in uh data types print I. This is only going to print you uh each key which is not usually what you want. Therefore it's very common to write here key value inside data types items because remember items is going to make us like uh tpples of two values like XY XY whatever right or key value key value. Therefore, we can assign two
77:36
Speaker A
things at the same time. So, key is going to be the first one. Y is going to be the second one. So, this way key some dashes and then value. Can see here are all our keys, here are all our values and they separated with these dashes. That's it about loops. Again, don't worry. We're going to cover loops in another lesson. That's not the point right now. All right. And that's the collection data types in Python. Remember you're going to work with list in 90% of the time if not 99. So focus
78:06
Speaker A
on them the most and then just understand the main difference between tpple set and dictionary so you know when to use them. Tapels when you have lots of data and it has to be fixed. Uh sets when it's you need to work with unique data or compare to sets and dictionary is when you need to map your items so you have key value relations. And don't worry if it feels a little bit confused. I covered a lot without explaining many concept in depth. That's exactly what we're going to focus in the
78:29
Speaker A
next lessons during this course. So give it some time and it will start to make more sense as you get more comfortable with Python in general. So wish you happy coding and I strongly recommend that you go over code examples for the lists to better understand how it works. Trust me, you will learn a lot and it will help you a lot with Python. And then I'm going to see you in the next lesson where we're going to create a simple exercise together. So see you soon. Goodbye.
78:55
Speaker A
You just learned about collection data types in the previous lesson. And now let's make a simple exercise so we can practice working with lists and a little bit with dictionaries as well. And before we begin, a quick note. We still haven't covered logical statements and loops, which are two essential Python concepts, especially for working with lists. So we're going to simplify this lesson as much as possible and focus specifically on collection data types so
79:17
Speaker A
you get more comfortable working with Python before we're going to introduce logic and loops and all other great things. So with that in mind, let's jump into the code and play this fun little game. So, we're going to play a backpack game to practice collection data types and we're going to make it very abstract. Here's how it's going to go. We're going to imagine different tasks and then we're going to try to accomplish it mainly working with lists. For example, for the very first step or
79:39
Speaker A
step zero, we're going to start a journey with an empty backpack. So, therefore, we need to create here a variable going to call it backpack and it's going to be an empty list. Now, it's also a good idea to print it. And then in the beginning, I also like to add here another emoji. And it's going to be a backpack emoji. As you remember, we make emojis with window dot. And now, if I'm going to run this, you can see we're starting the journey with an empty backpack. And then we're
80:06
Speaker A
printing the backpack itself. And now we're just going to have 10 different steps. And it's not going to be complicated. I just want you to see how we can keep working with the same list and still make lots of changes. And also before going to the next step, let's print here lots of dashes. We could make it like this, but it's also much easier to just multiply it by 50. Now, you will see there are dashes like this. It's going to be easy to separate all the steps. And for the next step, you can
80:29
Speaker A
see that we're going to be picking up starter kit. We're going to pick up armor, shield, sword, and potion. So, how do we do this? We're going to take our backpack. I'm going to use append method to add these items. Now, the first one is armor. Then, with Ctrl D, I'm going to duplicate a few times. Then, it's a shield, sword, and potion. We can also copy that one again. So we kind of keep printing it in the same manner. Let's run it. Look at that. Starting the journey with empty
80:54
Speaker A
backpack. And then we're picking up different items. And now we can see our backpack is growing. So far so good. Also for this lesson, it would be nice if we could grab this and put it here on the side like this. We can actually do that by right clicking anywhere then move to and we need a right top. And then it's going to be right here. It's going to be much easier. We're going to see way more code and way more statements at the same time. Now, let's go to the next step. And now,
81:20
Speaker A
we're going to loot a treasure chest. Let's say that we found a chest where there is map, potion, compass, and potion. And we want to add it to our backpack. So, there are different ways to do that, but we could either use our backpack and then use extend method this way. We're going to kind of merge two lists together. I just copy that one here and let's print it quickly. You can see we added all these items inside. or to be honest much simpler approach would
81:45
Speaker A
be to write backpack plus equals chest. This is the same meaning but to be honest I prefer the syntax way more. So I'm going to probably comment this one out so you can still see this. Now if I run it you can see still the same thing. We're looting a treasure chest and then right here we have a bunch of things. Maybe we also going to print the chest itself. So we're going to print uh chest and then we're going to specify here the content. We can either print multiple
82:11
Speaker A
things in print because it can take multiple arguments or we can also combine it with an fstring into a single string like this. It doesn't matter whatever you prefer. Both of them going to work just fine and print the chest. And here are all the items. Also, when I work in Python, I like to make my code as readable and nice looking as possible. In this case, it's a print chest print. I don't really like it. So, with control shift, I'm going to move this line down. But this way, you can
82:34
Speaker A
see we cannot execute this one now because chest is defined after that. So therefore, I'm going to select these two lines with control shift and up arrow. Going to move it up. So we're going to define the chest. We're going to add it to our backpack. And then we're going to make the print statements. It's little things, but I'm a little picky and I try to make it as nice as possible because the easier it is to read your code, the easier it is to maintain it and kind of
82:56
Speaker A
even add more features. So also try to kind of keep this mentality. Now let's go to the next step. And now we're going to visit a merchant. Going to visit a merchant and then we're going to sell the shield. I'm gonna upgrade the sword to magic great sword. So, how do we do this? First of all, let's grab the backpack. And since we're selling, probably want to remove our shield like this. This way, we're not going to have it anymore. Let's just do one step at a time so you can see everything in
83:20
Speaker A
action. Now, if you look here, the shield is missing. It was the second. Now, we want to replace sword with magic sword. And there are many things to achieve this thing in Python. And usually, you can select whatever you prefer. For example, we could just remove the sword and we could just use backpack append magic great sword. So that would work, but keep in mind that this way you're going to remove the sword from the second position and it's going to be added in the end. And maybe
83:47
Speaker A
you want to keep the same position. So you're actually upgrading it. So therefore, remove append might not be the best choice. And instead we can just take backpack then point to the right item and um and this is actually going to be here instead of potion. So let me just print quickly. So I'm going to print. You can see this word is here. So therefore we're going to write backpack one equals magic great sword. And remember in lists we start counting from 0 1 2 and so on. Therefore
84:15
Speaker A
this. However keep this in mind. Right now it works. We're just creating here magic great word. But what if we're going to go back to where we defined it first and just move it somewhere. For example, it's going to be here. Now we're going to run it. You can see we have magic great sword and swords. we are replacing something else and this is not good. So I think we're replacing this portion. So therefore it might be also a good idea to find the index with Python. Going to specify index equals
84:40
Speaker A
then we're going to find it in backpack index sword. So we're going to find the location where it's located and then when we have it just going to replace it like this. Now let's align it. Also I think I'm going to remove this one since we're not using it. And now if I'm going to execute this have a look. Now it's here and we replacing it. But if I would go back to earlier and move with it, for example, the first item and run it, still going to work. It doesn't matter
85:05
Speaker A
where I'm going to put this word, it will always find it and replace it. That's how we can visit the merchant. Now, for the next step, let's actually figure out what do we have here. There are lots of items, and it's a good idea to kind of inspect what's going on. So, we're going to check our inventory or checking backpack. For that, we're going to check what's the total count, how many unique items we have, and how many potions we have. And to begin, let's start with the total count. This is
85:29
Speaker A
going to be the length of the backpack like this. Then to get the length of the unique items like unique count, we would need to get the length of a set of the backpack. We could also kind of break it into two steps. It's going to be unique items equals set backpack. And only then we're going to use this unique items right here. You can use both of them. This one is a little bit cleaner, but often times it's might be much better to have one line less because then it's
85:58
Speaker A
also kind of readable enough. But for beginners, I know that many of you would prefer to have it like this. It's much simpler to keep one thing in line at a time. And lastly, let's count how many potions we have. And for that, we need to take backpack. And remember, there was a method count. We're going to count how many potions. I also misspelled it here. Now it's good. And then I'm going to make a bunch of print statements. So now let's run and see what we get. You can see right here
86:23
Speaker A
checking the backpack. There are lots of items and there are seven items in total. Five unique items and free portions. You can see it's not that complicated and it's actually very very often used because you often need to know what's the length of the list and how many unique items and so on depending on what you do of course. Now let's make something fun and let's say that we drop the backpack. Now it's everything upside down. For that we're just going to grab backpack and we're
86:45
Speaker A
going to reverse it like this. Or we could also say backpack equals uh backpack like this. So in this case we need to assign it because we're creating a copy of it. Then we're assigning it back to the same variable. And here we modify the existing one. I think this one is in this case simpler. So we're going to keep that one. Now let's go back. I think I forgot to print dashes here. And then I'm going to print this backpack. And now we can see everything is in
87:15
Speaker A
reverse. The magic for was here. Now it's in the end. And the same is here and all the items in between. So the next logical step we want to do is actually sort items. And as you know it's not complicated either. Just going to write backpack sort. We're not even going to define any rules because with control q you can see there's more information. We can specify here key in case there is more complex way how we need to sort it. Right? Maybe you want to sort by the amount of letters in each
87:42
Speaker A
word. And actually I'm going to show it in a second. But first let's just sort it alphabetically. So we're going to run it and you can see now it's going to be sorted. So it's armor, compost for sea, magic and so on. So everything is nicely organized but we could also sort it differently. There is a feature called key and in there you pretty much point to another function like length for example. In this way if you're going to run it have a look at this. Now we're
88:06
Speaker A
using completely different rules on sorting. Now the first one is map, armor, potion, potion, potion and so on. And normally you're going to use this key argument to sort with special features when you work with more complex data structures like with dictionaries with lots of data or maybe with a nested list or maybe with other classes and so on. Or in this case, length is actually a great example. You can see now everything is sorted very nicely with letters. But we're not going to do that.
88:30
Speaker A
Let's keep it simple like this. Just wanted to show you that it's possible. And now for the next step, we finally go to sleep and somebody stole free items from us. And let's notify which items are stolen and we're going to take them randomly. And there are different ways to do that. And we're going to use backpack method called pop. This way we're going to either take the last item or can specify maybe item the third one cuz 012 and maybe another last one. And here's the
88:56
Speaker A
thing going to write here what changed and just execute quickly. Now can see we're sleeping and then our backpack became smaller. And the beauty of this pop as you remember not only it removes item it also returns item which means we can assign it to a variable like a for example just going to keep it very simple. Then we can create a new list of stolen items. I'm just going to write a b and c. And now we can also report on it. So we're going to write here print stolen
89:26
Speaker A
items. And then we're going to with fring add here the list. Again you can just simplify it and make it like this. That's also going to work fine. Now, one thing here is I really like to align my equal signs. I don't know why. Not nobody really does that, but I really like how it looks. For me, it's more readable for you. If you don't like it, just keep it like this. It's also fine, but I like this way. Now, let's run it and have a look. Now, not only we kind of removed items, we also
89:54
Speaker A
know which items we removed. We could also kind of print each item on its own. So, we're going to print A, print B, and pre print C. But I think it's not necessary. You can see here the items having a list is more than enough. We're going to clear it and keep it simple. So now we lost a bunch of items and probably we are a bit sad because of that. But luck is still on our side cuz now we're going to find a magic ring and also a coin pouch. So we have here ring and coin pouch and we
90:22
Speaker A
want to add it obviously to our backpack. But this time I don't want to lose the ring. So I'm going to take my backpack and I'm going to insert in the very first place. This way I know that it's the first item deep in my backpack and coin pouch. I don't really care. But I'm just going to use a pen to add it. I know. Here's the thing. Also, if I'm going to print it, notice that this coin pouch is a list. So, I'm putting a list inside my bigger list. So, think about
90:53
Speaker A
the big list is your backpack. It has lots of space. And then smaller list, it's just like a coin pouch. And it's just kind of list inside of a list. Oh, another backpack in a backpack in a backpack. You know, like Matroska doll like keep kind of going inside. Think of it this way about nest at least. Now that's great. So let's have something more fun. But then something happened because we added magic ring to our backpack. Half of the items magically disappear. Damn you magic ring. So how
91:19
Speaker A
do we do this? There are many ways to do this as well. But we're going to first of all count how many items did we have. So we have this many items. Now we're going to get half of the items. Count divided by two. And then we need to cut our backpack in half. There are different ways to do this, but we can just write backpack equals backpack. And then use slicing like this. Either take the kind of first half or the second half. And let's start like this. And we're
91:44
Speaker A
going to print it like this because there is one little issue. You'll notice that we're going to get an error. Slice indices must be integers or not none or have index method. So obviously I did something wrong. And you can see line 116. So it's here. So therefore it's clear that something is wrong with the half. Let's print that one. And in here we can see 30 with the decimal point. So we know that this is not an integer but a float. But we need to turn it into an integer. And also if
92:12
Speaker A
you're going to scroll up you can see that error message is on the top in case you wonder that it fixed. No, it's still there. Now there are many ways to avoid it. So first of all we could kind of turn it into integer like that one. And that's actually going to work. And that's one way to do this. But there's also another syntax. We can just take count and double divide by two. And what it means, it's going to divide your number and then it's going to floor it.
92:37
Speaker A
Meaning it's going to turn to the lowest integer. So if you divided something, you have 3.9, it's going to become three. If it was 3.1, it's still going to be three because we are flooring it down. Now let's run it. And now we lost half the backpack. So the first three items disappeared forever. And now I think we covered quite a lot inside this game. Let's go to the very final step. And let's say that bandits stole our backpack. So they attacked us, backpack stolen. And now we need to
93:03
Speaker A
define that backpack. And you might think that we're going to define like empty list or maybe take backpack and say clear. Also an option. Here's a little thing I want to explain with the final step because if I'm going to print it like this, consider we still have a backpack. It's just empty. However, if we instead of kind of saying all of that going to say backpack equals none, then it means that we don't even have a backpack, right? There is no list. We cannot really add anything in
93:30
Speaker A
there because we don't have a backpack. And this is one of the ways where you use none. It's like where you don't have like an empty list because it's also something you actually have nothing. This is kind of the purpose of the none to show that you don't even have anything yet. And that's it. That's our little game. I hope it was not too simple and I hope that it helped you understand that we kind of create a list in Python. We can go through and kind of keep changing it.
93:54
Speaker A
We add items, we take it out, we sort it, we reverse it, we kind of use different sorting rules and we made quite a lot and I hope that you enjoy this very simple lesson to see list in action. Now, normally when you're going to write your code, it's really good that first you're going to write it, then just go and have a look and see if you can refactor it. And refactor is when you're going to go back and rewrite your code or simplify it in a way where functionality doesn't change, but it
94:19
Speaker A
becomes much better and easier. And in this case, you might notice that we constantly write this backpack backpack and it's really really big word. Maybe we want to make it something shorter. I'm going to go to the top, select that one. And in PyCharm, there is a great thing. We either can right click and there's refactor. There are a bunch of things. Let's go for rename. Instead of backpack, I'm going to actually write here something like maybe pack which is shorter. You can see it
94:44
Speaker A
changes it everywhere all at once because PyCharm is aware that this variable used everywhere and I'm changing in all these places by just writing a single time. I'm going to write here a pack and see it's much shorter. In general, the shorter your code is always the better. It's easier to read. But try not to keep it very kind of these abbreviations like BP because after a while you would come what the hell is BP? But pack is pretty good one. Oh, I canceled by accident. So
95:08
Speaker A
let's do it again. Right click, refactor, rename. Just start typing pack and then enter. Now it's done. There's another way also to do this. You can select it. And this is what I use the mostly in PyCharm. Hold alt, click on J. It's going to select the next instance of the same word. J JJ J. It's less precise because you can see it selected back in the variables. But usually when it was like a backpack, we could just go back and we would select pretty much all
95:34
Speaker A
of them like this as well. So we just kind of keep clicking click click click click how we got here we could also write back but again if we would have anywhere in the strings backpack it would become pack now let's see what else we can fix it and you might notice that we're constantly printing the same message over and over for example these dashes it's everywhere like 50 and I want to give you a very good rule in Python is that whenever you copy anything in Python over and over you're doing
96:01
Speaker A
something wrong programming is all about being efficient and it means that we have to be efficient in our code and there are always ways to kind of reuse your code in a way where you have a single control and then kind of reuse it. For example, in here instead of constantly printing the same message like this, it could go all the way up or just create here dashes. I'll define it once then I would go and replace it everywhere. In this case, I'm going to use alt J to quickly select all of them.
96:30
Speaker A
Now, the reason for that is look at that. If I'm going to print it, you can see there are 50 dashes everywhere. But now I have a single place to change it and it's going to affect it everywhere. Now it's 100 everywhere. It's much simpler to make changes in your code. Instead of going changing here, here, here, then you forget that one and so on. And you know what I mean is like whenever you copy paste anything, think about how can you make it better. And at the moment we haven't covered enough
96:50
Speaker A
concept cuz normally you would create functions, classes and so on. So don't try to over complicate it yet. Just keep it very brief. So that's it about lists. I pretty much explained everything I wanted. So I'm going to select everything with control forward dash comment it out and I just want to briefly talk about dictionaries. Many of you probably still don't understand why do we need dictionaries? It's a mapped collection but how do we use it? And I prepared a very simple exercise. We're
97:14
Speaker A
going to go very quick. I'm probably even going to just insert different items. Let's say that you have a phone book and it's a dictionary and inside it's going to be name number kind of sequence. It's good to create a comment for beginners so you know what kind of um what what kind of structure of data you're going to put and actually maybe instead of empty one let's have one with some data. So we have Ricky, Tommy, Claus and here are the numbers. These are fake numbers but I think it's quite
97:39
Speaker A
obvious. Now with dictionary the thing is that we have the names and their numbers. Let's say that we want to add more numbers. Add more items. We just take the phone book. For example, we say Eric number is going to be this. If it doesn't exist, it's just going to add a new one. And if it exists, for example, this, then it's going to overwrite existing number. But in my case, I'm just going to add a few numbers. Let's also add table. Again, I like to align it all the time. Way nicer in my
98:06
Speaker A
opinion, but many people would leave it at this. I'm very picky to my code. And now look at that. We're going to print the phone book. Now we have a data structure where we have names and their associated numbers. And now later, let's say that you want to call somebody. So we're going to say, I need to get a number. And from the phone book, I'm going to look up the number of the Eric. And then I'm going to create this print statement. Let me remove it. You see calling Ering. And we
98:30
Speaker A
inserted number with fring. Run it. Now calling an Eric. You can see we're getting the right number. We're getting the right name. And it's one of the simplest way to begin with dictionaries. First start with map collection. Instead of numbers like in list you use 0 1 2 just use something else like for example names that refer to a number. Maybe you're going to refer other object name to get actual object and so on. This is just kind of to begin with and later
98:52
Speaker A
you're going to get more comfortable. Then you can say you know what I also want to call maybe teo maybe I want to call Christina. Let's change a few things. And now you can see if I'm going to run calling different numbers to different people and everything is related. So if you want to kind of understand dictionaries start here with this one. And later on when you kind of get more comfortable with them can actually make more advanced data structures. But also
99:19
Speaker A
remember that during the game we created backpack which just we're adding items and very simple but we could actually create more complex data structure to store more information not not just about the backpack but also maybe a player. So let's create this kind of level two for dictionaries and it's going to be advanced one. Listen it might be a little bit over your head so don't worry you don't even need to follow the code. Just sit down relax and have a look. And I just like to
99:42
Speaker A
challenge you once in a while. So maybe it's going to help you better understand the dictionaries. If it's too hard, just give it time. You will understand it later. Now, I'm just going to paste here an example of dictionary. Look, now we define a player as a dictionary. And there are different keys like name, class, health, level, backpack. And you can see we have a list here, integers, strings. And this is this is maybe not the best use for dictionaries because
100:02
Speaker A
classes would be way better for this case. However, classes are more advanced concept and sometimes it can be actually beneficial to store data like this if you want to take it out of Python as JSON for example. So let's have a look how can we work with that. Let's say that we start as a player and we want to level up. So we're going to say player then we're going to kind of point to this health uh key and since it's going to return us an integer we can say it plus equals one. It's going to take
100:30
Speaker A
whatever value it already is and it's going to just increase it. And since it's actually we want it to take level. So like this. Now I can print player and let's look at that. Now run and see here's the dictionary. It's not the best looking one in the console, but at least you can see that level two was increased. Also, I will introduce here the for loop. Just follow along blindly. I'm going to explain it later. So like this and print key value. What we're doing, we're just creating a for loop.
100:59
Speaker A
We say that we want to iterate for player items, which is pairs of keys values. We're going to assign all the keys to K and all the values to V. Therefore, key V. And this way, it's just nicer to see in the console. Sorry, but I cannot really kind of print it in one line and expect you to get comfortable. This is way better. Now, here is modify stats. Then we can we can also add items to the backpack. I'm going to say player. Then we're going to take backpack. Then we're
101:27
Speaker A
going to append we're going to append some items. Let's just keep it very abstract. Keep it item A, B, and C. Also, if I'm going to spell the player right, it's going to work properly. Now, let me run this. And now have a look. We can see all the attributes. It's a Eric warrior health 100 level two. And now I have a bunch of items in my backpack. And I can even add other lists in the list while we are still in a dictionary. That might be a little bit overkill, but let's just show you
101:53
Speaker A
quickly that there's let's say we have some coins. Run it. Now you can see this is all a dictionary while in one of the keys the value is a list and then there's a list and so on so on. It might look a little bit overkill but just know that eventually it will make sense. Just think about different video games and about different softwares and just think that well often times you have one object but there's lots of data associated and and this can be one of the ways to represent it and again it
102:20
Speaker A
would be much better to use classes but we're not there yet you're not ready just keep patient all right and I hope that this helped you better understand collection data types in Python especially lists because you will use lists most of the cases anyway and later on you will also find more uses for the dictionaries when you need more complex data structure And don't worry if dictionaries are still confusing. Trust me, it will get much easier. We all start there. We all hate
102:44
Speaker A
them. Eventually, you're going to start loving them. So, I wish you happy coding and just make sure to follow along the code about lists. Just play this little game I've created and then I'll see you in the next lesson where we'll talk more about basic operators in Python. So, see you soon.
103:02
Speaker A
Hi everybody. In this lesson, we're going to talk about basic operators in Python. And I just want to show you what can you do with your strings, numeric values, and lists and what kind of operators can you use. Let's just jump straight into this and I'm going to explain as we go. First of all, I want to explain you the expressions because as you know, we can assign data to variables like this, right? We're going to say x= 10, y= 20. We can also say z= 10 + 20. And we're going to write here
103:28
Speaker A
an expression. And it can be any length, right? We can add as many as we want. We can really make a lot of things in one line. And then if we're going to go and decide to print print X, Y, and Z, let's just run it. You'll see we get all these numbers 10, 20, and 30. But also in here, we don't really have to always use the numbers. We can also use other variables. So in this case, this variable Y is going to be here, and it's just going to kind of reference the
103:53
Speaker A
value. I'm going to run it. It's also going to have the same value. So we can just write here X + Y. This is clear so far. But also, I want you to know that you don't always have to assign all your data to variables. You can directly make expressions inside of your print statements, arguments and so on. So for example, I could write here for example print x + y + z and then I would execute it and then I would get the total value right away. Let me just zoom out a
104:19
Speaker A
little. So I don't have to first kind of take all these values say total equals this and then print the total. This is kind of extra step. It doesn't hurt. Sometimes it's better to explain it. But you can also kind of take whatever you want and just print it right away. And you will see me do it all the time kind of work inside of print statements especially in this lesson. All right. And this is pretty much the basics of expressions that you can define your
104:42
Speaker A
variables with different expressions or you can take expression and use it in other places. Now let's comment it out and we're going to go and talk about different operators for data types. Starting with string operators. In here I can see it's very common also to make kind of lots of dashes to separate the logic in your code. So first of all, how do we join strings? Let's say that we have two variables A and B. And A is going to be hello, B is going to be world. We could
105:09
Speaker A
say that C equals A + B. And since it's strings, you probably already guessed it. We're just going to join them together. So we're going to print C. And in programming joining strings together is called concenation but you can just call them join right and here look at that we have hello world and obviously we also want to add either a space here or maybe in front of the world then it's going to be printed nicely in the console but also you can just say print a plus b
105:39
Speaker A
directly in here. So you don't always have to define a variable to make your calculation. You can just take it directly inside of your arguments. and I just run it. You can see exactly the same result. Now let me select it, hold control, click on the slash to comment it out. Now another thing, sometimes you're going to have a variable. Let's say name is going to be Eric. Then you want to print my name is and then you want to add whatever is the name. So you can
106:08
Speaker A
combine here also like a string that you define only here and then reuse a variable that also work just fine. My name is Eric. That works. Again, I'm going to select everything. Ctrl slash to comment it out. This is it pretty much about joining the strings. There's also formatting which I discussed previously. This is when let me actually just duplicate it. Ctrl + D to duplicate the line. And instead of joining, we can also make here like a placeholder. And then with the format, this is another
106:34
Speaker A
method. We're going to talk more about this during the course. We can provide here a value that will take place instead of this um placeholder. So I could write here like Eric or I could just reference this name if I would uncomment this variable. So if I'm going to run it and you can see in this case Eric goes right in here. The format is really really important method that you need to learn. All right let's control slash to comment it out and we're going to go to the next examples.
107:01
Speaker A
Next I want to briefly show you how to multiply different strings. And first of all the most common way to use it is to can take a dash. Let's say we want to print 100 dashes. I want to say print hello before and after. And then we're just going to execute. Now have a look. We have hello. Hello. And then we have 100 dashes right here. You could also multiply kind of 100 times hello. You can see it kind of doesn't make much sense. However, you could also use here
107:29
Speaker A
a special character called kind of slashn. And in strings, this backslash and n have a special meaning. This is going to be a new line. So now if I'm going to print it now, we can see every time it's going to be on a new line because it prints hello and then it takes kind of a new line. Hello, new line. But in general, you'll mostly use this to kind of make this kind of long dashes when you make your print statements. So let's comment it out. And the next important operators I want you
107:55
Speaker A
to introduce to is membership operators. And I refer to in or not in. So you can easily check is something inside of your string, lists or other containers. For example, let's say that we have a message. Just going to paste it. We need to build a brick wall. And then we want to test something like, okay, print. I'm going to make some tests. Is the word brick in message? I just print it. True. It is there, right? So, this is going to be true. How about we with Ctrl Duplicate it? And
108:25
Speaker A
we're going to say is the word brick in the message. Notice this one has a uppercase letter. Now, we're going to run it. This time it's false because as you know in programming lower case and uppercase they're two different characters therefore they're not equal to each other. However we could for example say here lower to make everything lowerase. I'm kind of going a little bit ahead of myself. Just know that we can also influence how we make this comparison but we're not going to
108:52
Speaker A
go there just yet. Now and these operators they will make more sense once we're going to go into the logic of Python which is really important concept. But this is how you make the first kind of statement like let's say that for example I want to check is the glass in message maybe we're building glass walls or something no it's not so this one is also going to give you false also you can give the opposite for example you can check glass not in the message right and this this case it's
109:19
Speaker A
going to be the opposite is going to be true because glass is indeed not inside this message this is just the basics of kind of starting to create logic later I will show you how to actually use it to make logical statements for now just know that there is this operators it's called membership operator and another one equal/n not equal operators and they are going to be kind of double equal sign or exclamation equal sign in Python this is the symbol for equal this
109:46
Speaker A
is the symbol for not equal and don't mistake it with just single equal sign because this is how you assign data to your variables here's how to do so let's write here two statements for example concrete 10 cm and 20 cm it can anything really. And then we can say print a equals b or a not equals b. Right? And now we're going to print it. And you're going to see I forgot to comment it out those ones. So let's do this right now. Run it again. Now it's simpler. This one
110:18
Speaker A
is going to give us false. This is going to give us true because indeed these are not the same statements. And true because they are not the same statement, right? And you could also kind of going to create here a check and assign this one right here. I know it might look a little confusing but just know that this is equal sign. This is equal operator. Is it equal? Right? It's always a check and this one is just assigns to your variable. Then we could print it and
110:46
Speaker A
obviously this is going to be false. All right, that's it about string operators. As you can see there are not so many of them. We can join our strings. We can also multiply our strings and we can check is something inside the strings or other strings equal. These are the basics you need to know and luckily for you they're pretty much the same operators in other kind of data types as well. But the next one we actually going to look at the numeric operators and
111:11
Speaker A
since it's a numeric we can do the math right a plus b a minus b and so on. This is arithmetic operators. This is what we're going to start with. Let's define a few values. And I'm just going to say a= 10, b equals 3. This is going to be more than enough. And now we're actually going to go and look at the all arithmetic operators. I think I'm just going to print paste all of them. I'm just going to kind of go through and explain one by one. It's going to be much simpler. I'm also going
111:39
Speaker A
to make print statements because I don't think you need much explanation that you can join two numbers, you can subtract numbers, multiply, divide, and so on. However, there are a few points I want to point out. Firstly, when you divide in Python, you will always end up with a float. Secondly, there is a special symbol, you can use double kind of divi division symbol, and this is going to floor division. So, it's going to take the lowest number, right? Even if, for
112:04
Speaker A
example, here we use something like 1199, this would still be three because we lower it down, right? If it's 3.999, we're still going to go down. Okay, let's just go back. So this is the division by using this kind of double division key. Also a special one in programming is this mod. It's used with the a percentage sign and this is to get the remainder. What it means is that you have a whole number which is 10. Then you want to check the remainder, right? So you're going to 10 minus 3 is like
112:36
Speaker A
seven. Then it's going to be four. Then it's going to be one. And firstly you take away all the whole numbers and whatever is left is going to be your remainder. This is called the modulo, right? It's very common to use to check if your values are even or not. But don't worry about this. It might look a little confusing. Eventually, you will understand when we actually going to use it in logical statements. I'm going to show you later as well. And the last arithmetic one is the power. You can
113:02
Speaker A
assign power to your numbers. So 10 in the power of three is going to be,000. 10 in the power of two is going to be 100 and so on. This one is the number you put here in the power. All right, that's it about arithmetic operators. The next one, I also want to point out that you have also comparison operators in Python. Let me just quickly comment out everything above and we're going to print it. And now this is just to check is A more than B? Is A less than B? Is A more equal? And
113:33
Speaker A
then again you have equal or not equal signs. In here you can see all the results, right? You get the idea. There's nothing special about this and you all pretty much know it, right? You just need to know that in here, for example, you use this symbol to check if it's equal and here to check if it's not equal. All right, that's pretty much it about numeric operators. Let's go and have a look at the list operators. I'm just going to provide here a few lists. And
113:57
Speaker A
first of all, I want to show you that you can create new lists. Let's call it list three. And we're going to take list number one plus list number two. This is the most common way to sort of uh join lists together. We're going to print list three. Run. And you can see we join two lists together. Now we have one single list. So this works pretty good. Now surprisingly you can also multiply lists. I don't know really a good use case for that. Sometimes you might need
114:26
Speaker A
it, but in general you can take your list, multiply it by a number, and you're going to kind of repeat this list over and over. So here you can see uh concrete steel glass concrete steel glass and it's going to be 10 times like this. Maybe you need it one day but just know that it's possible. I don't think I ever use this in Python really, but it's there in case you need it. All right, let's comment out this part. So control slash control slash. The next one is membership operators. And it's
114:55
Speaker A
similar like I showed you before with the strings. We can take something and test is it inside of something else. Uh I called it wrong here to be honest because I copied paste. It should be material free. So let's make some spacing here. You can see concrete in material, wool in material, wool not in material. And if I'm going to run it, you can see that true, false, true. So this one is true, false, and true. And this is just kind of for creating a logical statements.
115:24
Speaker A
First you need to make the check. And later I will show you how to actually make a logical statement to do something about this, right? because first you want to check is the concrete in this material and then you want to do something but leave it for another lesson. For now just focus on operators itself. So this is very simple and this is very very commonly used especially with lists because you will always have list of data and you would want to check is there a view with this name in my list
115:49
Speaker A
of views. If not I want to maybe add it if not do something else and so on. But you will see once we're going to start coding. And lastly, we can also obviously test kind of equality. Is list number one equal to list number two? And maybe also not equal. So obviously if we're going to run it, it's going to be false and true because our list number one is not equal to list number two. That's very clear. Oh, and before we actually wrap this up, I want to show you a really
116:17
Speaker A
good example for membership operators. Let's test something. I want to test is my material two, right? So I'm going to print material two in material three. Uh I misspelled it. It should be materials mods. And now what do you think is going to be true or false? Because list three is going to be all of this in one single list, right? And I give you a second to think about this. And now we run. And it's false. You know why? Because we're not testing if all items in the list uh
116:47
Speaker A
two inside of list three. There's going to be an a little bit different syntax with the loop. However, we're checking if lists is inside of the list. So, I'm going to create a materials for let's say that instead of joining two lists together, we're going to put this list inside and we're going to put this list inside. So, we can see that this is a list that contains other nested lists. Maybe this one is going to be a bit easier to see. So, we have our material
117:15
Speaker A
four is a list. Then, inside there are two items. This is item number zero. So this is item number one.
117:24
Speaker A
Now in this case if I will test is material 2 in material 4 in run this time it's true because this list is right here right I can see it's inside exactly with the same structure it's here therefore it's true because as you know we can make nested lists we can put list inside of the list inside of the list and we can also put different numbers here we can also put different text here and so on so later on I'll show you the loops and with loops you will also be able to go through the list
117:54
Speaker A
look at all the items inside the list and kind of make your conclusions. All right guys, that's it about the operators in general. You just need to know that if you work with numbers, you have available all the arithmetic operators as usually. And you also have logical operators to make different comparison, to make memberships, check if something is equal and so on, which will be important in the next lessons when we're going to start working with logic because in the end of the day,
118:18
Speaker A
writing code is pretty much writing logic. If this then do this, if this then do this and so on so on so on. This is really important that first of all you know how to make kind of logical checks, right? Is this equal to this and so on and you get true or false and later you can work with this one. But don't worry, we're going to get there. I'm going to explain you everything. Thank you. I wish you happy coding and I'm going to see you as always in the next lesson. Goodbye.
118:46
Speaker A
Hi everybody. Welcome back. It's finally time that we're going to add logic to your Python scripts, which is essential for any code that you're going to write. You'll often need to execute different code depending on the condition that you defined. For example, if you want to learn more, continue watching. Else, skip to the next lesson. Did you notice what I just did here? I just created an if else statement to decide the right action depending on the condition.
119:11
Speaker A
That's how we create simple logical statements. Let's look into this. We're going to go to code and we're actually going to start coding with a very simple example. And now here's what we're going to do. First of all, let's talk about logic and syntax basics in Python. So you know how to create your logical statements. To create logical statements, you need to do the following. You need to use the keyword if. This is how it always starts. Then you need a condition. Example, you can
119:34
Speaker A
write here five is more than zero and this this one is will be true. Pretty much your goal is to make either if true or if false. In our case, let's say five is more than zero. We know it's true. Therefore, it will be executed. You'll also notice that in the end, I put the colon. This is how we start to define our code block. And the next we need to use identation in Python. Let's just write here print uh I don't know uh code a. So, identation in Python is defined
120:04
Speaker A
either with four spaces or with a tab, which is pretty much the same thing. Identation it means the spacing before the code. And in Python it's really important because Python use identation or this spacing to define the code blocks. While other languages like C, Java, they don't care about the spacing before the code. They only care about the curly braces which define the start and the end of the code block. Now you also probably wonder what the hell is the code block and code block is pretty
120:30
Speaker A
much this. This is relates to if statement. If statement is true then we're going to execute our code block. We can write here anything we want. We can write it multiple times, but as long as we going to write here, print uh code B. This will always execute no matter what you're going to put inside of here. Let's just test it quickly. Right click, run. Look here. Code A, code A, code A, B. Okay, now this is because this one is equal true. Now, let's make it five is
120:58
Speaker A
less than zero. Obviously, it's going to be a false statement. PyCharm even kind of highlights that this will always be unreachable because this is kind of fixed values. Just going to ignore it for now. We're still going to run it. Notice that look now we execute code B no matter the result of the if statement. Right? So we have our if statement and then below it we have the code block related to this if statement which will only be executed if it's a true statement. If it's false then this
121:25
Speaker A
one will never be executed. And this one is just continuous our code. It will always be executed as long as we don't crash our code. Now what can we do here? First of all, let's define here a condition separately. Five is more than zero. That's okay. It's always going to be a true statement. So now what we do? We can replace this one with the condition. And it's the same thing, right? If true, execute this. So when you write if condition, it's it's exactly the same thing. If you would
121:52
Speaker A
write if condition equals to true. Some people prefer to do it this way as it makes it more readable for them, but it's not necessary, but it's also not harming anything. So, I'm going to keep it as if condition. All right. Now, let's select everything we just wrote. I'm going to comment it out. Select it, hold control, click on this division key, this kind of slash, and it's going to comment everything. Now, we're going to go to the next step where we're going
122:15
Speaker A
to look a little bit more into the if statements. As you see, we can actually use if, l, and else statements together. Here's how it works. Let's define a number something like temperature. For example, let's say it's 30° C outside. Now, we're going to make a few if statements. So, we're going to write if temperature is more than 25. Then, we can write here it's really hot outside. Now, I'm going to put some emojis. Fire and that one. Right? So, the temperature is 30. And
122:48
Speaker A
now, if I execute, it tells me it's really hot hot outside. Now, what if temperature is less than this? Let's write if temperature is more than let's say 15. Then we're going to write it's warm outside and we're just going to put here a sun emoji. Again, to use this emojis, I hold the window key and click on dot. Then I get this kind of menu where I can select it. All right, let's execute it. Now look at that. Now it's becoming confusing. I have the temperature, but I
123:17
Speaker A
have two statements. It's really hot outside. It's warm outside. So which one is it to you see when you use if statements it will always kind of make the test it will check this one then this one is already separate kind of logic right instead of that we can use l if which stands for else if so if temperature is more than 25 it's going to print me this one if this one is going to be false it will continue reading okay the first one was false what about that one if temperature is
123:44
Speaker A
more than this try this one again false okay maybe we try something else let's write here if temperature is more than zero. Then I'm going to write it's chill outside and let's find something like cold. And then after all of that, we also have an option to use the else statement. Else is pretty much when nothing worked. You can have here one if statement, maybe thousands of LE statements, but if nothing worked, you will always execute whatever you write here. And we can
124:14
Speaker A
assume that if nothing before worked, then it's really cold outside. So going to write here it's freezing cold. Let's make here a cold. All right, let me just zoom out so we can see everything at once. So we have our temperature which is 30°. And now we make multiple if statements. So let's run it. And now it's going to say it's really hot outside and it's not going to execute anything else because the way it works is here's our value. Then we make the first test. If it has executed
124:44
Speaker A
successfully then this one is not necessary. We just completed our if else if statements. Now if this one is going to be false, then it's going to try next one. If this one is going to be false, it's going to try the next one. And if all of them are going to be false, finally it will execute else as the last resort. So here is how it goes. Let's say it's 20° outside. We run it. Now it says it's warm outside, which is correct because it's pretty much saying true
125:10
Speaker A
here. Now we're going to say it's 10° outside and run it. Now it's chill outside. We can also say what what about if it's minus 50 and then it's going to say it's freezing cold outside. So this way you can control the logic and it's not just the kind of print statement. You can write anything if your selection contains walls do this. If your selection contains views then do this and so on. Later you will see how we use it in Revit API a lot cuz this is how you define logic. All right the next
125:37
Speaker A
one. Let me just introduce you to different operators for creating logic. So for now we just use this kind of more or less which is very simple but there's actually more available in Python. So I'm going to paste it here and now we're going to go through one by one. So let's start with the comparison operators. Obviously it's these operators right here. You can see and it's just symbols that we use to kind of compare stuff to compare if something is equal or not
126:00
Speaker A
equal. We either use double equal sign or we use kind of uh exclamation equals. So this one is not equal, this one is equal. So in this case five is equal to five. That's true. Therefore this statement is going to return you true. Three is not equal. Three is false because actually three is equal to three. So next one we have greater than less than. You already know that. And also greater than equals and less than equals. So we just add equal sign there. This one is simple. You all know it.
126:32
Speaker A
Next one is logical and logical or and logical not. So just to explain quickly let's say that you have two coordinates. You have x which is going to be let's say 20 and y which is going to be 40. And for some reason we want to make sure that our x y coordinates they between zero and 100. Right? So we are kind of in this area that we defined. But here we would write if x is more than zero and x is less than 100 then we're going to print x is good. Then we could do the same thing for the
127:03
Speaker A
y. If y is more than zero and y is less than 100. I'm going to print ycoordinate is good. Let me also copy that one right here so it makes sense. Okay. And also notice that these are two different if statements. They're not related. And it means that if this one returns true or false, it doesn't matter. This one will still return me as well true or false. Because if I would put here if this would never be executed if this one is true. But let's go a step back. and
127:33
Speaker A
actually focus on what we kind of started with this kind of end operator. This way we need to make sure that this one and this one they both return you true. Only if both statements return you true then you proceed. But you can make here as many as you want. You can also write and y is more than zero and y is less than 100 then we can write xycoordinates are good. Let me just kind of comment it out. Run. Look at this. It's still freezing cold outside, but that's okay. We are
128:05
Speaker A
here inside. But xy coordinates are good because we checking x here and here and y here and here. So we can add multiple statements together. This is similar if we would write something like if x is more than zero, if x is less than zero, if y is more than zero, if y is less than 100, then we would print this kind of statement here. Right? this case you can see it becomes very very redundant. It doesn't make much sense and it's hard to read. Both cases we get exactly the same
128:38
Speaker A
result because we kind of test it all through. But in case of simple kind of statements just combine them in one line. But when you actually need to make it more complicated maybe for to read the parameter you get the name you try to understand what's going on and then you compare it. Obviously then we would kind of choose this kind of stepped approach. Or maybe if each one of them would also have different logic inside and so on, right? Let me just kind of hide that one.
129:05
Speaker A
Okay, I'm going to select everything here. Hold control, click on division key. Now it's commented out. So this was just the logic to explain you that we can take multiple statements and with end operator they have to be all true. If this and this true then we can proceed or with or only one of them had to be true. For example, if I would select different elements in Revit, then I could check if in my selection a wall or a window or a generic model, then we
129:33
Speaker A
can proceed, right? There's going to be something in between that is not these kind of categories, but we can still proceed because at least one of them is there. All right? And lastly, the logic operator, there is not, which makes the opposite of what you're providing, right? In here, let's just make something like um is enabled equals true. So I could write here if is enabled then I would write print all good. However, I could also do the opposite. I would say
130:03
Speaker A
if not is enabled then everything is bad like really bad right? So not is just going to make the opposite result of your selecting. Now, let me clean it up a little and we're going to continue to the last one and a membership operators. So, in here, we can also check if something is part of something else or some kind of sequence. This case, we can check if wood in the list of materials, in this case, metal, concrete, wood, then we would get true because as you can see, this wood is contained in
130:35
Speaker A
there. If this wood wouldn't be there, we would get false. Also keep in mind that in Python it's really important everything is case sensitive. Something is lower case or uppercase these are different objects because they are not equal because little W is not equal to large W. Right? Keep this in mind. Then you could also use the same one to check inside of the strings because strings are sequences of characters. It's like a list of bunch of different characters
130:58
Speaker A
together. So we can check if W isn't the word of wood then it would also be true because it does indeed contain in there. But if that would be capital, that would not work. You would first need to make it lower case and then you would compare it. Now lastly, we can also again use this not and this is exactly what I mentioned here. Not is kind of the opposite action. If put not in the metal concrete bricks, then we can do something, right? We can add it there. For example, all
131:26
Speaker A
right, we're going to explore more of that in the moment. For now, let's go and practice a little more. Let's talk a little bit about nested statements. And we already covered it briefly. It's like when you can put if statement inside of if statement inside of in state if statement and so on. Here's like a simple example. We're going to have panel width and panel height. Let's say that width is going to be 900 mm and the height is going to be 2,500 mm. Right? And I want to make if statements to
131:54
Speaker A
check are this allowed height. We send it details to manufacturer and then they check if panel width is less or equal than let's say 1 and a half meter. Then we're going to write width is good. Otherwise we would write here something like width is not good. But then we would also want to check the height. So in this case we can also make either separately. Let's first make it separately. If panel height is less or equal than let's say 3 m then we're going to write
132:30
Speaker A
height is good or otherwise we're going to write height is not good. So let's just execute and see. We can still see it's still freezing cold outside. I forget to uncomment that one. But then width is good and height is also good. But now we could also take that one and put it inside. Right now, if you would execute it, you say width is good, height is good. But now, if width is not going to be good, let's say it's like, I don't know, 2,00. We're going to run it. Just going to
133:02
Speaker A
tell us that width is not good, right? It's not even going to execute that part because it's part of the width statement. So, it will always depend on kind of what logic you want to do, what kind of print statement, what results you want to do. But in general, it's not that complicated. It's just kind of you need to first think about this how you want to structure your code. If I want to continue learning then I'm going to look at the next lesson as well. If I
133:25
Speaker A
understood everything in this lesson then I'm going to go to the next lesson. If I did not understand everything in this lesson then I'm going to watch it again or maybe I'm going to read the description. Right? It's it kind of depends on what kind of logic you want to do because first you need to come up with the logic and only then you need to think about how do you take this logic and convert it into the code so Python can read it and kind of follow through and execute all your steps because
133:48
Speaker A
programming is very logical thing you just need to define all the steps computer have to go through to determine kind of the end result and then it will follow and usually if code doesn't execute well or doesn't do what you want it's not because computer is doing something with their code just means that you did not define enough rules or you did not define them correctly. That's pretty much how you define logic in Python. All right, and I hope that it makes sense and you don't feel
134:12
Speaker A
overwhelmed. But don't worry, it's just the beginning. It will all make sense once you start coding more because you'll be using if statements all the time like over and over to check everything in your code. And right now, I would recommend you to kind of go through and kind of test what I showed you here. Try to use your own examples. Just use simple numbers, text, and see what you get. You don't need to go crazy now. just going to experiment with the print statements. That's more than
134:36
Speaker A
enough. And then once you're ready, I'll be waiting for you in the next lesson. So, I wish you happy coding and I'm going to see you very soon. Goodbye.
134:49
Speaker A
All right, everybody. You already learned quite a lot about Python and now it's time to actually put it to practice because practice is the best way to learn programming. So, in this lesson, we're going to create a fun exercise together where we're going to provide two colors to Python. For example, red and yellow. I want to ask it what color do we get if we mix them and hopefully it's going to return us orange or other colors if we're going to provide something different. I think it's going
135:10
Speaker A
to be really fun and great way to kind of practice what you learned so far. Now listen, first of all I want you to try it yourself. use the Google. Try to avoid using chipt because that's definitely like cheating and you're not going to learn as much and you're going to try to do it and then you're going to come back to the video and see how I do this and follow along especially if you have any questions. Now for this program, how do we do this? Firstly, obviously we need the user input. So
135:34
Speaker A
we're going to use the input function and we're going to ask for two colors, color one and color two. Then secondly, we need to look into what kind of colors do we get. And we're going to use the logical statement like if we're going to check if color one is red and color two is yellow, then we're going to get orange. If color one is blue, color, I don't know, two is yellow, then we're going to get purple and so on. And for this one, I would recommend you to limit
135:58
Speaker A
to three colors. Then it's not so much if statements that you need to write. And it's also not that complicated. So now, pause this video and try to do it on your own. And then once you kind of tried it and hopefully you're going to make it, come back to the video and see how I do this. And hopefully you're going to learn another thing or two. All right ready set go.
136:27
Speaker A
All right, welcome back. So I hope that you actually managed to create it and in case you have any questions, you can always ask in the comments or let's do it together. So first of all whenever you start creating any program it's really good to write kind of this code skeleton or soda code you can call it whatever what it means is pretty much just think logically of all steps we need to write and make it as comments. So first of all obviously we need to ask
136:49
Speaker A
for user input. Then we need to kind of calculate new color. In this case it's very simple. So it's only two steps. We could also write here for example color red plus blue. But we're going to do all of that as we code. Let's just start with the basics and we're going to begin with user input. You already know how it works. So let's write here color one is going to be input. And then in here we're going to write something like enter first color. And to make it a little bit simpler
137:17
Speaker A
we're going to limit it to three colors. We're going to limit it to red, blue, and yellow. Let's also make here like colon and like this. Now I'm going to duplicate it and here enter second color. Let's mark it as color two. Make sure you use different uh variable names. And now let's also make print statements color one and color two just so we test and that everything works correctly. Right click run. Then here in the console you can see we can click here and start
137:48
Speaker A
typing. For example, first one is going to be red then blue. And you can see we're printing red and blue. One thing to keep in mind is that whenever you work with inputs, some people can write it as capital, some people can write it as kind of uppercase and this is going to affect your logical statements later on. So for that reason, we can also add here a built-in method called lower. This case, whatever user is going to write, it's always going to be the lower case. We're
138:15
Speaker A
going to talk about built-in methods more in another lesson where I'm going to cover all of them. But for now, just know that you can apply this to your strings. So this is going to be so this is your string and then right here this is how you add lowerase so everything becomes lower case or you can also make uppercase and there are a bunch of others you're going to learn about them later all right and now if we're going to run it let's say that first one I'm going to
138:39
Speaker A
be uppercase then here I'm going to make this weird one and you can see we're getting them uppercase and this is because we actually applied it in the wrong place you see instead of applying to the results we applied it to the string which is inside our input statement. Now here everything enter first color is lower case. So we have to take it and move it after parenthesis because what happens here when you call that one this returns you a new string and when we apply lower right here we
139:05
Speaker A
apply this lower to a new string and not to the text inside of it. Just keep this in mind. Now let's run it again. Try the same thing. Red blue. In this case everything is lowerase. That's exactly what we need and it's going to be very useful in the next steps. And also you can see that here it's everything correctly. It's not lower case. It's kind of capitalized like I wanted. Great. Uh we can get rid of these print statements because we know they work. Now I can start thinking about how to
139:31
Speaker A
calculate new colors. So there are different ways to approach it. For example, first of all we could say if color one equals red and color 2 equals blue, then we're going to print purple. Going to work on print statements in a moment to make it nicer. But now here is the thing. What if instead of color one red and color two blue, user provided otherwise. So first one is going to be blue. Second one is going to be red. So therefore we also need to make the second statement. Instead of that we
140:02
Speaker A
need to kind of continue and write here if color one equals blue and color two equals red like this. We can also put it on the new line so it's a little bit easier to read. In Python that's okay because Python knows that you started your statement here and it ends here. Therefore we can move it on new lines. Now, let's run it. And we're going to try red, blue, purple. Great. Now, let's try the opposite. Blue, red, purple. So, now it worked. But this one doesn't look
140:31
Speaker A
very good. So, instead of that, we're going to try another approach. We're not going to get two colors like red and blue. We're going to combine them into a list of colors. And we're just going to very simply write here color one, color two. Going to manually create this list and specify these two colors. And now it's going to be a little bit easier here because instead of kind of checking each variable one by one, we can just write if red in colors and blue in colors,
141:00
Speaker A
then I'm going to print purple. Let's try this. Red, blue. It works. Let's try the opposite. Blue, red, purple. So now it works. And it's much easier to read. And it's also less likely that you're going to make errors because now it doesn't matter if color one or color two is red. they it's all going to be the same now so we know it's purple here and let's make the print statement a little bit nicer and first of all let's make here a print statement on the top that we are
141:27
Speaker A
starting our program going to write print we're going to write let's mix uh colors and you can see I broke something because I tried to use single quote inside single quotes it's not going to work therefore we need to put double quotes outside this way we can use single quotes inside can see now it all works we can also specify what colors we printing and for that we can use our kind of formatted strings. So I'm going to make two placeholders and then I can either use format and write color one,
141:55
Speaker A
color two. Then each of these colors is going to take a respective place. Or to make it a bit nicer, we can also use the fing. So put F in the beginning. Now you can see it's highlighted specially. I'm going to write this is color one. This is color two. Let's make a plus in the middle. And let's add another emoji with window key and dot. I'm going to write here something like lab. And lab code is fine. going to write let's mix some colors. Now when we get result I also want to
142:21
Speaker A
make a nicer kind of print statement here. So instead of that we're going to write print. We're going to make an string again. And we're going to write color one plus color two equals and in this case it's going to be purple. Let's add some purple emoji like heart is fine. Then don't forget to include your color one and color two. So you can see in the console what we printing. And also I want to add here another one. Maybe something like this. It's going to make
142:50
Speaker A
print statements just nicer. Let's print it here. Run. The first one's going to be red, blue. Let's mix red and blue. Red plus blue equals purple. Also need some separation here. So maybe [Music] so maybe let's add some dashes here like 50 dashes. And then I also want to add the special symbol in the end. This is going to make an empty line after this statement. Let's just run it. Red, blue. Look at that. We specify our colors here. Let's mix red plus blue. Red plus
143:23
Speaker A
blue equals purple. Perfect. That works. Now, this is works for red and blue. Now, we also need to specify another ones. So, therefore, I'm going to come here and say we're going to try first to check if red and blue in the colors, then do this. If not, we need to use a if statement. So we go to the next one which is only going to be executed if the first one is false. This time I'm going to write if red in colors and yellow in colors. Then let's print this print statement
143:51
Speaker A
right here. And this in this case we're going to get orange color. I just make here something orange. The heart is also works. And let's also make the last one al if uh this time we're going to mix blue in colors and yellow in colors. In this case, as you know, we're going to get our green color. Let's change the emoji as well. I'm going to write green. Oh, that one is fine. Great. Otherwise, also when you whenever you write any Python code, think about how can you break your code as you write
144:24
Speaker A
it because users will always find a way to break your code. So, let's say that instead of red, blue, or yellow, they provide something else. Therefore, we're going to write else. And we're going to write something like maybe make here an X emoji that something is wrong. I'm going to write invalid color combination. Please use red, blue, or yellow. H. We can also put it on the new line. And this one like this. Okay. Now, let's test it. It's already really good kind of program. And now we
144:58
Speaker A
can mix our red and yellow. You can see red plus yellow equals orange. This is great. Now let's try to yellow and blue. It also works also. Let's try maybe capital red and blue and see it also works because right here we added this lower. Without it, it would not work because kind of wouldn't uh notice that lowerase red and uppercase red and they're not the same words. All right, so this works pretty good. But also what if we're going to provide red and red what's going to happen? It
145:31
Speaker A
says invalid color combination but it kind of valid because we said that we can provide red blue and yellow in both of them. Let's add another statement somewhere and it can be maybe on the top. We can actually make it as the first one. So this one is going to be if and here we're going to just check if color one equals color two then we get the same colors. And we're going to make a print statement. Let's add here an emoji like art. I'm going to write you mixing the same color. And we can also
146:02
Speaker A
make the same print statement and write here color one plus color two equals. And in here we actually can write color one because they are equal, right? It doesn't matter if color one or color two. And let's say I also want to keep this kind of emoji, but I don't know if it's going to be red, blue or yellow. So I need another if statement inside this if statement. This going to be kind of nested logic. Let's just make some more space. And we can specify an emoji, but we need
146:30
Speaker A
an if statement to verify is it red, blue, or what is it? Yellow. To do that, we're just going to check if color one equals red, then emoji is going to be red heart. If color one equals blue, then emoji is going to be uh blue heart. And lastly, al if color one equals yellow emoji is going to be yellow heart. And now we can use this emoji inside of here. So instead of that one, we're also going to make a placeholder and write emoji. We're going to test it in just a moment.
147:13
Speaker A
Let me just verify that everything is fine. So our user is going to specify colors. We're going to lowerase and we're going to make it a list. Then we're going to make this print statement. And then first of all, we're going to check are these colors the same? If they provided red and red, blue and blue, yellow, yellow. Then we want to specify what's the emoji. And it's also great to first specify your variable with an empty kind of string or maybe even none. And then
147:37
Speaker A
kind of go through if statements because if it's going to go through all of them and it's not going to match, you're going to get an error and it's not good. Therefore, it's always good to first specify kind of variable and then kind of override it. Then we're going to specify, okay, these are the colors you get. And then here are our rules. Red plus blue, red plus yellow, blue plus yellow. You can add more colors to your program, but I'm quite fine with three ones. It's going
148:00
Speaker A
to be enough. And now again, let's go and test it. Let's run it. I'm going to write red. And you can see, let's mix red plus red. You're mixing the same color. Red plus red equals red. Obviously, it's a very simple program, but you can already see how we can create logic and affect what user provides, what we do with this, and so on. Let's try something that doesn't exist. For example, purple and green invalid color combination. Please use red, blue, yellow. Okay, that works. And
148:29
Speaker A
let's make something like blue and yellow. And we get blue plus yellow is going to be green. All right. And just like that, you created your first program by just knowing a few Python concept we covered so far. During that lesson, we used variables, data types, input print format strings logic obviously, and that's already enough to begin kind of coding simple programs in Python. And the more you're going to learn about Python, the more you will be able to create. And it's just the
148:54
Speaker A
beginning. And now I want you to actually practice. I hope that you actually try to do it yourself or at least if you followed my solution, I want you to go and code it as well. Try it out. Code, try to experiment. Maybe instead use different colors and just make sure that it works because by coding you're going to learn so much more than just watching me code it for you. All right, guys. And once you're going to finish with this exercise, I'm going to be waiting for you in the next
149:17
Speaker A
lesson where we're going to make a second exercise because practice is the best way to learn programming. So, happy coding. Going to see you soon.
149:30
Speaker A
All right, welcome back. I hope that you managed to complete the previous exercise and that you had fun coding it. And now I want us to make another Python program. This time we're going to create a guessing game. It's also a fun one and not so complicated. So it will be great for you to practice. The idea is that we're going to hardcode a number and then we're going to ask user to guess it. And if they guessed lower or higher, we're going to let them know that they
149:51
Speaker A
are higher or lower and then ask them to guess again and hopefully they will get it right. Same as before, I want you to first of all pause this video and try to do it on your own. You can use Google, but please avoid using chat GPT because it will just give you the result and you will learn nothing. You actually learn the most when you try to do something, you get an error and you try to solve it. And then once you're ready, come back to this lesson and let's code
150:14
Speaker A
together. And I will also introduce you to a new Python concept in the end of this video to make this program even better. So, ready, set, code.
150:33
Speaker A
And welcome back. I hope that you managed to create this guessing game on your own. But even if you got stuck or confused, don't worry. I'm going to walk you through all the steps and explain all the logic as we code. It will be also useful even if you manage to do it on your own because I'm going to explain how I will do this. And as I mentioned in the end, I'm going to mention a new concept. So let's open the code editor and create our guessing game. So first
150:55
Speaker A
of all, let's start with brainstorming. So to create it, first of all, we need to define some rules. In this case, we're going to specify the hard-coded secret number. Then we need to ask user for input. And lastly, we need to check results if they guessed correctly, not correctly, and so on. And this is kind of the base we need to create. Let's begin with the first one, rules. First of all, we need to create secret number. And let's make it a seven. For example, you can set any
151:24
Speaker A
number you like here. Now we have the number. Now we need to ask user to guess it. So for that we're going to use input and we're going to call it guess equals input. And we're going to say guess the secret number. And it will be best if we're also going to see between which numbers. Let's say between between 1 and 10. This is going to be useful for the user. And later we can also change it to something else. Let's add here maybe emoji like this. Now we have our rules
151:54
Speaker A
which is the secret number. We have our guess. Now let's check the results. Now first of all we're going to check if guess equals to secret number equals then write correct. You guessed it. Again let's add here another emoji something like celebration like this. And now this is the absolute basic but don't worry we're going to extend it even more. Let's just test what we get. So first of all I'm going to guess like five. Obviously I didn't make any print statements. I get nothing. But now let's
152:25
Speaker A
try to make seven. I get nothing again. But my secret number is seven. So here is the thing. Uh remember that when we use the input function we're getting a string back. And this is in integer. When we check if our string and integer the same obviously we're going to get false because strings and integer they're completely different things. So there are two ways to do this. One way would be we can kind of write it as a kind of string then if we would kind of
152:53
Speaker A
guess it then it's going to work correct you guessed it or we could leave it as an integer and we could also convert this kind of guess into an integer and for that to make it simple let's make a new line integer guess this way it's going to become an integer now let's run it write seven now it also works so now we have absolute base for the program now let's extend it so So this is what happens if they guessed it correctly. Now we need another if statements. If
153:20
Speaker A
guess is more than secret number and this is why I wanted to leave it as an integer because I want to use these operators. We cannot use them on the strings. We need an integer to use them or a floating number. And also we need here l if guess is lower than secret number. Now we need to definitely write here something because in Python we cannot leave L if statements empty because if you try to execute it you're going to get an error saying expected and ident if statement. It means that
153:47
Speaker A
you need to write here something. So if it's more let's just write here X we're going to write too high and then try again. And in this case we're going to write too low. Try again. In this case, we could also instead of using that one, write just else because if it's not equal, if it's not bigger, then the only logical conclusion is that it's going to be else because there is nothing else, right? So, it's up to you. You can leave it like this or you can leave it like
154:17
Speaker A
this. I'm going to leave it like this. It's less code. It's always easier to read now. Let's try it again and see what we get. So, guess the secret number between one and 10. This time, I'm going to try five. Too low. Try again. Ah, damn it. Now, try again. This time I think maybe it's going to be like nine 2 pi. Try again. Okay, run it. This time let's try seven. Boom. You guessed it correctly. Good. Now it works. But we still not finished because for example here we say guess the number
154:47
Speaker A
between one and 10. Let's execute it. And users will try to make a 100 too high. Maybe they would not notice this one. Right? We can actually make sure that these rules are also respected. For that let's go back to our rules. I'm going to specify our minimum is going to be one and maximum is going to be let's make it 20 this time. Now it's also a good idea that in here that we don't have to always write okay 20 since this one says 20 we write here 20. This is never a good idea. Instead
155:18
Speaker A
use an string and make it here this is going to be minimum and this is going to be maximum. Then no matter what rules you set it's going to work. So let's kind of leave it like this. Going to say guess the secret number between 1 and 20. And now before we're going to check the results. Uh we're going to check the input. And here we're going to write if guess is lower than the minimum number or guess is more than maximum number. Then we're going to write here let's make
155:51
Speaker A
this emoji. And we're going to write incorrect input. guess a number between this and this and obviously we need to make it an F string and then write here minimum and maximum right so first of all we're going to check if the number is even correct and then later we're going to check here the results let's write here I like this that's fine and also notice that this one if statement and this is an if statement there's going to be a little issue let's try this guess the secret number let's
156:25
Speaker A
write a th000 and see incorrect input gets a number between this but then we're also saying too high try again and this is because we have an if statement here and if statement here these are grouped together and only one of them is going to be executed but this one is not grouped together so first we try to execute this one and no matter if it's true or false we still going to try to execute that one that's not good therefore let's combine them just going to add here if statement
156:51
Speaker A
this way it's all a single logic and only one statement can be executed be this, this, or this. This way, it's not going to continue when we fail. I'm going to run it right here at 1,000. Let's see. Incorrect input gets a number between 1 and 20. It's not telling you if it's too low, too low, or too high. Just telling you that you gave the wrong number. Now, another thing, actually, I just realized it. Um, remember that I told you that you cannot use any names for
157:17
Speaker A
your variables. And you might see this shadow built-in name, minimum, and maximum. I completely overlooked it because uh there's a actually built-in functions minimum and maximum. And now we can see they're not highlighted anymore because I overwrote their meaning. If you're not using them, that might be okay. Still not ideal because you can see once I comment it out now it became a function. Now it works as it's supposed to. So it's not ideal that you use min and maximum values like this.
157:43
Speaker A
But if we're going to add here an underscore, then that's going to be just fine. So let's just make sure that we replace it everywhere. here, here, and here. And same here, here, and here. I think that's all we guessed it. Now, we added even more logic to our game. We have this kind of minimum and maximum rules. Now, let's try it. Let's try maybe this time 15. Run. Okay, so this time 200 incorrect input. Great, it works. Now, this time we're going to write one, too low.
158:18
Speaker A
Now we're going to write uh 16 too high. And obviously if we write 15, yay, we guessed it. It's correct. Now, don't you find it quite annoying every time to come here and click on run. This is not really fun. And also there are no incentive to guess it on the because you have infinite tries of that. What if we had unlimited amount of attempts and for that we need to use a new concept we haven't talked about yet. Loops. A loop is when you're going to iterate for the same action over and
158:47
Speaker A
over. doesn't have to be the same, but the point is that you can iterate multiple times. This is why it's called a loop. You, for example, you can write print hello. You're going to loop five times. You're going to print hello, print hello, print hello. This is what loops are. And there are two loops in Python. We're going to talk about them in the next lesson. For now, just know that there is a for loop. And we can iterate multiple times. Now, somewhere here where we're guessing, I'm going to
159:09
Speaker A
initiate a loop. Going to write for i in range of five. Then, we're going to do something. There are two new concepts. First of all, loops. This is how we create a loop for i. This is the name of the variable in container. And this range of five is just going to create 0 1 2 3 4 number. So, this is just a built-in function to create a sequence of numbers. I'm going to talk more about this in the next lesson. Don't worry, just follow along. Now, I'm going to put it like this.
159:38
Speaker A
And now we also can add here a new print statement. Now, let's make here something like 50 dashes so we can separate all our tries. And we get then we're going to add here an F string. We're going to write attempt and then number one out of maybe five is going to be fine. And this I is going to go here. Listen that I'm going a little bit ahead of myself. I'm going to explain everything in the next lessons and in a moment as well. But first let's just see what we get. Look it here.
160:07
Speaker A
Attempt zero out of five. Guess the number. Okay. This time I'm going to try one. I failed. Okay. Attempt number one out of five. What about five? Maybe 20. Maybe 14. You see, now we don't have to run it every time. And now users have actually five attempts. So it's actually becomes like a game. You have to guess it right now. So let's try the final one. Uh I don't know. I don't know. Let's try 16. Ah, I failed right too high. Try again. But you see if I execute it correctly and I guessed it
160:37
Speaker A
correctly, you see the game is still continuous. I can guess it over and over over. So we need to do something about this. And for that we need to kind of stop execution of our loop. For that we use the break statement. Let's find the place where we actually win right here. And after that we write break. So let's start from the beginning. This range is going to just give you a list of 0 1 2 3 4 like this. And then we iterate for i in range of this. And I is going to become each
161:06
Speaker A
number one by one. On the first iteration it's going to be zero. On the second iteration it's going to be one and so on. Then we say here the attempt it's also great to make it plus one because it starts from zero and we want to start counting attempts from one. Then we're guessing then we're checking are the numbers correct. If it's the not correct well we need to say that okay it's an incorrect input. Guess a number between this and that then we're checking results. If you guessed
161:31
Speaker A
correctly we say yay you guessed it and we stop execution. Whenever we hit the break it looks the loop that we executing and stop execution. Right? we kind of reached our conclusion. We found the answer. That's it. We don't need to go over and over. But if not, we're going to say, okay, no, it's either too high or too low because otherwise it will be correct. And now let's run it again and guess it. Okay, this time 1 20 15. Oh, we guessed it. You see it stopped execution. These are what the
162:01
Speaker A
loops are about. You can make multiple iterations and in this case it's great for this game. Lastly, before I let you go, you already know the answer and it's not fun. So, I'm going to go again ahead of myself and we're going to write import random. Random edit is a built-in module into Python. When you need something random like random choice or random number, I'm going to write here that I want to use random and there is a function called rand int which is random
162:29
Speaker A
integer and it has to be between minimum and maximum values. So we're going to write minimum and maximum values because we already have them here. Notice that unresolved reference min. But I can see it right here. And the thing in Python is that you it always from top to bottom like this. So this minimum it doesn't exist at the line five because line five look at line four 31. Okay. Minimum maximum doesn't exist. I don't know what to do. Instead of that, let's put it
162:59
Speaker A
just on the bottom like this. And now look, now it exists because now min and max it looks above and like oh here's the minimum here's the maximum and it just inserts these values and now that's a proper game and you don't even know the answer. So let me try to play it run. Yes, the number between 1 and 20. Okay, let's start in the middle 10. Okay, too high. So we are working with low number five. Too low. Okay, six. I don't know. Eight. No, last attempt. Okay, seven or nine? Seven. I didn't
163:30
Speaker A
guess it. Let's try again. Again. I'm going to try 10. Too low. Try again. Okay. 15. Too high. 12. 13. 11. Finally. I get it. It's 11. I won the game on the final fifth attempt. So, you can see now it's a game. I don't know the answer. And it's actually you you actually start playing because when you knew the answer, you had to go, oh, I'm going to go a little bit above down. Here you actually have to guess it. And you have five attempts. And you saw I made it on
164:00
Speaker A
the second try. And let me know in the comments on which try did you make it work. Okay guys, I hope that this was a fun game that we created. You learned a little bit more about all the concept that we learned and about the upcoming concept of the loops. And again, if you found it confusing, I'm very sorry. But we're going to talk in the next lesson cuz I want to slowly introduce you to this. And now we're going to go really deep into that. So happy coding. Make
164:22
Speaker A
sure you code your game and see you in the next lesson. Goodbye. All right, time to talk about loops in Python because they allow you to execute the same piece of code over and over, which can be extremely useful. For example, imagine that you have a list of file names and you would like to go over them one by one and then determine is it an image or not. That's where we can use a loop. But don't worry, let's jump straight into the code and I'm going to explain everything that you need to know about loops, how to work with them, and
164:52
Speaker A
a few little details that you definitely need to know about. To create a for loop, we're going to write for i in container and then we're going to write print i. Going to explain everything in a second. Let me just create a container. It's going to be a list of numbers. So to create a for loop in Python, we're going to write the keyword for. Then we need to create a variable name which will represent each item one by one. In this case, since we're referencing this list, at first I is
165:14
Speaker A
going to be one, then I is going to be two, and so on so on so on until we go through all items one by one. And each item is going to execute whatever code we're going to write in the code block. Then we write a special keyword in and we need to point to iterable container where we have some items that can be iterated over. It can be string, tpple, list, dictionary, sequence, ranges and so on. Just logically think that you can iterate over items and then after that
165:37
Speaker A
we put a colon and we need to specify a code block which will be executed for each item. For example, at the moment we have print I but I can duplicate it a few more times with Ctrl D. And if I'm going to execute this, you can see that each number is going to be executed one by one three times. We can also add here some logic. For example, if I equals to two, we're going to write number two. So let's run that one. I can see at first it's going to be 111 22. Then since it's
166:04
Speaker A
a two, we're going to execute number two and then so on. So we executing the same piece of code on each item in this container. And this is really important in programming. Also before going to move on, keep in mind that this can be named anything. I is just kind of the placeholder stands for item or iteration. But you can write here for example item. But if you do just make sure that you use the same name everywhere inside your code block. I'm holding alt to select the multiple
166:29
Speaker A
places and then place it. In this case actually it would be much better if that's going to be a number. Then just make sure that you use the same variable everywhere. Then we're going to click on run and it's going to work just the same. All right. And this is the basic syntax for the for loops. Let's select everything with control and division key. We're going to comment it out. And now we're going to go through some examples so you better understand how it
166:49
Speaker A
works. And in the first example, we're going to iterate over a list. So I'm going to put here a list of materials. And you can see it's wood, steel, glass, bricks. Now I can write for material in list of materials. For example, we can print this material. I'm going to execute. Obviously, I'm going to go and print one by one. I can also do something else. For example, I can write here material lower, then material upper. Then you can see I'm iterating. I'm changing things. And I can also kind of
167:15
Speaker A
add more logic if I want to. So it's very common to iterate over lists. Now let's comment this out. I'm going to move to the second example where we're going to iterate over a string. For example, let's say that we have a variable hello Python hackers. And now I can iterate for each character. I'm going to write for character in text. And I'm going to print character at first. And you can see each kind of character printed one by one. We can also write here something more interesting. For
167:41
Speaker A
example, lower character is going to be character lower and upper character is going to be character upper. And now let's write here something like lower. Then we're going to make some dashes and then we're going to write upper character like this. Now right click run. You can see here now it looks already much nicer. So hello Python hackers. So as you can see since a string is a sequence of characters we can iterate over that as well. Now for the next example let's iterate over
168:09
Speaker A
digits. For that, let's create a big number. It's just going to be something like this. I'm going to write for num in big numbers. We're going to print this number. So, let's execute this. And you'll notice that integer object is not iterable. What it means is that you cannot iterate through the number because well, this is not something that you can go through and kind of dissect it. However, if we would really want to and kind of take each number one by one, we would just
168:35
Speaker A
need to convert it into a string. And we can also point here that this is a string of a number. And then that's going to work. Now we're going to print all the numbers. And if you would want to do some math, we would need to convert it into an integer. Something like this. And then we can for example create a square. We can also create a cube. And then we can print all the results. So it's going to be number square and cube. Let's remove the first one. Run it. Now we can see that we
169:00
Speaker A
iterating through this large number and we're taking each digit and calculating the square and cube. So there's always a way to kind of iterate through things as long as it makes logical sense. In this case, we had to convert integer into a string. So we can iterate over it. However, in case you wanted to iterate lots of times, for example, if you would want to write for i in 100 to execute something 100 times, you need to use the range instead. So let me just add here iterate over
169:26
Speaker A
range. And for that, we're going to write for i in range. Then we're going to specify a number here. Let's print i. So this range function is going to create a sequence of numbers. In case of 100, it's actually too much. Let's make 10. In case of 10, it's going to make you a sequence from zero to 9 including. So 10 is going to be excluded. Let's run it. And you can see 9 is the last one. Zero is the first one. Also in here there are different options how you going to run it. Example in this case
169:53
Speaker A
you just say 10. So it's going to be from 0 to 10 excluding can also say I want to from 10 to 20. So it's going to be start and end number. then we're going to include 10, but we're going to exclude 20. So we could make 21 if you would really want to see 20. We could also say that you know what we want to go from one to 200, but we want to have a step of 10. So every 10th item is going to be taken. Let's run that. And you can see in this case, maybe it's better if we're going
170:21
Speaker A
to start with the zero then run. Then you can see from zero 10 20 30 40 and until 190. But if we're going to make it 201, you can see that 200 is also going to be included here. This is really powerful function and it's very often used. Example, you don't always have to use the numbers. Maybe you just want to print print hello world lots of times. So let's print it, I don't know, 50 times. We would do it like this. You can see hello world over and over. There's
170:46
Speaker A
also more common way to use this range function. And for example, let's say that you want to create a list of floor plans. And for that, you would create for example for i in range. We're going to go from 1 to 11. So it's up to 10. Plan name is going to be and we're going to call it something like floor plan. And then a number right here with an f string. We're just going to write I. And then for the floor plans, we're going to append each item one by one. Then in the
171:10
Speaker A
end, of course, we're going to print our results. And you can see we're going to create a list of floor plan numbers from one to 10. can also be as I told you earlier let's say from one to 100 in a step of five for some reason maybe we want to do that you can see it's going to be 1 6 11 and so on I'm going to keep the first option and these are the basic examples when you want to use the for loops you can iterate over list sequences you can also use tpples dictionaries even create ranges oh and
171:39
Speaker A
by the way when you use dictionaries you're going to iterate through the keys unless you explicitly say that you want to iterate over values okay but also there are a few special keywords inside of for loops that we can use such as continue and break. Let's start with the first one break. So we have an option to break out of four loops. For example, let's say that we have a random list of numbers and we want to find the first even number in that list. For that we're
172:01
Speaker A
going to write for n in numbers and then we need to determine if it's a even or not. For that we need to use a special syntax. We're going to write if n mod 2 equals to zero. What it tells here is that if we're going to divide something by two, we're not supposed to have any remainders. For example, if we're going to do it for a number seven, we're going to have one because it's going to be 6 - 2 - 2 - 2 and one is the remainder. We can also add here n equal to zero. So,
172:30
Speaker A
we get only actual numbers. Let's actually put here a zero as well. And if that happens, we're going to write that uh first even number is going to be this. Let's make f and we're going to put here n. And then let's print finished. Now, if I'm going to execute this, look at that. I'm going to go through the first event number 40. First event number is six. But I don't care about the second one. I only want to have the first one. So, it's very common when you
173:00
Speaker A
search for something in a list or some databases, you want to stop executing once you find your answer. But that we're going to use the special keyword break. Whenever you're going to reach this statement in any loop, it's just going to cancel the loop altogether. So you're gonna the first item you're going to go through, second item you're going to go through, then the third one, so on so on so on. And then eventually you're going to get to this number. You're
173:22
Speaker A
going to hit the break and it's just going to stop execution, right? It's just going to print you finished. So let's run it and see first even number is 40. Now it's finished. So this way you can stop your loops at any time and you can always create different conditions how you want to do this. And another special keyword also is the continue. So let's actually comment it out and look at the continue. So the idea with continue is that you're going to iterate through items and if
173:46
Speaker A
something is not really what you want, you can just ignore it and go straight to the next iteration. And it can be really useful in certain cases. Let's just have a here an example. Let's say that you find a wallet and then there are these notes. But you want to calculate how much is the total kind of value inside. For that we could create total equals zero. And then for note in wallet we're going to add it to the total and then we're just going to print total is this many euros. Just make sure
174:14
Speaker A
we put here total. So we're going to execute it and you can see total is 120. However, notice that there is not a real note inside. There is no 25 note. So for that we can add a condition. Let's add here a tapple real money so we can verify if it's actually legit because you can see there's like 5 10 20 and so on. So you could either create if statement if not in real money then we're going to calculate it right it's going to now 95 or we could also make it
174:42
Speaker A
different going to write if note not in real money then we're going to continue and otherwise going to be like this. So if I'm going to execute, it's still going to get this answer. And what's happening is when you use the continue, it just kind of goes to the next iteration. So in this case, when it was five, it would go all the way through 20 all the way through. And then when it's going to be 25, it's going to hit this if statement and continue will just jump
175:09
Speaker A
straight to the next number 10. So 25 will never reach this total. So this is how you use continue. Or you could always kind of use this if statement and do it otherwise. And these are the basics of working with loops. You just have a container of items and then you take one item after another and do some action. Can add some logic to skip it to break it and so on. And pretty much it's just creating a loop to execute the same code over and over. Now this were all
175:33
Speaker A
the simple loops. Now let's have a look at a nested loop. For example, think of a structure of a course. Let's make it as a list and then inside there's going to be another lists which represents each module and inside there are going to be lesson. You can see 1 one one two 1 3 1 4 and so on. Let's say that I want to iterate for this and I just want to make a few print statements. So I'm going to write for module in course. First of all I'm going to write here
175:56
Speaker A
something like starting module and I'm just going to put here the module like this. So in this case we're just kind of iterating over lists inside of a list right so first second and third. Now for the nested loops it just means that we keep iterating inside. And in this case I can also write here for lesson in module because I can also do that. I'm going to write here print and let's write something like completed this lesson. Now let's execute that one. Now we can
176:26
Speaker A
see we're starting the module then completed lesson one, two, three, four, then starting another module. Let's actually also add a few more print statements. Module is complete. And I'm also going to make some separation so it's clear what's going on. So here's the code. Now when I'm going to execute now you can see starting module this one then completed lesson one two three four complete then the next one next one and so on. So we can see it's a nested loop. This is
176:52
Speaker A
example where you have already data structure with nested kind of lists. Let's have a look also on another example where you need to generate a sequence of kind of combination of two numbers. Think of coordinates like X and Y or maybe like when you work in Excel you often work with columns and rows. Let's just write here for x in range of 10 and then for y in range of let's also make it 10. Then we can print x and y. Now just pause for a second and think what kind of results are you going to
177:21
Speaker A
get because as you know this is going to give you a sequence of numbers from 0 to 9. Let's actually make it 11 so it's clear. And same is here. And now you're just printing x and y. Just think a bit. And when I click on run here's what's happening. First you can see x is always zero then we iterate over y's. So y is increasing then x increasing by one and then you again iterate 10 times over kind of y and so on. So this is because right here is the loop for the X
177:51
Speaker A
and when X is for example one this then we initiate another loop and this one has to go one 2 3 4 until it kind of finishes the loop and only then one becomes two and then again this repeats for 10 times and only then this becomes three and so on. Therefore this is kind of a nested loop to generate these kind of items and for me it was mostly used when I work with Excel. I need to navigate through the rows and columns to write data and read it or also sometimes
178:18
Speaker A
with coordinates when I was placing items in the software I was working and I was just placing first in one column then in second column and so on. We could also add here for Z in range of also 11. But this way would be a total overkill. Can see now we're going to get all these numbers. So the more you add it's going to be get more complex. But it's just an example to get an idea what the hell is a nested loop. And now before I'm going to let you go, I also want to cover
178:45
Speaker A
while loops. This is another type of loops in Python. Because you see the for loop, we can use it on the fixed kind of length of a container. Let's say we have a list with 10 items or maybe a string with 20 characters or range of 100 numbers. It's a fixed amount of numbers we can go through and then it's going to end. However, the while loop it's conditional. You create condition and it can even run infinitely. So if I'm going to write here while true print let's say hello
179:12
Speaker A
when I'm going to execute that one you can see it's going to infinitely print because there is no condition when to break. And if you would like to create a condition, let's for example create here count zero. Then here we're going to write attempt. And then we're just going to print here count. So this way obviously if we execute nothing changes. Count stay zero. So it's going to be an infinite loop again. And as a beginner be careful. You often end up in infinite
179:38
Speaker A
loops like this. Now for that we actually need to define a condition. Let's say that while count is less than 100, we're going to iterate that. Again, if I'm going to run it, still going to be an infinite loop. However, if I'm going to start changing count, for example, adding one on each iteration. Now, if I'm going to execute, you can see it's going to grow until 99 and it stops because condition was met because here it says while count is less than 100, keep executing. Once it's 100,
180:06
Speaker A
stop. We can also write here while true. So, we're going to write if count equals to 100, then we're going to break. Now, if I'm going to execute, it's going to write me attempt 100 because a print statement before I check. But if I move it down like this, now it's going to reach the same result because I'm breaking out of the loop once I hit the condition. And this is why while loops are so powerful when you need kind of infinite number of times or you don't
180:30
Speaker A
know how long it's going to take. All right, let's go to the final example. And it's going to make so much more sense. Let's say that you have 100 tickets to a concert and you want to sell, but you don't know how many people going to buy because somebody going to buy two tickets, somebody going to buy 10 and it's going to be different number of people. Therefore, it's a great use for the while loop. I'm going to create an infinite loop with a while true. And
180:51
Speaker A
then we're going to create an input and ask user how many tickets to buy. Then since it's a string, we're also going to convert it into an integer. And we're going to pretend that we always provide correct numbers. We're not going to kind of protect it from being a string and so on. And then when user provides a number, we can actually sell tickets. So we're going to write tickets minus equals n. Can write here print uh tickets left. Going to make it an f string and print
181:17
Speaker A
however tickets is left. So going to run and can see by 10 90 left. Let's 80 then 100. And you can see I'm actually going forever because I haven't made any conditions when to stop. Also I go with negative which not supposed to happen. Let's fix that. The moment I'm going to sell all tickets. So it means if tickets equals to zero, then we're going to write sold out and we're going to break out of the while loop. The while loop also has the break and continue statement. And once we reach
181:46
Speaker A
the break, we're going to stop execution. Also, you saw that we had negative numbers. We can avoid that as well. So we need to check here if tickets minus n is less than zero. It means that we don't have enough tickets. So, we're going to write print not enough tickets. Try again. Also, let me maybe add here emoji. So, we know it's a warning. And now the important part is we're going to write here continue. It's actually a very good example. So, what's happening
182:15
Speaker A
here? We're asking how many tickets they want to buy. Then we check if we don't have enough tickets. We're going to just say that and continue. So, the moment we going to reach continue, it's just going to restart the loop from start again. So, we're going to ask again. Otherwise, it's going to go through and we're going to sell tickets here. At the moment, we're going to reach zero. Just going to break out of the loop. So, let's run. I can see we're going to buy, I don't
182:38
Speaker A
know, 50 tickets, 20 tickets. Now, I want to buy 100. Not enough tickets. Try again. 35. Not enough again. Then 29. One. Then we sold out. And execution is finished. You can see. So that's a really good example of how to use the while loop when you don't know how many iterations you need and it's dependent on the other input. In this case, we can also see how continue and break can use. All right, and that's a wrap on the loops in Python. In general, there are
183:05
Speaker A
two types of loops. And if you work with a fixed amount of items, try to use the for loop. And if you don't know how many iterations you need, use the while loop. But be very careful with the infinite loops because they can crash your software as well. And now go through this lesson, try all the code samples. They will help you understand loops which is really important. You're going to use loops almost in every single Python code you're going to write in the
183:26
Speaker A
future. So I'm going to wish you happy coding and then I'm going to see you in the next lesson where I have prepared an exercise for you. So see you soon.
183:38
Speaker A
Welcome back. You just learned about loops in the previous lesson and now let's practice. I want you to write a simple script to calculate numbers in a Fibonacci sequence. And don't worry, you don't need crazy math for that one. You just need to know how to add two numbers together because in Fibonacci sequence, each number is a sum of the previous two numbers. So it's actually very simple to do. And first of all, I want you to try to do it yourself. Define first two
184:02
Speaker A
numbers and then create a loop to print a sequence of the first 10 Fibonacci numbers. And then if you want to play a little bit more, you can make first 50 numbers and write the name of the large number like is it a million, billion, trillion, bajillion depending what you get. Ready, set, code.
184:27
Speaker A
And welcome back. I hope that you had fun creating this Fibonacci sequence exercise. And now let's do it together. And I'm going to explain everything as we code. So right here you can see a sequence of Fibonacci numbers. And as I mentioned previously, each number is the sum of the previous one. So 1 + 1 equals 2. Then the next one is 1 + 2 equals 3. Then 2 + 3 equals 5. and so on so on so on. You get the idea. It's not that complicated. And to create it normally,
184:53
Speaker A
we have to define first two numbers. It's going to be a equals 0 and b equals 1. So we start with these numbers. Now we need to keep adding them and calculating the next number depending on the amount of iteration we want to create. So obviously we're going to use a for loop for that. We're going to write for i in range let's say 10. And in here we need to create the sequence on Fibonacci numbers. And we can right away write C equals A plus B. Now let's print C and see what we get. So
185:21
Speaker A
obviously if I'm going to run it through I'm going to get just once because I'm not changing any values. And if you're going to look right here first when we're going to make the calculation and get the first number we need to shift all our numbers. So this becomes a and this becomes b. And this is very simple to do. It's going to be a equals b and b equals c. So we kind of shifting numbers. So this will kind of help us to actually change numbers. Now when we run
185:48
Speaker A
it, we're already getting Fibonacci sequence. However, we are missing the first two numbers because we would like to start with the zero one and so on. And it's actually much simpler than you think because all we have to do we just have to print a instead of c because right here a will become b, b becomes c and it kind of keeps going and it will actually get all the numbers. Now let's run it like this. And now look at that. 0 1 1 2 3 5 and so on. So now we're getting an actual Fibonacci
186:17
Speaker A
sequence. And you can see it's not that complicated. It's just a few lines of code. Literally, you just need to know how to add two numbers. Now what if we're going to make a sequence of 100 Fibonacci numbers. Let's run it. We're already getting crazy numbers. And it's crazy how quick we get these numbers in a Fibonacci sequence. But now I wonder what kind of number is that? So let's format it a little bit better so we can kind of see and understand what it is.
186:40
Speaker A
There's a special syntax in Python. We're going to use the fring. We're going to put here an A. And we can put here a colon. And then there are special things how to define. We can show for example two decimals number and so on. And here I want to put this delimiter and D. So this is going to be for digits. And this will tell Python that I want to kind of mark my thousands like this. Let's run it. And you can see now it's already easier to read. But now I still not sure what it is because this
187:06
Speaker A
is this is thousands, millions, billions, trillions. I have no idea what these are. And I really want to see the number. So let's create here a list of large numbers. And I'm just going to paste it right here like this. So you can see that at first we get nothing. Then there's going to be thousand million billion trillion quadrillion, blah blah blah, and in the end bajillion. Just to be clear, I made that one up, but it sounds cool. Now, how do we connect these two? And
187:33
Speaker A
actually it's not that complicated either. So first of all let's turn it into a variable. So we can look into that and we can count all the commas because now if I'm going to print the number run it can see I can count. There is 1 2 3 4 5 there are six commas. So let's count this count commas equals num. We're going to use the function count which you're already familiar with. And we want to count our commas. Now let's put it like here. Run it. You can see there is six commas. So now we
188:05
Speaker A
can use this number six five and so on to get a number from this list because this is going to be zero right cuz look at that here in the beginning we're not getting any thousands and it's going to be zero. So we're not going to print anything. Then the moment we're going to print one comma it means we work with thousands. Two commas let me see right here it's going to be millions. Three commas billions and so on. And it's actually very easy because this is exactly the number we're getting right
188:31
Speaker A
here to get the right item from the list because when it's going to be zero we get nothing because this is our first item then one is thousand and so on. So let's get here large num equals I'm going to take a list of this number names and we're going to put here count commas like this. This way we should get the name of that number. Let's replace it here as well. Also I like to align my equal signs. It makes it easier to read. Now run it and have a look at that we're
189:00
Speaker A
getting here. We can see these are the thousands. These are the millions. And now we can know that the last one we're getting is quantilian. Now let's make it a bit crazier. 175. Run it. And that's how we get to the bajillion number. Again, I made that one up. But before that, we can see there is the before that there is nonilion and all these kind of crazy names. And that's how we use loops to create Fibonacci sequence. It's a good exercise in my opinion. And if you haven't coded
189:30
Speaker A
it yet, I strongly recommend that you do. It will help you understand loops and working with list a little bit more. I wish you happy coding. And once you're done, I'm going to see you in the next one. Goodbye.
189:54
Speaker A
If you start copy pasting the same code over and over, you're doing something wrong. Programming is all about efficiency, and that means reusing your code whenever possible. This is exactly where custom functions come in. You take a piece of code, you wrap it in a function, you slap a name on that bad boy, and then you can call this name to trigger the code over and over. So you're not going to copy paste the code. You're just going to call the name of the function, and it's going to refer to
190:21
Speaker A
this code and execute it again and again. But Eric, I actually have different word here or there, or maybe I have a different list of items. Well, that's also fine. Don't worry. That's exactly what arguments and parameters are for in a function. Think of Python's built-in print, for example, function. It doesn't always print the same message, does it? You specify what you want to print. That's where you provide an argument. The same goes for custom functions. You can define your arguments
190:48
Speaker A
and then they will be referenced inside your code. Listen, that might be a little bit too much to take in. So, let's break it down. Let's go into PyCharm and we're going to start with the basics. So, first things first, syntax. How do we write our functions? Let's say that you have a message hello beam world. Going to start very simple. So to define a function we need to use a very special keyword called def which refers to define define a function. Then we need to specify a name of that
191:16
Speaker A
function. This is like a name of a variable. Same rules apply. And here I'm going to say I don't know uh say hello. You can see it's already in blue. Now we need parenthesis and we need to start our code block by specifying the colon and then to include the code inside the function. We're going to use tab or four spaces to move it inside with identation. Now this print is part of the function. So we define a function. It's called hello. So and print is emit. So we can provide arguments. In this
191:48
Speaker A
case I don't want to have any arguments. Now let's go and run it. Click on run and nothing happens. Well, this is because we didn't call our function. We defined it as a template, but now we need to use it. So, we have to call this name. We're going to say say hello. And I also need to use parenthesis. So, this is how you create a template for your function. You define what is your function. What does it do? But this is how you kind of call it and say, hey, execute this function. And maybe you
192:17
Speaker A
know what, execute it again and again. Now, I'm going to run it and hello beam world three times. That's great. That's fine. And again, we're going to start very slow and we're going to increase complexity of our functions. This was the very first one. Syntax basics. Now, let me comment everything out. H control. Click on the slash. Now, the second one is going to be function with arguments or parameters. You're going to hear people using parameters and arguments both ways, but I'm going to
192:47
Speaker A
explain in a moment what's the difference. Now, this time, let's say that we want to make a function to say hello to a person, right? So it's going to be a print hello and then the name. So with the format, we're going to provide a name and we need our name is going to be Eric. Okay, I run it. It says hello Eric. Let's make it a bit more exciting like this. Now to make it a function, we write def. Then we write here, I don't know, name of the function is going to be greet
193:16
Speaker A
parenthesis colon. Now we select everything that we need to be part of the function. Click on the tab to move it kind of inside. Now this is my function. So I can say greet. Let's say greet two times and run it. Hello Eric. Hello Eric. That's great. How do I change this Eric? Right. I have this name right here. And I don't really like that it's always Eric. I want something different. So instead of that I'm going to write it here. Name. Okay. So this is like a variable we define. In
193:50
Speaker A
reality, this is a parameter. So the big difference between parameter and argument is this. Parameter is what you define the name right here inside the function template. And argument is here what you provide as this to this parameter. So parameter is the name which is defined here name and argument is going to be this Eric. So now we took that kind of defining variable and we broke it down. This is our variable name which is used inside the function. And here we provide the actual kind of
194:20
Speaker A
argument, right? So, so whatever we provide here, this Eric, it actually will go right here in the name. And this one later is going to come here. And this is what we're going to print. Let's see what we get. Click on run. And oh no, two thing has happened. First of all, we printed hello Eric. This was on the first run. The second one, we got an error. Says like missing one required position argument called name because we did not provide name, right? There are
194:46
Speaker A
two ways to kind of avoid this. First of all, we're going to provide here something like Christina. Let's right click, click on run. Hello Eric. Hello Christina. Now you see I reuse the piece of code and I can provide different arguments here. I can write here Claus. I can write here Ricky. Run it. Look at that. Hello everybody. Great. What if sometimes I forget to include the name? I don't want to see an error. So I want to have a default value. How do we do
195:14
Speaker A
this? And we can also say here you know what by default name is going to be Eric. So this is a default argument and I have an option do I provide it or not. And you already seen that there are some methods and functions we used in the past where there was like optional arguments. Sometimes we provide just one sometimes two and so on. So here we can also do this. So let's click on run. Look at that. Hello Eric, Christina Claus. Now if I would remove here as well it would print hello Eric Eric.
195:43
Speaker A
This time it doesn't crash because it has a default value. Sometimes you can make it like empty string. So you know you're not crashing but you're also saying just hello like right you don't say who you're talking to and so on. You can also include if statements and so on. But now let's take a step back. All you have to understand right now is this. You create your function by starting def keyword and give a name of the function which can be used to call it. This is
196:10
Speaker A
clear so far. Then for the arguments inside you can provide any name you want. This is like defining a variable for your functions and then later when you call your function you need to specify this exact variables. Right? If you want a name you provide here a name but you also have an option to have a default value here which will be used if nothing is provided. And you can also make here you know what the name is actually going to be mandatory. So it's going to be name.
196:39
Speaker A
And then let's say that you're going to also include here salutation I think it's called and this one by default is going to be nothing right so we're going to add here another kind of u placeholder and we're going to add salutation now look at that I always need to provide a name so I better write here Eric so I'm going to run it hello Eric Cina Ricky claus now I have an option to provide here a salutation this is an optional one so this is going to be a second kind of argument and I'm
197:09
Speaker A
going to write here mister and let's write here in Christina Mrs. Now we're going to run it Mr. Eric and Miss Christina also have our Claus and Ricky so we going to provide them as well we're going to write Kato and we're going to write another one Kato here this actual names of my family so going to run it so hello Mr. Eric, Miss Christina, Kato, Claus, Kato, Ricky. So this is how we can define different arguments to our function. And we also can create optional ones as
197:41
Speaker A
well. I hope this makes sense. And also when you refer to arguments and parameters, you can think about them as the same thing. As beginners, just think that this is the same thing. This is what it refers to. In reality, this is parameters because they are defined inside your function right here because these are the name of your parameters. These are the arguments. This is what you provide to your parameters. But many people use it for the same meaning. And
198:04
Speaker A
actually good way to explain it is like this. I want to take parameter name and I want to assign an argument Eric. I want to take parameter name and assign Christina. This name refers to the function. Right? So I saying that inside this grid function there is a parameter called name and assign this value to it. I can also say here is salutation. Now I'm also going to write here a salutation for Eric. Now if I run exactly the same result hello Mr. Eric Mrs. enough. So in here what you realize
198:35
Speaker A
that you don't always have to define the parameter name here. You can just provide the same order of kind of parameters. This was the first this one the second right first second same order. You can also define it and then there is a little trick. If I'm going to say here name like this run I'm going to get annoying error. Position argument follows keyword argument. Doesn't make any sense right? Let's break it into plain English. So if you're going to use these names, you have to make sure you
199:02
Speaker A
first address all the parameters with default values. So you could make here salutation equals cau then you run then it works but you cannot make it like this. If you make it like this you also need to provide that one for sure. This is just a rule in the syntax of Python. Okay. And this is the parameter argument parameter argument. You get the idea. All right. Now let's go to the next topic and this one is also very important. It's about returning a value. In this case, it's we
199:33
Speaker A
made certain functions that just create an action, right? I call my function. I just like, oh, it prints something. That's fine. Often times, you would want to actually make an action and get something out of your function. For example, let's say that we have a two numbers five and I don't know 10. Now, I want to calculate total equals a plus b. Very simple. Then we want to make a print statement going to be this plus this equals this. With the format we're going to provide a b and total.
200:05
Speaker A
So we're going to run it. 5 + 10 equals 15. Now I'm going to call it again. This time how much is going to be 50 + 5. Run it. Okay. Now we have this kind of print statement that makes us the print statement. But also I want to have this total, right? because ne next time I want to just print total. Doesn't matter why. I just want to do this run. Okay, 55. I'm going to use it later. Now, let me just copy. Let me just delete the duplication. Focus on that. So, let's
200:37
Speaker A
turn it into a function that will return us a value. Here's how to do this. We define a function. We're going to call it, I don't know, add numbers. We need arguments. And I already know that I want to have A and B, right? It's going to be these two. So I'm going to remove those ones. I have them here. We need a colon and we need to move everything inside with a tab. Okay. So we have a function called add numbers. It takes two arguments A and B. Then we're going to take this A and B, make a
201:11
Speaker A
total and make a print statement. And then we want to also get this value out of the function. So, I want to say x equals add numbers. And let's say 5 + 5. Okay. And we're going to print x. Click on run. And 5 + 1 equals 10. And then x is none. That's not what I want. I want x to be the total. So, this is where returning value comes in. There's a special keyword called return. What it does, it returns a number out of the function. So if I'm going to say return
201:42
Speaker A
me total and run it now x equals 10 this is what is going to be returned and it can be assigned to your variable right you're getting something out of the function now also really important once you're going to make return it's going to stop execution of your function let's say that I make here a print statement hello run it I return it nothing gets executed but if I'm going to kind of comment it out run it I have my Hello. Obviously my returning value is none. It doesn't
202:14
Speaker A
work. But thing is that return stops execution of your code. You will also notice there's a highlight. This code is unreachable because PyCharm knows that using the Python rules, Python syntax, you cannot reach to this line of code because it will always be interrupted here. All right. But this is in general how we return our values. Now with Ctrl D, I'm going to duplicate it. Let's say this will be Y. This will be Z. I'm going to say 5 + 5, 10 + 20 and 25 + 25.
202:47
Speaker A
Now we're going to print X, Y, and Z. So right click, run. Now we have these equations 5 + 5= 10 and so on. And then in the end we have all the results we want. So this is how you get returning value out of the function. There's also another special keyword. I'm not going to go there. It's called yield. This is going to create you a generator. This is more advanced topic. Think about like ticketing machine and it can give you all the values. You're not going to get
203:14
Speaker A
all the tickets at once. You're going to come take number one, ticket number two, ticket number three. Then you don't want any tickets for a long time. Then you come ticket number four. This is generator, right? In short, this is what it does. In like also in short, you probably won't need to use it any soon, right? Just stick to regular things. All right, that's pretty much the basics of the functions that I want you to know about. You take a piece of code, you
203:39
Speaker A
wrap it in a function, you slap a name on it, and now you can rename it by calling this name. That's the concept about functions. And listen, for this lesson, the homework is I want you to practice. It doesn't matter what kind of code you're going to take, but I want you to put it in a function. Just look for the previous lessons, select a piece of code, and try to put it inside the function and try to call this functions a few times. Try without arguments. Try with some arguments. Try returning
204:03
Speaker A
values. Try do not return values and so on. And I know that you might be a bit skeptical that I'm not sure it's really that useful. Trust me, you really need to understand this really going to help you. And this is absolutely essential. Maybe don't start using it right away, but at least try to use it afterwards. First, write your code dirty as fast as you can. As long as it works. Once it works, look at it and think like, okay, I actually want to make it into a
204:26
Speaker A
function. And obviously you can ask chat DPT to help you. Just provide the code. Say I want to refactor. That's a special name for kind of rewriting your code. I want to refactor this code. Can you help me make it into a function? And it's going to do a great job. All right. And that's it for this lesson. Just do your homework. I wish you happy coding and I'm going to see you in the next lesson. Goodbye.
204:52
Speaker A
Next, I want to talk about built-in functions in Python. We already used a few of them like print, type and a few others. And as you saw, they can be really helpful. And in programming, a function is a concept that allows you to create a reusable code snippets. So you don't copy paste it in the same code over and over. And instead, you just call the name of the function. To find a list of all functions, you can type Python built-in functions in Google or whatever search engine you use. And then
205:17
Speaker A
you'll find this built-in functions page from official uh Python documentation. Let's click on that one. Then on top of here, you will see different Python versions. And to be honest, it doesn't matter. If I'm going to go to lower version, you can see it all stays the same. And in here, you can see I think there are 71 different built-in functions. And don't worry, I know that's a lot, but we're not going to go through all of them. And we don't have to cuz a lot of them are honestly
205:38
Speaker A
useless. Some of them used for internal use, some of them for using like for working with like bytes, converting uni codes into characters, all this boring stuff. We are going to focus on only what really matters. And let me just cross out a few things here and there so you can see what really matters here.
205:57
Speaker A
But also on top of that there are a lot of basic things that you already know really well like for example float set released all of those. And you can see that this list is getting smaller and smaller and less intimidating. And also on this page if you're going to scroll down you'll notice that every single function is documented here. It says what you can do description how to use it. But honestly I don't really like the description because certain things they're too long
206:20
Speaker A
too technical and too in-depth. and we don't really need it. So instead of that, we're going to go to my page that I prepared for this lesson. And in here, I wrote all of these built-in functions and I categorized it into five categories. Essential ones that you absolutely need to know about. Then regular ones, which sometimes can be useful. Then there are basic ones which you already know like boolean, dictionary, list, all of those. Then there's OOP, which is an advanced
206:43
Speaker A
concept in Python called object-oriented programming. We're going to talk about it later in this course a little bit. In general, it's all about working with classes and advanced functionality. you don't need it right now. Forget about this. And on top of that, there are a lot of functions that are absolutely useless and they're like working with bytes, converting uni code number into a character and all of that which is rarely used. There's also a synchronous
207:05
Speaker A
iterators and this is really advanced or kind of more for internal use of Python. So, we're not going to even look at them. And during this lesson, what I want to do is first we're going to go through essential ones. And as you can see, there's a short description, short example, and I'm going to explain what they do, how to use them, and why you might want to use them. So, let's begin with essential built-in functions. The first one is deer, and actually it's the
207:28
Speaker A
great one to begin with, especially for beginners. It returns a list of attributes and methods of an object, helping you explore its properties and capabilities. And it's often used for debugging and understanding objects, classes, and modules. Great for beginner. So, we're going to copy all of that, and I'm just going to paste it here. the way it works. So, as you can see here, we're using two functions. First, I'm using print everywhere. So, we can actually see the results in the
207:51
Speaker A
console. And then I'm calling another function. So, we're putting function in a function. In here, we're calling the deer. We're providing an object like a string, like a list, a dictionary. Then, we can execute it. And it's going to provide a list of all available functionality inside of these objects. I know that's a bit confusing. So, there's another syntax we can use to make it better. Let's copy that one. Also, let's remove these two and paste it like this. So, in here, we're getting
208:18
Speaker A
a list of all available attributes, but we can also iterate for the list and print one item after another. Let's comment that one out. Run this. And this way, we're going to get it as a kind of list like this. It's going to make it so much more readable. Here's the important part. You will notice this kind of underscores everywhere. And this is called dunder. It means that it's for internal use inside of the function. What it means is that you should not use them. You should not try to use them.
208:43
Speaker A
They're just there for internal use. That's all you have to know. But if you're going to scroll through, sooner or later you will see this append, clear copy count extend index and so on. This is internal functionality of the lists. Let's change it for the strings. Run the same thing. Here's all the functionality that strings have. It means that any of these, for example, replace can be applied to something like hello world. It's going to be replaced like this. And replace is going to be
209:08
Speaker A
used like this. But again, we're going to talk about this in a moment. There's going to be a whole section during this course. For now, just know that deer is really useful to find all available functionality of your objects and also you don't have to specifically call it on the name of the class. You can actually provide the object itself. Let's say you have a string hello world and you wonder what can I do with this and here all available functionality. You can provide an empty list or maybe
209:32
Speaker A
not an empty list because it will see oh it's a list. Here's everything you can do with the list. All right, this is the deer. It's very useful for beginners. Now let's clear it and let's go next. The next one is enumerate. But actually I want to show the help because after there help will make perfect sense. Help invokes the built-in Python help kind of documentation. You can take any class, any method, any function, any module and you can ask Python for help and ask like
209:57
Speaker A
hey can you describe what the hell is going on? Here's how it works. You just for example write help and then let's say you wonder I need some help with the print. How does it work? you run, it's going to print you a message from the documentation. It tells you like, okay, here's how the print works, different arguments you can provide and so on. What about type? How can you give me help for the type? Here's how the type works. And you can see sometimes it can be really long
210:22
Speaker A
depending on how complex the object you are asking about. We can also not only ask the help about the objects and functions like this, we can also ask about specific kind of things. example inside you can see the documentation about every single method of the strings but I can say you know what I wonder about string format how can I use it and so on the thing is that you should not use the parenthesis you're going to use it you might get an error because you are calling a method this is not what
210:48
Speaker A
we're doing we want to kind of direct to the method itself I know it might be confusing just just remember that this is how we ask for help in here we can find okay here's the format returns a formatted version of a string using substitutions from arguments and key arguments and blah blah blah. You can see you can get a lot of help with like deer and help to understand how things work. It can be useful especially if internet goes down and you still want to learn
211:12
Speaker A
Python. Now let's go more to practical examples and we're going to talk about enumerate. This is a great one. I'm going to copy that one. And this is going to add counter to an iterable by pairing each element with its position index creating a sequence of tpples. And here's what's going on. Let me just explain. I'm just going to hide that one. So we have a list of materials right it can be a list of anything numbers whatever it's relevant here let's say then later you want to say for
211:37
Speaker A
material in materials creating a for loop to iterate through each one of them I want to print material uh I'm going to remove that one for now we don't need it going to run it and obviously I'm going to get all the items but then you decide you know what it would be great if I had a number of kind of iteration if this was the first the second and what people often do they create a counter then they this count and plus equals one and I sort of put it here right this is how many beginners
212:05
Speaker A
begin myself included this is how I would kind of count each iteration however there's no need for that there is much simpler approach we can apply here a function for this list we would write here enumerate like this to materials and now we need to provide here one more argument because what enumerate is going to do it's going to go through your list and it's going assign an index to each item like zero one two three four and this is going to become like tpples tpple number
212:35
Speaker A
one tpple number two so if I maybe I can make print enumerate materials this is so you can actually see what what is returned this way we're going to see an enumerate object which is fine this is a special kind of class but if we convert it into a list you're going to see it's going to turn this list into something like this now each item in the list becomes a tpple and there is the first item is the number 0 1 2 3 4 and the way we can use it we can assign at
213:08
Speaker A
the same time n and material so what all of these they going to become n it's going to be like this and all the items they're going to as usually become our item and now we can print both of them now we can see we can kind of always get enumeration and this is a very used in for loops when you're going to iterate for items and you need count of your elements. Can also say here that it should start from for example 10 maybe run. I can see the first one is 10 11 12
213:36
Speaker A
13 14. So you can make it like this because I know that many of you would probably do it like this instead of that but you know everything works. You can choose whatever you prefer. Right. The next one is input. I think this one is self-explanatory. We already kind of covered it. So input is to just ask for user input. What's your name? Eric. my age 29, right? You can get the input and you can work with this. And remember, you're going to get input as a string. So, if
214:04
Speaker A
you want a number, don't forget to convert it into a number before you're going to do some math. All right, this one is done. Let's see what next. The next one is is instance. And that one is quite useful. Let's just paste it. And this is to check what is the type of your elements. For example, you have X, Y, and Z. And they're just random. This is integer, string, and list. But sometimes you kind of go through your code and you're not sure or you want to make sure that for example x is an
214:31
Speaker A
integer and you can use this is instance people write it is instance then the object you are testing and then what do you want to test it to for example integer in this case right we're going to run it and it's like true true true false it's great for if statements if uh is instance x integer then for example print x* uh I don't know two in power of Right, let's run it. And you can see now we can execute it. If that would be a string, we would not execute it and we would not
215:02
Speaker A
get any errors. So this is why it's very very important. Alternatively, you could also kind of if type of x equals to integer then you can run it right beginners often prefer this syntax because this is instance you always forget what is first, what is second, what was the name of it. Both of them work fine. But this one is a bit more advanced because there are also other options. For example, you can at the same time check both of them. If X is integer load, that also might work.
215:31
Speaker A
That's actually might be a very good for that one. So we will just kind of copy that part. I think we need those, right? So if it's integer of load, then we're going to do this. That also works great. But also it kind of checks the subasses. Listen, this is more advanced concept about object-oriented programming. But just know that it can give you true for more than one object if they are related. I'm not going to go deep into that yet. We going to talk about this separately. Now the next one
216:01
Speaker A
is length. Again, you know how to use it, right? Let me just copy paste it. So you can check the length of the strings of the list of any container pretty much, right? We're going to run it. You can see there are 11 characters in this string. There are five items in this list. And we can also do it for dictionaries. You can see here. And yeah, very easy and extremely extremely useful because you will often use it to verify that you have some items in your list and so on. The next one is open.
216:28
Speaker A
This is going to create an file object in terms of Python allowing you to read or write or append using different modes. There's R for reading, W for writing, and A for appending. Appending is just kind of adding to the existing file. And here's the common syntax. If you want to read you would with open then you provide your um path to the file and you define do you want to read write or append as file. This is a special syntax it's called context manager when you use the width but in
216:56
Speaker A
general you write this then you say okay I want to read the content then print the content or you want to open it and you're going to write it or you can also append it. I forgot to add an example here, but you get an idea. And it's possible to use it without whis and it might look simpler, but this is like the way you should use it because this W has extra functionality. This will make sure that your file closes so it doesn't corrupt. This one, if you don't close
217:19
Speaker A
it, you're going to corrupt it and well, you're going to lose your data. Just avoid using that syntax. The next is print. I think we can agree we don't have to go into that one. You know it. Oh, and the next one is range. Absolutely must to know. Very simple, very useful. So I'm just going to print here a few ranges and you can see I'm making print list of range just so we can see the right output in the console. Here's what's happening. Focus on these ones. Range it's to create a range of
217:45
Speaker A
numbers like an array. If you say uh range just provide one argument five. Then you want to put it from zero until five. You want to create five numbers. That's going to be 0 1 2 3 4. You can also specify I want to start at two and at seven. Then it's going to be 2 3 4 5 6. Right? is always excluding the last number. And you can also say I want to go from one to 10 uh instead of two. So every second item we're going to take, right? So this way it's 1 3 5 7. I want
218:13
Speaker A
to say every fifth item then it's only going to be 1 six. If I'm going to be from 1 to 100 here kind of in steps of five very useful and the way it's useful is because often times let's say I want to print hello world 10 times. The easiest way to do is for i in range of 10 and print i. And let's think about this. What is it going to give us? It's going to give us from zero until 9 including or 10 excluding. So when I'm going to execute it, it's going to be zero 1 2 3 4 and so on so on so on. But
218:44
Speaker A
also write here uh hello world like this. And it's going to be like this zero hello world. It's great for working with for loops and you will often find use for that. Now the next one is round. Well, this one is very simple. Let's just copy paste. And I think it's self-explanatory. It's rounding the numbers. The first argument is the value. The second one how many digits you want to see. So if you want to see like uh just one digit after the floating point, then you just write one.
219:12
Speaker A
If you want to see two or three, you do this. And then you will see here's two, here is three, here's one. Yeah. And here what it was before, right? It might be messy. You don't like it. It also sometimes can mess up your math, but if you round it in the right places, then it's going to work just fine. All right, then sorted. Well, this one is simple and advanced at the same time. Let's begin with the simple stuff. This is for sorting your arrays. Let's say
219:40
Speaker A
you have a list of numbers. You can just write sorted numbers and then it's going to sort it. It's going to return you 1 2 5 8 9. You could also like sort it numbers equals sorted and then you provide numbers. This is going to sort your list numerically. You can also use the second argument reverse. If you want to kind of reverse it, that also work fine. And it works similar for alphabetic as well. If you have a list of uh strings, it will go through and alphabetically B is first, C
220:10
Speaker A
second, S is the third and you know you get the idea. On top of that, it can be used a little bit more advanced, especially if you work with special objects or like dictionaries. And let's say you have a dictionary of materials. We're going to define a function to get the second item. So, I want to sort by that one. Normally, you would use lambda, but this is a little bit confusing concept. So, we're going to skip that one. So, this function is used just to get this ones.
220:36
Speaker A
And then we say I want to sort materials as items. Items is going to make it like uh doubles, right? Everything is going to be like this double. This is the first and the second also going to be in parenthesis and so on. And then how do you want to sort it? You can specify a key like a function. And here I say okay uh this is how you get the right item to use for sorting. This case I want to get item number one which is this one in this case. Again this is might be a little bit more confusing. I
221:06
Speaker A
think in the beginning you will be using chbt a lot for this kind of stuff but over time you will understand it's not that complicated. just need to know that there is this functionality and you will always be able to find how to use it. This is what important with all this built-in functionality. Now then the sum well if you have a list of numbers you want to get the sum you can write sum if this numbers you're going to get a sum of these numbers also as you can see I
221:28
Speaker A
just print everywhere so you can copy paste and get results right away but in reality you actually would kind of write numbers then total equals sum of numbers and then you would print total this is more kind of convenient easier to read syntax but in the end of the day you can also take that one put it right here execute it same result right just saving one line of kind of code but for beginners maybe I should have written it out but it is what it is then the type
221:58
Speaker A
function you know how it works it just tells you what is the type of the uh value that you're providing or an object right type of hello world is going to be string integer list num type very useful for debugging again I put it in the print but normally you would make if statements with that or also kind of print it if you're debugging all right and now the last one is the zip. So this one is a interesting one. This is to combine multiple lists in a special way.
222:24
Speaker A
Here's what's going on. Let's say you have two lists. One is the material, the other is strength of the materials. And you want to relate all these values. So I want to relate like this, like this, and like this. And to do that, you can use zip. It's combining values from each list. So the first element of first list is going to be combined with the first element of the second list. The way you do this, you write zip and you provide two lists. It's going to give
222:49
Speaker A
you a zip object, but you can convert it into a list. So it's more readable. And then if you print it, you're going to see that you're going to you're going to get um a list of tpples. Tpple number one, tpple number two, tpple number three, right? Everything is simple. The way you would also often use it another one, let's say you want to iterate through these objects in a special way. Right? for materials strength strength in then you want to combine two lists
223:16
Speaker A
you write zip materials and strengths and then you write print materials strength I know I misspelled it but that's okay so this way have a look I'm going to iterate concrete 25 this this this I can also extend it make it better and something but this is in general how it's often used and another thing you need to know is that let's say that in here I have more items Let's add here. I don't know what glass. Let's iterate. Have a look. We are not printing anything about glass. This is
223:47
Speaker A
because it doesn't have any match. And when you use the zip, it's often that you need to check the length of your lists to know if they are kind of compatible. Well, they're still compatible. It just you need to know if you're missing any data. For that, you normally write uh if length of materials equals to length of strengths, right? We want to make sure that they are equal lists. If two items here, I want to make sure two items are here. Then we're going to execute it. Else we're going to
224:14
Speaker A
print um unequal lists and let's execute it. Now I can see unequal list. So I know that okay something is not right. Maybe I need more values here. Let's say it's 45. And now if lists are equal then we're going to merge them and we're going to do something else. It's really good to know that when something like this happens can still execute it if you want but it would be great to kind of notify that hey just so you know they are not equal so you might be missing data. Yeah these are the zip.
224:46
Speaker A
All right. So these are the essential functions. They are like absolutely must to know in my opinion. Again, this is like my categorization. I just wrote here what beginners need to know. You need deer, enumerate, help, input is instance length open print range round, sorted, sum, type, and zip. There are like 14 of them, but you already knew half of them. They very simple. And the other ones, you're going to get used to using them. All right. All right. And now let's have a look at the regular
225:12
Speaker A
ones to see what is here. There are also like 11 of them I think and they're not as important and many of them can be substituted with something else but I want to just show them anyway. For example, abs is returns you absolute value of a number. You had minus 10 and you want to get 10. Well, you're going to get absolute value of this number. Very simple. The next one is all and any. And these are for kind of checking multiple things at the same time. Honestly, I don't think you need them in
225:36
Speaker A
the beginning. And let me show you why. Uh let's just copy that one. I'm going to be very very quick. So I'm going to paste it, run it, and you can see I'm getting true, false, and true. So this one is just checking are all items inside are true or are they like positive numbers? Not positive, but are they nonzero numbers or non- empty strings? And if it's yes, then it's true. In general, it's pretty much about making comparisons, right? If all comparisons inside of this list correct,
226:02
Speaker A
meaning one is more than zero and five more than two, then I want to do something. And you will do something like and normally you would kind of take it and write if all of this then I'm going to print hello but let's be honest you would not do it like this. You would just write if one is more than zero and five is more than two then you would print. The first one it's more useful when you have maybe like 50 different comparison or you need to check your data make sure
226:29
Speaker A
that there no zeros or no non type then it makes sense. Otherwise just stay within like the conditional statements. Any is the same as all this one everything have to be true. The second one at least one have to be true right false true false going to give you true. One is true. Then the div mode. This one is a bit weird one but it might be useful sometimes. Let's say that we take 13 divided by 5. I have two as a whole number. This is called quotant or whatever it's way you pronounce it. And
227:00
Speaker A
then the remainder is three. This is what could not be divided cuz 2 * 5 is 10 and 13 - 10 is three. Right? This is the number which could not be divided as a whole. This is called remainder. Uh it's very rarely used. I haven't seen it in any code but maybe you need it for some math. Then there's a filtering. In general, this is how you can apply the same function to your list to filter items. The function have to kind of return true or false. So it's conditional and it's
227:31
Speaker A
used for more kind of complex conditional checks where just a single if statement might not be enough. However, majority of cases you're going to use list comprehensions. This is more advanced topic. I'm definitely going to explain you it in depth because this is my favorite thing in Python. But here's what's happening. If you're going to paste it like this, so pretty much um you have a function and you want to run this function on every single number inside of this list and you only want to
227:58
Speaker A
leave items which return true after going through this function. So one is going to go here. If it's not going to be true, then it's going to be excluded. Two is going to be here. It's going to return you true. So it's going to be included in kind of results, right? It's for filtering items out of your containers. But this one, let's agree this is not good. Instead of that we can use list comprehensions again later I'm going to go really in depth what's going on here in short return me number
228:26
Speaker A
for number in the list of number if number kind of returns true to this value right this is very very great way to kind of make oneliners which are easy to read once you know the syntax and they save a lot of space we're going to talk about this more in a moment then format this is not the string formatting thing which I showed you before because that one was a method related to strings. This is a function and it's all about literally formatting your values. Again, it's going to be
228:54
Speaker A
about strings and numbers. And you can see you can format your numbers in different ways. These two not as useful. You say it's a digit, it's a float, not so useful. This one is amazing syntax. It allows you to kind of round it inside of the string. So it's in here. You can see it's two. Then there's like with an alignment, you can say, okay, I want to take the word center. I want to put it in the middle of the string. I want to make sure the string has 10 characters.
229:19
Speaker A
Going to count it. It's going to be 10 characters. 10 characters here as well, but it should be aligned to left and to right. Might be useful for certain uh outputs you make in the print. Then here you can also define that I want to take digit and I want to kind of add this uh what is it called? Thousand separator. So it's going to print like this. So it's easier to read that it's 1,200 something thousand and so on. And you can also convert into percentages. This has like very simple formatting.
229:45
Speaker A
It's rarely used, but sometimes you might look into that. Oh, then here is the map. Again, that looks a bit too complicated, but I'm just going to explain it very short. It's used to take a function and apply to every single item inside of the list. Similar to uh filter, but filter was used to kind of kicking out elements. Here, you're actually getting results. So, you're doubling items. So you take this function you apply to every single number in this uh list by writing it
230:13
Speaker A
like this right this function apply to this list and then you're going to get results and results is going to be just a list of doubled items. It can also be used with the built-in functions like integer and it can also be with functions that take multiple arguments. Then you provide use this function here is arguments A here arguments B. But again we're going to cover list comprehension. You can already see how much better they look. Look at that. We have a list of numbers. And then I want
230:40
Speaker A
to just make list comprehension. Or x in numbers. Do this. And we get results. Or for x in string numbers. Actually, it's going to be the same numbers. Do this. Very simple, very readable, and also beginner friendly once you know the rules. We're going to talk about this later. The next one is the max. It's going to return you the largest item in the collector. If you have a list of numbers, it's going to give you the largest number. If you have a dictionary, you can specify the rules
231:04
Speaker A
how you're going to get it. And again here, for example, in a dictionary, I specify that I want to use a a height property for kind of getting the max value. Then I know the max value is bhalifa because it has the highest number. Then the min is the same as the maximum. Exactly the same logic. You're just getting the lowest value. Then p this is for uh raises raising a number in specified power. I think we can ignore it because you're going to use this syntax instead, right? If you want
231:31
Speaker A
to have two in power of three, I suggest you use that one instead of using this one. And this one also has extra argument which you can ignore. Lastly, reversed. You can always reverse your items. So let's say you have a list of levels and you want to reverse it while you're looping. You're just going to reverse your levels while you loop. Then it's going to be 3 to one. We're going to remove that one. It's going to be one, two, three. Very simple. Sometimes used.
231:58
Speaker A
Now these are the essential and regular ones. Then op stands for object-oriented programming and I'm going to briefly talk about closer to the end of this course. For now all you have to know it's all about working with classes. This is a more advanced concept. It's it kind of it offers more functionality and as a beginner just don't worry it's going to be a little bit confusing until you get comfortable with basics. So just forget until the time is right.
232:22
Speaker A
Then the basic is just going to be about converting into boolean dictionary float integer list. We already know it from one of the previous lessons. Lastly, ignore this. Absolutely. You don't even have to look at them. It's all about working with bytes, converting asking number into a character, making some weird syntax and working with a synchronous. And this is very advanced or very useless function. Some of them are internal. They're used internally in Python because Python needs this
232:48
Speaker A
functionality. But you personally, you don't need it, right? There is like a lot of garbage in here in my opinion. So, don't even look here. Focus on essentials and briefly have a look for regular. This is going to be more than enough as a Python beginner. I promise you the rest you will come to you if you need it. Even from regular is probably half you won't ever use, but it's there in case you need it one day. All right guys, and that's it about built-in functions in Python. Now you know where
233:13
Speaker A
to find it, how to use them, and more importantly, you know that majority of them are useless and you will never use them. So you can ignore them completely. just focus on the essential ones and look through the regulars and you can decide for yourself what is what's important later on anyway you will be using chat GBT and looking in like stack overflow on how to do certain things and you will find different functionality of the Python and how to use it there also
233:36
Speaker A
some tricky ways and some smart ways to use certain functionality and you'll all discover that through kind of asking the right questions because the best way to learn Python anyway is by doing and when you do something you don't know it you're going to Google it anyway all right let's not pretend like you're going to learn everything and and start doing it. All right. Now, I'm going to see you in the next lesson. So, I'm going to
233:59
Speaker A
All right. So, you've learned about the most used data types in Python and you know how they look, how to use them and just in general what they are and also you know that they have certain functionality associated with them and I've even used some of that functionality by using for example format in the strings right and this is a called method. In short, methods are actions associated with the types to modify or convert your data. For example, think about a coffee maker. If
234:23
Speaker A
that would be a valid Python type, it would probably have methods like make black coffee, cappuccino, latt, make espresso, clean itself, repair, and so on. Some of these actions would also need some arguments to make this action possible, right? Provide some beans, water, and so on. Listen, this is what this method is, and it's an action on your object. And it I understand that it still might confuse many of you, but don't worry. We're going to have a look at all built-in methods in Python and
234:48
Speaker A
you will understand what they are and most importantly how to use them in practice. All right, let's just open the code editor and you would also want to scroll down this video because you're going to find this kind of uh table that I created. I separated them first of all by the classes like string, list, tpple, but also added you filters. So you can filter what's essential, what's regular, what's ignore. You can see all of them. I think at first let's go through all
235:12
Speaker A
essential ones and you'll notice that all of them have the symbol this is something I added so you can recognize it but then in general this is the syntax you put a dot count parenthesis so the way syntax work for the methods is you take an object like string or maybe it's better to use a string itself and then you put a dot then you can apply method for that string obviously PyCharm is aware about all these beautiful methods we can use so we can choose from here and also you can see
235:41
Speaker A
there is like in gray there is like arguments but for this one doesn't take any arguments itself imagine self is not there it just refers to this this is self and then you put it here like count and then you provide arguments if there are any no but in general I provide all the code here and if you run it you're going to get results which I put right here as the comments that's sort of to help you you can literally go through these tables and learn so much about all the methods
236:09
Speaker A
Now count how many times letter H is in this text. Zero because lowerase and uppercase they are not the same character. You you need to make sure that you're checking the same one. So ends with is going to return you true or false if string ends with a specific value. So you have a text here. We can check does it end with hackers? That's true. Does it end with pyavit? That's false. That can be useful when you rename things and so on. find this one searches for a value and returns its
236:38
Speaker A
position. So you have some kind of text and then it's going to look for the value and as you know in Python we start counting from zero one two. Therefore when we look for the L it's going to return you index number two and notice that there are multiple L's. It's just going to return you the first one. It's going to find if you look for something that doesn't exist that's going to give you minus one. And also if you would look here for something like parit it
237:03
Speaker A
would return you where the first letter is. So it would be 0 1 2 3 4 space character is also five and then six. So if you would look for paravit it will return you it starts at index number six. Format format is absolute must for any of you to know how to use. This one is going to be overused. Now let me make some space and just go into that. I already explained it but I want to make sure that you understand it. When you make your strings, often times you want to combine things and put
237:32
Speaker A
something in the middle like different variables or names and so on and you can use the format uh method inside the text. You need to put these kind of braces as a placeholders and later you can fill them up with whatever. I can make as many as I want. I just need to make sure that I provide the same amount of arguments. Hello parade hackers hooray. All right. If I print it, hello parade hackers, hooray. because this one went in here and the second one went in right
238:02
Speaker A
here. Very simple. You also have an option to name your placeholders. So I want to say this placeholder A, B and C. Then in here you would say A equals this, B equals this, C equals this. Right? So this way it becomes a little bit easier. So you know that this one is going to be right here. It's very useful if you have large text with lots of different placeholders. So it's a great way to kind of name your things. In new version of Python, it's normally used like this.
238:31
Speaker A
Uh hello, my name is and then name is going to be Eric and I provide it here. You're going to see this syntax quite a lot. And this is from Python 3.6.6 I believe, right? Pi 366 plus. Now with that out of the way, format absolutely must. You need to know it. Now index. Hello parade hackers. index is similar to find and the biggest difference is this one. I also wrote a comment with the find. If you look for something that does not exist, you're going to get minus one, right? You look for test
239:03
Speaker A
doesn't exist minus one. But index, it's more strict. If something does not exist, it's going to give you an error saying that no match is found. And this is for a good reason because in Python, errors, they're not always bad. Sometimes you want to make errors. You want to make sure that something is right before you continue. And errors is a great way to kind of stop execution and point out that whoa there is something wrong here. There's also error handling which you could add to make
239:28
Speaker A
sure that if I get an error do this. If I don't get an error continue, right? But more on that later. All right. Then is lower is upper. To be honest, they're not supposed to be here. They're not essential. I'm going to move them. I think I wanted to make lower and upper. This one because it has is. It's a kind of test. Return true or false if all characters are lowerase. Okay. If at least something is not lower case, false. Same here. True or false? Depending is it uppercase or lower case.
239:55
Speaker A
Next one, join. Oh, this one also absolutely must to know. I even going to copy and explain it in more depth. All right, let's say you have an collection of different items and you want to join them, right? You create a string with a separator. For example, in here there are no separators. Then you say join items. So, what it does, this join items is going to take, okay, I'm going to take that one, that one, that one, that one, that one, blah, blah, blah. And I'm going to put that one in
240:24
Speaker A
the middle of all of them right here. Since it's empty, there's nothing. It's just going to be ABCD E F. There's empty space. There's not even a space, right? There's nothing in between. If you're going to decide that, okay, I want to use comma and a space, right? Comma and space. So with this one, it's going to put it in between this one, between this one, this one, and so on. In this case, it's going to be space and dash and space. So we're going to put it here in
240:50
Speaker A
between, here in between, here in between. And here are the results. Right? So I'm going to run it. And here are the results. What's next? Next, again, the lower it convert string to a lower case. Absolutely important one. So let's paste it here. run it. Whatever you had before, every letter is going to become become lower case. There's also case fault which I think I put in the regular ones. Yep, it's very important to show it right away because they are very related. When you're going
241:25
Speaker A
to read description, both of them do the same thing. You take a letter, make it lower case, but they have a bit different kind of strictness. Case fold is very strict. If you use characters from other languages like for example in German this one represents like double s this a special character case fold is going to break it into double s because this is going to the simplest way to bring it into simple characters and make them lower case lower we'll just ignore
241:49
Speaker A
it whatever you can use whatever I'm just taking the upper case making them lower case as normally that's the difference all right the next one is replace absolutely necessary for any Python coder imagine renaming thousands of use without using this replace cuz in here is find me this replace with this and it's going to make it for all kind of matches. You had hello I want to take my lower case replace it with L like kind of uppercase going to be hello with uppercase let's say you say here hello
242:16
Speaker A
parit hackers and I want to replace py ravit with python and I also want to replace hackers with coders because I can stack multiple methods together that's everything is fine click on it hello python coders so we took that one we replaced it it's super useful just imagine renaming thousands of views in rivit it and you already see how it can be useful. All right, the next one is split. Absolutely essential. This is necessary when you have a your data in a string. Then let's say I want to make a
242:47
Speaker A
list out of it. I want to take this, this, and this and separate into kind of items in my list. I need to decide on my separator. In this case, it's going to be this uh want to say the split is empty space. That's going to return you a list. So, if I'm going to run it, I took that one. I decided, you know what? Separate me by using this right here. And then it's like okay the first item hello parade hackers and so on. Then here I said you know what I actually
243:13
Speaker A
want to separate it by using the parade word. So it's going to take this one sort of remove it and then take whatever is here and whatever is on the other side. And spaces also going to be included. So you might also want to kind of clean that one. But also if I going to put it something like this let's actually make it inside the word. Run it. You can see hello huck errors because right here is huck then remove private and then errors. All right and by default if you don't
243:44
Speaker A
not going to provide anything it's going to be same as you provided empty space. All right the next one start with pretty good one just to check what does it start with. Hello parade hackers if it starts with hello then it's true. If not then it's false. just used in if statements to make sure your string starts or ends in a certain way. Strip is just going to get rid of spaces before and after. Sometimes you get data which is a bit dirty. You have different spaces before and after. This one is
244:12
Speaker A
great to kind of get rid of all these extra things. I think you can also get rid of the special new lines and so on. Right? Very useful when you work with dirty data. And that's the essential ones about the strings. So I'm just going to briefly go all the way up and use the filters. So we went through all essential ones. But also if I click on regular ones, you can see there is like capitalize just convert the first character to upper case. Okay. So this one is going to
244:42
Speaker A
become that one. Case fault. I mentioned this is like lower case but more strict. I described it here. Then you have a bunch of different tests. Is al-n returns true if all characters are alpha numeric. So it means if characters are either letters or numbers, it's true. If there's at least any symbol or even space, it's going to return you false. Right? This is symbols false. Here's like you have a space. Not good. False. All right. Then is it alpha? So is it
245:10
Speaker A
only alphabetical characters? So ABC is going to be alpha true. ABC 1 2 3. No, because it contains numbers. Then you have is decimal is digit is numeric. And don't true if all characters are decimals, digits and numeric. There are a few different rules. What is a digit, decimal numeric? In general, you can see that this one is only going to be if it's just numbers from 1 2 3 until 9 and zero. Nothing else is allowed. Then digits, it's also going to allow you the special ones like
245:40
Speaker A
cube, square, and so on. And numeric is pretty much similar to that one. I think there are also special symbols allowed like 1 divided by two when it's but just ignore it. You can always ask JPT what was the difference. Then is it a space? So is the single space? Is it multiple spaces? Yes, true. And then here is there anything else except for the space? Then it's going to be false because not all characters are space. Is title then it's going to be does it follow the title rules true
246:11
Speaker A
false depending like if every single word starts correctly then lrip I actually need to put it in ignore. I don't think you're going to use it. It's going to get rid of spaces only on the left side. So, not very useful, right? That partition, it's somewhat like a split, but it always kind of splits it in three parts. Um, I for a long time didn't even know it existed. But what it does, you take partition, for example, space. It's going to find the first one.
246:39
Speaker A
It's going to ignore the next ones, and it's going to put you whatever before, whatever after, and partition itself. Here I have hello world. I want to have hello then partition then world. If I'm going to write here something that doesn't exist, it's going to put everything inside first and then empty empty. Not very useful to be honest, but just know that it exists in case you need it. All right, and that's all the regular ones. So the last ones I ignore. We're
247:05
Speaker A
going to go very very quick. Center. You don't really need to center your strings. You're just going to add spaces on the left and right. You can try it. Encoding. Usually you don't need to do this. Python is already fine. You just need to define which one. In general, UTF8 encoding includes all the characters and all the symbols from other like languages and so on. For example, Russian cileric or certain German letters, they use UTF encoding because they're not standard letters. If
247:28
Speaker A
it's standard, it's asky, but we don't care about this. Now, expand tabs, set tab size of the string. Well, you can ignore that one. It's just going to is your tab two, four spaces, six, and so on. Doesn't matter. Format map. I think you have to ignore it as well. It's like formatting but it's over complicated and you have to use dictionaries. So you define okay I can have my name and age here then I create my dictionary and then I can match them together. You're
247:54
Speaker A
not going to use it. Trust me just ignore it. You can check is it all asy. This is again back to encoding. Is it UTF8 or is it ASKI? You can check here what is what. All right. The next one is I identifier just checks if kind of the string is a valid variable name. You're not going to use it. Trust me. Just ignore it. Is it printable? Well, you can test it, but I would just print it and see if I get an error. Also, we can ignore it. I'll just justify by the left
248:20
Speaker A
side. So, it's just going to add lots of spaces on the right. Never found a use for that. Just ignore it. Make trans. Not even going to explain it. If you do this, it's going to return you a dictionary and it's going to return characters to certain asy position. Just ignore it. Okay, this is like you're not going to need it. Now, we're going to go to list built-in methods. And first of all, there are much less of them. strings have the most methods. Therefore, it took so long to get to
248:44
Speaker A
this point. Now, we add the lists and you can see there are nothing to be ignored as well. So, we're going to start with essential ones. Then, we're going to go to regular ones. And I'm going to go to tpples. And you can see it's always less and less and less. So, essential ones. First one, append. This is how you add an element to the end of the list. Let me copy that one. Paste it here. Run it. So, you had a list one, two. You decided you want to add item three. Now it's one, two, three. Let's
249:14
Speaker A
just meet it two times. Run it. One, two, three, three. All right. So appends add the item to the end of the list. Then extend is the way you kind of combine two lists. Again, this one, notice it extends existing list. This is this one I don't like to use. Add list number one, list number two. You take list number one, and you add there another list. And when you print list and number one, you get this one. Majority of people don't like to use it. They prefer much better alternative. You
249:43
Speaker A
just take list one. You combine it with list two. This is going to be list three. Now you print it. Now you have list three. Much better than extend index. Similar to what we used inside the strings. It's going to find the location of the item you're looking for. If you're going to look for this, it's going to say it's in the position number one. If there are many of them, check this out. It's also going to see more of them. Sometimes you can start click on
250:09
Speaker A
it hold control-click on Q it's going to give you the dock string you will see here okay you provide the value this is the first one but then there are also optional arguments they already have the what is it u default values for example you can always use the start and it says here zero is by default but let's say I want to see I want to find the position number two after second item so I'm going to run it okay the next one is going to be three and so on you can also
250:37
Speaker A
to do these kind of things. Not going to go deep in that but it's good to know the index especially when we look at the next one is insert adds element at the specified position. Right? So you have your list one three let's just add a few more items four five six now this is position number zero one two three now we say that I I want to take my list and I want to insert something at the position number one and this something is going to be two. Let's actually make here word two like this.
251:10
Speaker A
So now we're going to run it. And now look at that. So this was 0, one, two, three. Now we said number one should be two. So we added it right here. And then everything became kind of whatever it had to become like one more, right? Five, six. So this is insert. You just add items not in the end. Append adds it in the end. This adds in specific location. Sometimes you actually need it. Pop is the opposite. It's how you get items out. There are two ways to do this. One
251:44
Speaker A
is pop is actually when you want to take the item. For example, in this case, you pop item at position number one. So this is actually going to be two. When you run, you have your list, you have your item which you just took from the position number one. And now your print item is going to be uh two. And in the list is actually going to be one three because you just took the number two out of it because you specified by position. You could also say you know what if you're not going to
252:12
Speaker A
provide anything and run it's going to take the last kind of position. It's going to be like minus one and most importantly it removes it from the list. So you could say item equals my list for example first item and then print it and then you get your item but also you're not modifying your list right? Your list is not going to be modified. It's only going to be modified if you use the pop. All right. Now, the next one is remove. This one is just going to remove the
252:40
Speaker A
items. So, let's just make that. Let's make a few more tools. I run it. It's going to remove the first two only. So, I could make it multiple times and I would see remove. Okay, one more. You're not going to duplicate it over and over, especially until you don't even know where how many there are. You could do it in a for loop in a special way but in general you can remove items like this. Another one sorting super useful very often used also sort is very powerful.
253:10
Speaker A
If you're going to hold control click on Q you'll see that um let me see the sort it has a key and in there you can provide the rules in a special way. The best way is to just ask chat GPT and say hey I want to have this list I want to sort it like this. Just give me the code. It's going to make it correctly. just copy paste and learn from it. Right? These are the essential list method. Let's quickly go through regular ones in there. You can copy existing list. Add one list. You made a copy as a
253:39
Speaker A
new list. Totally fine. You can count how many um numbers of elements with a specified value are. For example, here this is the list. Count how many number ones are. It returns you number two because there it's used two times here and here. You can reverse your objects. And again notice that this one is modifies existing list. So it was one two three and if I'm going to reverse I actually had to make here like 3 2 1 right everything simple in clear it's just going to kind of clear existing
254:10
Speaker A
list. So if I'm going to do that and run it then it's going to become clear right in my case I prefer to say you know what if my list is supposed to be empty list I'm just going to override the value. It's a bit different, but pretty much you get the same result, right? You had a list, now you have empty list. Much simpler. All right, we're done with list methods. There not so many of them, and they are actually very useful. Then the tpple methods, they're not really useful. You
254:38
Speaker A
can ignore them to be honest, but I didn't want to only show you ignore methods. So, first one, return number of items specified in a tpple. Uh, same like lists. Index, return the index of the first occurrence of specified value. same like lists, right? Dictionaries. There are a few more. Let's start with the essential ones. All right. And the first one is get and it's like this. You have your dictionary and you take the dictionary. I want to get item A and you get the value, right? I
255:08
Speaker A
want to get item A. You get this one. It's exactly the same as if you would use this kind of alternative by using this syntax. My dictionary provide a here. I would name it value and run it. I get my value. Just use this syntax. Way better, way more readable. This is just what's happening in the background. Items very essential. Let's run it. Click it. This is going to return you. It has a class of dictionary items, but just treat it as a list. If you don't
255:38
Speaker A
like seeing this, you can also say print it as a list. So you see less disturbing thing. But it's going to return you tpples, right? Tpple number one, tpple number two, tpple number three. The first item will always be uh key. Second one is going to be value or 01. Right? And the same as items, we can also do the same thing with the keys. We're going to print it. You'll see this time I'm only going to get A, B, and C inside the list. Now I'm going to go a little
256:07
Speaker A
bit ahead. Then there is also values. This is exactly the same thing, but you're getting a list of all possible values. You often when you work with dictionary sometimes you need keys, sometimes you need values, sometimes you need both. Sometimes you need just one item. Now set default is well this one is a bit unique. Honestly I never used it before. I had different workaround but it's actually very nice. It gives you options because let's say you have a dictionary and you want to get a value.
256:37
Speaker A
I want to get value C for example. I'm going to say my dictionary set default C. The way it's going to do this, it will look into the dictionary and have a look. If it already exists, I'm going to get existing value. So if if I'm looking for the C and C inside the dictionary, I'm going to get whatever is in the dictionary, right? It includes free, I get free. However, if I'm going to look, you know what, here's my dictionary. Give me item D. But my dictionary does
257:04
Speaker A
not include item D yet. Therefore, I say, okay, if it doesn't exist, give me number four. right here. I gave you a little bit wrong return values, it's actually going to return you the value itself because normally if instead of that you say, but why bother? Why not just use my dictionary? Uh what is it? C. And then value D is going to be D. Well, obviously C you're going to get the value, but D you're going to get an error because this value does not exist,
257:36
Speaker A
right? There is no D. Therefore, you get key error. D does not exist. Right? This is when the set default can be nice. You can say, you know what, it can be none. Run none or doesn't will not exist, right? You're not crashing your program. Well, I did right here because I forgot something here like this. But in general, you get the idea. You do not crash it. You get something different. And you can play around with this. All right. And the last one because we
258:10
Speaker A
already covered the value is how to use update update dictionary with another dictionary. So you pretty much joining to dictionary together. So you had dictionary ABC123. Then you want to add it with this one. You take dictionary number one and you update it with dictionary number two and you're going to get a new dictionary containing all of them. Now let's say that a is four and then these ones are new. In this case it will overwrite existing value. That's why you cannot plus it. You have
258:38
Speaker A
to update it. So because you already have kind of the same values and items from the second one is going to override items from the first one. Just keep this in mind. Very important. Yeah, that's all essential ones. Now there are just a few regular ones. And let me see. We can clear everything in a dictionary. We can create a copy. Very clear. Then here return a dictionary with specified keys and values. So you have a list of keys. You want to create a dictionary from keys. You kind
259:13
Speaker A
of provide your list of keys and you say all values should be zero. Going to create your sort of like a placeholder, right? You specify placeholder should be zero and it creates your dictionary with this values. Could be useful when you have like I don't know list of views. You want to create it with empty list. Later you're going to add there something. Again you have the pop similar to how the list works but this time it's the uh dictionary and the difference here if you use the
259:37
Speaker A
pop is going to remove element with the specified key and it's going to return you a value but pop item is going to return you like a touble right you're going to get C free or whatever and that's it guys so we finished pretty much all the built-in methods I actually forgot the sets but it's not as important we can just ignore it you can learn about it some other day But I will add it here as a table. Don't worry. Now for your homework, I want you to actually go through and just have a
260:05
Speaker A
look. Just copy paste what's interesting to you. Just see how it works and just get a feel of working with methods. And that's it. I hope it was useful. I know it's much longer than I expect. I did not want to go that deep, but in general, you get really good overview what's available in Python by default. And these are really important things to know as a Python beginner. And if that was overwhelming, don't worry. You can always come back to this lesson, have a
260:28
Speaker A
look at this tables, scroll through, find what you need. You're like, "Okay, I need something with dictionary. Ah, here's the keys. Here's example." And that's how you use it. So, I wish you happy coding, and I'm going to see you in the next lesson after you finish with your homework. So, goodbye.
260:49
Speaker A
All right, it's time to practice again. This time I want you to focus specifically on creating your custom function so you can create a piece of code and reuse it over and over. And obviously you're going to use many other Python concepts you learned so far. But for this lesson the goal is simple. You're going to have a list of words and then I would like you to iterate through all of them one by one. And firstly report how many times each letter was used and what is the most frequent
261:11
Speaker A
letter used in each word. And same as before, first of all, pause this video and try to make it yourself and then come back to it and let's make it together. Ready, set, go.
261:31
Speaker A
All right, I hope that you managed to do this exercise on your own and now let's go through it together. Also, keep in mind that there are so many different ways to get the same result in Python. So, if you did it differently, that's still fine as long as you get the same result. All right, here's the PyCharm, and we're going to find the most frequent letters in words. First of all, let's paste here a bunch of different words. You can see this is a list of random words. You can ask chatg to
261:55
Speaker A
generate 10 words and create a list like this. This is totally fine. And I'm also going to create here a section for functions. Let's insert this and main. This is where we're going to execute all our code. I also like to use a lot of emojis. So I'm going to drop that one and that one here. All right. So first of all, let's create some code skeleton. So we have a little bit base and we're clear on what we are doing. So as I told you, I want you to create your own function. So let's start
262:21
Speaker A
with that. I'm going to write define. I'm going to call it most frequent letters and it's going to take a word as an input. So now inside I'm just going to write pass which is just a placeholder for do nothing. Right? This way it's kind of not breaking any Python rules but also nothing is going to happen. Then in the main we can also write a little bit of code. So we're going to iterate through all the words. I'm going to apply this function. So I'm going to write here for word in random words
262:48
Speaker A
and I'm going to use this function most frequent letters and we're going to put this word inside of here. Also right here you might notice that I'm using word as a parameter name right here and also word right here as a variable name. These are two different things right? This word will only be available inside of the function. It will not be available outside of it. And this one is going to be like in global scope which is can be used anywhere else. For example, here. So it's totally fine to
263:13
Speaker A
use the same words even though PyCharm might warn you that shadow name word from outer scope. It's not a big issue but some people prefer to avoid it. So inside of here we're going to count letters. Then we're going to find max count letters and we're going to report our results. And that's pretty much it. It's not so much and it's fairly simple tool. So let's start with that one. So first I would like to report what kind of word we actually iterating over. So I'm going
263:42
Speaker A
to make a bunch of different dashes. Then I'm going to print here something like uh checking word and then insert the word itself. Let's make it an F string. So we can put the word right here. And I'd like to use emojis a lot. So I'm going to put here magnifying glass like this. This is already enough to run and you will see that at least we now have a base. We're checking each word one by one. Now it's time to actually define some rules and we're going to start by counting letters. We
264:12
Speaker A
can iterate through each letter in the word and then use a built-in method called count. For that I'm going to write for letter in word. And in there we're going to look for the count. We can take the word and then there is a method called count which takes kind of letter or another keyword that you're looking inside of your string. This is exactly what I'm doing here. And now let's also quickly print here letter and count. Now let's run it. And in here you can
264:41
Speaker A
already see that we're displaying all the letters and how many times they were used. For example, in Stobury R was used three times even though it mentioned here three times in a row. Then if I'm going to scroll through I can notice that actually the first letter is always uppercase and all the other are lower case. And this is going to be an issue because if the first one and the second one is going to be uh the same for example let's add here a word TNT right
265:03
Speaker A
I'm going to run it and look right here T was used one time and one time here because in reality in Python uppercase and lower case they are not the same. So we need to solve this first. Let's go back to the function and before we're going to count any letters, we need to make sure that this word is all lowerase or uppercase depending on what you want to check. So in my case, I'm going to make word equals word and I'm just going to lowerase it. This will make all
265:28
Speaker A
letters inside the word lower case. This will be easier to count it. Now if I'm going to run it, look in the AT&T, it says that T is used two times. This is great. Now we need to find the maximum count letters. There are different ways to do this. We could create here two variables, one to hold the longest word and the count of this word. But the problem is that sometimes maybe we're going to have multiple letters with the same length. And this is going to be not
265:52
Speaker A
so ideal. So instead of that, I would change how we do it here because here it's fine to go through to kind of display it as it is. But it would be even better if we would record it inside a dictionary first cuz look at that. This is already almost a dictionary structure because dictionaries need a key which could be this letters and then it needs values which could be the count and this is exactly what we're going to do. So now somewhere in here for letter
266:17
Speaker A
in word. First of all we need to create our uh dictionary of count. This is going to be an empty dictionary. Uh the word actually I'm just going to move it here so it doesn't distract us. And then look we're waiting for the letter. And now we're going to add an if statement. First of all, if letter is already inside of the dictionary, then we're going to do nothing. But if letter uh not in the dictionary of count, then we're going to do that. And we're going
266:45
Speaker A
to add it to the dictionary, obviously. So, let's make it like this. I'm going to write letter as the key and count is going to be as the value. We don't need to make this print statement here. We can just print here dictionary of count. Now, let's run it. You can see we're getting this dictionary s1 t1 for example here t2 and one. Okay, so that works. But I don't like this print statement. So we're going to work on that. But it would be better if we make here some kind of print report. And
267:17
Speaker A
we're going to write for key value in dictionary count items. Print key and value. I'm going to fix the print statement in a moment. This is a very common syntax when you work with dictionaries to print what's inside. We define here for key and value in dictionary items and items is going to return you tpples of all your keys and values one by one. Example, it's going to return you t was uh two as the first tpple and then the second one was n as one. This is another tpple. Yeah, when
267:49
Speaker A
you work with tpples like this, you can assign two variables at the same time and they will take their kind of respective value. Now let's just run it. And you can see we're printing it as before. So far so good. Now we can also work on this print statement a little more. This time I'm going to write print going to make an F string and inside I'm going to write this key use this many times and we're going to put the key and value. You can also rename it if you
268:19
Speaker A
want to. We can call it we can call it letter and then count. Then just make sure that you use it here as well. That also works. See, now we see S used one time, T used one times, R used three times. Uh there's an issue with times, multiple, and singular, but we're going to fix it in the end for now. It's it's fine. We already have a print statement. We're going to fix this little things in the end. Now, we need to find the max count letters. And actually, to find the max
268:49
Speaker A
count, it's quite easy. I'm going to write max count equals actually going to use the maximum function. And inside we're going to provide all the values inside our dictionary of count like this. This way we're going to find the biggest value inside the dictionary. Can also print it. Print max count. Let's run it. You can see two three and so on. So that works. Now we need to find the letters. And you see sometimes it's going to be more than one. For example, in the word cat, all of these
269:16
Speaker A
letters they used once. So technically all of them frequently used. Now let's go back here. And to find the multiple letters, we can actually again iterate for key value in dictionary count items. Then I'm going to check if value equals to max count then this is the letter that we need. And since there could be multiple letters, I'm going to write here max letters. I'm going to add it to the list. So append uh key, right? Key is the word, V is the count. I'm going
269:46
Speaker A
to write the most frequent letter is this. and it was used this many times. Let's make an F string. I'm going to put this list of letters and here this is going to be the max count. We can also break it in two lines. I'm just going to click on enter. Can see PyCharm autocomp completed a bunch of things. Now, if I'm going to run it, it was used two times and used one times. The most frequent letter is T used two times. So far, it works great. Let's scroll through. For example, here you can see
270:17
Speaker A
that I and S in Mississippi was used four times together. So I and S are the same. This is interesting. And that's pretty much it. Our function already works exactly how we want it. And we're getting the exactly results that we need. There are just a few things we can also fix. First of all, I also want to show you that we can return things. Let's for example return max letters and max count. And notice that I can return multiple values from a function. This is something I haven't
270:45
Speaker A
mentioned before. And when you're going to return like this, let's check what kind of type you actually get. So first of all, since we return something from a function, we can assign here a variable. For example, results. Then we're going to print results and also we can print type of results. So we get an idea what what it is actually. Let's run it. You can see this is we're printing results and this is a tpple. So when you return multiple values from a function it's going to be
271:13
Speaker A
returned as a tpple where each item is one by one and later when you're going to get your results you can actually come here and say you know what the letters is going to be results the first item and count is going to be results second item and remember in Python we count from zero not from one. Now I can print letters count and you can see we're getting it right here. Now we could do something else because the first one is a list of letters. The second is just a number.
271:43
Speaker A
All right, I'm just going to comment out this part and let's just fix one more thing. Have a look inside. For example, B used one times. Should be one time. All right, now let's go and find it. And it was used right here. And it's actually a very simple implementation. We could add here an if statement and say if count is more than one then we're going to print it like multiple and otherwise let's just copy that one here. We're going to print it as singular. This is
272:12
Speaker A
not the best implementation that but that's is going to be one of the simplest and you can see one time one time one time three times. Now it reads much better. All right. And that's how we create our custom function to find the most frequent letters in a word. As I mentioned there are multiple ways to get the same results in Python. So if you did it differently, that's totally fine. You don't always have to go and kind of copy somebody's solution if yours is different. If you're getting
272:34
Speaker A
the results that you are happy with and it's fast enough, then there is no need to change anything. And if you haven't done it yet, then I strongly recommend you to follow along the lesson and make this function work. I wish you happy coding and then I'm going to see you in the next lesson.
272:53
Speaker A
All right, you learned about more Python concepts and now it's time to practice because practice is by far the best way to learn anything. And in this lesson, I thought that it would be fun to create a password checker. You know how on the website when you provide your username and password, it checks is your password secure enough? Does it have enough character? Does it have digits, lowerase, uppercase symbols, all of that stuff we all hate. But it's going to be
273:14
Speaker A
a really good program to test our skills inside of Python. And it's also not that complicated. So here's what I want you to do. First of all, I want you to do it on your own. I want you to pause this video and try to write this program. You can use Google, but please avoid using Chad GBT because it will give you answers, but you're going to learn nothing. Instead, try to do it on your own. If you get stuck, look for certain solution for this specific step, and
273:36
Speaker A
that's how you're going to learn more. And for this program, ask user for the input. Then check, does it have enough characters? Does it have lowerase, uppercase letter, does it have a digit? Does it have symbols? And then kind of print is the password secure or not. Ready set go.
274:01
Speaker A
All right, welcome back. I hope that you managed to create your password checker on your own. And if not, don't worry. I'm going to walk you through all the steps and I'm going to explain you everything that I do inside the code and hopefully you're going to learn even more about Python. So, how we're going to check is your password secure enough. First of all, it's always worth to kind of brainstorm your idea and write all the kind of comments of all the steps
274:22
Speaker A
you're going to do. That's exactly what we're going to start with. First of all, obviously, we need a password. So, we're going to ask for user input. We're going to ask for username and password. Then, we need to make a series of security checks to make sure that it has all the things that password should normally have. Let me just copy paste so I don't spend a lot of time on it. And you can see we're going to check length. Does it have at least eight characters?
274:44
Speaker A
Does it have at least one digit? one uppercase, lower case, and special character. And I think that's going to be enough to check the password. All right. And that's pretty much it. Then in the end, we're also going to display results. All right. And I think that's enough to begin with. Later, we will also make it a loop. Make it a function to check multiple password at once. But first, let's focus on that. Now, the first step, user input. This one is very simple. We need to ask a
275:10
Speaker A
username with a input. We just write here username like this. and password like this. Input password. Let's also add here a print statement. Something like creating a new account. Then I like to create dashes to separate my print statements. I think it's going to look nicer in the console. And also I remember when I started I always printed everything that I did. Even as simple as this, I always like to print and ensure that I haven't misspelled anything and it works. So let's do that too.
275:44
Speaker A
run. So, creating new account, username, Eric, password. Let's try Eric Fritz. I can see right here it prints it. That's fine. So far so good. Let's remove this one. And let's begin with the security checks. So, there's length, digit, uppercase, lowerase, and special. We going to begin with the length. Now, this one is simple. Remember, there's a built-in function called len to check the amount of characters or items inside your sequence. So we're going to write if length of password
276:15
Speaker A
is more or equal 8 then it's going to be secure enough and now we also need to store this data somewhere we can either make here like safe equals I don't know true or false and then modify this but it would be also nicer to report all of these checks so later we know okay it doesn't have enough characters or it doesn't have special symbols so we can actually report what's wrong with the password. So for that we going to create here placeholders. Let's add here a box.
276:46
Speaker A
Also here maybe let's add another emoji. Yeah, that's fine. Here we're going to write check length equals. And at first it's going to be false. Let's also make all the other ones with Ctrl D. So I'm going to duplicate it and then write uh digit then lowerase then uppercase and in the end it's going to be symbol. Okay. Now we can store this data somewhere and we're going to come here. If length of the password is more or equal to 8 then check length equals to true. That's the first check.
277:21
Speaker A
It works. Then the next one contains at least one digit. Okay. How do we check it? So we have a password which is a sequence of characters. We can iterate through all the characters and check is there at least one character that's a digit. We're going to write for character in password. And as you remember from the lesson from the built-in methods there there are methods to check is it a digit is it alpha numeric and so on. That's exactly what we're going to use now. So we're
277:48
Speaker A
going to write if character and then there's is digit and if that's going to return true then check digit is going to be true. Now, to organize it a bit nicer, it's best to include all of them inside of this for loop. Let's just move that comment to here. And also, I don't like this green ones. Let's make something like this. So, it's less bold. I'm going to quickly replace it everywhere. Yeah, now it's a little better. Now, the next one, does it have at least one
278:18
Speaker A
uppercase letter? And we're going to write if character is upper. Then check upper is going to be true. And for some reason I always misspelled that one. Let me fix this. Now let's copy that one to here because instead of upper it's going to be is lower here. We're going to check lower. Oh, I forgot to make it here. Is upper equals true. Okay. Then and then contains at least one special character. This one is a bit trickier because there is no method like is symbol. Well, there might be a few
278:53
Speaker A
workarounds, but but we're going to keep it very simple. So, let's actually define here symbols and it's going to be a string of characters. I'm just going to go from my keyboard and click on bunch of different keys to get as many different symbols as I can. I know here, here, this. And I think that's enough to begin with. Later we can add more, but I think it's going to be okay. And now we're just going to check if character in symbols then check symbol is going to
279:18
Speaker A
be true because right this character is going to be there. Now let me just address one concern because you can see we iterating through the password and making one if statement after another. Maybe let's make it if and usually one character is going to return only true to one. That's why it's okay to use the if statements. Now here you will notice there is an issue because between this alif statement and this if statement we have something else. So we need to move
279:43
Speaker A
it somewhere outside. See, I removed it. It works. But now symbols is not detected. So let's put it somewhere on the top. Maybe here. Yeah, that's fine. Now we're making all these checks. We're making true, true, true, true. Now I think it's okay. Now we need to display results. Have all these checks right here. Let's just copy quickly. I'm going to paste it here. So we have it right here on the bottom. And now we're going to write a long if statement. If check length and check digit
280:18
Speaker A
and check lower and check upper and check symbol, right? We're going to check all of them and all of them have to return true. Therefore, we're using and and not or. And if we go through all of that, we're going to write here account created successfully. And here we can add a tick maybe. So it's clear. All right. And else print uh password is not strong enough. All right. We cod it quite a bit already. So it's time to test it. We're asking for the user input. We create placeholders
280:58
Speaker A
to remember information about kind of security checks. And then we check is the length enough. Is the does it contain one digit, one uppercase, one lower case, special characters? and so on. Now let's right click run. I'm going to create our account. So it's going to be Eric and then let's write Eric Fritz password is not strong enough. Aha. So there is an issue. It says password is not strong enough but user doesn't know what why it's not strong right do I need
281:25
Speaker A
more characters more this or that. So therefore we need to add more checks here in the else statement. And also now that I start to think when you define the password I don't want it to contain any spaces. If it contains the space then it's an issue. So we're going to add another check somewhere here. Check no spaces equals false. And somewhere here we're going to add where do we add it? I think we're going to add it here on the top as a new if statement. um contains no spaces. And I'm going to
282:01
Speaker A
simplify it a lot. If space in password, then it's going to be false. But I can write if space not in password, then it's going to be true. I misspelled it as usually. Let's copy that. Paste here. True. Now, we have the security check as well. Now once we added it, don't forget to add it to this line of code. It's not very readable. Maybe we break it in multiple lines. That's still not very nice. There is a workar around for that. Instead, we're going to write checks and we're going to
282:38
Speaker A
include all our checks. Let me just copy all of that. Going to paste it inside and we're going to fix a lot of things. So, we're going to delete all of that and we're going to separate them with the comma. So, comma here, here, here, here, here. Yeah, that's okay. Now, it's way more readable, but now we need to check. And we can make it if all checks, then we're going to do this. This is going to be the same as this one. This one is easier for beginners to write,
283:12
Speaker A
but this is not very readable. Let's agree on that one. So I'm going to kind of comment it out. Maybe I'm going to delete it, you know. And here we define here are all the checks we want to do. And then if all checks, this all will ensure that all items inside a true. If at least something inside is false, then the whole thing is going to return as false. It has to check that all is true. That's how you use it. Now I like it much better. Look at that. Looks so much
283:37
Speaker A
better. Always try to make your code readable. You will appreciate it. And now let's focus here. So our password is not strong enough but we need to know what's wrong. Uh and let's start here. If not check symbol then we do this. If not check what is it upper then do this uh this pass pretty much a placeholder that look there is going to be a code here but not right now. Maybe there's nothing happens but it's good idea to kind of lay out your code first. Uh if not uh check lower then pass. If
284:14
Speaker A
not check no spaces pass digit I forgot somewhere here is going to be if not check digit and if not something with length I think that's okay. So 1 2 3 4 five six. Let's check 1 2 3 4. Yeah, we have all six. And now we can make all the print statements. We're going to write here print. Maybe put here an x symbol. And we need to write uh at least eight characters. And we're going to first write password is not strong enough. And then write y. Then here we're going to write at least one symbol.
285:06
Speaker A
We can also specify here all the symbols we allow because we don't allow all the symbols. We could but you know I quickly made all the symbols like this. Then um at least one digit then at least one uppercase letter. And this one is going to be lowercase letter. And then lastly should not contain spaces like this. I know the code is getting longer. We already add 75 lines of code. Half of them are spaces but still but it's not that complicated. Let me zoom
285:45
Speaker A
out. We ask for user input placeholders. Then our security check. So define is it true or false. Then we combine all the checks. And then in the end we're printing the messages. Obviously, we could simplify it, but I think that's good enough. Now, let's test it. So, we're creating a new account. I'm going to call it Eric. Eric Fritz. Boom. Password is not strong enough. It has to have at least one symbol, at least one digit. Should not contain spaces. Okay.
286:12
Speaker A
Okay. All right. Then, let's write Eric. And then password. We're going to write Eric one. Like this. Click on enter. Booms. At least eight characters. That's right. It has to be long enough. Eric. Now we're going to write Eric Fritz1 123 and this now account created successfully. Great. Obviously we could add even more checks and we could also check the username because username should not have any spaces and I think that's a homework for you because I want you to complete this tool after this
286:41
Speaker A
lesson to also check the username. That's going to be great exercise. All right. By now we have already created a successful program to check the strength of your password. It's a little bit long but it works and it does exactly what we want. But let's not stop here. Let's also add a few more concept that we learned such as loops and functions because let's say that for example here on the top I'm going to paste we have a list of passwords and we want to check that all of them are
287:04
Speaker A
secure enough or not and to do that first of all we would need to use the loop right so we'd have to write for password in passwords and then do all of this but also that might become a little bit complicated because we have so many passwords so it's best to create a function so we can reuse this functionality. Functions are the best when you want to reuse your code. And whenever you catch yourself copy pasting the same code over and over, you're making the greatest scene in
287:28
Speaker A
programming. Programming is all about being efficient. Therefore, you have to reuse your code. And that's where functions come in. Now, let's go through this code and we're going to convert it into a function. So, we provide a password inside and then it tells us is it safe or not. Now, somewhere here I think. So these are the inputs and this is everything that has to go inside of the function. So here on the top we're going to write def check password. Obviously it takes password as an
287:55
Speaker A
argument. Then I'm going to click and drag all the way down. And then we're going to click on the tab. So everything moves to the right. So you can see shift tab to the left, tap to the right. And I can do as many as I want. So also I don't remember if I showed you before but you can create a dock strings for your functions to document what you do. You make it by triple kind of quotes. It can be double quotes or it can also be triple single quotes. And here you can
288:19
Speaker A
write description of your function over multiple lines. This is great to document what you do, how it works and so on. Here for example, check the strength of your password. We can write returns true or false. We're going to add it in a second. And also we can kind of write here what does it check. Let me just copy paste it right here. These are all the checks. Let's write checks. Then on the bottom returns true or false. So this is just description for developers. So I think
288:56
Speaker A
it's still best to use double quotes here. Let's do that. Yeah, much better. And then we're going to go through all of that. And here. And here we're going to return true. or otherwise it's going to return none if there it doesn't have any return statements which is also fine. None and false they're pretty much the same. Now we have it as a function. Now let's just hide it for a second and let's use it. We're going to write check password. First of all we're going to do it with
289:26
Speaker A
that one. So run. We just need to ensure that it still works. Eric Eric. Okay. Password is not strong enough. And if I'm going to run and give it strong password, this this this this. Oh, it's it was supposed to be a password like this. Account created successfully. That's great. So, it works. Now, maybe we also going to add here one more print statement. Checking password. And we're going to insert here the password that we check password because now I want to go through all of
290:04
Speaker A
these passwords right here. So let's kind of collapse that one. You can see there is this u icon right here to kind of hide it. Now we're going to write for password in passwords. We're going to check this. Now we can also comment this one out because we are not asking user for the input. We're just kind of checking this password. This is where loops and functions come in handy. Now let's run and now have a look here. Checking password successfully. Password
290:33
Speaker A
successful. Actually, let's go and maybe remove a few things. Let's make this one super short. Let's remove capital letters. Let's remove all lowercase, then all symbols. Yeah, I think now we should get at least some errors. Now run. It's a bit hard to read. Again, let's go to print statements and add here dashes. I love printing dashes. It's easy to separate what you do. Run. Look at that. Checking password ABC at least it has to be at least eight characters and missing one digits. Okay,
291:09
Speaker A
this one needs at least one uppercase. Then this one password is not strong enough and so on so on so this successfully and you can see that first of all we created our code to check the password which was great. And then we created a function. So now it's a reusable piece of code you can apply to many passwords and here is obviously we use a loop to iterate through all the passwords and check them one by one. When I code like this, I also like to separate it like functions. I like to
291:36
Speaker A
maybe something like this, right? So, this is our password. Then here is going to be our main. Let's use the target emoji.
291:49
Speaker A
Uh, then somewhere here we're going to have maybe box and we're going to call it variables.
291:58
Speaker A
And this was well it was supposed to go into the main but we don't need it anymore. Let's just move it here. And now have a look. Our code is now even more readable and see. Okay, these are our variables. Then here is our functions. This case we only have one function. It also has documentation. So we know what's going on inside. There's a bunch of different checks. Eventually it's going to return you true. We can also check it. And it makes a bunch of print statements. And now let's also write
292:28
Speaker A
here check equals. We're going to write is secure. And we're just going to print this check right here. Let's run it one more time. And you can see is secure true is secure none. Okay. In this case, we would need to return false somewhere. And probably it would be great idea to put it here. Return false in the end. Run. Now, true, false, true, false. Now, it's actually reusable function. All right, guys. I hope this was a good lesson to show you how we can apply all
293:04
Speaker A
this Python concept that we're talking about into an actual program. Now, we can check your passwords. And if you would like to practice a little bit more, I want you to create a second function to check username. Make sure there are no spaces, that it's long enough, maybe four characters minimum, and maybe some other checks. That's up to you. I'm not going to go there. I'll just leave it all to you. And now I want to wish you happy coding. Once you're done with this exercise, let's move to
293:27
Speaker A
the next Python concept. See you soon. Hi everybody. Today I would like to introduce you to recursive function in Python. It's a function that can call itself over and over to solve a smaller piece of the same problem. It's also often referred to loops because technically you can achieve the same result with loops. But with recursive functions often times you can make your code much simpler to read and express algorithms way kind of easier. We're going to cover a few examples. We can
293:55
Speaker A
actually see and compare between loops and recursive functions. So let's just jump into code and I'm going to explain as we go. So as I've mentioned that recursive functions it's a function that can call itself. I'm just going to provide here a simple example and it's just a countdown. We're going to give a number. We're going to count down from this number until we reach the zero. So here's our function and the call using the while loop. Here's the same using
294:16
Speaker A
recursive function. Let's start with the Y loop because it will help you understand recursive how it works. So in this case I created an infinite loop because I wrote while true and then I created here a condition. If n is less than zero then we're going to break right? So we're going to start at 100 998 blah blah blah 0 minus one. Booms we break. And then while we're iterating, we're going to be just printing the number and we're going to be decreasing n with each iteration. This is going to
294:42
Speaker A
help us to kind of get to this condition to break. And it's very simple. Let's just execute it. You can see iterative 5 4 3 2 1 0. Very straightforward. We could also kind of make it a little nicer by writing here while n is more than uh zero. This will replace this statement, right? You can see everything still work the same. Now the reason I wanted this syntax is because it will help you understand recursive function. When you create recursive function there
295:10
Speaker A
are two parts. First of all you need to define the base case. This is the case that will tell you that you reach the end. And then the recursive case where you say like okay we're still not at the end. So we're going to recurse. So we're going to create recurse. We're going to recurse recurse recurse until we reach this base case. And in this case the example is fairly simple. We just have a number. Let's say it's 100. And until n is not less than zero, we're going to be
295:36
Speaker A
in recursive case. Once we're going to reach this b base case, which means that once n is less than zero, then we reach the base case and we're going to start returning. We're going to start unwiding the stack. Going to explain it more visually in a moment. Let's just focus on the basics first. So for example, here you have a number five. We're going to go through here. It's going to be false. So we're not going to reach the base case. And then we're going to be
295:56
Speaker A
here going to print five. Then we're going to execute the same function but this time with a four. So it's going to be four. This one again skip. We're going to print four. Then we're going to execute this function again with a three and so on. Then with a two and so on. And then eventually we're going to reach I think minus one. Here we're going to check. Oh, it's actually true. So we're going to return and we're going to start unfolding this tag. Going to explain
296:19
Speaker A
what it means in a second. But it's important also a really really important point here is that if I'm going to comment this out if you don't have a base case you literally just all you do is just printing the value and calling it itself. Going to run it. You can see we're going to hit an error. It's called recursion error because in Python there's a limit on how many times you can run it. It's a little less than a th00and can see here it's 990 and we started at five. So it's think it's 995
296:46
Speaker A
or something. And here we can see we started we're going like one by one just like a loop until we reach a thousand iterations then it's just like okay probably you're doing something wrong and well makes sense but once you have the base case you're going to create this kind of recursive iteration until you reach your condition and you stop. Okay, this is just the basics of how it works. Now it might still be a bit confusing but let's go to the actual example where it's going to start to
297:11
Speaker A
make more sense. Now we're going to have a look at a very popular example on how to creating factorial. If you don't know what is factorial, factorial is the product of all numbers from one until the given number. So if factorial of three is going to be 3 * 2 * 1, it's going to be six. Factorial of four is going to be 4 * 3 2 1 24. Factorial of five is going to be product of all of these values from 1 to 5. It's a very common example for recursive functions and you will see why in just a moment.
297:40
Speaker A
But again just to have some example, I prepared here the same factorial using the loops. So in here we're going to say we're starting with number one and then while true and n is not equal to zero just going to go through the numbers and kind of multiply them one by one over and over and eventually we're going to return the result. So rec factorial of five here is going to be 120. Now I'm going to do the same but using the recursive function. I'm just going
298:04
Speaker A
to paste it here. Now have a look. This one looks way cleaner than this one. Even though we can simplify this one that say while n is not equal to zero. We're going to just kind of run like this. You can see we're getting the same results. But this one to be honest is more logically written in terms of math. But now I'm just going to remove it. If you want just pause and compare them, but I'm going to just remove that one because I want to focus completely on
298:31
Speaker A
this. So here's our factorial function. And first of all, here's our base case. Meaning that once we're going to reach this point, we're going to stop creating new kind of recursions. So if n is equal to zero, then we're going to return just one. Then here is our recursive case. So for example, if n is equal to 5, going to multiply it by factorial uh 5 minus one. So it's four, right? It's going to be five * factorial of four. And factorial of four is going to be by
299:00
Speaker A
factorial of three. Factorial of three is going to be factorial by two. and so on so on until we reach the base case and then we're going to get all this results unwinding and now to best understand what's going on here we need to add here a few print statements so we can track down and I can explain the biggest concept of recursive function first of all I'm going to write here entering factorial with this number so it's going to be n let's make it an f string
299:26
Speaker A
then if we're going to reach the base case then we're going to write base case reached and returning one and here we need to change a little bit because I want to return it first. I want to get it result and only then I'm going to make the print statements and then I'm going to return it. Uh something like returning this result for factorial n something like this. So it's going to be f string result is going to be here and here is the actual value we have right now.
300:05
Speaker A
Then I'm going to return result. It's exactly the same thing but we just added print statements so we can understand the logic what's going on. And it's really important because well it's not what you expect. Let's remove that one. I'm going to run factorial of five. Let's run it. And you'll notice there is like a lot going on. Let me just fit everything on screen so I can explain what's happening. So when we giving number five, so n equals 5. First of all, we're going to go here print the
300:34
Speaker A
number which we see right here. Then since we are not reaching the base case, we're going to go right here. I'm going to create a new factorial. So we're going to write 4 32 and we can see it right here. So it's 5 3 4 2 1 0. So when you create a recursive function that calls itself, we start to stack the function calls on top of each other. Think of it as a plates that we stacking one after another. So this is going to be our tower of plates. And this is what's going on. So we start with a
301:03
Speaker A
five. We're printing this one. This one is false. So we're not executing it. And then here we're creating a new kind of uh recursive call. So it's going to be five. Then we start again. It's going to be four. We start again. It's going to be three. And it's just going to be like this. So it's five into four, four into three, three into two, one into one. Then eventually we're going to reach this base case zero. This is where we stop and we say okay we reached the
301:26
Speaker A
point and it's going to be here for example zero. We printing zero and we say okay we reached this point. I can see it right here as well. And after that we start to unwind the stack means that we start to work with the actual values we get out of them. I can see right here returning one for one returning two for two returning six for three because each of these results dependent on the previous one. Eventually we're going to return this 120 which is going to be right here. So
301:54
Speaker A
let's just have a look at it again. We start here at the five. Then we're going to go and kind of stack all these calls one after another till we reach the base case. And at this point we kind of kind of backward solve the problem. And now we start kind of adding all the solutions like answers. And here it's going to be 1 1 2 6 24 and 120. Eventually this is our answer. This is what we returning right here. So in short in recursion first of all we're going to create a call stag and then
302:22
Speaker A
we're going to unwind this tag to get back to final answer and that's the recursive function and probably you wonder but when should you use it and usually it's great when you have a problem that naturally breaks into smaller steps of the same problem it's very common to use in math algorithms like Fibonacci factorial that I showed you then working with nested structures like nested folders JSON uh different tree structures then various searching and sorting algorithms add in puzzles
302:49
Speaker A
where you need to explore all possibilities. In general, use recursion when the problem can be broken down into smaller versions of itself. And if you want to practice and create a recursive function, I can give you a really good exercise. Create a function where you give a number and then take every digits in the number and add them all together until you left with a single digit. So what it means that for example, if you have 38, it means that there is 3 8 3 +
303:15
Speaker A
8 11. It's still not a single digit. So we do it again. This time it's going to be 1 plus one. It's going to be two. Now it's a single digits. We return that. And on large numbers it's going to be a little bit like I'm going to show you on the screen how it looks. So get an idea what you need to create. And to do this first of all create a function that takes a single number. Then you need to create your base case. In this case it should return you a single digits.
303:36
Speaker A
Right? So just check if it's a single digits then return it. Otherwise it's going to be recursive call. First of all you're going to calculate the sum of every digit in a number. Right? So it's see if it's 1 2 3 4 5 it's going to be 1 plus 2 + 3 + 4 + 5 use a loop and you will figure it out and then create recursive call on this new total number and then you're going to do this until you reach a single number and you're going to get it. You can also use while
304:00
Speaker A
loops but for this case I think it's a fun easy exercise to practice recursive functions. And now I want to wish you happy coding and we're going to see each other soon. And again, try to practice yourself before you see the example because I'm going to show it in three, two, one. Enjoy.
304:28
Speaker A
During this course, we have encountered a few errors already, and I told you to just ignore and avoid them. That's not the best advice, but it worked until now. However, now finally, let me introduce you to error handling basics. And firstly, let's make an error and then I'm going to explain what to do. And that shouldn't be that hard. So, I'm going to go to code and let's just write here print one divided by zero. Right click, click on run. Here we go. We get
304:54
Speaker A
an error right away. Firstly, what is happening here? You see trace back and it tells you most recent call last. So, it means that we executed code and we got got an error. tells you that file then refers to what file you're working on and it's going to be this one and it tells you that you encountered a specific error called zero division error. You see in Python there are different types of errors and they have their sort of like categories. Let me just pull up on the screen one website.
305:21
Speaker A
It's from the W3 schools and it's Python built-in exceptions. It's just a nice table to see all different kind of built-in errors. You can see you can have syntax error, system error, system exit, type error and so you don't really need to understand what's going on here. The most important one here is the exception. This is the base class for all exceptions. So this one is like the main one and everything else is based on that, right? And it just tells you that
305:46
Speaker A
okay encounters when something happened in numeric calculations then there is somewhere going to be our what is zero division error and so on. So this helps you to identify what kind of error you get to make specific kind of solutions if you want to. You can also create your own specific error messages in your own way that you can trigger but not going to go this far. What's going on? Let's start with the basics of error handling in Python. How do we do this? When you
306:12
Speaker A
have a code where you might have an error, let's say that you want to divide it a by b. There's a chance that b might be a zero. So it's good idea to make try. So first we make a try block. This is kind of where you write your code that might have an error. And when you write try, you have to write accept. You write here like okay, first we're going to try to execute this. However, if we get any other errors, we need to do something else. And by default, you can
306:37
Speaker A
write pass and you can see there are three keywords. So try is like your try block. What you want to try to execute. Except is your exception block. It's only going to be executed if you got an error inside of this block. Pass is just a word in Python. do nothing, right? Absolutely do nothing. Just ignore it. So, I'm going to right click, click on run. Boom. Now, I don't have any errors, but I also don't get any hello print statements. I don't get this print
307:03
Speaker A
statement. Nothing happened, right? Let me just write here the end. It might come in handy later. So, I run it. You can see my script works. I actually print something. It's just not nothing is printed here. What's going on? So first of all, we're going to write in accept something. Let's write print error has happened. I'm going to put some stars to make it more, you know, like pop out in the console. And we're also going to add some emoji here. Now click on run. Look at that error has
307:37
Speaker A
happened. We don't know anything about the error yet. We just know that it happened. And in our code, it's clear like we printing 1 / 0 gives us an specific error, right? Let's just kind of copy this on top. and run it. Boom. Now, first thing you I want you to understand that whenever you run code and get an error, you stop your execution because this one was never executed because after we reached line two and failed, it did not go any further. Just get there's an error. I'm
308:04
Speaker A
not even going to try go further. This is why try and accept so important because sometimes you might ask user, hey, you can give me an optional value, but if you don't, that's okay. They give you an optional value and it's wrong. You can either crash or you can just ignore what they gave you and use your default value. This is one of the ways. Now when you deal with try and accept, normally this is like the absolute basic you will use all the time. You try, you
308:27
Speaker A
accept, right? There's also another thing that we can go a little bit deeper. So we know that in here we're getting zero division error. We can copy that name. And right here, you know what? Accept zero division error. Now I have a specific kind of handling for these specific types of errors. I'm going to have I'm going to write here that dividing by zero is not allowed. Now I'm going to run it. And look, I get an error because I forgot to remove that line. That's okay.
308:58
Speaker A
I'm going to remove that one. The one that's on the line two. Going to run it again. Now division by zero is not allowed. All right, this is good. Now let's say that I also make another mistake here. Before that, I actually say x= x and x is not defined. Right? You can see it's red unresolved reference. That's okay. I'm going to run it. Boom. I get an error. This time it's a name error. So this time I get another error. I could say okay, you know what? I can also make another handling. I can
309:27
Speaker A
write here except name error. Print uh fix syntax. You know, I'm not going to go descriptive. That's more than enough. I'm like, okay, let's try that one. Okay, now it works. So in here you already have different options to prevent different kind of issues. But to be honest especially as a beginner don't you even worry just use try and accept and just say like hey error happened because most cases you will already know what kind of error you get because you write your code here and you probably
309:59
Speaker A
have an idea what could go wrong. So just use this and that's totally fine. Now sometimes you might want to see the error message itself. There are different ways to do this. One of them is like except exception as E. That's a very special syntax. Just give me a moment. I'm going to print E. So in here, what's going on? We say except you know what it does. Then specific exception. This is the base class for all of them. Every single exception based on that. But in here, we
310:28
Speaker A
say except exception and we put it inside the variable E. This time we can print it. I'm just going to print a short definition division by zero. Right? You can refer to the error as well. To be honest, I did not find it as useful even though many people teach you to do that. Instead, I like to do the following. So, in here, we're going to write import phrase back and then we're going to write print phrase back. Then there's a special method called format exe. That's what we need, I believe. So,
311:00
Speaker A
we're going to right click and click on run. Have a look at this. Now, we're not crashing anything, but we're still getting this error message. And often times this is really important message because it tells you a lot tells you what file it happened on what line it happened what's what's actually the code and the name of the error and often times with try and accept you kind of remove this kind of trace back message which is really important. Now with control and slash I'm going to kind of
311:25
Speaker A
comment it out. All right guys and that's pretty much it about error handling. These are absolute basics and this is more than enough to kind of begin with. Later you will know what kind of errors to expect where you can encounter them how to avoid them and then this concept will be more useful. For now just know that if you get an error use try except and just try and see if you can solve the logic in the accept block. All right in this lesson I just want you to go and try this try and
311:52
Speaker A
accept block. Just try to make a few errors try to make a few print statements and just understand the logic in general and then I'm going to see you in the next lesson. See you soon.
312:06
Speaker A
want to introduce you to the next advanced technique called comprehensions. They will make your life so much easier in coding. And the best part about them is that they make your code more compact and also more readable, which is really important aspect as well. That's also the way how you can create great oneliners. And oneliner in programming refers to writing a piece of code that performs an entire task in a single line. Some people can also go crazy with that and
312:31
Speaker A
make make it a challenge to create a very crazy long oneliner that nobody can read. We're not going this far. All right, this is a bit overkill, but I want to show you how to actually use comprehensions to make your code more readable and also faster to use. And to be honest, they're not so complicated once you understand the syntax. So, let's dive in and I'm going to explain you everything. So, the first thing you need to understand that there are different comprehensions. We're going to
312:54
Speaker A
start with the if statement comprehensions and we're going to start with a very simple code and actually let me just kind of paste it so you can see it. It's going to be much much quicker to do this. So let's break down what's going on. We have created a variable and assign it a true value. Right? So toggle is true. Then we make a simple if statement. If toggle is true then we want to make word enabled. Otherwise the word is going to be disabled. Then we're
313:17
Speaker A
just going to print the word. Right? So we're going to run it and you can see it's enabled. All right. Firstly, we can kind of remove this part right here because the if toggle and if toggle equals true is exactly the same thing, right? But it's still not so much better. So, how can we make it better? And this is where comprehensions comes in. So, we're going to make here some space. I'm going to rewrite this part right here. Instead, we're going to write word equals and then you write
313:43
Speaker A
your first value enabled. Right? Now, we can add here comprehension. And we write here the word is going to be enabled if toggle otherwise the word is going to be disabled. All right, that's not complicated. Here's your expression if true. Here's your condition. Here's your expression if not true. Right? Exactly the same thing, but you already save some space. There's another thing less popular one, but when you create your code blocks, you have an option to make
314:11
Speaker A
it in one line as well, as long as it's just one line. In this case, you could also do it like this. This is probably many Python experts and instructors, they're going to hate me for showing you this, but that's also an option, right? It doesn't make make it look Python, but sometimes I do this to save some space. But still, it cannot compete with that one, right? This is so much easier. And at first, it might be more complicated to read. But eventually, you just look,
314:37
Speaker A
okay, the word is going to be enabled if toggle else disabled. It's it literally reads like English in this case, right? You can see this. And now let me just comment out that part right here. And we just run it. And now we see enabled. So if that one would be false, let me just fit everything. Run. Now it's going to be disabled. Right? This is the first comprehension I want you to know about. So there are if statement comprehensions. All right. And that was the first if
315:06
Speaker A
statement comprehensions. You can also take it a step further, make it more complicated, include more functionality, but we just focus on the basics. I just want you to know that this is an option. Later, you will figure out what else you can do. Now, we're going to go to the next one. And the next one is probably the most important one. So, we're going to spend the most time in this video on that one. And as you guessed it, it's going to be list comprehensions cuz you're going to
315:26
Speaker A
use them a lot. Well, at least if you follow my tutorials because I love using them. They look great in tutorials. They are easy to understand. And most importantly, they are also going to make your code much more compact and more readable. That's the goal here. So, how do we create list comprehensions? First of all, I'm going to break down the syntax and the meaning what they are. Let's say that you have a list of items, right? It's just going to be item A, B,
315:48
Speaker A
C. No need to go crazy here. Then later you decide, you know what, I need to take this list and create another one. But this time, I need to maybe filter something. I need to make sure that only something with item goes in. Or maybe I want to take all items in my list and transform them. Maybe I want to make them all uppercase. Right? So, normally with the code, you'll do it like this. Uh let's say I want to make everything uppercase. So we're going to create a
316:14
Speaker A
list of upper items. Then we say for item in items. Then we're going to say item upper is going to be item upper. And then upper items we're just going to add new values, right? Item upper. So we create empty list as a container where we're going to add everything. Then we iterate for the items and then we say okay for each item make it upper and then add it to the list by using the pet append method. We could simplify it a little and let's say that I don't want
316:43
Speaker A
to create this variable because I can just directly make it here right the same thing. Okay, not bad. But still it's not it doesn't look as good. All right, as the alternative which I'm going to show you in a second. So for the alternative we're going to write you know what I want to have a list of upper items. So this is going to be a list. Now I'm going to make my list comprehension inside of that list. I can write here expression then for item in iterable uh container
317:12
Speaker A
and then if condition if I want to right this is just kind of the syntax. So this is kind of the logic we're going to follow. Obviously everything highlighted in red because it's incorrect. But let me just break it down here in the middle for item in turbo collector. This is going to be your follow, right? So this is going to be this part right here. We're literally going to write it in here. Then um then this expression is going to be what goes inside of your
317:37
Speaker A
container. So we're interested in this part right here. This is going to go right here. Right? This is the result you are getting. And then optionally you also can have if condition. We're going to cover it in a moment. Right? First let's make it without any if conditions. So this is what we will need to do. Now let me just clear it. And now let's kind of create this comprehension. For beginners, I also recommend first put the kind of loop for item in items. But
318:00
Speaker A
the result have to be always in the beginning. So in here, we're going to put this item upper, right? And that's exactly the same. This three lines or four lines if we will do it also with the variable is exactly the same as that one. This one is actually more efficient in terms of how fast it runs. But you will never notice this difference anyway. So, let me just kind of comment it out and I'm going to print my upper items. Run. And look at that. Now, it's item A,
318:28
Speaker A
B, and C. And everything was uppercase. Now, let's take it a step further. I also want to make maybe some if statement. So, let me maybe just copy this whole thing. And I'm going to make it like option three list comprehension with conditions. And I'm going to paste exactly the same code. But first, we're going to go and kind of work on that one. So, we have our upper items list. We iterating through the items. And then I want to make some if condition. Let's say that we also want
318:58
Speaker A
to make sure that we're getting the right elements. For example, we might have wrong data in our list of items. I want to make sure that item is correct. So, I'm going to write if let's say item and underscore in item. That's going to be our condition, right? Then we're going to do the thing that we are doing. So we're going to iterate through all items. Item A, B, C, wrong data. Then we're going to check if item this kind of part inside of the actual item. Then
319:28
Speaker A
we're going to continue with the code. Otherwise, we're just going to do nothing. So we're going to take the first one. Okay, there's item. That's good. We're going to add it. Okay, item we add it. Item we add it. Then oh, I don't see item here. So we just going to ignore it, right? We could also add here else state, but let's keep it simple. Now if I'm going to run it, look at that. I don't have the wrong data inside my list, right? Because well, we kind of excluded it. We filtered it. This is
319:53
Speaker A
also very common in list comprehensions. You filter your list as well. Now, how do we do this inside of here? And it's simple. Here you have your results. What's going to be end up in your list? Here you have a loop. What you iterating over and where you get all your items. But in the end, you also have an ability to make an condition. So I'm going to write if I'm just going to copy this part, right? If that's the thing. So let's just run it. And look at that. No
320:21
Speaker A
wrong data. So if I would remove this last part in the end and run it, you'll also notice the wrong data right there. But now we're adding the condition. So you here define what is going to be inside your list. Then the loop where you get your items. And optionally you also can define you know what I actually want to make some if statements. There are also kind of more advanced way to use them. But remember the goal of using list comprehension is to make your code
320:46
Speaker A
more readable and also kind of easier to write. And I think we can agree that these four lines they are okay they are basic but once you understand the syntax this one is actually very simple and readable. Most importantly just takes one line right because on larger code when you're going to have like lots of different things just imagine that we need to do this three times. You make upper here, lower here, something else. Then you scroll and it already becomes
321:09
Speaker A
much bigger piece of code. But here I could just kind of duplicate it multiple times and say, okay, you know what? This one is lower, this one is something else. And then you see, okay, these are all the same part. So we iterating, we're filtering, we're just doing here. There are different ways to do this obviously, but I'm just trying to kind of drive this point home to show that list comprehensions, well, they are amazing. Now let's just go and create a few more
321:34
Speaker A
exercises with our list comprehensions. So I'm going to make some space here and let's say that we have a list of numbers from one to five, right? Very simple. Now I want to create a list of all the squared values, maybe q values, maybe something more, right? So how do we do this? Obviously you could create your for loop and so on. I'm not going to do it now. So I'm just going to go straight to my list comprehensions. So I'm going to write number squared equal.
321:58
Speaker A
Then again I'm going to write for number in numbers and then in beginning what do you want to do with the number? Obviously I want a number times number or I can create this power two which is going to make the squared. What if we want to make it cube right? I'm just going to duplicate cube and let's say this time it's going to be number in the power of three. And you know what I also want to make number of cube for even numbers only right? I'm only going to take for example two and four. Right?
322:33
Speaker A
So how do we do this? We just need to add here if statements. I'm going to write here if uh number and we're going to use the mode two uh equal to zero. This is a unique feature in Python. This is kind of mod. And the way it works is that you can actually kind of take the value divide it by something and see if you have any remainder. So if you will try to take a 9 and mod it by 5 you will get four because 9 - 5 equals 4 and four cannot be divided by five as equal
323:06
Speaker A
number. Let me just explain two more two going to be zero because there is no remainder. 2x two you get the answer. Four mod 2 is going to be zero as well because four divided by two is just zero right? There is no remainder. However, if five mod 2, so it's going to be two and then one is remainder. Uh, it's just a I know it might be a little confusing, but it's actually very simple thing to do. It's usually used to find even and uneven numbers like this. Now, let's just take all of that
323:39
Speaker A
and we're going to create lots of print statements. So, the first one goes here, here, and here. And now, let's see what we get. Just going to run it. So 1 4 9 16 25. So these are the squares. These are the cubes. And here you can see we only got cubes for number two and number four. Now let me also make another one maybe even odd. So we're going to go through this kind of list. We're going to create is it even or odd number. So inside as usually we're going to write for number in numbers.
324:10
Speaker A
Then beginning what we want inside I want to have even if number mode two equals zero or else it's going to be odd. Right? I think that's how it is. Let me just put it here and print it. Now I can see odd even odd even odd. This is a little bit already the next step because in here you can see it's even that else odd. But you can see we're going to put a lot of functionality inside of a single line to get the right result. And also a big tip for you unfortunately when I started I
324:43
Speaker A
didn't have this option but you have a chat GBD right and you can always explain hey I want to make a list comprehension for that can you do that or you can write the code however you like it then kind of copy that let's say that that part you copy this thing give to chat and say hey simplify it use list comprehensions if possible and so on and it will do really good job at making those kind of things well it should at least all right and now if you think we're
325:07
Speaker A
over with kind of list comprehensions we are not they are this important Just one more example. Let's say you have a list of materials, wood, steel, concrete, brick, glass, whatever. Now, you want to make sure that you filter certain things. I can override it as well, right? I'm going to use the same name. So, this one is going to kind of get rid of the items. And then I'm going to have a new list. I'm going to write for I don't know uh material in materials in the beginning.
325:31
Speaker A
Then I write what do I want to get? I want to get just material. But obviously, I want to filter it. So, I'm going to add filter in the end. if how we're going to filter it. if let's say s letter in material name. So once more this is our for loop. This is our condition. This is what we get in the end inside of our new list. I'm going to print mats. Let's print it. And here you can see steel, bricks, glass, plaster because they have s S's inside of these words.
326:00
Speaker A
And for example, concrete doesn't have one, wood doesn't have one. They were excluded. So obviously this is a very simple kind of filter example but that's how you will be filtering a lot of things in Ravit API. Now we are done with list comprehension. I hope this example is going to help you understand it and most importantly use it and I highly recommend you to go over this examples and try to change a little bit. Also experiment with filtering. Right? This is really important concept. I
326:23
Speaker A
really want you to understand it's not essential but it's going to make your life much easier. That's what I I can promise you. Now before I'm going to let you go, I just want to get one more example. It's going to be very quick. I don't want to go into this, but it's going to be dictionary comprehensions, right? There also dictionary set and generator comprehensions, but focus on the dictionary and we're going to make it very very simple. Let's take the same list of uh I don't know materials
326:52
Speaker A
and we're going to make something easy. Let's make a dictionary. I don't know. We're going to have key as a lower, value as upper. That's a very simple example, but I think it's more than enough. So, dictionary of materials would be dictionary. Same as lists. First, we need the loop. We're going to write for material immaterials. We also could make here an if statement, but let's keep it simple. As you know, the biggest difference between dictionaries and lists is that
327:19
Speaker A
in dictionary you have key and value. You have two items. And here we can define as well material material similar how you normally create a dictionaries. So if I'm going to print that one, let's just run it. Can see it here. Wood wood steel steel concrete concrete. Now I want to make sure that this one is always slower and this one is always upper. So right click, run. Now look at that. Lower case, uppercase, lower case, upper case. Yeah. And overall, that's the kind of
327:49
Speaker A
list comprehensions also if statement comprehensions. They make your life easier. You don't have to start using them right away. First focus on the basics like if statements like loops like even functions and then slowly you will find where you can use this list comprehensions especially when you're going to write okay you're going to write this code and you're going to remember I remember Eric said I could make list comprehension here somehow I'm not entirely sure how to do this but why
328:12
Speaker A
don't I ask Chad GPT or why don't I kind of reference this lesson again and try to make it eventually your code is going to look like this it's going to be so much better to read and kind of more enjoyable for you and whoever is going to code after you Because as you know in programming, you might write a great piece of code today. You remember how it works. Everything is great. But a few months later, you come back, you open it, and you have no idea what's going
328:34
Speaker A
on, right? This becomes complete mess because maybe you didn't comment it enough. Maybe it's just not as good and you became better. So overall, that's amazing way to improve your code. And that's it for this lesson. Again, just go practice it and then I'm going to see you in the next one. Happy coding. See you soon.
328:56
Speaker A
Hi everybody, welcome back. It's finally time to talk about packages in Python. And remember how we talked about how important it is to reuse your code? Well, what if I told you there are thousands of people how much smarter than us and they developed a lot of reusable code for Python? And they also shared all of that to use for free as a package and we can import these packages into our code and do all this amazing stuff that they already kind of coded. There are packages for everything.
329:22
Speaker A
Windows operating, maths, video editing, plotting, UI, photo editing, AI, machine learning. There is so much you can do with Python. There is literally a package for everything. But we won't download all these awesome packages and learn how to use them because sometimes learning how to use a package is a like a whole journey on itself. Instead of that, we're going to talk about a few built-in modules that are included with Python by default and how they can help
329:44
Speaker A
you in your job and in general how to work with packages. So, let's go to PyCharm. And first of all, I'm just going to briefly show you that normally you can go to settings. You're going to go to your uh project. Select your Python interpreter. And then in here are going to be all the packages, right? These are the extra packages. These are not the kind of built-in ones. But if you would want to get some special package like for example pillow. Pillow is used for image kind of editing. You
330:10
Speaker A
could just click on install package and you would have it in your Python. Just make sure you use the right version of Python. But we're not going to do that. As I said, we're going to focus on the basics, right? the built-in packages that come with Python. I'm going to start with one of the basic ones like OS. To bring this package into our code, we need to write import OS. And before going to go further, just a few syntax rules about importing. When you import,
330:32
Speaker A
you have different options. Here you can see I imported the package itself. Now, think of packages as a folder that contains other folders that contains other Python files that contain all the functions and classes. Normally you can reference the documentation but you'll probably just ask Chad GPT how to do certain things and you will find an answer. In this case you can use OS dot and you'll find different kind of module functions and so on. Example we can see
330:55
Speaker A
there's like the path we can click on that one then dot again then there are common functions like a join is it exist how to make maybe get the dear name absolute path and so on so on so on also sometimes let's say you don't want to import the whole OS module you could write just from OS module import path and now I don't have to write OS path I would need to write path and access all the same things right in the end of the day you can kind of have OS then inside
331:22
Speaker A
there's going to be path then inside there's going to join and so on and you want to just join you could import this but it depends on the package and normally you will see in code examples how it's done now let's just do some quick things with this package so stands for operating system and this is package for just kind of the folder structure explorer creating folders checking them and so on it's very useful we can already see this there are different kind of suggestions what we can do but
331:46
Speaker A
I'm going to bring here a folder structure prepared just a few folders nothing fancy and we're going to create a few stuff also I haven't really showed how to use AI assistant yet. So that might be a perfect way. So let's first kind of copy the path. Just going to be that one. And we're going to create here path variable. Going to save it like this. And when you're going to be working with path, you'll notice that certain symbols and combinations have some meaning. For
332:13
Speaker A
example, as you know, this one is going to create a new line. I'm going to kind of make it here in the middle. Print path. You'll see that it's going to look very very weird. First of all, it kind of breaks it. That's okay. Let's run it. You can see in here it's kind of split in two. And this one also has some other meaning. To avoid this, you can put R in the beginning. Then it makes it as a row string. Also, let's get rid of that one. Right? So, this way we kind of remove
332:41
Speaker A
all the special meaning from different characters in a string. This is great. Now what we want to do this first of all let's say that we want to test does this folder exist or not right so we're going to say first check if folder exists and now you can see with AI it already gives me suggestions and that's pretty much what I want so I'm just going to accept this part and you can see if OS path exists print path exists else do something else actually definitely saw
333:12
Speaker A
what I was doing before this lesson because it's my second recording So it's definitely copied that but that's fine. That's what we need. Going to print it. Path indeed exists, right? Otherwise, if I'm going to add here something like a kkk run it path does not exist. I also realized what I just did here. Let's just imagine I did not say it all out loud. Okay. So now the path exist or doesn't exist. If it doesn't exist, we can actually create it. Let's say that we
333:41
Speaker A
want to add here another folder called I don't know what we're going to call it. new folder, right? Why not? So, if it doesn't exist, we're going to create it. And we're going to write here create new folder. Now, you can see it's going to use OS make deers path. Let's do that. Also going to say folder created successfully. Why not? Let's try it. Click on run and can see path doesn't exist. Folder created successfully. And right away, I can see here appeared new
334:10
Speaker A
folder. If I run again, it's just going to tell me that it already exists. That's great. That's how you create folders, right? Check a folder exists or create it. And go to the second part and we're going to look at how to join paths because we're going to copy this path as a base. Let's say you want to add another folder, right? You want to add here like ABC folder or whatever. A very common way to do this would be new path equals OS path join. Then you can
334:36
Speaker A
provide all the part you want to join. For example, here you can see it suggest me path. then subfolder which is fine. I can also say here like ABC and then we're going to print this new path. You can see it's going to be joining all of them and you can see it was we provided that part then it added subfolder and ABC. This is much better than kind of taking your path plus subfolder. Then you add it here. Maybe it was already there. Like this is so much better, right? Definitely use this part.
335:06
Speaker A
And just to make you a better example, let's make a list of folders. And we're going to have something like folder. Yeah, that's perfect. Folder A, B, and C. Then it suggest me for folder in folder. It already knows where I'm going with this, right? Because I want to define my new path. If it doesn't exist, we're going to create a new folder. So let's just see what we provided. Yeah. So if I'm going to run this, I'm supposed to get here new folders. Actually, I can see that I created it
335:35
Speaker A
inside this one here. Folder A, B, and C. So we created it. And that's not complicated, especially if you use AI assistance. You can see it already kind of helping me quite a lot. Now let's just now go to items. I'm going to take this path. I'm going to say here uh rename files. So our path is going to be this one. And now we're going to see what AI gets us. So get all files in directory and I write for file name in you can already see it suggest me OS list year. Exactly what I need.
336:09
Speaker A
Then let's just see what it gets. So first it's going to print the file name. Then it's going to define new file name. Here we're going to add something. I only want to make sure that I rename my PNG files because sometimes you're going to have a folder with PDFs and some other files and you only want to rename PDFs. So you'll write if uh png in file name. Then we're going to do something. So the new file name is going to be uh file name replace. And it already sees what
336:40
Speaker A
I'm trying to do. I'm going to take this png and I'm going to add the date in the end. Let's remove that part. And then with OS rename, we need to provide the old path and the new path. In here, I didn't like how we did it. So, I'm going to write old file path equals this. And new file path equals this. Therefore, instead of providing it like this, we have to make it like this. So, this is much more readable. So, we're going to iterate through all the files
337:09
Speaker A
in the folder. If it's a PNG file, we're going to create new file name by adding date in the end. Then we're going to define old and new file paths. These are the absolute file paths, right? This is absolute paths that you need. Let's also put it here and here. And then we can rename it. And in here we can also include what we did renamed file this to this. I think instead of that actually going to use old and new file paths. Right now let me just quickly open it. You can see here
337:39
Speaker A
now it's just the names of the files. Now if I run it you can see boom instantly added the dates to everywhere right I have the date on every single file and you can do quite a lot in renaming your files for example the PDFs that you get from your Ravit and so on. There's really a lot you can do. Now let's comment it out. Now you know that how to list different files and locations and even how to rename it. Let's see what else we get. All right, another one I quickly want to just show
338:07
Speaker A
you. I'm just going to paste it and explain it. We can provide a path and then there's a special one os by providing this path. This will give you the root directories and files and directories is a fancy word for folders. So in here we're going to print our kind of root for every iteration. Then for each folder in folders we're going to print it as subfolder and for file in files we're going to print a test file. We're going to run this. This will allow you to go through all the nested folders
338:32
Speaker A
and find the files that you need. Sometimes you have like a location for your project. You want to go through and find all the PDF files. Then you want to check does it have this in the name and you can easily find the files this way. So you can see here directory subfolders there are no files so nothing printed. Then this in this directory there are bunch of different files and so on. So let's say that somewhere here I'm going to add a new file. I don't know test
338:59
Speaker A
and then I would just want to kind of go through all the files. So I'm going to write for file in files if test in file print file. Oh actually it's supposed to be test not text. Now if I'm going to run it you can see I get test dictionary. Now I'm going to write here get absolute path. Absolute path equals this. Right? And now if I'm going to run it you can see here is the test file that I was looking for. This might be really useful on your project. So I would paste it
339:34
Speaker A
here. And here is the test file that I was looking for. And see these packages already can save you quite a lot of time on renaming stuff that you get outside of Ravit. So this might be useful. All right. Now enough with the OS package. Let's go to another package. This time I want to import CIS. There is just one that you really need to know. Let's say that you have something before, something after, and then during the code something happened and you want to
339:59
Speaker A
stop execution. you would use cis exit command and with this it stops script execution. So if I'm going to run this you can see I get before then I stop execution and therefore I will never get after. Normally you would write here something like if five less than 10 then you kind of exit and then you never print it. However if that would be false for example I don't know 100 less than 10 then this would never be executed. Therefore we continue. That's very often
340:29
Speaker A
you need to use this kind of sys to avoid errors. All right, let's comment it out. I'm also going to add here a comment and we're going to go to the next one which is called daytime. This is also very useful especially I can already see that it gives me suggestions. I I clicked accidentally did not accept it. Yeah, but overall I'm just going to paste the code that I have here and explain. So from daytime you can import daytime. I know it's confusing to import daytime. From
340:52
Speaker A
daytime it's just different files. One is the package, other is the module. Just keep this in mind. Then we can get for example the time of now and we can also format it the any way we want it. There is like a whole table how to do this and you can see if we're going to print now we're going to get just like this generic everything right included the date the time and stuff. I can also say that you know what I'm going to use this string format time and I can use
341:16
Speaker A
the special symbols for example this is going to be the year this going to be the month the day and so on this is going to be the time hours and there are many more like different like even as maybe you want to get the months as the first three letters maybe full and so on you all can get it from here and usually you're just going to ask Chad GPT and say like hey I want to get time in this format and then it will do it for you now the next one is the time module and
341:40
Speaker A
this one is an interesting one there's a function called sleep which will let you kind of wait. So you're going to import time then for i in range you can use time sleep. So I'm just going to run it and you'll notice that kind of on each iteration it's going to wait a second then print wait a second then print and you can see it kind of continues like second has passed second has passed and so on. So we're going to cancel it. I don't want to wait until the end and that's
342:05
Speaker A
okay if you see keyboard interruption just means that you stopped execution. Let's comment it out as well. Now the next module is the math. This is very often needed when you're doing different math operations. For example, let's say that you have a number, you need a square root for some reason. There is a obviously a function for that. Often times you would want to kind of use the pi. You can use math pi to get the actual number. You don't have to write 3.14 and all these numbers.
342:36
Speaker A
Another one quite useful is the ceiling or floor. Ceiling is to round it up. Floor is to round it down. So if you have a number 15.7 and you want to kind of ceiling or floor, you will see we can get 16 or 15 right up or down. Yeah, that's there are a lot more to the math. Usually you will just ask Chajb and you will get all your answers. And another module I really want to introduce you to is the random module because oftent times you need to get random values. Let me just paste it in
343:07
Speaker A
here. I'm going to import random module and I can from random module I'm going to use random integer function and I need to define from here to here. So I want to get my RGB values from 0 to 255. Then I'm going to print the RGB. Let's just run it. This is the RGB value. Let's run it again and again. You can see every time just going to be random numbers. Another thing with the random module you want to do, you want to select a random item from your list. So let's say you
343:34
Speaker A
have materials, wood, glass, brick, concrete. For some reason, you want to get the uh random material. So I'm going to run it. The first time it's wood, then it's brick, then it's glass, and you get the idea. It just randomly going to pull out one of the items. That's pretty much it. This is just the main packages I want you to be aware of. You don't have to go and study and learn and read the documentation. Usually you're going to be writing the code. Then somewhere in your code, oh actually I
344:00
Speaker A
need to rename some files that I already have. So you're going to go to chat GPT or just pull up your AI assistant and write how do I rename uh files in Python, right? And it will explain everything you need to know, but most importantly it will provide you the code sample. And if you give it some context, I have this folder, these files. I want to rename it this way. It's going to give you complete code. It's very well doumented. So AI knows it really really well. So
344:27
Speaker A
don't be afraid of using other packages and actually be encouraged to do so because they are made so you can save time, execute your task faster and move to more exciting parts of your script. All right guys, and now I want to wish you happy coding and I'm going to see you in the next lesson.
344:49
Speaker A
Welcome back. It's finally time to learn how to read, write, and append data to different file formats using Python. And we're going to start with the text file format. Then we're going to move to CSV and JSON in this lesson. I will also cover Excel files, but that's going to be another lesson because this involves way more steps to work properly. But for this lesson, let's jump straight into code and start working with text files. Let's begin with absolute basics. And
345:12
Speaker A
you need to understand how to use the open built-in function in Python to read contents of any files. First of all, we need a file name. Let's define here something like example txt and I have this file right here right next to my Python file. Next to open it I'm going to define file variable and with open I need to provide my file name and then I need to provide the mode of how I want to open it. I can open it in the read mode in write mode or append mode. In
345:37
Speaker A
this case let's open as a read mode and we cover the rest later on. Once you have your file you can get the contents by using file read and we can print whatever is inside. And also if you use this way, it's very important that you close your files after you finish working with them. If you don't do that, then there's a chance that you might corrupt your files and you might even lose data. So it's really important that you close your files. Now let's run it. And you can see I get a bunch of text
346:05
Speaker A
right here. This is the Zen of Python by team Peters. And here's the file that's exactly the same as you can see in the output. So we read it correctly. However, before I go any further, I have to warn you that this is not a recommended way of using this open function because you have to manually close your files. And if you don't, well, you might have some issues. And there is a better way in Python to do that. So, let's comment all of this out except for file name. We're going to use
346:30
Speaker A
the same one. Maybe let's keep it above. Then, let me add here read text files. And here's how you normally do this. We're going to use the context manager, which means we have to use the keyword with. Then we're going to write open file name in the read mode. Let me specify here as file. Then we have to create a code block under the W statement. And we can get the content by using file read. I can print the content. And the beauty of using it this way, you don't have to worry about closing the
346:58
Speaker A
file. Whenever you use this V statement with the open file, it will automatically close your file once you're done working with it. And now let's run it. And you can see it works. We're getting all these text statements here. But also this way we read everything as a one single plain text. Can also read the text file line by line. For that let's kind of comment out the print statement. We're going to write for line in content. Then there is a method called split lines. We don't
347:28
Speaker A
need any arguments in this case. And I'm just going to print line by line. Also to make it a little bit different, let's add here an enumeration. I'm going to write enumerate this list of data and in here we also going to assign new data. So this is going to be the numbering. This is going to be the lines from file. I'm going to print all of them. Let's run that. And you can see that now we're printing each line. First the number and then the content of this line. And you can see
347:56
Speaker A
there are 20 lines inside our text file. This is another alternative to read files. Let's move to the next example. Now I'm going to talk about how to append text to your files. We're going to work with the same file as before. Example.txt. Now to append anything we're going to write with open same as before file name in the append mode as f. Then we can also try to read it. So content is going to be f read. Let's print content. And if you want to write it, I'm going to
348:26
Speaker A
write f write. And then in here we need to write the data. Let's say uh hello world. We're going to write it three times. So let's run and see what we get. First of all, we're going to get an error and don't worry tells you that it's not readable. And this is because we're using the append mode. Whenever you're going to use the ride or append mode for your files, you cannot read your data. So therefore, this F read is not going to work. Let me just comment
348:52
Speaker A
it out. And I'm going to copy this error message right here. Unsupported operation not readable. Obviously, we cannot print it. That's fine. But we can write and in case of append when we're going to write can see nothing happened. So it executed correctly. So I'm going to open it. You can see right here in the last line there is hello world. Hello world. Hello world. So it was all mashed together like this because this is a plain text file. To create a new
349:18
Speaker A
line like this we have to use this kind of slashn symbol in Python. So let's clean it and go back. And this time I'm going to add here this symbol. So slashn slashn slashn. So run it again. It's executed. Let's run it two times. And now look at that. Hello world six times on each line every time. This is how you keep adding kind of data to your files. Can be great for logging. For example, you create your log file and you keep adding logs in the end in the end without deleting your
349:47
Speaker A
previous ones. All right, let's get rid of the hello world statements. Now let's have a look at how to write files. So I'm going to comment that one out. And I'm going to paste here a new block. Write files. Let's define file name. We're going to keep working with the same one. We're going to write with open file name in the mode. W stands for write as f or as file whatever you prefer. Now again we cannot read anything from this file. But to write we're going to
350:15
Speaker A
write f uh write and let's write just hello this time. Okay this time we know that we need to end with a new line. And let's make a few statements. Now we're going to write it. Let's open it. Oh no, we just deleted all our previous data because we overwrote the file. When you use W as the bright mode, it's going to create new file even if it already exists. So it's really worth checking if your file exists before you're going to override them. Now in this case, I think I can use control.
350:46
Speaker A
Yeah, I can use Ctrl Z to get my data back. But this is going to corrupt my file. So instead of that let's actually kind of change it a little bit and make another example. I'm going to import OS module because there is a function I'm going to write if OS path exists and I can check if file or file path already exist. This case we need our file name. Let's put it here. So if this file name exists then we're just going to do nothing. But if it doesn't exist, not exists,
351:21
Speaker A
then we're going to open file name in the W mode to write and we're going to have right going to write hello uh five times. Otherwise, we're just going to write file exists. And here we can also add print uh creating a new file. And let's put the name of the file right here which is going to be example .txt in this case. Now the first one if I run file exists. It also means that I can even print the content and here with open file name in the mode R as F. I'm just going to print F read.
352:06
Speaker A
So we run it. It already exist. But if I'm going to name my file something else like example two. Let's just fit all the code also like this. Now if I'm going to execute like creating new file example two. Now if I'm going to open it hello hello. That's how you kind of write your files to and ensure that they do not exist because if they exist well you will either override it or you better not touch it depending on what kind of data you're working with. Great. That's it about working with text
352:36
Speaker A
files. You learned how to read, append, and write your data. Now let's have a look at the CSV files. I'm also going to generate this kind of comment to help you navigate the code and then I'm going to paste read CSV file like this. Now to work with CSV files, we actually have to import CSV module because it's a little bit different and I'm going to show you why in a second. I have here uh just a regular file CSV as you know CSV is like Excel files. It's like a rows and
353:06
Speaker A
columns of data and there's a separator to define um different for example column items in between. So there's item material cost per unit and then all these items under these headers. Now to work in Python we could also use the same with open uh we need a file name. So file name is going to be items.csv. So open file name in let's open in the read mode for now again it's going to be as f then content is going to be f read and print content we can do that right
353:44
Speaker A
we're going to run and it's actually going to work but we're going to get it all as a single text file and that might not be the best because now if you want to make it a list and find different rows you have to actually put more work in so this is not ideal when you work with CSV files this is why there is a CSV module and here's how it works first of we need to create kind of reader and then there is CSV reader we provide the file which we define right here so you
354:07
Speaker A
define your file you provide it here now we have the reader and we can write for row in reader print row and here is the biggest difference once I'm going to run it you now can see that all rows they made as each uh individual list and inside the list there are three items item material cost per unit and so on I can also write for item in row print item because That's a nested list. Let's run it. And look at that. Here is the row data and then each item one by one.
354:38
Speaker A
Now, I'm not going to do that, but you get the idea. We can either read it as text, which is not very convenient, or we can use the CSV reader. Now, let's comment it out. And now we're going to have a look how do we append and write to CSV files. Same as before, I'm going to copy the first two lines. Then I'm going to write with open file name in the append mode as f. And then before we create it as a reader but this time we're going to create it as a writer. And you guessed
355:07
Speaker A
it we're going to use csv writer then f. And then in writer there is a function write row or write rows. So I'm going to select that one. And now I need a list of data. Let's create a list. There's going to be the first one is going to be toilet. Uh the second one I think was materials gold. And then the price of something like this. It's going to just be 9999. Now, let's just open the file. Have a look. This is what we have right now. I can also split it on the side so
355:32
Speaker A
we can see it at all times. Let's make it like this. Now, I'm going to run it. Boom. You can see right away here toilet gold appeared. We don't get any print statements. That's fine. But you can see I can keep running and it's going to be creating more and more and more. You also notice that there is an issue with a new line. It always going to create blanks. This is why when you work with CSV files, you also have to specify here new line equals like this. It's quite
355:57
Speaker A
okay when you read it, it doesn't create any blanks. But for writing files, it's really recommend that you write here new line because now if I'm going to delete all of that, then run it. Now you can see it kind of keeps adding just one time and I don't have any blanks in between. So don't forget to use new line with CSVs. And usually using right is going to be exactly the same. Let's just kind of copy paste it. Change to write. I'm going to run it. And obviously all
356:28
Speaker A
my data is going to be gone because that's going to be a new file. So always decide if you want to add data or remove it completely. All right. So this is the CSV files. Now you know how to read, append, and obviously write. And it's not that complicated. And now the next we're going to have a look at the JSON files. Let me make this ASKI art. All right. And we're going to start by reading JSON files. So I already prepared here a simple file. I'm going to right click
356:55
Speaker A
and split it on the right side. So I can have my code and this one at the same time. Now inside JSON files is pretty much a dictionary in a dictionary in a dictionary. And it's very popular format for certain things, but it's not very readable for kind of human eye. When you look at that, that one is still okay. But when you see a real JSON format files, well, that's going to be so hard to read. But there are many JSON readers which make it very easy to read. Now,
357:22
Speaker A
first of all, let's try to read the data and then we're going to figure out the rest how to do it. Firstly, we need to import JSON module. It's also included in Python by default, so it's all easy. Then we also need to use with open and inside we're going to specify materials JSON and we're going to put it in the read mode. Then I'm going to write as if in this case we kind of can't try to get the content as if read. But as you guessed it if when we're going to run this is going to be just
357:53
Speaker A
text. It's going to be the most useless data you have and it's going to be a whole kind of challenge to turn it into a dictionary. So instead of that we not going to use the content for sure. Not of that we're going to use the JSON file and to get the data we're going to write JSON load and we put F right here. I'm going to print data and also let's print type of the data just to know what we're getting. Run this time you can see here's all the data and this is going to
358:20
Speaker A
be as Python dictionary. So in case you want to read some data let's say uh materials is going to be data materials because that one we reading right from right here because there is the first one is materials then inside is going to give us a list. So it means that I can make for material a materials print material. Now I'm going to comment out these two statements. Run it. Now look at that. I'm getting each item inside because inside it's also a dictionary. It's a
358:52
Speaker A
dictionary for dictionaries and so on. Now for each material let's say I want to get uh name with Ctrl D. I'm going to duplicate and get something like I don't know density. Let's run it like this. And look at that concrete density wood density brick density and so on. I should have made in one statement but you can see how to read JSON files. It's not an issue. Normally you're going to read it as data and then you're going to make bunch of manipulations with your Python code.
359:21
Speaker A
There are different ways. There are more libraries to work with. So don't worry. Now next let's have a look at append to JSON files. I'm just going to go straight forward tell you you cannot append data to your JSON files. That's it. However, you can read your JSON file, then get it as a dictionary, then add your data to this dictionary and write to a new one. Now, use the same name as well to override it. This is how you would append to JSON files. Let's do that together because you're going to
359:46
Speaker A
learn how to read plus write at the same time. And first of all, let's just go and grab maybe until here. That's going to be fine. With control slash, I'm going to uncomment it. I don't need these lines. And this ones first we need to read it and we're going to get the data I just write read JSON in this case new material is going to be uh let's just copy one of the examples example that one paste it here and we're going to change it to 05 material I don't know plaster I have no
360:25
Speaker A
idea what's the density let's put something low and cost well that's something cheap let's make 100. All right, here are my random values. I can also make it in one line. It wouldn't be an issue actually. Let's make like this. Now, we need to add this material to this data. In this case, this data is going to have this dictionary. And I want to add it inside materials. So, we're going to append new material. And this is exactly why there is no such thing as append because you never know
360:53
Speaker A
where you want to add your data. This case, it's going to be data materials like this. Then I append and I'm going to append new material. So with this part right here, we going to get our list which starts here and here. And inside we want to add this one right here as the new item. That's exactly what we're doing with Python code. If I haven't misspelled anything. And lastly, we kind of created this new data. Now we need to uh override the file or create a new file depending what you do
361:33
Speaker A
with open materials JSON. Then this time it's going to be in the right mode going to be as f and then in this case it's not going to be fright. We're going to use JSON dump. This is the function to dump your data. We're going to use data and file. data is going to be your dictionary which first you read and then modify here and f is going to be this file right here. So I think if h misspelled anything that should override this one and add a new material. So let's run it and you can
362:08
Speaker A
see it did it but it did it in one line. So we did not really do it correctly. This is because in here in the dump we also need to specify ident. Now let's run it. Okay, this time it works. Ident stands for kind of how many spaces you want for each of your identations. And if you don't specify any, well, then it's going to be just one liner. But if you're going to specify, for example, 10, which is not a common thing to do, but well, it's going to look very ugly. Four is the standard.
362:39
Speaker A
And that's your new JSON file. Obviously, every time I was clicking on it, I was keep adding new and new and new and new plaster. So now I have lots of plaster elements. But you can see how it works. We could also check if it already exist before add. But this is not the scope of this lesson. And that's the basics of reading text, CSV or JSON files. And I would recommend you to go through this lesson and at least code with text files just to get some practice to read and write it to
363:05
Speaker A
see how it works. And then let's move to the next lesson where I'm going to show you how to work with Excel files. Going to wish you happy coding and I'm going to see you soon.
363:18
Speaker A
All right, we just learned how to work with text, CSV, and JSON file formats in Python in the previous lesson. And in this one, we're going to focus on how to automate Excel with Python. And also keep in mind that there are plenty of packages available to work with Excel in Python. But I'm going to focus specifically on Open Pi Excel package because it's a very straightforward package that allows you to do anything you need to automate your work in Excel. Now, let's actually jump straight into
363:39
Speaker A
code and I'm going to explain as we go. All right, I hope that you're ready to excel your Python skills. And whenever you start working with a new package, it's always best to start by referencing documentation because you can find a lot of information and code examples of how to work with this. And for this specific package, we can go to the website open pixel.docs.io. Then endstable is just the version and the language. So let's just copy that and open it in a new browser. Now this
364:02
Speaker A
is the documentation for open pixel a Python library to read write Excel files. And in here you can find quite a lot of information. For example, here on the left side you can see there is next topic tutorial. You can click on that one and you're going to find a lot here. how to install the package because this is not an built-in package. You need to install it, then how to create a workbook. Then there are also notes that for example you need to install pillow
364:21
Speaker A
package if you want to work with images and a lot of code examples with explanation how to do pretty much everything with the package. How to read, how to write, how to create images and so on. But don't worry, in this lesson I'm going to cover all the essential parts and then later on if you need something specific you can come back to documentation and reference it. Also this is not the best looking documentation. So I found there is another website just need to put SS in
364:43
Speaker A
the beginning. It's going to be exactly the same documentation but it's going to look better. Information is all the same but for me it's way more pleasing to read. And also the navigation on this side is also a bit better structured and it's easier to find. So later let's say you want to do something with charts. You come here for example pie charts. Here's going to be the documentation examples and you will understand how to use it. But now let's go back to PyCharm
365:05
Speaker A
and actually start with the basics. And to begin, we need to make an import open pixel to bring this module in. And you'll notice that there is no module named Open Pixel because we have to install it. If you're using PyCharm, it's very simple. Just click here, go to settings, then in your project, Python interpreter. Make sure to select the right virtual environment. And right here, you're going to see all the installed packages. These are default packages pretty much all of you going to
365:29
Speaker A
see. And now I can click on this plus sign to add even more. Here, I'm going to search for Open Pixel. You can see it's right here and I'm going to click on install package button here. Takes literally two three seconds and then you're going to see a message that package open pixel was installed successfully. If you have any issues, usually it's because of your virtual environment or because of the Python version you are using. So I would recommend you to create new virtual
365:52
Speaker A
environment, try it again or maybe use even different Python version because it depends on the package that you're installing. But in this case, I think you're not going to have any issues. This is a very common package. Now we can see we added this package right here. And also you might notice that there is another package up here. This is because many packages depend on the other ones. And if you want to install for example open pixel, you also need to install this XML file because it depends
366:13
Speaker A
on it. Just leave it here. Don't delete anything you need it. Now click on okay. And now you can see we don't have any any issues. Even if you're going to run, you see no issues whatsoever. But if you're going to write something that doesn't exist, you would have an issue otherwise that no module open pixel blah blah blah. And also if you're not using PyCharm, then you would have to use the terminal in there. You would install it with a pep pip install open pixel and
366:36
Speaker A
you can see I already have my environments. Now let's go back to the topic. So we import open pixel. Now let's start with absolute basics. First of all let's open existing workbook. I'm going to open the project right here. You can see I created a folder. Here's my python file where we work and I prepared here example.xl file. And I'm going to create here file name equals example.xlsx. And to read your Excel file, you're going to write here VB for the workbook
367:04
Speaker A
equals. And with open pixel, there's a function to load workbook. And it takes file name as an argument. And if I'm going to print VB, let's hide that one. Run. You can see I'm getting an object which is a workbook. And later I will use it to get the worksheet read, write, and I'm going to show you everything. Don't worry. In case you want to create a new project and not the read, uh then you would create VB. And here we would use uh open pixel workbook like this. Doesn't need
367:31
Speaker A
any arguments. This is going to create you a blank Excel file. But here's the thing. If I'm going to run it, I'm going to see the object in the console. So I create something. But I don't see anything here. This is because I need to save it. So it actually becomes a file on my computer. And to do that, we going to write here VB save like this. Inside we need to provide a file name. We just quickly make here something like test xlsx. Then let's run it. And now in my sidebar
367:57
Speaker A
menu you can see it appeared right here the new file. It's going to be just blank Excel file. Now I don't want to really create new one all the time. I will be working with existing one. So let's add here a few comments. This is going to open existing and this is going to create a new one. All right. Another thing you can see I don't really want to use this open pixel all the time. So let's actually write here from open pixel import everything. This way we don't need to use this open
368:20
Speaker A
pixel anymore. We can directly use the functions or the kind of classes. All right, this is how you get your workbook and save it. Now, let's comment it out. Remove the print statement. I'm going to move to the next step where I'm going to show you how to get worksheet from the workbook. As you know, if I'm going to open that one right here on the bottom, you're going to have different worksheets and you have different data. In my case, I have here sheet one with some basic data that
368:44
Speaker A
we're going to read later. And to get any worksheets, you would kind of create VS for the worksheet and then you would write here VB. There are different methods. First of all, you can get your active uh open worksheet. My case, you can see here the active is sheet one. So, if I'm going to print uh vs. You can see that I'm getting a worksheet sheet one. If I would open here sheet two, and run it, you'll notice I'm still getting sheet one because I haven't saved the file yet. So now, if I'm going
369:11
Speaker A
to click here, Ctrl S, and run it this way, I'm getting sheet two. So, it's going to get the active sheet last time that you saved your document. Let's go back to sheet one. Save it. Run. Now getting sheet one. So this is how you get active worksheet. Uh you could also get it by name. So I'm just going to paste it here so it's a bit quicker. You can see we can use the VB for the workbook and then we can read like in dictionaries. Just write here the name of the worksheet. This is going to get
369:36
Speaker A
sheet by name. So if I'm going to run, you can see I'm getting sheet two which does not exist because there should not be any space. So let's run. Now you can see we're getting worksheet two. Now, in case you're not sure what kind of sheets are available, you can also get all of them. This is going to get you a list of all worksheets in the Excel uh file. I just print it. You can see here's a list of sheet one, sheet two, sheet three. We could also get uh there is sheet names.
370:04
Speaker A
This is going to be a list of all worksheet names. This one is less useful because it's just the strings and you cannot really do anything with them because in the first one, it's actual objects you can work with. In here, it's just the names. And I don't recommend you using that one. I think it's way better to get first the worksheets and later you could just kind of for worksheet in all worksheets. You can print their titles one by one and kind of keep working like this. You can see
370:29
Speaker A
sheet one, sheet two, sheet three. Now I'm going to remove the print statements. And out of these ones, let's uncomment the first one because I want to keep working with the active one. And I'm also going to drop a few comments here. And it's going to be like this. These are the lists and these are the actual worksheets. And now next let's actually have a look how do we create copy or remove worksheets. Let's say let's say that I want to take sheet one and I want to
370:51
Speaker A
make a copy of that one. We're going to write here the new worksheet is going to be we need to take the workbook then in there there going to be something like copy worksheet and inside we need to provide our uh worksheet that we are copying like this. Then we can take this new worksheet and we can set a new title. Let's say this is a copy. So it's like this. Again, I'm going to run and you'll notice that nothing is happening because remember you need to save your
371:16
Speaker A
Excel files. So therefore, we're going to come here VB save and we're going to use the file name to override the file. Now I'm going to run it and as you guessed it, we might get permission denied. If you're going to see this, don't worry. It just means that we have the file open and we cannot override it. It would be the same if two users would try to open your file. Now we need to close it. Now we're going to execute it again. This time you can see no errors.
371:38
Speaker A
It worked well. Now I can go and open it again here. Open in and then there is open in associated application. And now let's check if you have it. So there's sheet one, sheet two, sheet three. And now I have my copy. And this is the contents of my first sheet cuz the other two you can see they're empty. So this is working well. This is how you uh copy worksheet. Now let's hide it for a moment. This is change worksheet name. Now what if I don't want to copy? I
372:06
Speaker A
actually want to create one. Actually that was supposed to be the first one. So new worksheet would be and then there's bb create sheet and it takes an argument for the name of the sheet. In this case we're going to call it new sheet 4 something like this. Keep in mind that in Excel I think there are 50 or 55 characters limit for the length. So if you have very long names of your sheets you need to kind of limit it but in this case that's going to be more than enough.
372:30
Speaker A
Now let's uncomment these two because this is how we create a new worksheet. And also make sure that you're still saving your file because we would need it. Let's close the Excel file. Run it. Now I'm going to open it again. Now let's have a look. Sheet one, sheet two, sheet three. Copy. And now the new sheet right here. Great. And lastly, if you would like to remove a worksheet, let's write VS to remove equals VB. And let's get it by name. I'm going to use exactly the same name like that one.
373:03
Speaker A
And then we're going to write VB remove. You can see there are two methods. Going to select that one, it will be crossed over. If you're going to hover over it and you can see it was depreciated and it tells that use VB remove instead. Let's use that. We need to provide the worksheet that we are removing. This is remove worksheet. Now our Excel is closed. So we can run it and see no errors. It works. I'm going to try to run it again. I might get an error saying that it does not exist because
373:29
Speaker A
now we cannot get it. We just removed it. Now let's comment it out so we're not messing with anything. And that's how we create, copy or remove your worksheets. And always don't forget to save your files when you make changes and you actually want to apply them. Now you know how to create workbooks, worksheets. Now let's have a look how do we actually read the data. And first of all, it's getting a little bit messy. Maybe I'm just going to copy parts that I need. I'm just going to copy that one.
373:56
Speaker A
Paste in here. And I want to keep the first two and delete, delete, delete, delete. And that one. So this is our base. We have the file name. We have the workbook and worksheet. Let's also print here. Uh worksheet is going to be going to be VS title. Let's make it an F string. And just let's make a bunch of different dashes. It's going to look much nicer in the console. Now, let's start by reading specific cell because well, it's also very common. Let's say that you want to get cell A1. That one
374:27
Speaker A
you take your worksheet and we could just get it literally by typing here worksheet A1. Alternatively, we could also write here if you want to get A2, we could write here worksheet cell. Then we can write here the row is going to be two. The column is going to be one. And keep in mind that in here in rows and columns, we have to start counting from one and not from zero. Because if I'm going to type here, for example, zero and zero, many would imagine that's going to be A1. But it's
374:58
Speaker A
actually going to give an error saying that it must start from one. So make it like this. This is going to be A2. Now to read their data, we can write here cell A1 value and the same for A2. I did it otherwise, but you get the idea. Now let's run it and see. We're getting item code and C 001. Let's just open and keep it maybe on the side so we can see it at all times so we can compare what we get and what we see inside the Excel. Going to put it here on the side.
375:30
Speaker A
Maybe make a little bit smaller. And you can see that we are reading this data right here. That's fine. Now let's uncomment that one and let's try to read more data. We can for example use for loops with a range. And for that we'll write for for example row in range 10. And for column in range 10 as well. We're going to get the cell and cell is going to be vss row equals row column equals column. And remember range is going to start from zero. This one is taking numbers from one. So either start
376:01
Speaker A
here from one or you can just make row + one, column plus one, whatever you prefer. Now let's print. We're going to write cell value. Let's execute that. And that's going to print us a bunch of different data. You can see there's lots of none. It's not very easy to read. There is like a absolute mess going on. So instead, let's just write here row data as a list. And then for column in the range of 10, we're going to write it to this uh row data. So instead of this print, we're
376:30
Speaker A
going to write row data. Append. We're going to append the cell value. And we going to print it just as rows. It's going to help us read in the console a lot. Run it. Now you can see each row is a list. And it's great. We're getting a lot of data here. But you might notice that we are getting a ton of this none, none, none, none, which we don't really need. We could filter it. It's not an issue. For example, we could write here if cell value. Then we're going to add
376:53
Speaker A
it. Going to already clean it a lot, but we're going to have a few empty list and so on. We could also clean it obviously, but there is a better way to do this. Let's have a look at that. So instead of creating these ranges, we can actually loop over data by using the rows inside the worksheet. For that, we're going to write for row in worksheet. Then there's iterate rows and also iterate columns. I think one example is going to be fine. And the beauty of that if we're going to
377:19
Speaker A
print the row and run it, you can see that we're actually going to get a tpple with all the cells inside. Uh let's convert it into row data. For that, I'm going to take our value for R in row. This is a list comprehension. I absolutely love using them because they make your life much easier. Let's print this row data. Now we can see actual data inside. This is working pretty good. Now, if you look at the data, you might notice something inside my Excel right here. You can see that I'm
377:47
Speaker A
actually having here total values and it's a formula. It just multiply these two values together. But here in the console I can see actual formula D2 * E2 and it's not very useful inside of Python sometimes. So I would want to get the actual value and the way to do this is we need to go back to our workbook and then there is a property data only. Let's select true. And now if I'm going to run that one and you can see in here I'm getting all the values. And I noticed that here I have daytime,
378:15
Speaker A
daytime, and the value. And I think I clicked somewhere by accident because in here was 12th of September. So let's just copy the formula from here. Now it's going to be fine. Save the file. Run it. Now I'm getting the value. Now it's fine. So now we actually have all the values we can work with inside of Excel. That's fine. And if you would want to read at the same time the value and actually the formula, you can actually do it like this. This is the workaround. You can see we can open
378:39
Speaker A
workbook twice. First time we're going to open it just as usual. Second time we're going to open it with data only. You can see this is VB data vss data. This is pretty much the same worksheet. And then we can iterate for row row data in and then we can kind of create a zip and get rows from the regular one and rows from the data. Also I convert it into row values and row results depending if I want to have formulas and not. And now if I'm going to run it and maybe also going to add here little
379:07
Speaker A
separator. Run it. Now we can see that I'm getting the same things but one time it's going to be with a value. The other time it's going to be with the formula. This is up to you how you use it. I'm just going to comment that one out in case you need this workaround. Normally you just going to open it once and keep working depending on what you do. All right. So this is how we read data. But let's have a look how do we write data. And for that let's say that we want to take
379:31
Speaker A
worksheet cell number B2 and we can assign data directly. So this is going to be new B2 value. Also write here um for example B3 dot value equals new B3 value. That also works. Can also write here VSL. Then inside we can provide I want to have row number four. Maybe I want a column number two. Then the value is going to be new. I think it's going to be B4 value. I'm going to check it. Then I'm going to run it and see it worked successfully. And if I'm going to open it,
380:10
Speaker A
you can see nothing happens because again we have to save our workbook. Even I forget during this tutorial, but I thought that I had it on the bottom. That's okay. Let's close it. Run it again. Open it again. Now you can see right here we have our new B2 value, B3 value, B4 value. Uh I'm going to tell you more about formatting in a moment. For now, let's just cover a few more things because this might be not the best way to add your data. You could kind of iterate through your rows
380:39
Speaker A
and columns and kind of replace them and so on. But there is a better way to do this. Instead of writing specific cell data, we can append data row. To do that, we're going to take worksheet. Then there's append. and we're going to provide kind of row of data. And for example, I want to add something similar to that one. So I'm just going to paste this part right here. So we're going to insert all this data. And as you remember right here, we have a formula where we multiply two numbers. So we
381:05
Speaker A
could write here. Okay, that's going to be D8 time E8. So let's close it. Try this one and see what we get. So I'm going to run it. I forgot to close my list. So let's do that and run it again. Now it's successful. Now let's open it again. Here you can see I have my new data and here is my formula multiplying these ones. But the issue now is that I need to kind of know which is the number which is not really good. And for that we actually can use n there is vs and
381:33
Speaker A
then there is max row and max column. This case let's say that we're going to take the max row and it's already existing data. So we want to make plus one. And then in here we can make like this and like this. And we need to insert n here and here like that. Oh, and before we're going to run, I actually noticed there's an issue. I put the F string here, but it should be here because we are trying to modify this string. My bad. Now, let's execute it a few more times. Run, two, three, four,
382:01
Speaker A
five whatever. Now, let's open and see what happened. And now I can see we're getting all this data. Obviously, it says the same thing always. And now here, I'm not getting any data. And also, all my formulas actually broke. I think that's because somewhere I opened it with data only true. So let's turn it off because we are trying to write the formulas and this messes it up. Let's quickly fix it. So I'm just going to write this times this. Then I'm going to copy and with Ctrl Alt I'm
382:32
Speaker A
going to paste only formulas. Now it's all formulas. Then let's clean this up quickly. Now it should be fine. So let's close that. And now we're going to execute it again. So let's click two, three, four, five. That should be fine. Let's open that one. And now you can see we're getting this data. And each formula will always be on its own row because we're calculating it with here max row. That's how we can append data. You can create formulas here. And this is a bit more
383:03
Speaker A
efficient than just writing specific cells because then you have to think about how you're going to calculate it. Now you can also insert or delete rows or columns. For that there is VS insert rows. Let's say three. Then there's insert columns. Then there's also delete rows. Let's delete one. And delete columns. Let's also keep it one. Let's run it. Now it runs successfully. Open. And you can see that I added something here and also missing data in between. So it worked. Now actually I
383:35
Speaker A
should have maybe duplicated my copy first. Let's delete everything. And then in the copy still have that one and that's going to be fine. So now let's move it here. Going to save it. So be careful with this ones but you can do that and then later you can add your data there and so on. Uh what else can we do? We can move data if you want to. So we can uh move data. Do that. We're going to take worksheet. Then there is move range. Let's say we're going to take A1 to B I don't know
384:06
Speaker A
10 just to take kind of the first two columns. Then we need to specify how many rows and columns we want to shift it. If you want to go up and left, you have to use negative values. If you want to use positive, then you're going to go right and down. Let's go and rows down and columns. We also going to go 10 to the right. Let us see what happens. So I'm going to click on run again. As usually I have to save and close it. Run. And now let's see what happens. So
384:34
Speaker A
I'm going to open it. You can see the first two columns, they're gone. If we're going to open it, it's right here. So, we took these ones and then it moved it. As you can see, 10 down and 10 to the side. 10's going to be here. You could also take it back. So, in this case, it's K11 to L17. So, let's try K11. L17. And this time, we're going to make them negative values. Minus and minus. Run. And now if I'm going to open it, you can see we brought it back into its original
385:06
Speaker A
place. So we can move it kind of down, up in all directions. All right, that's pretty much it about writing data. I haven't missed anything. If I did, just let me know in the comments. Uh next, we can talk about reading ranges. As you saw in here, we're working with a range, but you can also get different columns, rows, and so on. This case, let me actually insert the code. So it's going to be much quicker. So we can create a range like we just did before. We take a
385:30
Speaker A
worksheet. We say from this to this, can I create the range? can be multiple columns can take the whole column just right here the letter then it's going to take the whole column you can also take the column range from C to D same with numbers we can take either just kind of the rows by writing the number or the row range and if you're going to execute you can see getting a bunch of different tpples now to read it more effectively let's for example start with column C
385:54
Speaker A
right for item in column C print item so let's make here separation so we know what is But we're going to run it. You can see we're getting all the items from the column C. Even though we just said it's C, it's only going to go through the data that exists. It's not going to go infinitely for the non none statements. Uh in case you would go for something like a range with multiple rows, let's just go for the first one, cell range. Here for I in cell range, print I.
386:27
Speaker A
Check this out. when we print it, we're going to actually print here tpples because when you have multiple uh rows, each row is going to be the new tpple itself. So, it's going to be for row in cell ranges and then for item in row going to print I. Let's also print row. It's going to help us navigate a bit better. Run. Uh I should have renamed that one. I can see here's the first row. There's data one, two, three, and then the second one. So this is how you can work
386:57
Speaker A
with ranges of your like the whole columns, whole rows or specific values as well. All right, for the next one, let's talk a little bit about formatting because that's quite important. Even though you're not going to do very heavy formatting, it's good to make at least bold. Maybe give a background or something like this. First of all, you need to import some stuff from open pixel styles. You might need to import font font and there is pattern fill alignment. Be honest, I'm just going to import
387:24
Speaker A
everything. and you will see what we use as we go. But if you want to know more about this again, we can reference documentation. Let's just quickly take that one and open it here. Then somewhere here we need to find formatting and it's going to be here in working with styles. Now we can go through introduction. Here we can see okay here's how to define font, how to define fill, border, alignment. That's a big code sample but you can see there's just bunch of different properties we define.
387:55
Speaker A
There is cell styles, copying styles, basic font colors, applying so on so on so on. I'm not going to go through all of them. You can see there is quite a lot. But overall, it's not that complicated and it's all here. So, let me just insert here a bunch of different samples. Example, this is how we can create simple styling. Let's just take maybe instead of A1 A5. So, we're going to set the font. We're going to make it bold. We're going to set the color. Let's
388:18
Speaker A
actually make it orange. Then for the fill, we're going to use pattern fill. Again, we're going to start color, end color. We're going to start with that one. And we're going to go into gray area. We're going to make it center. Now, before I'm going to run it, I'm also going to show you a few more things. For example, we can also merge cells by using worksheet merge cells and then define the range to become. It doesn't have to be single line and column. It can be like the
388:41
Speaker A
whole thing. And I think it's going to keep the data from the top left if I'm not mistaken. Now, we can also resize the row and column. That's actually a good one. Let's also use the same value we used here, A5. So for the row, we're going to set 50. And for the width, we're also going to set 50. Then in the font, maybe we're going to set the size to 24 because now we have so much space, right? Uh also this is where you define the size because everything related to font is going to
389:08
Speaker A
be inside here. Then also we can add a comment here. For that you would need to use open pixel comments and import the comment class and you would assign it using the comment property and then create a kind of comment instance like this. And lastly I want to show you is in case you want to add any hyperlinks you would use the property hyperlink and then assign the HTML uh URL. You can also make the style here and see there is hyperlink. Now let's run this code. I
389:33
Speaker A
hope I haven't misspelled anything. Okay, colors must be different. Let's have a look. It comes from this. This line right here. Oh, and I think it's just the space right here in the end color. I don't think there's supposed to be any space. If I'm run it, that was it. So, it was just a stupid typo. Make sure you don't have any spaces in your hex codes for the colors. Now, let's just open and see what's going on inside. So, I'm going to open it. Now, you can see we messed up quite
390:01
Speaker A
a lot in here. So, we made here like a merge cell. We gave it the background. We also changed the color. I think the colors didn't work here. You can see this worked. It became the link to the Python. But the this one it's still black color. Huh. Interesting. Now, let me just go back quickly. Pattern fill font color. Setting up it here. And I think I know what's going on. It's not here that we're setting the color. We're actually setting the color here, but we
390:29
Speaker A
also make it a hyperlink. Therefore, it's already kind of having the hyperlink color. You can also see there's our auto comment. Let's just change it to something like I don't know A6 because I made everything to the same one. So let's run it now. Let's open it. Now it works. Here we go. Here's the large text and so on because here we applied this styling. This where is it? We applied it right here. The style hyperlink. So we're not going to use this style anymore. Let's make A5 A5 A5.
391:00
Speaker A
Then let's run it. and an example. Now have a look. Now we are changing it everywhere. So now it works. It also has the hyperlink still to Python, but it doesn't have the styling anymore. Okay, so it was not my issue. I just put too much on one cell. All right, guys. And that's pretty much the basic for working with open pixel for Python. Uh let's just comment it out here and somewhere here. And before I'm going to let you go, let's actually make a simple exercise
391:28
Speaker A
together. And I'm going to create a simple example in there. Let's say that I have a list of data which I'm just going to paste here so we don't have to write it. It's going to be a room schedule. Going to be room numbers, room names, area, occupancy, and so on. And this is just kind of a list of nested lists. Now let's say that I want to create a workbook and inside there's going to be VB workbook for the VS. We're just going to take the active. It's going to take the
391:52
Speaker A
first one. I can also change the title which is going to be something like I don't know EF rooms. This is workbook plus worksheet. Now we have this data but I want to separate headings and the actual data. So separate data. So let's create headings is going to be data zero and actual data is going to be everything else. So it's going to be one like this. Now we're going to write our headings. And the reason why we split it because I want to make a little bit different styling. So we're going to
392:20
Speaker A
write from open pi excel styles import uh we would need font. Now in here we're going to write for uh it's going to be column number and uh heading and I'm going to apply enumerate so I also get the numbers of each item inside and the cell is going to be vs cell the row is always going to be one but the column is going to be column n + one because this is going to start counting from zero. We could specify that start from one then we don't have to do that and um the value is going to be heading
392:59
Speaker A
that's one of the syntax as I showed you earlier there are multiple ways to write it this is fine for me and then I want to modify I want to say the cell font is going to be font and we're going to apply some stuff for example bold equals true and size equals um don't remember what's the default let's make 16 so it's definitely bigger then we're going to write data and in this case we can make it Even simpler, we're going to write for row in data because this is just going to give us
393:26
Speaker A
this ones. We can just append it. We're going to write vs append row. Uh lastly, we need to save workbook. So let's make file name equals uh ef rooms.xlsx. And now vb file name. Right. I think that's okay. So usually that's how you're going to use it. You're going to calculate different data inside your Python file. Then you would want to write it. I'm going to click on run. It's going to work just fine. Here somewhere I can see EF rooms. Let's open that one. And now you can see this is
394:05
Speaker A
what I get. I have very huge headings. Definitely overkill. And then all the data is right here. You can quickly create your data, add some formulas, formatting. You know how to do this. Now I also want to show you another example. If you're going to be often working with Excel, it's also worth learning another package as well. But first of all, let's say that you have a list of data like this. You're going to create a JSON file in there. There's just going to be the
394:27
Speaker A
projects with their heights and so on. And now I want to make an Excel file out of this. If you're really serious about Excel, I would also recommend you to look into another package which is called pandas. It's very powerful module. It's very often used in data science. You can see powerful data structure for data analysis, time series, and statistics. It's often used with numpy as well. I just quickly install it. All right, we installed it. This time you can see we have way more
394:51
Speaker A
dependencies. There's also numpy which is another important scientific package. And now the thing is that in pandas there is a class called data frame and it's similar to the structure of the excel. So you can create here a data frame. You write data frame equals actually I need to import pandas. So let's write import pandas. It's also common to import it as PD. Then here we can create a data frame from our JSON data. And the thing is that you're going to print it. It's
395:21
Speaker A
going to be the data frame class. But to save to Excel, it's actually very simple. It's going to be DF. Then there is a method called to all these kind of formats including to Excel. Then we need a name. Let's write uh big buildings xlsx and then we can also specify index is going to be false. Now let's run that one. You can see this is how it's going to be printed because this is a data frame in pandas. It's way more powerful package. It includes a lot more
395:54
Speaker A
information. You can see somewhere we're going to have big buildings. Let's open it in application. And now you can see we wrote it here. It even made the headings in the kind of bold statements even though I haven't asked for it. This is by default. This is another very powerful package. There's a way more learning curve to actually analyzing data and working stuff. And for dumping data, it's perfect also working in Python. You can see it can be so helpful to have this kind of even print
396:21
Speaker A
statements. It's it's going to be very useful. Definitely recommend you to check pandas if you are serious about Excel. All right. And that's how you can automate Excel with Python. We covered quite a lot in this lesson. I didn't expect to go for this long, but now you know pretty much all the essentials and even more. And if you would want to know something else, it's always best to reference documentation or even ask Chad JPT. It's also pretty good with that.
396:43
Speaker A
And now we are ready to dive into the next lesson. So happy coding and I'm going to see you soon. Goodbye.
396:54
Speaker A
Welcome back. Today we're going to dive into the context managers in Python. It's a great concept that allows you to add extra functionality before and after your main code. And to be honest, it's a little bit more advanced concept and even some skilled Python users don't know how to use it. But don't worry, I'm going to break it down and make it very simple for you to understand. And most importantly, I'm going to provide you a few very useful use cases that you can
397:17
Speaker A
start using right away. And let's begin with the basics. What the hell is context managers in Python? Context manager is a concept that can include custom functionality before and after your main code by using the with keyword to initiate a code block for it. And listen, I'm going to show you very nice examples. But before that, let's start with a simple template and I'm going to explain you how it all works. In here, you can already see that we are using a function and we call it EF context
397:43
Speaker A
manager. Inside this function, you might notice that we have a code block before, code block after. We can write whatever you want, right? before you want to do this, after you want to do this, it can be anything. And then in the middle, you can see there is this yield statement. This is probably a new keyword for you, but yield is like a return statement, but a little smarter. In this case, think of it as a sandwich. You have here preenting code that you execute before.
398:06
Speaker A
You have here preenting code that you execute after. And this right here is whatever you write in your voice statement. It's going to be placed here instead of the yield statement. You're going to see how it works in just a moment. But let me also explain another concept here. You might also notice that right here we have a new syntax and it's called a decorator. When you can see this add symbol and then something here. This is called decorator. And this is
398:29
Speaker A
how we can add additional functionality to your functions. In this case, it will convert your function into a context manager. And to use it, you need to import context lip, then put this add symbol, and then execute it like this. In reality, it's like a wrapper function and a function that adds functionality. But we're going to talk about it maybe some other lessons. For now, just kind of know that that's that's what you write and you forget about this. Now, once you're going to create this
398:55
Speaker A
template, you can use it like this. You're going to write with EF context manager, which is the name of your function which which became context manager. This case, it doesn't take any arguments, so we don't need either. Then we can write here whatever we want. So, let's just execute and see what we get. And you can see we're getting before, then test, then after. So, as you can see, whatever you're going to write right here, it's going to come right here instead of the yield statement.
399:22
Speaker A
It's just going to be replaced here. And then whatever you wrote before and after is going to be executed before and after your kind of this with statement block. For example, let's just duplicate it a few more times. Run it. You can see now we print two times before and two times after. We can also make this one multiple times. You can see it's just kind of always like a sandwich in between your before and after statements. All right, this is a template. You can make a screenshot of
399:48
Speaker A
it and I'm going to show you how to actually make it something useful. So, let's remove all of that and first of all, I'm going to show you an example without context manager and then how to add it. Let's say that you want to time something that you do in your code. Inside, we could import time module and in there there is a function called time time. This is this is going to get the actual time right now and it's also can be used to get your start timer. Then
400:13
Speaker A
you can also get end. We're going to do something in between. For example, print your code here. And then we can calculate how much time has elapsed between these two points. Now we can write timer equals end minus start. If this has started at 12:00 but ended at 12:05, then this will result you 5 minutes because you subtract one from the other and you get the time that elapsed and I can print here time taken and so many seconds. Let's make it an F string and drop a timer inside.
400:49
Speaker A
Now if I execute it, you can see that time taken 00 second. Maybe I'm going to make for I in range 1,00 and we're just going to pass, right? We're just going to iterate thousand times, maybe 100,000 times. Finally, we're going to see some seconds. Let's add a few zeros. And you can see now it takes a little bit of time to execute it. That's fine. But this syntax is can be very annoying if you want to time, for example, two things and compare which one execute faster. Therefore, let's make it a
401:17
Speaker A
context manager and then test a few things. Now, we're going to get this kind of main part and we're going to make a function out of it. We're going to call it define time it. We don't need any arguments. Let's put it all inside. Same as before, we need to use this context lip. So we put this at sign context lip. Then there is context manager. That's it. Now it's a context manager. We only need to add the yield statement in between. So and we want to add it between start and end. So it has
401:45
Speaker A
to be right here. This is going to be your before and this is going to be your after parts. Great. So now we have a context manager where before executing the code we're going to start the timer. Then we're going to execute whatever we're going to place inside the width statement and then we're going to calculate how much time has passed and we're going to write it. And this is going to be fine. We can also make less space if it's easier to read. Now let's make here context manager.
402:16
Speaker A
And now somewhere in here we're actually going to use it. For example, let's start very simple. I'm going to write with time it. For example, let's check how long does it take to print thousand hello world statements. I'm going to write for I in range 1,00 and we're going to print hello world. Now I'm just going to run it. You can see it went through everything. And then here it's going to write time taken this many seconds. And it's actually very fast. Let's make it
402:44
Speaker A
maybe 100,000 times. Run. This time it took 02 seconds. Also, I don't like that I can see so many digits at once. I want to see maybe two or three digits. There's a special syntax you can do inside your formatted strings. Can put here a colon then dot 2F. What it tells you is that you're going to display a float with two digits. Now, let's run it. This time we can only see two digits and this is perfect. Now, you know how long it takes to print 100,000 times hello world. But let's have something
403:14
Speaker A
more interesting example. I'm just going to paste the code right here and we're going to compare what is faster using loops or list comprehensions. And you can see it's going to have exactly the same results. We're going to iterate for 100,000 times and we're going to try to get the square values. In here, we're going to create a list and then do all of that. And here we're just going to do it with list comprehension. First of all, write in the comments what do you think is faster, lists or
403:41
Speaker A
comprehensions. And now let's actually test it. this case it's zero because it's way too fast. So we need more. Let's try one million times. Run it. Okay, now we can see some time. Let's maybe try 10 million times. Okay, now it takes some numbers. So this one takes around half a second. This one takes one/ird of a second. So there's a substantial difference already. Let's run a few times just to make sure that these results are accurate. Also, we can also switch the places. Let's first make
404:10
Speaker A
comprehensions and then loops and see if it changes. So now you know that comprehensions are actually faster than your loops and you can test a lot more things with this time it function. Now let me select it and comment it out. Just want to leave it here. It might be useful later on. Now let me actually show you another very great example. Often times when you will be coding sometimes you're going to use try and accept blocks, right? Let's say accept is pass but in try you're going to do
404:38
Speaker A
something. this case let's just kind of print let's just try to divide some numbers and sometimes it happens that you're going to do something many times try except try except let's say you need to read five files you're going to read it here here you always use try except and it becomes a mess right the more try except you use the messier it's going to become especially when you use pass you just want to suppress the warning you don't want to do anything and we can
405:00
Speaker A
solve that with context managers so let me remove that let's go and create a context manager for try and accept blog where we control exactly what happens in the accept. So we don't have to write it over and over and over. I'm going to write here uh I'm going to take this line and I'm going to write define try except inside I'm going to try and it already suggest me yield and then accept we're going to write pass. So you can see we can also not only do something before and after we can also
405:32
Speaker A
wrap yield in other statements like try and accept and this is going to be great. And this time, let's say again that we want to do something. Going to write with try and accept. And we're going to print one divided by zero. And now let's make lots of those. And this time it already looks way better than before. I'm going to show a screenshot how it looked before and after. And I hope that we can agree that one looks certainly better than the other. And if I'm going to execute,
405:59
Speaker A
nothing happens. We suppressed all the errors, which is great. But also, we can extend this functionality. Let's add some arguments here because I want to have an option to print error message or not. So we're going to come back right here and I'm going to introduce to another module as well. It's called trace back. So let's import it quickly and actually we can combine all imports like this. Now inside of here we going to make this print statement. We're going to write
406:27
Speaker A
print going to write trace back and then there is format exe method which doesn't take any arguments. Now, if I'm going to run, you'll see that I'm getting this error messages, but it does not interrupt my execution. I still go through and I see it over and over and over. And it's really important because it's super helpful. Tells you exactly where and what went wrong. So, we can go and actually fix it. But I don't want to always see that. So, we're going to come back here and we're
406:51
Speaker A
going to add here a debug option. Now, we only going to print it if debug is true. And by default, it will always be false. So we don't even always have to define it. And now let's actually go and try it here. So for example in the second one I'm going to write debug equals true. And now if I'm going to execute I'm only going to get one statement and it's only from here. So you can see we keep adding functionality to our context manager while we also decided that certain
407:20
Speaker A
things are optional to help us in our development which is a great feature for sure. This is just a few features which I find really useful for all the beginners. All right. And this is the context managers in Python. And you can see you can create quite a lot with them. It's quite an advanced concept, but I think it's not that complicated and I'm sure many of you will find it useful in your scripts. At least these examples that I showed you. Please use them. And also, it's possible to create
407:44
Speaker A
context managers with classes by using enter and exit methods. But OOP is less beginner friendly. So I would avoid it as a beginner. But if you love OOP, you might explore this method as well. And now go through this lesson and practice creating your own custom managers. I wish you happy coding and then I'm going to see you in the next one. Goodbye.
408:09
Speaker A
All right. During this course, we absolutely abuse the print function and it's great to see what's going on in the console, but on large projects, it can get messy and hard to find hundreds of print statements. That's where logging module comes in. It will help you to lock messages with different severity levels and you can even save this messages to a file, send the message in Discord, Slack or even email. It's really flexible and it can be very helpful for debugging and tracking your
408:33
Speaker A
live projects. And it's not even that hard to implement. But unfortunately, many beginners often skip that, but thanks to this lesson, you're about to become the pro with logging. So, let's dive in. And let's start with a quick overview to understand how it all works. And let's start with the print statement. So, for the print, you have some kind of message. Then you're going to take it and call the print function and then it's going to display in the console. This is very straightforward
408:55
Speaker A
but as you can see it's very limited. In case of logging let's say that you have some kind of message and you would like to log it. And first of all you would need to select the logging severity. There's debug info warning error critical. These are default ones and they all have respective levels. And the idea is that each message can have associated level and then later on you can say which messages you would like to display or to log. example by default it's always kind of showing you warning
409:20
Speaker A
error and critical and debug info is going to be skipped so you're going to create a program and then create bunch of different log messages with different levels and you know what right now I'm working on development so I want to see all messages including the debug it's going to be all the messages showing what's going in what's going out and so on later on you decide you know what I'm not really that hard in development I'm going to just see the info just to see
409:41
Speaker A
the results for example and then when you're going to publish the project for example you're going to say I want to see only the warning errors and critical messages because that's where you need to react. You don't care about these ones on a live project because there's going to be a lot of them. And you can always go and change the level to kind of display only the messages you want. That's the point about selecting the severity levels. Then you're going to create your logger
410:04
Speaker A
where there's going to be a config for example where you can select which level you want to display and it also going to contain the message. There is also ability to include a filter to kind of filter what kind of messages can go through. But we're not going to do that. Just know that it's possible. Then logger. It can have a handler. By default, it has a stream handler. So it's going to display the message inside the console. And this handler, it also can have a formatter. So you can decide
410:28
Speaker A
how your messages look. For example, it's very common to include time stamp, a level of severity message, and also the message itself. So it looks something like this in your logging file. And then for example in case of string handler it's going to send information into the console. So you're going to see in Python console messages like this. However you can connect multiple handlers at the same time. For example we can also connect file handler which going to write all the same
410:51
Speaker A
messages inside the log file and we can also create custom handlers where we can send the message anywhere. We can send it to Discord to Slack and pretty much anywhere you would like to. And that's pretty much what we're going to cover in this lesson. And you're going to learn about how to create log messages, how to choose the level, how to configure logger, how to create multiple streams of data. And don't worry, it might look complicated in this graph, but it's
411:13
Speaker A
actually very simple if you're going to follow along the code. All right, and let's dive straight into code and learn how to use logging like a pro. So, first of all, let's start with absolute logging basics. Uh, to begin, we need to import logging. And this is a built-in module, so you don't have to install anything. You already have it. Once you're going to import it, you already can start making the messages. Just going to paste it here. And you can see by using logging debug info, warning,
411:36
Speaker A
error, exception, critical, you can create messages. And all of these messages has associated level to know what is more important than the other. This one you can see has the same message as the error. That's because it can also include the trace back uh exception message which I'm going to show you in a moment. For now, let's just run the code and see what happens. So when we going to execute that part of code, you're going to see this messages. However, you might have noticed that by
411:59
Speaker A
default, we starting to get messages only from warning upwards. So, we only see these messages right here. We don't see any debug and info messages. This is because by default, it's configured to only show warnings and app. And for that, we can actually change it. We can set up our logger. For that, we're going to write logging. Then, we're going to create basic config. Inside, we can include level equals logging. Then you write debug info uh warning in capital
412:27
Speaker A
letters. Let's say debug this way. Now when I'm going to execute this code, you also notice that this time we also get debug message and info message. So this is the thing about login messages. You can decide on which level you want to log your messages and then you have a control what messages to show because you can even say here you know what I only want to see errors, exceptions and critical messages. You're going to run it and this is going to be error. This
412:50
Speaker A
is another error. This is exception. Again, I'm going to explain it just in a second. And then there is critical. And you can always jump between kind of these levels depending on what you do. Maybe you're in development, maybe you work on the live project and so on. For now, let's just keep it in the debug. And we're going to create a simple example to just kind of understand it a bit more. And let's say we have two numbers AB. It's going to be 10 and 20. Then here with logging debug, going to
413:14
Speaker A
create a message uh executing. And then this divided by this. Let's make it an F string and make it A and B. Then I'm going to create try statement and I'm going to write C equals A / B. And then we're going to create another logging debug message. Maybe actually this is going to be not debug but info message. We're going to write result is going to be C or except we're going to write here logging error and then something went wrong. But let's say that we execute it. You can see okay
413:46
Speaker A
now we're in debug mode. So we're going to see executing 10 divided by 20 result 05 all works. Later you decide you know what I'm going to go in live and set it to warning. So you run it. You're not going to see these messages anymore. You're only going to see errors. And let's say that somehow you get the wrong arguments and you're going to execute. And now we're going to see something went wrong. Here's the thing. You don't know what went wrong. You could actually include
414:10
Speaker A
here a message with a trace back module or by accept exception as e and so on. Or you could also use this logging exception. It's the same as error, but it's also going to include the error message. If I'm going to run, you can see here is the message and we're not crashing our program. We're just displaying the message because if I'm going to write here, print hello world. You can see I'm going to execute it and I have it right here, hello world, and then the trace back message because
414:37
Speaker A
we're still executing all the way through. It's just displayed here in red the same way as the errors would, but we can log it also to a file and so on. And actually that's the next thing I want to show you. So let's comment out this example and we're going to go and have a look how do we actually log all this information to a file because that's a very common thing because you want to your log messages to leave somewhere. Imagine you have a website for example
415:00
Speaker A
running and you want to kind of log all your errors. So once in a while you can open it and see okay I had some issue two days ago with this or that and then you can go and fix it. Now to log it to a file I'm going to write here import logging. Then you know that we need to configure it by using logging basic config level is going to be again debug but also we can provide here file name. This is an easy way to create a logger into the file. I'm going to call it
415:25
Speaker A
something like I don't know uh ef.log doesn't matter what you call it but keep the log extension. Now next we can create a few messages and let's keep it simple just like free messages. Now I'm going to open the sidebar project. You can see right here is the login.python Python file where we work and the moment I'm going to execute it you can see the EF app log appears let's open that one also right click and split it on the right so we can always see this you can see we just logged all these
415:55
Speaker A
messages example I can write here 222 run it you can see it appeared again and again so now all the logs go inside the file and it's not that complicated however I don't like this styling yes it tells us the severity level then it always says the root and the message. But we can do more than that. We can apply formatting because as you remember in the graph I showed you there was a formatting where we can define what kind of messages we want to display. Maybe we
416:21
Speaker A
want a time stamp, maybe message and so on. And let's add that as well. So I'm going to create here something like log format and we're going to define here the format we would like to kind of get and for that it's best to reference documentation. So let's open a browser and write logging python. Then search for this Python docs. That's fine. This is the official documentation. In here, there's a lot of information, even too much. And let's just scroll until we can see the table.
416:48
Speaker A
The first one going to show you the levels as I'm already mentioned. And scroll until the second table. And eventually, we're going to see this. And these are all the attributes we can use in our formatting. So, we're going to use for example this syntax, and it's going to give us human readable time when the LO record was created. Can also use it in the other format. We can get the level name, file name, and so on. And let's do that. So, first of all, I want to grab the time because that's
417:13
Speaker A
going to be important. So, let's paste here time. Then, let's come back. I want to grab the level name, which is going to be this one. Then also, we need to use the message. You can see with autocomplete we have here the message. Now, that's great. And now we need to apply it. The way to apply it is we need to include it inside basic config. So, let's put it down. And here example after the level we're going to write format equals log format. And now when I'm going to run it you can
417:44
Speaker A
see the message going to change. Now I have the time stamp. Now we have debug message. And now we have here the message itself. So now it already looks better. Also we're not going to stop here. We're al actually going to make it look much nicer. Let's make it a triple quote. So it's a multi-line. And we're going to first write the severity level. Then we're going to write time equals this. Then the message is going to be this. Then also I'm going to write here
418:09
Speaker A
the file is this. And the line is going to be this. And finally I'm also going to include here a bunch of different dashes. Now this is a much nicer format in my opinion. Now let's run it. Have a look how it looks now. Now I get the message, the time when it happened, message file. And now they also separated with the dashes which is much easier to read. Also tells you the file where it is and the line on which it happened. For example, this is 49. Can go and see. Okay, 49 is here. This is
418:38
Speaker A
the warning message. Let's actually clean it. I'm going to just delete everything so I can run and I can only see logging in this format. That's how you apply formatting to your logging. But also, you might have noticed now we have a nice formatting into the file. But you can see that nothing happens in the console because if I'm going to write here print where are login messages. Now when we run it, you can see here is the print statement, but none of the LO
419:06
Speaker A
messages was actually printed. This is because we used the file name. Therefore, it decided to only send to one location. And many of you would prefer to have it both in the console and also the file. That's what we're going to do next. So again, let's comment everything out and go to the next step. This time, we're going to have a look how do we log to a file and the console. So in this case it's going to get a little bit tricky because we won't be able to use this logging
419:32
Speaker A
anymore. We would have to create an actual instance of a logger. It's not that complicated but just keep this in mind. So I'm going to write here import logging and then I would need to write logger equals logging get logger. I can leave it empty to get the root logger. I can also write here for example ef logger. It doesn't really matter much right now because I'm only working in one file. If you work across many files, that might matter more. Then to set the level, we're going to
419:58
Speaker A
use that one. Loging debug. Then to apply formatting, we're going to grab this. Let's paste it right here. Now, first of all, we need to create it a formatter. So, we're going to write a formatter equals logging formatter log file. All right. So, we creating it as an instance of formatter. later we need to add it directly to the handler because once you're going to create it in this way it's more customizable but you also have to configure more things. So here you configure logger.
420:26
Speaker A
Here is your formatting. And now we need to create handlers. Actually we're going to create handler one for the file handler and handler two for the stream handler. This is for the console. This is for the file. It's not that complicated because we can use built-in handlers inside of logging. I'm going to go to documentation somewhere here. If I going to go to contents handlers, I know that there I can see all handlers right here. Logging that handlers. Going to click here.
420:52
Speaker A
There's a list of different kind of handlers available, but stream and file are the most common ones. So, you can also kind of set up the cleaning by watching or rotating the files and so on. I don't know much about other handlers, but this one here is how to do this. So, we're going to create our file handler. For that, we need logging file handler and then just specify the name. Let's use the same EF app block. Just going to clean it right away. So, later
421:17
Speaker A
we're going to see messages right here. And now we need to apply this handler to the logger. Going to do this in just a second. Let's just create console handler and it's going to be logging stream handler. Doesn't need any arguments. That's fine. Now, as you remember in my graph earlier, so we created a logger. Now, we created two handlers. We're going to connect them in a second. But also, we can create a formatter and apply to different handlers. But we can also keep it the
421:40
Speaker A
same for that. Right here, we're going to take file handler, then set formatter equals formatter. And here's the same console handler set formatter equals formatter. So this is how you create different handlers. And remember handlers decide where your information goes. We're going to recap it in a second, but we just grab this configure logger. We can actually put it all the way down because here we create formatter. Then we create two handlers. Here we're going to configure logger.
422:07
Speaker A
We're going to create logger. We're going to set the level. And now we're going to uh take logger and add handlers. The first one is going to be file handler with Ctrl Duplicate and then it's going to be console handler. Now we have everything. The only difference is now instead of using the logging debug and all these messages, we actually have to use our logger. Whatever you named your variable, it can also be maybe uh EF logger. Then I'm going to write here EF logger. Uh debug.
422:42
Speaker A
This is a debug message. Now let's run it. And you can see this time I'm getting the message inside my log file, but also the same message goes inside the console. So now it's going to be in two places at the same time, which can be great. And we're not limited to two places. You can add as many handlers as you want. Now let's recap a few things. Logger is the main object that holds configuration and creates login messages. Handler determines where logs go to the console, file, email, Discord,
423:12
Speaker A
HTTP request, so on. Formatter controls how your log messages appear and each handler can have its own formatter. There's also a filter which we haven't covered and I'm not going to talk about them, but we can apply filtering on which logs can go through. It can be applied to logger and to handlers as well. And lastly, so far we covered quite a lot, but also I want to show you how to create custom handlers. This way you can send your messages anywhere, any way you like and it's not that
423:36
Speaker A
complicated. So, let me show you how to send messages to Discord and Slack channels because I think it's quite nice. You can have a special channel for your project where it's going to report if anything goes wrong. In this case, first of all, we need to grab another package. Let's go to settings button interpreter plus. Then look for requests. Don't need any other packages. Just logging and requests. You can see it's right here. Click on install. And it should say that it was successfully
424:01
Speaker A
installed. If you're not using PyCharm, just use the terminal. Just write your regular beep install requests. My case, everything is satisfied. So I have it. We're going to write import logging and requests. And the way we're going to send it to Discord or the Slack, we're going to use web hooks. If you're not familiar, web hook is a way for the apps to communicate using HTTP post request. In a nutshell, this is how we can send JSON data to a URL and the other app usually knows what to do. So
424:27
Speaker A
first of all, we need to get the special URLs and then we're going to be able to create our class. So, we need to get our web hook Slack URL and web hook Discord URL. It's not that complicated. Let's start actually with the Discord. I have a server right here. And to create an integration, right click on the server and go to server settings. Look for integrations which is right here. Let's click on that one. Right here, you can see web hooks. That's exactly what we
424:55
Speaker A
need. There's also a nice description. What is a web hook from Discord site? Create a web hook. It's going to create one right away. But you can also click here new web hook. We can select it. We're going to call it EF logger. And then we can select the channel. In my case, I prepared here something like log tutorial. That's an empty channel. Once you have it, right here is a button copy web hook URL. We need that. So copy and save changes. Now we created web
425:22
Speaker A
hook for the discord. So let's go and save it right here. It's going to be just a long URL, something like this. and it already knows where to post the messages. And now for the Slack. I don't really use Slack, but I know it's very common in development, so I thought I'm going to show you as well. So, you're going to create your Slack workplace. Then, let's add here a channel. I'm going to call it, it doesn't really matter what I call it, but let's call it log tutorial as well.
425:48
Speaker A
Create. That's fine. We don't want to do anything. Now, to create a web hook in Slack, there are different ways, but in general, go to more. Click here on automations. We can either create it as an app or as a workflow. This time I'm going to create it as a workflow because it's a bit quicker. Then create workflow. Now choose an event. In here we need to look for the web hook. And you can see from web hook start from third party event. That's great. Click on that one.
426:17
Speaker A
In variables we need to set up a key. We can set it as a text or content. Discord for example needs it as a content. So we can keep it the same. But I think Slack by default prefers to have it text. That's also fine. You can see here's an example of JSON format we will have to send. If you modify this to content you can see we have to send as a content. Let's keep it like this. Then we're going to continue. So when we send this web hook here we can define what we
426:43
Speaker A
want to do. Add the steps. Example we can go to messages and we're going to send the message to a channel. Select the channel log tutorial and here you can write whatever. Here you can see insert variable and this content is from the JSON JSON that we're going to send. We can also write here made by EF. So this is a little bit more customization here. So let's save that. And now we have our web hook. Can change the name right here. I'm going to call it again EF logger.
427:15
Speaker A
Save is okay. Now we need to go back to the web hook to copy the URL. When you're going to scroll down, you can see here is web request URL. the link. Just copy the link and make sure that you also finish your workflow. You can see right here there is a publish. There are some settings. I don't care about anything. I'm just going to click here and that's going to be fine. Sure. Yeah, whatever. Just add it to the channel. Now I can see here you can see this logger appears and this is the channel
427:45
Speaker A
where we're going to send the messages. In case of Discord, we're going to send it right here. But now they are both empty. Now let's go back to Discord and actually create it. So here's our web hook for the Slack. This is for the Discord. All right. And now we need to create a custom handler. Let's create a class and call it custom handler. You can call it whatever you want, but make sure it's based on logging handler. Inside I'm going to simplify it as much
428:10
Speaker A
as possible. We only going to write one method which is emit and it takes record argument and this is what's responsible for sending the message. Once you're going to write logging debug, it's going to use the emit uh method from the class. Now inside we want to do first of all log entry is going to be self format record. So this is going to apply the formatting as you remember we defined here this formatting for example that one and we're also going to apply to that
428:41
Speaker A
handler. So with this line we're actually going to apply to the message. Next, we want to make try and accept block and in try, we're going to send HTTP request. So, we're going to write here requests post and then it's very simple. We need our uh URLs and actually let's put them inside the class. This way, we're going to get itself web hook discord and then the message is going to be JSON. In case of discord, it has to be content and then log entry. And the second one is going to be for
429:14
Speaker A
the slack. And I think we set it as a content earlier. And here we're just going to print uh something went wrong. Again, I'm oversimplifying it. Ideally, you would want to create separate try and accept statements for both of them because maybe this one fails and but you still want to send that one. But again, this is the simplest it can get. This is the custom handler. In here, we define we want to format our message and we want to send it to these two web hooks.
429:41
Speaker A
But now, let's just collapse it and write the rest. So, I'm going to go up and I'm going to copy this formatting part because I like the way it is. Next, we need to create handler. In this case, we're working with custom handler and we need to just call our class which we created right here. So, this is going to create a handler and it already includes all the functionality because it's based on the kind of template. Then let's take custom handler and we're
430:11
Speaker A
going to set formatter which is going to be this formatting. And now we're going to configure the logger. First of all, we're going to create logger. We can call it logger. We can call it logger chats. Whatever you want. I'm going to call it actually logger ef logging uh get logger. Again the name for now you can just choose anything. If you work across multiple files then it makes sense to have the same one. Then for this logger, we're going to set level and let's set it at maybe debug.
430:44
Speaker A
And lastly, we need to add our handler. So add handler. Custom handler. That's it. This is the bare minimum to create your kind of custom handler. This case I chose to work with web hooks because they very universal and you can communicate with a lot of things. The only thing that's left is to actually make some log messages. So lo messages let's write here logger ef debug hello from python
431:15
Speaker A
then we're going to write for i in range 10 let's send 10 messages so logger ef is going to be this time let's make it a warning and we're going to write attempt this out of 10 something went wrong. And and we're going to make it an F string and include here an I. I also misspelled here for I in range 10. Now it should be fine. So let's quickly overview what we're doing. We need logging in a requests. Then we create this custom class. Use your own URLs. Don't use mine. Don't send to my chat.
431:55
Speaker A
I'm going to disable this work set after the lesson. Then we define our formatting. We create a handler. We configure our logger. Now we can use it. Normally you would do this kind of configuration in one file once and then you're going to import your logger across all your files. So they all use the same logger. There are also a way to create parent and children loggers but this is quite advanced. Just leave it at that. Now let's just run it. First I'm going to open Discord. You can already see
432:21
Speaker A
messages pop up. Then let's quickly open the Slack. And also see here the messages pop up. In Slack we also added this kind of footer made by EF which I think is nice. You have a lot of controls how you do this. But now we can see all right attemp 7 out of 10 something went wrong. We can check file logging python.py at line 136. I'm going to check it. So it comes from here. And I think this is a really great way to monitor the health of your app. Imagine you create a website. It's running.
432:50
Speaker A
You're not going to go and check the console every here and then. But you can set it up to send you an email, Discord, Slack, whatever you want. And you're going to just see a popup. You're like, "Oo, something critical has happened on my website." user has broken something, you can go and address it right away. Or maybe in development, you can go and say, you know what, let's set it to debug to show all my print messages, well, login messages that I want to see
433:12
Speaker A
during development. And this is a really powerful way and it's not that complicated. You can start using it very easily by just using this kind of severity levels and just simply even displaying it in the console. later on the app. It's going to be very quick to set up the file handler, the custom handler, whatever you want, handlers, whatever you want, formatterers, and it's also going to make you look as a better programmer because if you're always going to be using print
433:36
Speaker A
statements, other programmers will know, okay, this is still a beginner. But once you're going to start using logging, they're like, okay, this person knows something about Python. All right, guys. I hope you enjoyed the lesson. I hope that I managed to made logging simple for you. And please use the code from this lesson and start logging yourself. I'm going to wish you happy coding and I'm going to see you in the next lesson inside this course. Goodbye.
434:03
Speaker A
Hi everybody, welcome back. In this lesson, we're going to talk about Git for Python beginners. G is absolutely essential for any developer. You can secure your code, track any changes made to your code, provide version control, collaborate with others, create branches for new features without affecting the main code, and many more advanced features. It's very easy to begin. So, please do not skip on the git. You don't have to be an expert to start using it. And imagine this, you can spend days
434:27
Speaker A
working on your project and then you can lose part of the code by accident. And it can happen to all of us. And it's always because of some stupid mistake we make in a rush. Personally, I lost my code multiple times in the past and it was never fun. The most annoying part is that I could have avoided that with just a few clicks. And that's exactly what I'm going to teach you during this lesson. And let's start with a general overview of what is Git, how it works,
434:51
Speaker A
and then I'm going to show you how to actually start using it right away. So, let's jump in. So, first of all, what is Git? G is a version control system that tracks changes in your code. So, you can save versions, undo mistakes, and calibrate with others. Think of it as a smart backup tool with a time machine for your code. And to start using it, normally you would have to create a Git repository. And GitHub is the most popular and free choice. And repository
435:16
Speaker A
is just a fancy word. Think of it as a project folder on the cloud. The way it works, let's have a look right here. So you're going to start with your G repository with just a folder on the cloud. Then you're going to take your project, for example, version 1.0, and upload it there. Meanwhile, you're going to keep working on the project, make some changes in the file, and then you decide to make another milestone or save or commit. There are different names for
435:37
Speaker A
that. And then you're going to have project in the version 1.1. Then you can keep working again, make some changes, then you make another save. for example 1.2. The beauty of G is that at any point in time you can reference all the previous versions. You can see all your code state, all the changes and you can also undo changes. If for example something worked here, then you pushed a new change and then something stopped working then you can go back and kind of
436:02
Speaker A
try again from this version. This way you will always have all the versions of your code available and it's super efficient. On top of that you can also work in parallel. This is called branching. And for example here you can create a separate parallel project. So your project is going to exist at two stages at the same time. Think of it this way on your computer you have a folder with your project. We just Ctrl Ctrl + V and now you have the same project two times. This is what
436:28
Speaker A
happening here with the branches. And then on this branch for example you can make one change and on this branch you can create another change and then later you can bring this second parallel project into the main code and overwrite existing code. This is extremely useful for collaborating with others. So each person works on their own branch and when their work is done they're going to merge it into the kind of main branch and also it's very uh useful for working
436:53
Speaker A
on new features. Imagine now let's have at this example a little bit different. So here is the kind of nutshell of the g version control. Same as before you're going to create a g repository where your project lives and you only going to have one branch. This is the simplest way to start and this is what I recommend to majority of you to do. You're going to upload your project. For example, there are going to be four files. All of them going to have version 1.1. Then you're going to keep working
437:15
Speaker A
on the file one and you're going to save it and only this file is going to be updated. Then I keep working again and only these two files going to be updated. And as I mentioned before, at any point in time, you can look back and reference all the versions. What was file one here? File one here, here. And then you can restore code if necessary. This is the simplest way to begin. And this is what I recommend to you. just kind of upload your project and just keep saving your project over and over
437:39
Speaker A
and don't overthink it. Over time, you're going to start using it more advanced. For example, you could start using branching like this. So, you create your repository, then you upload your main project for the first time and you keep working. At first, for example, you work on file one and so on. But also, you decide that you know what I want to start working on a new feature. I'm going to start working here in parallel. So, this is one state of the project. This is another state of the
438:05
Speaker A
project until it merged back and also we can have multiple branches at the same time. So let's imagine that our main branch is a website running on a server. This is kind of the what people see when they go to your URL. Then right here is a working website. You published it. Then you decide that you're going to work on some kind of maybe AI feature like AI chatbot. And you don't want to affect your main branch. So you're going to kind of branch out and work here
438:28
Speaker A
independent of your main website. And when it's ready, you're going to bring it into the main code. And only from that point forward, it's going to be visible to all your website viewer. Also, at some point here, you decided to work on another feature. For example, advanced search or something or maybe a blog feature. And in this case, you just work for one file and then you bring it here into the main. And only here is going to be the main codebase of your website. All right. And by now, you
438:53
Speaker A
might be thinking, okay, it sounds wonderful. Probably I should start using git. But how to use git? And mainly there are three ways to start using git. And first of all you can start using git in the terminal. This is the most common way, the most advanced way and this is what majority of uh YouTube tutorials cover but also this is the least preferred way for the beginners because honestly it looks scary to start using the command prompt. For example, let's go to documents. You can see we can
439:18
Speaker A
create g here for this folder. Then you can check g status. There are a lot of these commands. Everything happens inside of the this kind of terminal in the text format. It's infamiliar for most of you. Can write g. You can see all of these commands. You need to figure out what command does, how to use it, and so on. And it might be overwhelming. I get it. This is why it's least preferred way for beginners. And this is why many people prefer to avoid g all together. We're not going to look
439:42
Speaker A
at that. Second way, it can be integrated in the IDs like PyCharm, VS Code, and all the popular ones. For example, if I'm going to open PyCharm of one of my projects, and in PyCharm, here is the button for the Git. I'm going to click on that one and it's going to open this menu where I can see all the GS that happened. You can see here's the sort of branching. There are multiple branches I created. I'm going to click on any. I'm going to get additional description. Here are the files that
440:07
Speaker A
change. Here's the description I did. And I kind of go can go in and have a look what happened before, what happened after, and all of these things. To be honest, it's a little better looking than terminal, but it's still a little bit confusing for beginners. And to be honest, I haven't really used it inside of IDs because I don't like it. I mainly use it inside the terminal or the next version UI application. This is the most beginner friendly. You literally need
440:32
Speaker A
five minutes and you an expert on how to use it. And here is how it looks. Uh during this lesson, we're going to use GitHub desktop. And actually, let me quickly open it so we can see an example right away. In here, this is just a UI interface for the Git. So you're going to have a little bit more limited functionality, but that's going to be more than enough for all the beginners. Here you're going to select the project in case you have multiple ones. Then in here, you're
440:56
Speaker A
going to see all the file changes. For example, here you can see I have one changed file. Once you're going to select a file, it's going to have a preview window right here. You can see that on line 66 and 67, I added some code. Then I can also remove some pieces of code and it's also going to be detected. And also if you change something, then it's going to show you like this. Then right here on the top you can select branches. You can work just in the main or you can create
441:18
Speaker A
multiple branches. And this button is pretty much where you're going to fetch original where you're going to push your changes and pull changes from the git. Think of it as a save and load button to connect with your GitHub account. There are other features available. For example, when you work on a file and you don't like changes, you can just come here and discard changes. Then nothing has happened. You can see it's very easy to use. That's exactly what we're going
441:41
Speaker A
to do during this lesson from start. And I think we're going to try to reproduce something like this, maybe simpler, but we're going to see how it goes. But first of all, we need to create you a GitHub account. Just open your browser and go to github.com. In here, you can create your account. So, you're going to write your domain name here, sign up for GitHub, and then you can see you're going to fill out and you're going to create an account. And don't worry, it's absolutely free to
442:04
Speaker A
use. Then, once you're going to have it, the next thing you need to go to GitHub desktop website. That's what we're interested in. Going to open it. experience g without the struggle. That's exactly what we want to do. Now, just find a download button somewhere. Just click on it and download the installer as usual. Now, once you download it, just double click and install it as usual. In my case, I already have it so I don't need to install it. Once you're going to
442:30
Speaker A
install GitHub desktop, you can run it and then you can log into your GitHub account. In my case, I already logged in and also have some projects. But in your case, you're supposed to see menu a little bit different. And there is going to be a button to create project. If you already have repositories, then I'm going to click here. Click on add. We're going to create new repository. It's also very simple. We just need a name. I'm going to write something like G
442:51
Speaker A
tutorial. And notice that you cannot use spaces. It's going to use dash in between. So you can write it right away with dashes. In description, you can write whatever. You can also leave it blank. Then next, you need to select location on your computer. I like to create something like in documents a folder called GitHub. Then I know that these are all my GitHub repositories. Just select this folder and don't worry, whatever you put here, it's going to create a new folder like this. In this
443:14
Speaker A
case, let's just leave it. Now you can initialize with the readme file. I'm going to explain what it is and how to fill it in a second. And for the g ignore, you can keep it none. For the license, keep it none. Eventually, you can choose like MIT get a new license. Just ask judge what's the point of the license. You will learn more. Let's click on create repository. Now we have this repository. And if I'm going to also open the folder can see here's my documents GitHub and I created new
443:38
Speaker A
folder inside inside you can see we created readmi file which is I think it's empty then g attributes comes by default and then there's the hidden folder git this is what defines what happens in your git now let's keep it here on the side something maybe like this so we can always reference what's going on in our github and here now we can see that we have to publish our repository to github because it's not there yet we could add here some files first or we could first kind of publish
444:05
Speaker A
and then add the files. It's whatever you prefer. Let's just publish it first. It's going to be a bit simpler, I think. So, click here. Here you're going to confirm the name, description, and also if you want to keep it private or public. I'm going to keep it private. I think that's fine. Click on publish repository. Since it's empty, it's just going to take a few seconds. And now it should be published. We can go to GitHub to verify. Just go to your account, go to repositories.
444:29
Speaker A
And for example, here is G tutorial private repository. I'm going to open it. This is the repository we created. Now, you can see it's at the moment pretty much empty. In the readmi file, it's just the title of my project and description, but we're going to change it. So, this readmi file in the markdown format is going to be whatever you're going to see in your GitHub page. So, if you're creating a public one, it's worth documenting what's going on, how to use it, and other comments you want to
444:54
Speaker A
leave. For example, if you want to have a good example, you can go to the search and write awesome read me. There going to be a few repositories of collection of kind of best examples. Let's just open that one and that one. Here is going to be list of all different projects which documented really well what they doing and you can see an example how it looks. You can also create your page that looks the same. When you find all these examples for example that one you can look at the
445:19
Speaker A
source code of this page. For that you just need to find here readme.md file. It's going to be right here. Click on it and then right here you can see the code how it's written. When you're going to click on it, you can see that it can also be styled as HTML or it can also be styled as markdown. So, it's combination of the tools. So, it's really flexible how it's going to look. In our case, I'm going to keep it even simpler. Just going to use chat GBD or clo whatever
445:43
Speaker A
you want and write create me a readme.md template for my let's pretend that I'm creating something like Django project. Obviously, you can go much deeper with that. I just want to show you an idea of what's going on. You can see it even provided a bunch of different code snippets, examples. It went really deep on how to contribute, deploy, and so on. Doesn't matter. We just have to download it as markdown syntax. I'm just going to open it in my PyCharm. And right here, you can see the markdown
446:14
Speaker A
example. I'm just going to copy everything. And then I'm going to come here. And in my G tutorial, I want to override this readme.md file. I just open that one. And I'm just going to paste whatever it is. I'm not going to modify this. But you can see on the side how it's going to look. There are a bunch of code snippets. There are headings, description. But now once we're going to modify this readmi file, we can see here that we have one file changed. It's readmi. And here we can see all the
446:40
Speaker A
changes that we did. We remove the initial two lines and then all the other lines we created. Now to push it, we just can write here a little description. For example, updated readmi file. Or you can also use this icon right here. Generate commit message with copilot. So AI just going to look what did you do and kind of describe it kind of went crazy a little bit with that one but you know what I'm just going to keep it for the sake of tutorial. Now once you're going to click on commit you kind
447:07
Speaker A
of committed this change but now you need to send it to the GitHub. You kind of confirmed it but it's not yet sent. So it's not going to be on GitHub until you're going to click on this push origin. Click on that one. And now it's going to send it to the GitHub. But just come here and I'm going to going to update it. Now you can see it updated readmi file. Now I can see this new uh description of my project here. Obviously it's just a placeholder from cloth. I have no idea what's
447:31
Speaker A
written there and I'm not even going to read it. But I showed you where you can find good examples. Now let's work with g a little bit more. So here is the folder and I'm going to add a few files. Example I prepared here file a b and c. The moment I'm going to add them here. Once I'm going to click on GitHub desktop. You can see free files were added. This case it's just stupid asky arts because it doesn't matter what we do. There is file A, B, and C. You can
447:58
Speaker A
decide which file you're going to post because sometimes you're going to make bunch of changes and you're like, okay, I want to commit file A and B, but C is not ready yet. I'm going to write here addit files A and B. You can also try copilot and see what it's going to write this time. Okay, two new files ask. That's fine. I'm still going to use my commit statement. simpler. I also like to use uh emojis in my GitHub. So, I'm going to put it like this. So, commit to files to
448:24
Speaker A
the main. Now, we need to push it. You can see here that we have one thing to push. Just click here. Now, we're sending it to the GitHub and we're only sending file A and B. C is not going to be on the GitHub yet because if I'm going to open it, I'm going to reload it. You can see now here's the file A, file B. And here you can see the committed messages that you made. There's also the time and right here the whole history of your project. If you're going to click on it here, you're going
448:51
Speaker A
to see A B C like all your commit messages. This is similar to what we discussed right here. Right now, we're just kind of updating the project. We're adding file. We're modifying them. That's actually go back here and open one of the file. I'm going to right click on the folder and then I want to show more options and then there's going to be open folder as PyCharm project. It's going to be a little bit simpler for us to work. Now in the po charm there's file A, B, and file C. Let's say
449:18
Speaker A
we want to modify that one. We're going to remove maybe the bottom of the cat like that. Now we're going to go back here. You can see there are lots of changes happen. Whoa. Let's figure out what's going on. Here's file B C. That's right. And then there's those also this that idea. When you use your ID like Python for example, it can create extra folders. this case it create that idea to keep different configuration about your Python project and different settings inside there are bunch of
449:45
Speaker A
different files but we don't want it to be on the GitHub therefore we need to make sure that we exclude it forever because you don't want to always come here and click on all these messages and for that we can create create g ignore file which have the rules on what can be included and excluded and with GitHub desktop what I like is you can right click you can see ignore file ignore folder ignore all XML files let's say that I want to ignore the folder then the folder is that idea just click on
450:12
Speaker A
that it's going to create a new file get ignore inside we can specify what we want to ignore because maybe for example we're going to have there a folder for CSV files and let's just create a few CSV templates a CSV and then a copy now let's say that you know what in my project I generate lots of CSV files but I don't want to send them on the GitHub this is for kind local results. You can right click and there you can see ignore all CSV files. You're going to click on that. It's
450:44
Speaker A
going to keep adding whatever it needs to get ignore. You can also use chat dbt to ask how it works. But you can ignore specific files, specific folders. You can ignore folders that contain something, specific formats, and it's really powerful what you can ignore. And now you can see we never have any of these things even it's suggested as a change. This is really important to keep it clean. Then for the B, you can see we removed this line, added that one. you
451:06
Speaker A
always can verify what you're changing because often times you can sometimes make accidental changes like a few spaces maybe delete some code you can right click discard changes because you know what I did not want to modify file B now it goes to initial state of my kind of current commit uh state and now we need to also add g ignore and file C I'm also going to add I'm going to make it a separate so create file C commit and and uh created.git ignore commit. Now look at that. Now we have
451:43
Speaker A
two committed messages right here ready to be pushed to the GitHub. Now once we're going to push it, it's going to go straight to the GitHub. So we now following just this format. We're just creating one change after another in one line on one branch. We're keeping it very simple and it's pretty much like just a cloud for storing your code which is great because later on you can use even more features. Let's go to GitHub update. You can see all changes here. If
452:09
Speaker A
I would modify something, you could always kind of come back here and see what was what happened here. For example, in this commit message, I added two files. If I'm going to go to created git ignore, you can see I created git ignore like this. And you also can see how your project progresses and you always have ability to go back and nothing is going to be kind of messed up. Also, let me go back example. Let me create a folder. Let's say my ASI arts. I'm going to take this file AB and C and
452:40
Speaker A
put it inside. Now, when I'm going to have a look right here, you can see that it's going to remove these three files. It's going to create the same files because they are in a new location. I can just write here moved files. Let's commit it. Push. Now let's go to GitHub. Update it. Now I can see we have a folder ASKI art. Now here are all the files. Now let's talk about branching a little bit because it's right here. You can see the current branch is main. And to be honest, just
453:09
Speaker A
keep working on the main until you're more comfortable and slowly learn more about Git. But let's say I want to work on maybe a new art. So I'm going to create a new branch. I'm going to write something like feature new art. Let's create a branch. Now here's what's happening. So we were something like this state. And now we created a new stage, but we also have the main stage. And here's how it works. I would need to maybe create I'm going to just copy one of these ones just for the sake of
453:38
Speaker A
tutorial. Let's also make here new feature. Now I'm going to paste it there. Now you can see this feature is right here and I can commit it. However, I'm currently on the branch feature new art. I can switch back to the main. That's going to ask do I want to bring my changes to the main or do I want to leave it at this feature. Right now it's not finished. Let's say I just want to switch to the main branch. This is going to disappear. You see there is no folder. There is no
454:07
Speaker A
file because I'm on the main and at the main state it does not exist. However, I'm going to click here and go back to feature new art. You can see there are stashed changes because I haven't committed them yet. Here I still don't see anything new. But if I'm going to view the stash, you can see I can restore it. Let's restore it. Now it's going to create this one. So I have at the same time in the same location two states of the same project. I have my main one kind of main branch which you
454:34
Speaker A
can see right here and I have feature new art and I can keep working on the both and save both of them. Now I haven't published the branch yet. So let's do that. So what I'm doing is I'm kind of creating this A1 right now and adding there a new file. Now I published it. Now I can commit my message as well. New file A. I'm just going to commit and push. Then you know what? I'm just going to kind of duplicate it a few times. I'm going to keep it very abstract because
455:01
Speaker A
it doesn't matter what kind of code I write. Now I see here even more of those. Now let's actually go to GitHub updates. You can already see that there is a new branch added and right now I don't have this feature because by default you're going to be working in the main this is the main state of your project but here you can also see there is feature new art let's click on that one and here is going to be new folder so you have parallel projects right this is kind of two folders of your project
455:28
Speaker A
going simultaneously in the same direction now I can go back we can write addit more files then commit this messages then push You can see it's very kind of simple. Just commit push commit push. And it's very easy to kind of keep working. Now there's also a message for the pull request. We're going to talk about this in a moment. But now let's just go back to GitHub. I'm going to update it. Inside you will see there are bunch of these new files. Now if I'm going to go to commits
456:00
Speaker A
you can see I can select here for example on the main only these changes happened on this feature all the changes before we created new feature. uh new branch plus the new uh commit. So now you can keep working and let's say that now it's time to bring it back. All right, at the moment we at this stage where we work on a new feature and now we want to bring it to the main kind of code base so it kind of continues to be our kind of final code and in this case
456:25
Speaker A
it's called pull request. This is when you're going to bring your kind of branches into the main uh branch. To do this right here you can see preview pull request. You can also create pull request. my case I'm just going to preview. Then it says merge two commits into the main base from this kind of branch. This is all the files you're bringing. It just explains what are you doing. Let's create pull request. Then it's going to go to GitHub and here we can create like what's the name of the
456:52
Speaker A
pull request. For example, feature new art. This is the name of our branch. We're going to create pull request or we can also create draft pull request. Just going to create pull request. This is already more for collaboration. You can see there are like you commit it then somebody else can check it and then they kind of allow it to join the main branch. You can see there are no conflicts. Also if there are conflicts you would have to resolve it. Decide which code to take from where in this
457:19
Speaker A
case I can click here to merge pull request. I'm not going to change the commit message. It can be whatever. You click on that one. And now we merged it together. So now all the changes from the current branch feature new art. They should also be inside the main branch. And if I'm going to go here, you can see that now I'm on the main branch and and actually nothing is here. And I'm going to explain why in a second. I just go back to feature new art. You can see this folder appears.
457:49
Speaker A
Now let's go back to the main. Now you can see disappears. This is because you also have to click here on fetch origin. Let's click on that one. It will check what's going on in your GitHub. And then you can see there are actually some changes to the main branch and you can pull these changes from the cloud. Let's click on that one. Now this folder is here. You can see now we up to date. So not only you send your files to GitHub, you also click here to kind of get whatever is there. So it's
458:16
Speaker A
push pull push pull. And now we still have this feature. We're not going to work on it anymore. We can we're just going to kind of focus on the main one now. Now in your GitHub, you can see here there are all the commits, all the changes, everything is documented. I can always come back and see what was happening. All right, guys. And that's the basics of working with GitHub desktop. It's not that complicated. You just kind of manage whatever you want to save. You push it, you pull it, and just
458:39
Speaker A
kind of go back and forward. When you're ready, start using branches, but you don't have to. You can just work on main branch, and that's going to be fine. Another life hack I want to show you, many people don't even know about this. Now, once you're going to be in your Git repository, just come here and replace.com todev. That's going to be mind-blowing. All right. And this is actually crazy because now we're going to open this repository in the online version of Visual Studio Code. And then
459:04
Speaker A
we still have G here. We can explore our features. And this looks more like your ID. This way you can actually open the files. You can even work here, right? We can still kind of come here and say print hello world. It's not as good but still you can see there's for example it asks us to install Python language let's do that install anyway let's come back to our file then import OS we also have autocomplete here OS path join kind of knows what's going on we also can click here the source
459:36
Speaker A
control can create commit messages from the internet so now you have your ID in an online version of the GitHub Visual Studio Code this is crazy now for the commit message. I'm going to write testing def mode commit and push. Now we actually modifying our extension in the browser. And it's actually quite nice. It's not something I use all the time, but I like to show this feature exist. And sometimes it's actually nice to explore other GitHub repositories because this
460:04
Speaker A
is a more natural way to explore the codebase. Look for files, look what's inside than the regular. If I'm going to go for for example github.com going to go to regular GitHub and it's okay but it's not as good because here you're going to click let's say file a it's not as nice. Now let's change it to def. It's going to be night and day difference. We're going to also open that file right away. Here is the code. We can modify it. We there is a lot we can do and we can even push it. So
460:33
Speaker A
definitely try it out. No, not many people actually know that this feature exists. I don't even know where I learned about this, but use it. All right, and that's how you can start working with Git, even if you're an absolute beginner. Now you know how to back up your code, have history of your projects, how to use branching for parallel development and collaboration, and more. As a beginner, just focus on the simplest workflow with a simple branch. And when you need it, then you
460:58
Speaker A
can also explore other features. How to work with multiple branches, how to go back in time, and so on to better manage your project. But now it's your time to practice. create your GitHub account, then load GitHub uh desktop, create a repository, drop a few files back and forth, and just make a few commit messages. Don't worry, just kind of do it dirty because later just delete it and start in brand new repository. Now, I'm going to wish you happy coding, and
461:20
Speaker A
I'm going to see you in the next one. See you. So far, you already learned quite a lot about Python during this course, but let's talk about how to organize your project to work with multiple files. Because you see on larger projects you'd want to separate your code into individual modules and packages and work on one of them at a time. This will help you kind of maintain your code and make everything very structured. And also in the end you would want to run your program from a single file which will bring all of these parts together to
461:51
Speaker A
produce you some results. So let's dive in and I'm going to explain how to work with multiple files in Python. This lesson I created a new project and it's very simple. Right now it's just the main.py file and you can call it anything. Inside you can see there's just a single print statement and let's create a few soda functions which we later going to put in the modules and I'm going to show you how to bring them back with the import. So for that instead of just kind of taking all of
462:13
Speaker A
that and using them one by one like this even though you will get result it's not going to be very maintainable code. So therefore it's good idea to create modules. We're going to rightclick new Python file. Module refers to Python file from where you can import different classes, functions or even variables if you want to. Let's call ours gen audio. Then another one is going to be gen video and then gen images. Now to make it a bit more structured, I'm going to
462:40
Speaker A
right click and split it on the right. So it's right here. I'm going to close these ones. And now after I click on the right side, and when I'm going to open new projects, you can see they're going to be added there. So we need images, audio, and video. Now I can easily take all of these functions and bring it into these files. So gen images.py Pi is going to be this one. Then gen audio is going to be that one. And gen video is going to be this. Also, it's a good idea. Let's make a
463:07
Speaker A
print statement from where we're running the files. It's going to help us in a moment. So, this is run gen images.py. Then in this case, it's going to be running gen audio and lastly video. Now, we already separated our code into multiple files. And now we can work in these files. For example, in this one, I can come here and say, you know what, generate video. And if I would execute the right part of the code run, you can see running gen.py file generating videos. But later on when this is kind
463:34
Speaker A
of works, you want to bring it to the main where you will actually execute everything and kind of combine all the pieces. For that, you just need to make an import statement. So I'm just going to write from and then we can reference all the modules right here. So we're going to write for example from gen video.py file. Then we can grab whatever is inside. that we can grab functions, classes and so on and bring it into the main file and it will still keep all
463:56
Speaker A
kind of the connections and logic you're going to define there. So I'm going to write gen video import then there is a generate video then from gen audio it's going to be generate audio and the same for the images. Now look at that. Now we can execute this code and have a look here. There's a little bit of a mess happening in the console. Let's break it down. So first of all it says that running main.py file which is correct. It's this one. Then we're making this
464:20
Speaker A
free import. And you can see that you're running gen video, generating video, running this one, running this one. So whatever you wrote in your files is actually being executed once you're going to import this. This is not good. We're going to fix it in a second, but let's just kind of keep going. Then it also says running gen audio, running gen images files, and only then we produce the results, which is going to be this part right here. So how do we fix this
464:42
Speaker A
part? Because we don't want to see this. This comes from other kind of files. We only want to execute when we actually working in the files. And for that we need to use the special statement. You can in PyCharm just write main click on enter and it's going to generate this kind of statement. It tells you that if name refers to the name of the file equals to main. So means that you're running from the file itself. Then we can execute it. And now for example we can put everything here like this.
465:08
Speaker A
We still have our function here which we can import if you want and we can still execute everything inside the file and can see we're getting the results. However, now if I'm going to run the main file, this one, you can see I'm not getting anything from generating video.py file. Now, let's do the same for other two. So, we running gen audio file. Let's actually generate audio. I'm just going to copy and paste it in the last one as well. Now, you can keep working on all your modules separately.
465:36
Speaker A
You can test whatever code you want. However, it will not be executed in the main file when you execute it. Now, if I run it, you can see I'm running main.py the pi file and I'm only getting the results from these functions and that's exactly how I want it to be structured. So this is the first thing you need to know about modules and when you import them and now it's already working pretty good. We have here free modules that we can work with. We can add more files
465:58
Speaker A
inside but eventually you will be adding more and more files and your project can get messy if you just going to have one long kind of list of files one by one. So therefore we also can put it in a package and in Python terms package think of it as a folder. So, we're going to go click on new directory. I'm just going to write generators. Now, we have this folder. We can grab all of these files and bring it inside. Now, you can see we have them inside. Obviously, if we're going to try to
466:26
Speaker A
execute main code, we're going to get an error. No module gen video. Because at this point, when we execute this, it's looking for files right here, right? It looks that is there gen video? No. Is there gen audio? No. Therefore, we get a error. And to solve it, we just need to import from the package. The way you do this, you just write from generators. Gen audio and then you import whatever you want. I think in this case it should be gen video. Let me just organize it a little. So
466:58
Speaker A
then here it's going to be generators this and this. Now, so we first look into the generators package and then we look at all the files. Now let's run it and you can see it works. And here's also a thing. Before Python 3.3, I think we also absolutely had to add here a new Python file which is underscore_init_.py file. I'm going to explain what it is in just a second. But before it was absolutely must also the moment you're going to add it, you can see that PyCharm sees this as a package. Now,
467:29
Speaker A
however, it's still a good practice to add it just for the sake to show other developers that this is a package. Now, if you run it, nothing going to change. So what the hell is this init file? And let's actually put here a little statement. Just going to paste it. Now I can execute it here. You can see in it and just going to be the absolute file path to the file from where I execute which is this variable right here. So I'm going to go here, click on run. You
467:54
Speaker A
can see running main.py. Then I initiate the package. So whenever you're going to import anything from generators like this one, you're going to execute this code inside the init statement and can be really beneficial. I'm going to show you a few tricks in a moment, but for now also notice that it's only printed once even though we call it three times. So it's going to be called only first time when you initialize your package when you use it for the first time. And
468:17
Speaker A
then same we just printing the statements. Now if you look at that, it's very annoying to have multiple print statements from generators blah blah blah. Can we simplify it? And the short answer is yes. Let's go back to the init file and right here we can write from gen audio import generate audio and you get the idea. I'm just going to quickly make for the rest. Now what's happening is we looking into all of these files getting all the functions and we're bringing it
468:46
Speaker A
into the init file. The reason we do that is because once we're going to do that we don't even need to look inside all of these files. I can comment it out and I'm just going to write from generators import. Then I'm going to go straight for the functions. Now if I execute can see, oh no, no module named gen audio. It's a bit tricky. Let's just go back here and see. We're importing it here. And we can also try. Can we actually execute it here? Run it. Yes, we can execute. So something is wrong.
469:17
Speaker A
It's also a good idea to make main right away. Let's just put it there. And here's the thing. when you execute it from here from this init file it looks right next to it and sees oh here are the files however when we execute it from the main.py by right from generators import all of this can see no module named gen audio because when you execute from main it still looks kind of nearby is there something and for that we need to make a little bit different syntax right here
469:48
Speaker A
we just have to specify that we're using the relative path for that using the dot this way it's going to make relative paths all we have to know is that now it's going to work I'm going to run you can see it works so the moment we added these dots when we run it from the main file it start looking right inside the package next to init statement because we said look nearby like relative path. So it works. However, it's a little bit weird. But now if I'm going to try to
470:12
Speaker A
execute from the init now it's not going to work because attempt relative import with no known package. We just have to deal with that because well you're not supposed to run init files anyway. Therefore it's okay to do that and you don't really need to even execute main. So let's just leave it like that. Close it. And now you can see we can from generators import all of it and also just see the star run it also going to work just fine. Now let's say that later on you're going
470:40
Speaker A
to start working on the audio images video. You're going to produce results. You're going to need more Python files more logic and it's again going to get messier. Let's create it nested packages as well. So I'm going to create folder for the audio for the video and for the images. And then I'm obviously going to put all my packages inside audio to audio images to videos and so on. Now obviously I'm going to break something if I'm going to run. You can see there's no module name
471:05
Speaker A
generators gen because this time we need to go into the init statement right here. We also need to add that we look into audio gen audio. Then here we look into uh video this and then images like this. So you can see we keep kind of traveling along the packages and into the modules to look for the functions. Now if I'm going to execute can see now it works. So this time we brought it in. In case you don't really want to set up this init file, you could still write here from generators audio.
471:39
Speaker A
Gen audio import everything. Then you go to the next one from generators uh video.gen video and you get an idea. So I'm going to execute. Well, it still works, but we also import it here. You can see it's the same functions we import. And maybe we just remove it so I don't confuse you because we changing the structure of the project. Now, also inside of this, it's would be a good idea to add the init statements. So, let's just click here. Python_init_.py. It can be empty. It's there is no reason
472:12
Speaker A
to kind of fill it, but it's good to kind of keep it as a package if you're kind of working with imports from it. Now we can see they all marked as a package we can easily import in out. Oh and obviously if you're going to go here in in it and add here some print statement for example just add it everywhere just so I show it once and then we're going to remove it. So I'm going to execute can see I'm going to get lots of this in it because when I'm going to uh import something from
472:37
Speaker A
generators inside the generators init file I'm going to also import from all these files kind of one by one. So do not try to make something complex inside your init files. In my case, I like to make this import so it's easier to bring from one package instead of going into all and maybe sometimes a little logic to maybe document to print something. Otherwise, just don't put it there. Also, I nearly forgotten. There is also an amazing feature in PyCharm that will
473:02
Speaker A
help you work with multiple files. For example, let's say that I'm working on these generate functions and I'm going to close all these files and I don't want to really look where they are in the project. So I'm going to select the function and then click on CtrlB and you can see PyCharm automatically finds where this function is located and takes me there. Now I'm going to select this images CtrlB. It takes me there right to this function where it was defined. It
473:27
Speaker A
also works on variables. For example, I can write here test equals hello. And then let's say we're going to go all the way down to the code. It can be hundreds of lines. Then we're going to write print test. And then later I can select this test CtrlB. It's going to take me to the place where it was created. And in order to work, you just have to make your import right and as long as PyCharm is aware where your files at, it will be able to find it. We got to again Ctrl
473:50
Speaker A
+B. It's going to open the file and can make it so much easier to work. All right. And that's how you can work with multiple files and folders in Python projects. Just keep in mind that module is a Python script from where you bring your function and classes. And package is a folder that can contain modules or other nested packages. Overall, it's not complicated. You just need to follow the path as you import it. But as you've seen, there are also a few tricks that
474:13
Speaker A
you can use in the init file to make it even better to use. Now, I'm going to wish you happy coding and follow the lesson and create a few modules and try importing these functions into your main file and execute it. It will help you better understand this concept and then I will see you in the next lesson. See you.
474:34
Speaker A
It's time for you to learn about OOP in Python, which stands for object-oriented programming. It's a specific way to structure your code with classes and has a lot of benefits. To understand classes, think of functions for a second. You take a piece of code, you wrap it in a function, and then you can reuse it over and over. And well, classes, they're going to take it to the whole next level. They allow you to create a blueprint of an object. It can contain multiple functions and can also
474:59
Speaker A
have its own variables. And then you can use this blueprint to create multiple instances all based on the same blueprint. And even though all of these instances can use the same functionality and access to the properties, they can all hold different data and also use different functionality depending on your needs. It's just a better way to structure and reuse your code. For example, think of strings for a second. It's a class that has a bunch of methods to provide additional functionality.
475:24
Speaker A
Then you can create multiple instances of a string and then use different methods on each one of them to modify their values. And they are all based on the same class, but you can use them differently. This is just a basic example, but let's jump straight into code and I'm going to explain everything you need to know about classes in Python. So to create a class, we need to write the keyword class. Then define the name of this class. At the moment, I'm just going to write pass to create just
475:47
Speaker A
empty blueprint. And this is already enough to start using it. So this is going to be our template. And now we can also uh create object instances based on this template. For example, I'm going to write element A equals element and element B equals element. So these instances are based on this class. Let's just make some print statements to see the types of these objects. I'm going to run it and you can see it's a class. Main refers to the main file we have
476:14
Speaker A
open and based on the element class. So this is our custom class and it's similar to if we would take kind of a built-in data types like string, integer or maybe a list. We're going to print it. You're going to see that these are based on the class of string, class of integer and class of list. So what you need to understand first of all is that the classes it's a blueprint of an object. Then we can create instances that have all this functionality. At the
476:36
Speaker A
moment it's just empty but let's kind of keep building. When you define class the first thing usually you do is define the init method with double underscores on each side. This is the constructor of the class. It's going to be initiated every time we create an instance of a class. For example, we can write here uh creating an element. Now, if I'm going to run this code, you can see that first of all, I'm making the print statements every time I create an object. If I would create three of
477:01
Speaker A
them, let's click it. I would execute it three times. Now, let's get back here for a second because you might have noticed that we're using here self. And this is a very special keyword used in classes. It's not an argument. It's just a way to refer to itself, to the class. And we need it because later on this self is going to be replaced with the actual instances. For example, let's define here self width is going to be 10 and self height is going to be 20. Now,
477:29
Speaker A
whenever we're going to create this elements, we automatically going to set these attributes to the class and think of them as a variables inside of a class. For example, we can also access them. Let's write here print element a width and then element a height. We're going to do the same for the B element. Now, if I'm going to execute this, you can see right here I'm getting 10 20 10 20 because at the moment all of my classes have the same values. And here
477:56
Speaker A
is another thing. We can also modify all these properties inside the elements. Let's say that right here I'm going to say element a width is going to be not 10 but 15 and element uh a height is going to be 30. Now, if I'm going to print it now, I can see I'm getting different values. These are the default ones for the second one because we haven't changed it. But for the first one, we modified it. We can also modify attributes that doesn't exist yet. For example, element A custom is going to be
478:23
Speaker A
uh B. And now right here I can kind of print element A custom. If I'm going to execute, it's going to read this value because we assigned it inside. It's right here. However, this is not a recommended way because on all other instances like element B, this attribute is not going to exist and you're going to get an error. So it's really recommended to first of all define it inside the class. So later all of your instances have access to the same functionality even if if it's a default
478:49
Speaker A
value. Now let's take it to the next level because at the moment we're just creating element with default values and it's not very useful. So we would want to create it and provide here different attributes. For example going to define width, height. Let's also define location and material. Again we're going to define here self location equals location and self material we're going to equal material. Now you'll notice that here we have an error. If if we're going to try to
479:15
Speaker A
execute it's going to tell you that we cannot create it because it requires four positional arguments. So inside the init first of all we're going to define self which is going to refer to the element itself and then all the next kind of parameters. This is going to be the input to create this instance. This is the point of the init constructor. So let's define here this is going to be 10 20 and location is going to be 0 0 and for the material we're going to define
479:40
Speaker A
here for example wood and here we're going to define 15 30 location let's make it 1010 and for material we're going to make it I don't know like concrete and also let's clear this away we also don't want to work with custom anymore and we don't want to override anything
480:00
Speaker A
so now if I'm going to execute it's going to execute correctly And now all of these objects going to have their own attributes here. Just like in any software when you create elements they can have different locations, different sizes and so on. It's already start to become useful. However, we still don't have any functionality because attributes they just hold data like a variable. But we can add here another function. When you going to create a function inside of a class, it's going
480:22
Speaker A
to be called a method. We're going to write here define. Then let's create here something like print uh data. And you can see it's also going to take the self as the first argument. We're not going to provide any other parameters here. We're just going to write here print material is this. Then I'm going to duplicate the same for the width uh for the height and for location. And inside we can reference all these values by using the self this kind of uh keyword. So I'm going to write here self
480:54
Speaker A
material then self location self width and self height. Now the beauty is that I can come here and just say element a uh print data just use it as a regular method like on any other string list or whatever. And if I'm going to run it you can see that now I'm getting all these elements. Now let's fix a little bit. I also want to add here maybe separation dashes so it's easier to read in the console. Now here's the second element. Here's the first element. You can see they have
481:24
Speaker A
different values associated with them. Also keep in mind that in here if instead of self we'll just write material and try to execute we're going to get an error because element doesn't have attribute material. We cannot also reference it by using like this because if I run it you can see it's not referenced and this is because of the concept called kind of global and local kind of uh variables. The idea here is that when you create a function or a method, this is going to be locally
481:50
Speaker A
defined variables and they're going to be only available in this orange box because they are local. So this material is not available anywhere outside of this class. But if you're going to put here self, then you associate this property directly with the class and therefore we can reference it in other kind of methods as well. Let's write it here self. Another thing to understand this self um keyword is for example instead of using self here later it can be used with the actual instance because
482:20
Speaker A
at the moment this is just a blueprint but later when you have actual instances you can come here and reference actual values of instances for example element a uh we're going to get width we're going to get height and for now it's enough let's just run and you will see here is 1020 because we are referencing them here manually so this is the idea about the self it's just a way to refer to the class itself. Now let's add here some other functionality. For example, let's create
482:46
Speaker A
abstract move. Just going to write here moving an element. Maybe we're going to provide here arguments as well X and Y. For that, let's write here X and Y. We're going to make it an F string and we're going to just provide X and Y here. Then also we can modify the location because right now it's here the default one. So we would want to get the initial data and since we're defining them as tpples, we would need to write here alt x equals selflo the first value
483:15
Speaker A
and y is going to be the second value. And then we're going to write here self location equals we're going to write old x + x because we're modifying these values and y + y. Now we're going to print some data. Then we're going to take element a and we're going to move it. Let's move 100 and 200. And then again we're going to uh run this print data to see what happened. I'm going to execute this. I can see we're moving an element this kind of coordinates and later they become some
483:45
Speaker A
other value. We started with a 0 0 and now we added X and Y. So we not just printed the value, we also modify the attribute. This is how your additional functionality can help you modify your objects and defining your logic inside. Also right now we're moving elements but we don't know which element it is. And I want to show you a little bit more advanced concept. But I want to show you how to create global kind of ID for all your instances automatically. So inside
484:10
Speaker A
the class, we can also define here another attribute. We don't need self. We're just going to write here something like uh for example ID count. I'm going to start at a zero. When we do that here, it becomes an attribute for the type. And this is attribute for the instance. It will make sense in a second. Just give me a moment. Uh I'm actually going to create a new method here. And I'm going to write generate id. Now I'm going to write here element id equals and to reference this value
484:39
Speaker A
going to write element do ID count. Then I'm going to take this element id count and plus equals 1. I'm going to return element id. The reason I want to return it is because I want to create here a new uh attribute. I call it self ID. And I'm going to reference this method here. Generate ID. And the thing is about these type classes is that they are shared across all instances. So as we kind of iterating here and adding more values, it's going to be remembered across all
485:13
Speaker A
instances. Therefore, when we create new instance, it's going to get new values. So it's going to be zero, 1, 2, 3, and so on. Let's actually start with one. And I also want to move this print statement down. And for example here I'm going to write something like element with this id going to write here self id. Now look at that once I'm going to execute and let's come here creating element one element two. Now I can keep track which element I'm working with. It's a very common way
485:41
Speaker A
in kind of 3D softwares when create lots of elements inside they need some kind of identifier which is unique to all elements. And that's one way to create it. Now we can also come here and say for example right here that we are printing data for element with this self id. Let's make it an f string. And here moving an element. Uh also we're going to add the same thing self ID like this. Now when I'm going to execute now it's going to make so much clear because look at that. We're creating
486:12
Speaker A
element one. Creating element two. Now element one these are the kind of attributes element two. Then we moving here element one and again element one these are the attributes. So again let's recap something. So this is a type attribute which is going to be available to all instances independently and we can also modify it inside by using not self but the name of the class in this case the classes element. We also have instance attributes which are just common way to create it and we can also
486:40
Speaker A
reference all this data outside of classes as well for the instances. And these are the basics you need to begin with classes. So you know how to encapsulate all your kind of functionality. And this is one of the kind of four pillars of OOP. First of all, encapsulate is just you keep all your logic inside a single element and it's much easier to develop your code. Also right here you have a structure in the ID. It will help you navigate inside your classes. Now here you can see
487:06
Speaker A
everything happening in your script. I have variable element A, element B. It also takes me there. And also here in the element I can see all my all my methods, attributes and so on. Can see that right here it says M for the method. Then here F for the field for some reason. Normally it's attribute or even a property can be called. And we can also notice that this one is a special one. It has a log because this is a kind of dunder method. Dunder stands for double underscore. Therefore dunder
487:33
Speaker A
in this case it's a special constructor that will be executed every time you create a new instance. But there are many more of them. We can create a few. But first of all, let me just clear up some data. And I'm just going to print here element A and element B. Let's just see what we get when we print instances. And you will see this weird way. It says that it's a class of the element and then object at this location in the memory. It's quite use useless to us,
488:00
Speaker A
but it's useful to Python in the back end. But we can modify what's going to be returned here. For that, we need to use this special kind of dunder underscore methods. example we can use here rapper represent and whatever we return here it's going to be how each instance is uh shown in the console by default example I want to show that this is an element with this ID let's make it an string uh made here a typo this should be here and this should and this should be here
488:30
Speaker A
like this now when I'm going to execute can see now they're going to be shown as element one and two and I have full control of what's going on and this done methods, they're really, really good. And this dunder methods, they're really good because they allow you to kind of apply the special logic to your classes. Let me just open one page in the browser. This is a really good overview of all the dunder methods. There are really a lot of them. You can you can
488:51
Speaker A
see here if I'm going to scroll through, there's like lots and lots and lots of different types. The idea is that we can define what happens if we for example print it, right? This is the repper. It's going to return you a string of how the object represents. We can also say what's going to happen if you're going to say element A equals element B. what we want to actually check we can kind of define the logic by using this special dunder method or if I'm going to scroll
489:12
Speaker A
further I can see what happens if I'm going to for example try to make boolean of my objects or if I'm going to create a context manager and so on there's really a lot of functionality we can add can even define what happens when you run len function on on your element maybe you want to count here the length of the element and many different things you can have full control over that but also don't be discouraged because there's really a lot of them and I can
489:34
Speaker A
guarantee that even Python users with 5 years of experience they don't even know all the dunder methods because that's fine you just need to know a few and that it's possible and where to find them and then later if you want to really crazy with classes you can always kind of define it here but as a beginner don't you even worry about dunder methods I just want to explain why there is this kind of double underscore methods that have special meaning now before going to wrap this lesson let me
489:57
Speaker A
just select everything and comment it out and I want to explain you another really important concept in OOP which is called inheritance the idea here is that we're going to create a base class then create other classes based on that that going to inherit all the functionality and properties and I'm going to explain why it's important in a second let's create again class of element I'm going to define here a few functionality let's say all elements supposed to move
490:22
Speaker A
going to print here moving element then we want to rotate them and let's also say that we want to scale them as well so we created this base functionality and now I want to create other classes which have their own specific and a functionality, but they're also going to reuse this one. We don't want to definitely copy paste here and say this is a class of wall, this is a class of floor because this is a horrible way. First of all, whenever you copy paste anything in Python, you're doing
490:48
Speaker A
something wrong. There's a better way always for that because in here, let's say that later you're going to come here and say I want to make it like this. Then you have to modify it in three places and it's very inefficient. So therefore, we're going to use inheritance. And to create our children classes, we're going to define here class of wall. And in parenthesis, we're going to define that we want to base it on the element class. For now, let's just make them empty.
491:13
Speaker A
This is going to be class of floor. And it's also going to be empty. Now, I'm going to create wall A, and I'm going to create floor A. Now, here's the beauty. I can take my wall A and say I'm going to move it. And then I'm going to also move my floor because right here you can see they are empty but we still have functionality that we defined in the element class. This is why it's so important because now I can just come here modify it in one place. Let's write here modified.
491:42
Speaker A
And if I'm going to run you can see now it's modified. It's a really advanced way to reuse your code especially when you work with a lot of different classes. Now let's also make it a little bit more interesting. I'm going to define here a category and inside I'm going to return there's a special syntax self going to refer to the class itself and I want to get the name this dunder methods they also mean usually that this is internal kind of functionality that you're not
492:06
Speaker A
supposed to mess with but you can use it to access different values and the reason I want to get it because I want to use it here going to write selfcat and since it's a method I need to call it now we're going to do the same thing moving element here and here. We don't need that anymore. Let's make it f strings. Now, since we working with methods, don't forget parenthesis. It's really important. Now, when I'm going to run, have a look. Moving wall, moving floor. I haven't even start working on
492:37
Speaker A
these classes individually. But I can already kind of have different outputs depending on the name of the class. For example, this really really powerful workflow. Another thing that let's say that we want to move our walls and floors differently. We can override this functionality. We're going to write here define move. I'm going to write inside that moving wall based on line. And then here we're going to define move. I'm going to moving floor based on point.
493:11
Speaker A
Now I can execute this and have a look. Now we have overridden the initial kind of functionality. And this can be useful. For example, this is going to be the standard one. and can apply to maybe a few classes and then other classes they need something different because it's different logic behind how they operate and this is polymorphism in OP. It means that you use the same uh method but they can have different outcomes or even different logic behind them. Now
493:35
Speaker A
let's take it to the next step. Let's also define here init. I'm going to keep it very simple. We're not going to take any arguments. I'm just going to write here that uh element in it executed and then inside I'm going to write self category equals selfcat. Now when I'm going to execute this for now we're going to see that it's executed because we have not overridden it. So we're going to go back here and define here define init and we're going to print wall in it executed.
494:11
Speaker A
Then we're going to execute something similar in floor. It's going to make sense in a second. Just bear with me. So now I'm going to make it. Now I have my new init statements, but I'm not executing anything in element. And often times you would want to define here maybe a set of uh attributes, self like ID should be this. Then self height, self width, whatever. We're just going to keep it simple. You can still call this init method by using very special syntax. Let's just go back here to the
494:38
Speaker A
init. And since we overriding it, we need to call the initial init from inside. It's a bit confusing, but to do that, write super then init like this. The super it's a special kind of keyword and it's going to execute and it's going to refer to the parent class before you modify anything. And then we're going to execute the first init. Let's also do it right here like that. Now I'm going to run it. And now you can see that I'm still executing the init from the element class. And
495:09
Speaker A
then I execute my wall init statement. And this is just a very simplified example, but you can already see the power of reusing your classes at a multi- kind of layer. And also in the beginning, maybe you don't really want to use inheritance until you really start working on large kind of code bases. All right. And that's the basics of OOP in Python. I went a little bit too quick and I wanted to pack in a lot of different concept you need to know about and if it's overwhelming it's that's all
495:35
Speaker A
fine just give it some time it will come to you naturally as you get better with programming there's really a lot more possible to do with classes you can even learn about design patterns so you actually take the right kind of structure of the class for right execution but just don't worry about focus on the basics and just get comfortable it will be more than enough in your programming journey and if classes are a little bit too much just don't worry just code with functions
495:58
Speaker A
eventually your code will get so messy that you will start looking for a better way and probably you will end up with kind of learning more about classes. Now before you leave just make sure to follow along the lesson and experiment with the kind of code I provided you. This will help you get better understanding of classes and op in Python. It's really powerful concept but it takes a bit time to get actually used to this. And also I want to share a quick story that when I was learning
496:22
Speaker A
Python I understood how to write classes right I understood init methods attributes and so on but I could not find any use case where I would actually use them. This is just because I was writing very simple scripts where I did not need that but also I watched a lot of courses videos and it did not make any more sense. So I kind of ignored it and just went on with functions and I haven't learned anything about OOP. it just maybe a few weeks passed by and then one day it just clicked out of
496:47
Speaker A
nowhere and I was like, "Oh, that's where I can use classes." So, I guess my subconscious finally figured it out and just gave it to me. But what I'm trying to say is just give it time. It at first it might be confusing. You might not see much value in it, but over time as your code grows, as you get more comfortable, you will actually come to classes naturally and you're going to love them a lot. But again, focus on the basics first. Now, I wish you happy coding and
497:14
Speaker A
I'm going to see you in the next lesson. See you soon. Hi, welcome back. It's finally time to practice what you learned so far during this Python course. I had an idea that my download folder is an absolute mess. There are hundreds of files and I often need to go back to find something that I actually saved there. And probably many of you have it even worse than me. So, why don't we automate sorting with Python? It will be a great exercise and also quite useful. The goal is to go for the loads folder and sort all files
497:45
Speaker A
depending on their file extension or maybe even certain keywords in the file names. And this way we'll have dedicated folders for images, video, invoices and many other categories. And then I will also show you how to turn this kind of Python file into an executable command from your terminal. It will be a fun exercise. So let's just begin. All right. So let's sort your messy downloads folder. Here's mine for example. I can see it's absolute mess. There are invoices, there are images,
498:10
Speaker A
folders, installers, zip files, and many different file formats. And I'm sure some of you going to have it even worse and comment down below if you think so. Now, to begin with this tool, first of all, let's write step-by-step instructions how we're going to create it. Then we're going to go and actually write the code. So, first of all, we need to get the loads path where all of these files actually located. Then, we're also going to go and read all these files. And actually before we're
498:34
Speaker A
going to read all the files, we're also going to define here some sorting rules. It's going to be based on the file format or maybe some keywords in the files like invoice for example. Uh then we need to get file category and it's going to be based on the rules. For example, if file has thatpng, then it's going to go to image folder. If it does, if it has that MP4, then it's going to go to videos and so on. This is where we're going to define all the sorting rules.
499:00
Speaker A
Then when we're going to have all that, we're actually going to create sorting folders and we're going to move our files into these folders. Then we'll also like to report results. And lastly, as a bonus one, I also going to show you how to create a cmd command so we can run it at any time. So we don't have to open Python, right click, click on run. Instead of that, you will just open your cmd right here something like sword download and loads and then it's going to work. So it's going to be
499:27
Speaker A
very easy. Don't worry. So this is our code skeleton obso code, however you like to call it. And now we're going to go and code it step by step starting with the first step. Get the load path. So we could literally just come here, grab the path and write it here. Actually I like to create here like global variables on the top. And this one I like to make all capital because it's going to be used in many places. It's good to know that this is my global variable. So I'm going to paste that
499:52
Speaker A
one. And when you work with paths, notice that you might have this kind of combination of symbols and letters. As you know, this famous one, the new line. This is going to kind of break our path. To avoid this, just put R in the beginning. It's going to make it a row string and it's going to ignore all the special symbols. And also keep in mind this path is fine for the for local kind of environment. But if you're going to share your code with somebody else or you're going to try to execute on
500:14
Speaker A
another computer, it's going to fail because this is kind of user specific. So for that, we can avoid it by finding it universally. The easiest way would be to use some other packages. For example, from path leap. We can import path. This is a special class that can help you to get certain paths and work quicker. And in there we can write path downloads is going to be path. Then there's home. This is going to point to the user's home. This is going to be like this path
500:40
Speaker A
right here. Then we need to add here also the loads. And when you use this special path class, there's special syntax where we can divide and write kind of the next file directory. This way it's going to combine it. This is a bit unusual syntax and many of you might prefer to kind of do it uh like this. So we're going to take path home. We're going to join it with the loads. Exactly the same result, but this is uses OS path and this uses this special kind of path class. Now I'm going to use the
501:07
Speaker A
universal one. This way all of you can follow along and you don't have to use my specific path right here. So let's add here a separator and an emoji. Now this is going to get us the load path. We can delete it. The next step is going to be define sorting rules. And actually, I'm just going to paste here already prepared categories. And I'm going to explain as we go so we don't waste time here. As you can see for the keys, these are going to be the names of the folders where we're going to put
501:30
Speaker A
bunch of files like images, G, videos, dogs, and so on. And then there are lists of all the file extensions we're going to check for. For example, if it's a JPEG or PNG, it's going to go to images. If it's MP4, MKV or other formats, then it's going to be videos. And so on so on so on. You can also adjust it to your needs or just begin with that one. Then you're going to see all the other files that kind of doesn't fit these rules and you're going to add more. We're also going to do that during
501:53
Speaker A
the lesson, don't worry. So, we're actually ready to go to the next step and we're going to read all the files and for that we will need to import OS package. Let's go on the top. I'm also going to write here import and we need to import OS. Let's make it on the top. And now to read all the files, we need to write for file in and we're going to use OS package. There's list deer and see it's going to return a list of containing the names of all files in the directory. By the way, use control q in
502:21
Speaker A
PyCharm to get these doc strings. Now I just need to provide here the path which is going to be our path. Then loads. Now let's just kind of print each file and see what we get. I'm going to run. You can see I get all of my files. There are like hundreds of them. So it works. Now the next thing we just need to find what is the category of this file. So we're going to work with this category. For that we can just write here for key value in categories items and key is
502:52
Speaker A
going to be a folder name or category depending how you want to call it. Cat is actually shorter. Let's use that. Then it's going to be the list of uh keywords. Now this is a nested loop. So first of all we're going to go through each file and for each of these files we're going to go and iterate for the categories. And now to check for the list of keywords, we need to get for example an extension of a file and check is it inside one of these lists. Can do it the following way. We're going to
503:18
Speaker A
write here file extension. I'm going to take the file name and the easiest way would be to split it with a dot and then take the last result. So let's say that I have here a file name equals eric.frids.mpp4. So if you're going to split it by the dot, it's going to give us a list like Eric Rits and then MP4. Obviously, it's going to be strings. And in this way, we want to make sure that we getting the last result, which is going to be MP4 because well, the file format always in
503:50
Speaker A
the end. This is why we taking the last one. Now I'm going to check if file extension in list of keywords. Then we want to grab the category. example um let's say sorting category equals none first and then here it's going to equal to cat and we're just going to print here the category and then the file name itself so we can just verify that that it works correctly let's also remove that one here because we're already printing file name here oh and it's also important to
504:21
Speaker A
break it the reason is let's say that you take a file you start iterating for the categories you take that one you take that one then you find in the videos that your file was that move so you No, it's videos. But if you're not going to make break, it will still keep kind of checking all these categories, which doesn't make sense because you already found the result. This is why we need break. Now, let's just run and see. I'm not getting anything. So, we have messed up
504:44
Speaker A
somewhere. So, let's print file extension. Oh, and the the issue is because I'm getting all these extensions, but here it's all with the dots. So I can either kind of go through and remove all the dots here, but to be honest, I would rather keep them. So instead, we're just going to go and fix this one right here. So it needs a dot in the beginning. So let's just add here dot plus whatever is the file extension. I think now it's going to work. Let's run. Yeah, now it works. You
505:16
Speaker A
can see docs for the PDF. The JPEG goes to images. Then MP4 obviously goes to videos and uh CSV goes to data and so on. So so far it works. We're getting the right data. Uh we also going to put it inside the function to make it a little bit nicer because it's getting a messy here. So let's define here a function get file category maybe and then here the file name. Now for that let me just think about it. Okay. So we're going to probably take the whole thing. We're going to put it inside here. So
505:54
Speaker A
getting a file name. Then we setting sorting categories none. Then here instead of file should be file name. Then we iterate for categories. We check this. And well the print statement we don't need anymore actually. And instead of break we actually going to return cat because this is for one file at a time. So there is no need to kind of keep going and iterating. Now, PyCharm doesn't like that explicit return statement expected. So, it wants me to write here return false or return
506:23
Speaker A
none or something like this. Well, if I spell it correctly, then it's going to be fine. I don't really like it because it's pretty much the same result here. So, we're just going to ignore it. Now, for file name in this category is going to be get file. Category file. Now, let's also print it. Just make sure we're getting the right thing. So, cat file run. Okay, our function works. We're getting what we want. And it's also much nicer. Now, we can also create here a section for functions.
506:53
Speaker A
Let's separate it with this one as well. Let's drop an emoji. That's fine. Now, actually, now that I'm starting to think, we're not always going to actually get to the end. We just write here print file name. So, the only way this print file name is going to work if we're not going to find any categories. Now before we run just make sure you disable this print statement because it's going to make a mess otherwise. Then execute and see I'm getting bunch of different file. There's just autoes,
507:20
Speaker A
there's PDF, there are a lot of actually folders as well because you can see this is a folder. It doesn't have any extension. This one doesn't have any extension. This is a Figma file. So we're going to update some rules as well. Oh, and another thing there are also capital extensions. I don't know why it has here CSV and PDF. I downloaded from somewhere but obviously it also works. So let's keep working this file extension to solve all this issues. So first of all I want to
507:45
Speaker A
actually make a separation if you're working with folder or a file because that's an important one. Let's add some emojis so it's nicer to see. Now we need to say how do we actually check if it's a folder or not. So obviously we're going to use OS package. Then there is OS path. There's is file or there's also is dear which directory which stands for the file. Let's put here the file name. I'm going to print its folder and otherwise going to print it's a file. Let's add a here as well.
508:18
Speaker A
So let's just see if it works. We're going to run. Can see it's a file. File. File. File. Alo. When I look here, you can see that I think this is a folder, but it still says that it's a file. So I think it never says it's a folder because if I'm going to comment this one out and run it, it will never say it's a folder. Even though these are all f not all, but these are folders. That's because here instead of file name we need a full file path like absolute path
508:42
Speaker A
for that let's write here file path and we're just going to use the old OS path join and we need to kind of add the downloads in this case it's very simple because we're using um we're kind of going through the loads folder so we can just merge these two paths and now if I'm going to replace that one and run it's supposed to say it's a folder it's a folder it's a folder so it works now I can unlock that one Run it. Now it says it's a file. It's a file. But this is a
509:11
Speaker A
folder. Now it works. So we have separation between files and folders. Also for folders, we're not going to do anything. Uh because with a folder, you would have to look inside and there can be many images, there can be installers, there can be many things happening. I think we're not going to sort folders. We're just going to put all folders into a folder folder. You would just return here uh folder and then it would be just like one of the categories. You could also do that.
509:38
Speaker A
Maybe we do it later. For now, let's just ignore it. Then if it's a file, we're going to work this one. Uh so you see this one is grayed out. It's not used anymore because we returning it directly. So we're getting our file extension. Also in this part when we kind of uh looking for category, we might not always find it because we might not have enough rules or maybe there are weird files. So here we might return something like uh others. This is going to be the folder where we're going to place
510:09
Speaker A
everything that we're not even sure where to place. That might be a good thing to start and later we're going to define new rules. Another thing let's also update this print statement so it looks nicer. Just going to write that this file extension is not supported file and then the file name so we can kind of check it what's going on. And another thing is that we need to make sure that this one is lower case because if I'm now run it oh make sure return is after print
510:34
Speaker A
statement. So we actually make it. You can see for example here CSV is not found. PDF not found because these are the capital letters. So we need to fix that. This is very easy. As you know there is lower method which is going to make everything lowerase. And now you can see they are gone because they found it. There are a few more files here. So, let's go back and I'm going to add a few more. I can add here something like design. This is going to be my Figma files. I also going to write maybe
511:04
Speaker A
Photoshop and maybe SVGs. Another one is VTT. This is the special um file format for the subtitles. We need to download it from Vimeo. So, let's write sub titles. Often get it to bring it to chat GPT and stuff. So, we're going to write VTT. And don't forget to put here the commas. Now it's all good. So now when I execute, I'm only going to get this random which is actually a good. I'm going to have one file which doesn't fit the category. So we actually going to
511:31
Speaker A
return others. And we're still going to report that something is happening. It's not supported file. Going to write placed in others. Well, it's not placed yet, but we will place it there. Let's run it. You can see blah blah blah placed in others. That's good. So I think we addressed the main issues with the function. I know it's a little harder to read now, but firstly we checking is it a folder? Is it a file? If it's a folder, we could also write here that it's supposed to be a folder.
511:59
Speaker A
Actually, let's do that. Maybe we're going to kind of sort folders as well. And in here, we check in the file. We need file extension. We look for the category and we report if something is not happening, which will be a good idea. I'm going to go a little bit ahead of myself, but these are going to be also the folders. So the first time you run it, we're going to create them. We're going to put the files there. But the second time you're going to run it, they're going to exist. And we might
512:21
Speaker A
kind of find, okay, they actually exist. We're going to try to put these folders in f it's going to be a mess. So be careful with that. This is why I put here all these symbols right here because we need to differentiate your sorting folders from all the other folders that you already have in the loads folder. This kind of steps, they are not always obvious, but usually you're going to find them during development. This is what I did while I was preparing for the lesson. Maybe I
512:42
Speaker A
should have recorded it to show you how it actually happens. But yeah, I nearly kind of messed everything up. Then I realized, oh, I actually need to separate these files from the rest of the files before I start moving folders. So I was lucky I did not mess up anything. But this is why I have this kind of underscores. In general, in Python, this underscores means that do not touch it in when you use it in properties, in the names of the methods and so on. So also going to add here another step,
513:08
Speaker A
ignore sorting folders. I'm going to write if file name then there is a great method start with and we're going to check if it start with a underscore then we're going to kind of ignore it or return none let's put here an x so we ignore our sorting folders here what we do with regular folders and then with the file now this function is ready we can kind of collapse it here in PyCharm and let's test it one more time and you can see it works fine we're getting all the data we need for now so we're
513:38
Speaker A
getting our category based on rules could also add the dock string to the function. Actually to the dock string you usually create triple quotes. Pycharm also can help you if you're going to expand it. It's going to automatically add this statement. It's going to write and here you can write some description. So file name is going to be file name inside of downloads folder returns name of sorting folder and then here find sorting folder by name. Okay. Okay, now it's a little bit better
514:10
Speaker A
documented. And a good thing now we can also reference from here. Ctrl Q. I'm going to see find sorting folder. Here's the file name. Here's return. So it helps PyCharm be aware of what's going on. Now next we need to create sorting folders because now we have the category for the folder. We have the file. So now it's time to sort of create folders or place it inside. Also I want to change this name because now it's not just the category. It's going to be directory
514:33
Speaker A
name. Can write a dear name or sort dear name, whatever is less confusing to you. So I'm going to keep it like dear maybe like this directory name. Now I'm going to write if we get the directory name then first of all we need to check if if it exists or not. So if OS path there is something like exists and here we need a full path. You can see PyCharm also suggest us that we're going to join path downloads and this one I would rather keep it separately. So dear file path equals that we're
515:04
Speaker A
going to check if it exists then do nothing. But actually if it doesn't exist then we can write OS make deers l deer file path has the multiple one because you can kind of create lots of nested folders like for example downloads Eric Eric Eric and so on. It will create all of them. Now this way we know that we definitely have a sorting folder created. Actually can run it and check if it works. No errors. And if I'm going to go to the loss you can see all of these folders appear now. dogs
515:36
Speaker A
folders. This uh this one actually let's delete. Let's call it folders. Where is it? Should be in now our function somewhere here. Folders like this. Let's hide it again. Back to the code. Now to move files, we actually need to make an import. Let's go. And we're going to import sh util. I think it stands for shell util. Uh the point is that there is a function to actually move files. Let's go. It works very simple. This sh util see there's move there's going to
516:10
Speaker A
be old path new path. Now I just need to define this old path was this and new path equals this. It's good to separate it so later we can also report the results otherwise we're not going to kind of save it anywhere. Uh for the old path it's easy. OS path join path downloads and then we're going to join it with a file because we're iterating through the file as you remember. So we can use it here and for the new path it's actually going to be the same but here in between we also
516:50
Speaker A
want to add the sorting name folder which is going to be let me see dear name. Yeah. So this is the old path. This is the new path. That's fine. Also, we could instead of that one just use uh path downloads divided by file. As you remember this one we make with a special class which comes from this path path and we did it right here. So we could also do that. I just like to show you multiple ways. Whatever you prefer. Just know that this one is very commonly
517:21
Speaker A
used. This one is not as commonly used but it works great as well. Uh this is move files and we're going to define old new paths. For now we're just going to go and test. So we're going to run. Uh we did not do any reporting which is bad actually. But now let's go and check. I can see I only have my sorting things. So here's the code. I'm going to go to design. There's Figma file. In images there are going to be lots of different images. In videos, lots of videos. So far it works pretty good.
517:55
Speaker A
There's format so on. Let's go to folders. Here are all the different folders I had. There are mainly different folders from fingerverse for 3D printing. All right, that so far it works. But we do not have any reporting. And actually that would be really really good. So let's kind of report all the results. Oh and also I forgot to actually show you but whenever you kind of write any script with Python always try to break it. always think how can I break every single step that I have
518:22
Speaker A
because if you don't then one of your users will in the end or maybe you will do it later when you don't really want to fix it and one of the use cases for example let's grab some I don't know maybe in data some CSV file for example I'm just going to grab one of the random things and I'm going to bring it out imagine that one of the files is actually open let's keep it open and now execute the script so we're going to run it and we're going to get an error obviously we
518:48
Speaker A
cannot kind access the file because it's being used by another process. It makes total sense. Now to avoid it, we can write here try and accept statement. Just the basic error handling. I'm going to write errors run. You can see we're catching the error. Obviously, we can make it a bit nicer. We're going to write here as exception as e. I'm just going to replace this message with something like this. So, I have my directory name, the file name, and also the exception
519:16
Speaker A
itself. Let's run it. Now we can see oops reviews CSV the process cannot access the file because it's being used by another process blah blah blah. Now that's good. We're kind of handling the error. Another good thing is to report actually successful results. And for that I'm just going to add something like this because you see if I'm going to run uh I need some files. So let's go add images. Example, we're going to grab all the images, cut it, place it here. So
519:48
Speaker A
now when we run it, we have some files to play with. You can see all the images were moved like this. At first, I used to make print statements like this. So there was like old path and new path. But there is a big issue with that. Well, not big, but I did not like it at all. Oh, if I'm going to go to images, bring them back again and run it. You can see becoming a mess because you have two very large file paths before and after. you can barely keep track. Okay, this is before, this is after or
520:17
Speaker A
otherwise. Well, it's a little bit too much. Therefore, I like to use just this kind of simplified version. Just very short the folder name and the file name because we only work with the lowest folder. Let's run it. Obviously, we need to bring back some files. Let's bring back the images. This time, I actually copied because I don't know what's going to happen if I'm going to override it. I think it's just going to override it. So, keep that in mind as well. And see, nothing is here.
520:46
Speaker A
No duplicates here as well. So, it's just going to override the files, I guess. But yeah, so far it's pretty good. Now, I can see we're getting an error. So, we will need to separate it. We do this a bit later. So far, it actually works. Let's just check it once more. And you can see that well, it's very nicely organized. Now, if I need to come back, I'm like, okay, I need to find an image. Boom. All the images. Maybe I need some data file like CSV. All it's
521:14
Speaker A
here. You may need to go to documents. And here are all the documents. Oh, actually I have another great idea. Contain documents. I can have here invoices, receipts, some other files. Huh? Text file is here for some reason. I think we haven't added it. So this is actually something I want to separate. I want to have all my invoices, receipts in another folder. Now we can easily do that. Let's create here uh invoices. I'm going to add here the invoice uh receipt. What else? I live in Austria.
521:51
Speaker A
So it can also be renong which is invoice in German. And don't forget your kind of commas after the new list. So on now let's just see if that's going to be enough. just going to kind of take everything, put it back here. We're going to run and everything goes back to docs. So, we need to figure out why and how to fix it. Obviously, it's going to be in the function because that's where we define the category. We interested what's going on here. Right here, we're getting our extension name.
522:27
Speaker A
And then we're going to go and iterate through. And this is a little bit an issue because well, we just check the extension. Is it PDF? Is it text file? Is it a doc format? Doesn't matter. But we want to check actually for the keywords. We need to change the logic right here. So there are many different ways to approach it. I think we're going to do it as following. So we're going to write here option A for file extensions. Can also collapse the comment in the
522:54
Speaker A
PyCharm. I'm still going to open it. Actually, let's copy that part here. Now I'm hide it. Paste it here. And we're going to uncomment it. So this time we're going to iterate for category list of keywords in this. But then we're going to flip it. We're also going to write here for keywords in list of keywords. Then we're going to check if keyword in file name. So we don't need that part. And if it is, then we're going to return category. Here's what's going on. Before
523:23
Speaker A
we sort of took the file, we took the file extension and they check is the file extension here? No. Is it here? No. Let's say it was MP4. Is it here? No. Is it here? Yes, it's MP4. Since it's MP4, we're going to get the folder of videos. And now we're kind of flipping it. Let's say that we have here some kind of file name. We're going to take the first one and check is invoice in the file name, no. Is receipient file name? No. Is Gong file name? No. Okay. Then we're going to take the next list
523:52
Speaker A
and check is it JPEG there? Is PNG there? Is MP4 there. So we're going to kind of flip how we check it. Now we're checking all this list one by one. This is a bit more flexible because now we can include various words like invoice, receipt, renong and obviously the order here is important because first if you're going to find invoice it's going to stop execution. If docs is going to be above that for example somewhere here then it would first get the docs and never reach the invoices.
524:18
Speaker A
So think about that as well. Now I think that's enough. We're going to see how it works. Oh, also it says that file extension not supported. Let's leave this one out. So, it's going to stay here. I just bring it right here. Okay, now this works. So, we can try to execute it. Let's go back to downloads. Then in documents, let's just bring it all back. So, we have all the PDFs here. We can test it now. Run. Aha. I can see it works because I It detects invoices. It detects docs
524:58
Speaker A
doesn't detect PDF because I forgot to make it lowerase somewhere. Let's go back file keyword file name. So I think it should be here. So we're checking if the keyword in the file name lower. And also make sure that this one is lowerase as well. In case we write here something with a capital letter, it's also going to be fixed. Now let's run once more. It should fix issues. Well, first we need to get some files. because I think it went to others. Yeah, you can see there are two files which
525:30
Speaker A
had the PDF in the upper case. We bring it back. We run it and I can see we move them to the documents correctly because now it's all lower case when we check. All right. So, so far I think it works great. I really like how it looks now. It's very organized and it's going to be actually very helpful. Now, let's turn it into command because well, I don't want to always kind of look where I save this sort file. The way we're going to do this, uh, we're going to copy everything inside
525:58
Speaker A
the file and we need to create a new location somewhere on your computer. And for that, for example, we can go to, let's say, documents. In here, you can create a new folder and call it Python automations or maybe EF Python automations depending on your initials. Just make it very clear that it's your file automations. I already have it right here. Let's go inside. And inside I prepare there sort and loads and sort and loads.bat. I'm going to explain what it is in a second. But
526:27
Speaker A
first, let me actually delete this one. I'm going to create it a blank empty. Sword and loads.py. Open that one. Paste all your code that we've wrote so far in the first one. Now, we're going to keep working here. First one, well, doesn't matter where it was located. Now, let me just make it side by side like this. The next open the cmd. Just write cmd in the search. It's going to open you like this. Let me actually reorganize it a bit. So it's like this. So one of the ways we could kind of run
527:00
Speaker A
it. We just need to grab the path right here. For example, Python. Then in quotes you can insert your path like this. And make sure that path includes the file name. So sword.py. Run it. You can see we execute the file. It moved all the files. everything disappeared. So we executed this file correctly. However, you're never going to remember to write Python this full file path and so on. This is going to be quite annoying. So instead, we're just going to write cls to clear it. I'm
527:32
Speaker A
going to write something like sword and loads. How do we do this? For that we need to create that bat file. This is exactly what it's here. Let me just edit in the notepad. Now inside this bat file, I only have three lines of code and it's very simple. Also, in general, this file is the file that's going to execute everything inside the terminal. So, it's going to go line by line and it's going to be like echo off. Then, we're going to write cd this. I'm going to explain
527:56
Speaker A
in a second what's going on, but then we're going to write this and then it's going to execute it every time. So, now we just need to execute this file through the terminal. Then, it's going to work. Also, let me explain what's going on with these commands. And I might need to open a new cmd for that because we broke a few things. I want to explain what's going on. So let's start here. So first of all we need to set the current directory to our Python automations folder and it's like this cd
528:22
Speaker A
and whatever path is you can see it changes here because before it was executing from the system now we are inside this folder and then we can actually write python this one. See that's how it works and also the first one is going to make sure that nothing is displayed in the console. So for example, if I'm going to write this line a few times, you can see I'm writing over and over and over with cls. I can clear. I'm going to write echo off. It refers to do
528:48
Speaker A
not print anything, right? We don't have anything from the terminal itself. Then we're going to say cd should be this. And then python should be this. See, we only see the commands. We don't see any return statements of the terminal doesn't update, but we get results from the Python code. And this is all that's going on. Just copy this also ask chbpt if you're kind of creating something different but that's enough but then there's another big issue let's open a new cmd and now the
529:16
Speaker A
idea here is that I just want to open it at any time and write sort download then loads like this it's going to say it's not recognized as internal external command blah blah blah and way to fix it because I don't want to always depend on the path where I am because otherwise I need to manually kind of set the path and it's not nice instead we're going to open kind of start menu right here environments. You need this edit system environment variables. Then look right here environment variables
529:44
Speaker A
and inside go to path that one. You just need to double click and these are the default paths that your Python and terminal is going to be aware of. So if I'm going to write some command for example sort downloads, it's also going to check is there a command in any of these folders. You can see there's also Python. So when we write Python command in the terminal, it also knows that okay, maybe if I look here, I'm going to find Python and it does. So all we have
530:08
Speaker A
to do is grab the file path and add it here like this. Can see here's the new one. Make sure you add it to this path. So let me try again. Environment variables. Double click on this path then new. Make sure you have here path addit. Then okay. Okay. Okay. Now, if I'm going to write exactly the same command, uh, it's actually doesn't work. But I think I need to open a new cmd this time. And if I'm going to write it here, downloads. This time it works. When you
530:39
Speaker A
write this sort and loads, it actually executes this one's sort and load file that bad. And now lastly, before we're going to end this lesson, we're also going to format the code and fix the print statements because it's not enough. It's really good to report what we're doing, how many folders, how many files, what was moved, and so on. So, let's open it back. Since we are formatting this, we also going to make some changes. We're going to write here main. I explained it in the lesson about
531:03
Speaker A
modules what it does. It makes sure that it's only executed from this file and we're going to grab what we we're going to grab everything here. We're going to make it a function. So, Ctrl X and I'm going to write define sort loads and paste it back. And now we're going to execute this one from here. This is going to be main. Let's make it a target like this main. This is going to execute the code. And this is the function. It's very common way to kind of put everything you do
531:40
Speaker A
inside the functions. It's going to make it much nicer and a little easier to kind of control. And here before we're going to begin all of that, you can make a lot of print statement. actually example let's report how many files and how many folders we have in in downloads before we start sorting so we're going to get all files and for that we're already kind of using this one just going to replace it like that then for folders going to use list comprehensions and for files we're going
532:09
Speaker A
to use another list comprehension write file for file in all files same here and then we need to check is it the folder or file. I'm going to write if OS path is dear. You can see I already have suggestion. I'm going to accept it. So I'm checking is it dear and I need absolute path. So I also write OS path join downloads. That's too complicated. What's going on is I create uh folders. Then for file in all files then we're going to take that one. It's going to be
532:41
Speaker A
file path equals this. And then EOS path is there. file path then folders append f. These are exactly the same things but this is in one line. It's a little bit packed but it's still kind of readable and this is kind of ex expanded exactly the same result. This one is going to be a little faster though. Can see for here is the loop which is this part right here. Then this return statement is going to be that one because this is what's being added to the folder. Then
533:12
Speaker A
this if statement is right here. And the only thing we also created a variable by taking this path and creating it as a file path. I prefer kind of oneliners. It makes it much nicer. And a good thing that also we're going to duplicate it with Ctrl D. Make it files. And all we have to do is write if not because it's the opposite. Now we're going to make print statement. I'm just going to paste it. Scanning downloads folders folders found. Let's try to execute it because now we can
533:42
Speaker A
write just sorts in the cmd. So scanning the loads folder found 12 folders. Now also we need to exclude our special folders. Oh it's going to get messier but please bear with me. So so we're checking if this this is one statement and we also need to add another one and um f starts with underscore make it not. So let's break it down. So we're creating a loop. We're checking if uh this file getting the absolute path here is a directory and it does not start with this underscore then we're going to
534:16
Speaker A
count it as a folder. Then here we're going to count files and actually we forgot the print statement like this. Now let's open another cmd sort downloads folders found zero fi files found one then the issue comes here just going to speed up a little because just the print statements here for example you saw that we had only one file but sometimes it can be zero for that I'm just going to make an kind of if statement if we have any files at least one then I'm going to
534:45
Speaker A
write how many files found and I'm going to write that I'm starting to sort otherwise I'm just going to write files not found and nothing to sort here. Let's also finally close this Excel file. It's already annoying. So, cmd uh sort downloads scanning the loads folder. Folders found files found. Sorting. And we're going to move that file. I'm going to run it again. Files found zero. Nothing to sort here. Great. That's already usable. Now when I'm going to go to downloads bring
535:19
Speaker A
for example all my bring for example all the documents here then run it you can see scanning downloads found 39 files and sorting then I can see here where everything went see dogs to invoices and everything works pretty nice I also like to have a final summary how many files we moved and so on and also it's good to maybe kind of count the errors as well. Uh so somewhere here on the top we're going to create placeholders. We're just going to create for count and fails
535:56
Speaker A
going to be zero and zero. You can also create count zero fails zero. But we can also combine it. And another one is failed messages. It's going to be a list. You'll see why in a second. And actually let me demonstrate right away. We need to get our CSV back. Let's get the reviews. Put it here. Open it. So this is going to give us an error. Let's also add some images. Now I open cmd. I'm going to write sort download then loads. You can see the issue. First I'm adding
536:29
Speaker A
here then somewhere in the middle I get an error. Then I keep adding. I would like to errors be separately in the end because this is really important and I wouldn't want to miss it. Therefore we creating here these uh failed messages. We're going to capture it and print in the end. Then for count and fails, we're going to go to where we move it. We move it here and going to count it here. Plus equals one and fails. I'm going to count it here. Fails plus equals 1. Then I don't
536:59
Speaker A
want to print it here. Going to grab this message. And there was failed messages. Append this. So we're counting it. And so on. Now we can kind of go and in the very very end we can make print statements. Just make sure it's not part of any if statements or so. So it has to kind of follow this line right here. So it should be something like here going to write if count. So if we moved any files then we're going to write that successfully sorted this many files.
537:31
Speaker A
And then if fails I'm just going to paste it here. So we're going to make lots of dashes. We're going to say we we failed to sort certain files and then we're going to display all the messages. Now it's going to be everything super structured. And I think this is the final refactored code. We changed a lot and now we're going to have way better examples here. We're going to bring all these files back. So let's just kind of bring images out. Let's bring invoices out, videos out.
538:03
Speaker A
There's already a mess. Others, let's bring it out as well. zip files. Why not? Okay, I think it's enough. You can see it's already a mess. So, the idea is that at any time we can just open cmd and we can just write here sort downloads. It's going to do all of that. And you can see all files disappear except for one obviously. And now we have reporting here. I'm going to make it like this. Let's go all the way up. So, scanning downloads folder. There are zero folders found.
538:34
Speaker A
And here 153 files found. Then here's the sorting process. You can track everything what's going on. And then successfully sorted this many files and failed to sort one file. And then we're going to have here an error message. And actually something is wrong here. We made a typo here. It should be failed messages. That's okay. Now let's see if we fixed the error. All right. I brought a bunch of stuff back. Even some folders. As you can see, now is the true test if it all
539:05
Speaker A
works. So, all we have to do is write sort downloads, knows where the file is. It sorts the load folder. And if we're going to check it, that already looks better. So, found eight folders, found 37 files. Then, it's going to go through sort everything. Then, it's report that it sorted 144 files, and then it failed to sort one file. And then it's going to report what actually happened. Because if there would be more issues, it would say, well, fail to restore 20 files.
539:35
Speaker A
Then here are the errors. For example, this file had this error message. The process cannot access blah blah blah. And then you're like, oo, it actually makes perfect sense. Yeah. So now it's automated at any time. I can come back, write sort loads, scanning, found, blah, blah, blah. Sorting. Well, in this case, there's an error. Couldn't sort it. Let's close it and try it again. Now sorting. No folders. Found one file. Moved it. Successfully sorted. Sort it again. Nothing found. Nothing to sort
540:08
Speaker A
here. All right guys, and this is how we sort our files in downloads folder with Python. So it's not that complicated to create this automation. But it's actually very rewarding once you're going to create it and have this amazing tool. You can kind of open cmd at any time, write sort download and it's going to everything for you. And you know how it works. You can control everything, create your new rules. It's going to be amazing. And also let me know in the
540:30
Speaker A
comments if you enjoyed this lesson, if you created this tool, and if there was anything complicated. Maybe I can help you in the comment section if something was confusing. And then as always, I wish you happy coding, and I'm going to see you in the next lesson cuz we're not done with the course. There are a few more examples I want to show you. See you.
540:52
Speaker A
All right, welcome back. In this lesson, I'm going to show you how to connect AI to your Python scripts to create awesome AI automations. We're going to start with Open AI platform and then I will quickly show you how to use Deepc Platform in the end as well. This is also a necessary step before the next lesson where we're going to create an awesome AI automation project together from start. So, let's just begin with the basics in this lesson and I'm going to show you how to supercharge your
541:16
Speaker A
Python with AI. As I mentioned, we're going to start with Open AI platform. So just go to Google or whatever you use and write open AI API and you want to end up on this website API platform from OpenAI. Uh you would like to click here on start building or login if you already have an account. You can already see the developer platform. There's a lot of documentation some snippets but we actually want to sign up because we will need to generate an API key in here.
541:40
Speaker A
Just use one of your emails. Then it will ask something about you and then you're going to go through a little bit of an onboarding process. Then in organization name I'm just going to write Python click. This is the website where you can also get an ebook for this whole course. Then what describes you? Well very technical little technical doesn't matter. All right we're not allowed to use points. Then I'm not going to invite anyone here. And then here it tells you that you can create
542:03
Speaker A
your first project and generate a key. A key is like a password to connect to the platform to actually use the AI. For the key name you can write whatever you want. For example, Python course and project name. Well, I'm just going to leave it also default project. It doesn't matter. and then click on generate API key. Then you can see right here is the key and it's called a secret key for a reason. So make sure you copy this because you won't be able to use it later on as well. Then let's click
542:26
Speaker A
continue. And to use Open AI, you actually need to add some credits to your account. But here you can see that $5 can cover about 2 million inputs or half a million output tokens. So you can either purchase credits now or you can also do it later. And then here is going to be very simple code sample that you can use. But don't worry, we're going to cover it during the lesson as well. I'm actually going to quickly switch the accounts because I already have an
542:49
Speaker A
account with some credits on. It will be much better for this lesson. All right, I just changed my account because in here I have some credits. Now I'm going to click on search and actually need to grab another API key. I'm going to come here and here I'm going to click on create new secret key. Going to call it EF Python course. The project for me doesn't matter. It's going to be default project and I'm going to create secret key. And notice that it's called secret
543:13
Speaker A
for a reason. You're not supposed to share it with anybody and you're not supposed to expose it in your code either. And it even tells you that you need to save it somewhere secure because you won't be able to view it again. You would have to create new key if you lose it. Let's copy that one. And I'm going to come back to our code editor. And for now, I'm going to keep it right here. Open AI API key. Whatever. Just going to paste it here. But keep in mind, you're
543:37
Speaker A
not supposed to expose it like this in your code. There is a workaround to keep it safe. And also we're not never supposed to upload it anyhow to the clouds or GitHub. But I'm going to show you in a moment. First of all, let's just come back to the platform and just explore a little bit. First of all, there's a developer quick start. There's a code sample right here. You can see it's very very simple. That's literally all it takes to ask chat GPD. Also, right here, you can see all the models.
544:00
Speaker A
So, I'm going to click on all models. You can see there are bunch of different models here. And there are some reasoning models which are really expensive, but they can give you advanced kind of replies if you have a lot of data. Then there are also cost optimized models. This is exactly what we need for this lesson because we want to choose very cheap models. You can click right here for the pricing. And here you're going to have a list of all the kind of prices. For example, you
544:22
Speaker A
can see that 4.1 takes $2 for the input, eight for the output. And this is quite expensive. Now let's find our mini, which is this one. This one is very cost effective and nano is like extremely cheap. And also if you're going to scroll through you will even notice that 01 pro for example it takes 150 for the input 600 for the output. This is how much more computation is involved in this kind of reasoning models. We're not going to use them. We're definitely going to use something like mini and
544:49
Speaker A
nano. You can also go and find the benchmarks. For example, we're going to write open AI GPT 4.1 mini versus and let's say deepseek chat v3. Let's say we want to compare these two models. There are a bunch of different websites where you can find the studies. For example, let's just go for the first one. I'm not recommending to use specifically this website. There are plenty of them. But you can see that there is comparison between two models. Here's some kind of
545:14
Speaker A
model overviews. For example, you can see it has different limiting tokens. There is also uh different prices right here. By the way, this one is wrong. It's actually 110 of a dollar. But you can see it's not that bad cuz when Deep Seek first came out, it was I think 28 times cheaper than Chad GBT. But Chad Gypty kind of addressed it with mini and nano models. And most importantly, you can scroll through and find the benchmarks. There are different tests how they kind of
545:38
Speaker A
test models, but you can see here in the percentages that it's not so bad in comparison to Deepseek. It's just a little bit better here, a little bit worse there, and so on. And also in general, we can see here different prices across different models. But this can help you decide on the right model. Now, let's go back to the platform, and again, we're going to go to overview. We're going to grab this piece of code right here and copy. Now, let's go back. I'm going to paste
546:03
Speaker A
it here, but we're not going to use it for now. Let me just comment it out. First of all, I want to address the key. As I mentioned, you should never paste the key like this. Instead of that, we need to create a new file and call it env. That's a special file used specifically for storing API keys and other kind of variables you don't want to expose in your code. And most importantly, you should never upload this to GitHub or anywhere else. You keep it locally for your project. This
546:29
Speaker A
way, you're going to secure whatever keys you need. So we're going to paste it right here. So variable name, we don't need any spaces. We can keep quotes and put here the key. Now to load it inside the Python, we actually need to install a package. So let's go to settings, Python interpreter plus, and the package is called env. I'm going to install that one. Even though it says depreciated package, it works just fine for me. Alternatively, you can also install.n Python in case
547:00
Speaker A
this one gives you any trouble. But I'm just going to go with this one. This is more than enough. Oh, and also we can install Open AI package. Just type OpenAI. Find that one. Click on install package right here. Now it's installed successfully. Now we're fine. If you're not using PyCharm, then just use the terminal pip install OpenAI. You know how to install packages by now, I'm pretty sure. Now to get the key securely in our code, let's make here an import from env import there is
547:29
Speaker A
load.env. NV also need import OS and now to get API keys we need to first of all execute this function what it's going to do it's going to kind of parse your project look for that NV uh file and then read all the variables in your system so you can get it with the code later on we're going to write open AI key equals and we can use OS get environment and just provide exactly the name you have right here and now we're going to read it so print this key. This way we're not exposing
548:01
Speaker A
key in our code and we are still getting it whenever we need it. So that's why we have to kind of separate our keys from our Python file. It's really important. Now we have the key and let's prepare actually an AI message. I'm just going to paste here and I prepared like this. So I wrote here a tone. I wanted to speak like an old pirate who just woke up and hit his toe on the side of the bed. Let's just make a little bit fun. Then for the question, I'm going to list
548:25
Speaker A
the 10 tallest buildings in the world. And then I'm going to combine them into a single prompt message. So for the style, it's going to be the tone. And for the message, it's going to be the question. So I'm just going to merging it together. So it looks like this. This is what we are sending to chat GPT. Style speak like an old pirate. Message list and tallest buildings. That's fine. You don't have to do it this way. You can just write here message tell me something, right? It doesn't matter. I
548:48
Speaker A
just try to keep it a little bit more fun. And I think that's going to be just fine. Now let's get this piece of code from OpenAI uncomment it and we're going to use it in just a second. So what's going on here? We importing OpenAI from OpenAI package. Then we're creating a client. Inside the client we need to provide our API key. It's going to be this open AI key that we are getting right here. We can also move it here. So it's nearby. Then to get a response we're going to
549:18
Speaker A
use client responses create. This is a simplified version. In the model with control space, I can ask for suggestions. And here all the bunch of different models I can use. You can see there are a lot of them. We're going to go with 41 mini or 41 nano. I'm going to go with mini. And then for the input, I'm actually going to insert my message right here because I want it to speak like a pirate. Now, we're going to print how we asking chat GPT. And then we're going to get
549:45
Speaker A
the response like this. Let's just execute and we're going to get an answer. So asking AI speak like an old pirate blah blah blah list 10 tallest buildings in the world. It's going to take a few seconds because it need to send request to chat GPT then it need to compute all the answer and then it sends it back and it doesn't feel that long in a chatbt because you can see it kind of by chunks like each word is being written but here we need the full answer. Therefore we need to wait and
550:07
Speaker A
here we go our blast me to be screaming like a bunch from the deaths but here be the 10 tallest buildings in the wide seven seeds of the earth matey. And then we have our 10 buildings from our angry pirate and it's actually quite fun. So BH Khalifa is the first one. 828 meters all steel or glass the crown nest of the world. I really like how it's written now. So this is the simplest way we can use the chatbt and that's fine when you just want to get a simple answer to a
550:35
Speaker A
simple question. You can add some kind of don't question. In reality it's just kind of one uh text I'm providing. I just kind of broke it down to separate it because we're going to need it in a moment. However, if you want to kind of keep the conversation going, you need to do a little bit different syntax because you will need to uh send back all the context that you talked about, right? So, you have to create a history of your chat. What did you what did you write?
550:58
Speaker A
What the chat replied and so on and then send it back every time. Now, let me make it a bit more compact. It doesn't need so much space. We're going to comment it out. So, instead of that, we need to use a little bit different syntax. We're going to use the same beginning. Actually, it's a comment uncomment that one and bring it back on top. So, we're going to create a client. Then for the response, we're going to write client chat completions. Then create. Then inside again, we need to
551:25
Speaker A
specify the model which is going to be the same one as this one. And then we need to provide here messages and it has to be a list. And then inside we're going to put a dictionaries. So there is a role. It can be a user going to explain it in a second. And then the second one is the content. And this is going to be your actual message. Mes messagees message here. It's going to look a little weird, but don't worry, it's going to get fine in a second. The reason here is that first of
551:52
Speaker A
all, there are three options. The role can be uh system. This is will be great for our tone. This is we can specify how we want it to interact with us like pretend as a software developer, pretend to be a pirate and so on. Then user is going to be our questions, right? So we're going to write a question here. Then there's also assistant. This is what we getting from the chat GPT. itself. It helps it to kind of keep track of who said what. And here, for example, in the RO system content, this
552:18
Speaker A
is where I want to put my tone. And for the tone, it's going to be this one. Speak like an old pirate. Blah blah blah. Then right here in the user, it's going to be the question. And for the assistant, we don't have any messages yet. We're just going to send it like this. We also need to read it a bit differently. This time, we're going to print responses choices zero message content. This way we're getting the latest message. Now we're sending exactly the same
552:44
Speaker A
question. Just let's see what we're getting. All right. And here's pretty much the same answer as before. It's a little bit different, but you get an idea. Now, to make it an interactive chat, so we can kind of keep chatting and we can still keep the content of the chat, we need to create a while loop, and also we need to create a history of these messages and kind of keep updating them. For that, let's modify that. First of all, here we need to create a history. And in the
553:12
Speaker A
beginning, we're going to put the tone inside there because well, it always has to be the first one. Then for the second one, it's going to be our question. So, we're going to put it here as well because first of all, we need to send it like this messages equals our history because history also going to include our latest question. It's going to reply and later we're also going to add uh more context. What who said what? Now let's turn it into a while loop.
553:39
Speaker A
So it's an infinite. We're going to move it like this. Let's save some space right here. It's not necessary to break it down. Now first of all, user input. We need to ask something. So user says this. Then what I want to do is I want to add this one into here as a question because user is going to ask something and it's going to be right here. So it's supposed to be history append then this. So user content is going to be this. Then we're going to ask for a response.
554:07
Speaker A
We can create output is going to be uh this one right here. Then we're going to print it in the console. AI says this. And with the f string, we just put output here. And lastly, we need to again add to the history. This time we're going to append. Let me just copy that one. Make sure this is assistant. And for the content, it's going to be output. So this way as we speaking with the kind of chat GPT we're going to be updating everything in the history. So in reality
554:35
Speaker A
it's going to be first it's going to be this then let's say that here then let's say here was question one then after that there's going to be answer one and so on and then these two is going to be kind of repeating one after another and we're going to kind of keep this history as we talk to chatpt and we will be sending it every time like this full back to cht so it has the context of what we're speaking now let me clean back I I just want to show you as an example.
555:09
Speaker A
Now I think I haven't forgotten anything. The only thing we can add here also to leave kind of the chat but we're going to write if user input equals to something like kayamba. Then we going to break out of the loop. We're also going to write here uh something like a mati say well. Now it can also be exit if you want to keep it simple but let's keep it a little bit more fun. So this one exit chat this one get user input get AI output and obviously kind of update it update history.
555:48
Speaker A
H more like this. All right. So this way if I haven't misspelled anything we're going to run uh well the first message comes from the top. We actually don't even need it. We're just going to kind of keep the tone and actually bring it all the way down and write it in the system directly. So since our where is it here roll system equals tone tone equals this and then this one is initiate open client. Now let's rerun. Now we don't have any messages. user. Uh, what are the best battle ships in
556:27
Speaker A
1600s? Let's see what's going to answer. It's going to take just a few seconds depending on how large the answer. This case, it's actually very large. I would probably as added here in the tone. Keep it short and fun because I don't want to read it here. It looks very in-depth. So, let's rerun it. So, it gets the new tone. Let's run it. Now the user, what are the best battleships in 1600s? All right, now it's very short. Are made in the year 0600 the CSB ruled by mighty
556:59
Speaker A
gallons and car cars and blah blah blah. You get the idea. Well, it's all in one line. But now we can keep talking. All right, what about best weapons? And now have an actual chat GPT chat inside our Python script. And I can see we can control the tone. We can add even more logic if we want to. And most importantly, all right, what was the first question I asked you? And as I said, most importantly, it will keep track of all the history of conversation that you're talking about
557:30
Speaker A
because now I can ask what was the first question. Uh, you ask me about the best battleships in 1600s, Yed, and so on so on. Right? So now you can keep going and then when you're ready, Karamba. Well, I need to spell it correctly. Kamba like this. Yeah, even Chad Gypt got confused what I'm trying to do. Aoy mate say well now we finished talking to Chad and this is kind of the beginning that you can start using for your AI automations with Chad GPT or any other models.
558:03
Speaker A
Now this is how we use OpenAI and as I mentioned before I'm going to let you go. I also want to show you the bonus how to use Deep Seek because I used it in the past. I have the a little bit of credits so we can run it as well. For that, we're going to go to deepseek.com. If you don't know, this is the Chinese model that came out and became like 30 times cheaper than all the other models and it was as good. Now, I'm not even sure who is better. It's constantly
558:29
Speaker A
changing. Like today, it's one company, the tomorrow it's Grog, day after it's glow and so on. It's quite annoying how they kind of keep getting better and better. But now, I already created here an account. I already have here the balance. So, same as before, whatever LLM you decide to use, you need to generate API key. Let's create here Python course again. It has to be secured. So let's copy that one. Done. Now let's go back again. Add it to environments. So deepse API key.
559:01
Speaker A
It's going to be this. And by the way, if you want to try it out, you can use my deepse key for now. Just $5 and it's not going to last long, but it's going to run probably a couple tens of thousand requests. So if you're early in the course, you can use it and try it out yourself. But please don't drain my account just for the sake of draining it. Just use it for the tutorial sake. All right. Now, uh what I do then I want to go to the docs right here and in
559:24
Speaker A
there somewhere on the top here invoke the chat API. Let's go to Python. You'll notice something funny that they use open AI package as well. The only difference is right here when they create a client they provide the API key. They also need to change the base URL where to send their request. The rest is going to be the same which is very very great. Now let's go back to our Python code. Going to put it here. I want to keep my tone of an old pirate. Why not? So in here we need to provide
559:54
Speaker A
our deepse key. And as I told you earlier, deepse API is going to be OS get environment. Then it's going to be deepseek API key. Make sure it's the same name as an environment file. So we're going to provide it here. The base URL we're going to keep the same. Then for the tone, we're going to put it inside the system right here. Your helpful assistant. No, you are a pirate. And then for the role, here's the message. Um, let's write here. What's better to use for beginners?
560:30
Speaker A
Open AI versus deepseek AI models in Python. Then right click and run. And this is going to send the request to deepsek server. Now your authentication key is invalid. Huh. Let me see what's going on. So here is the name of the key. I put it here. Oh, I made it as a string. That's the thing. It's supposed to be a variable. So my bad. Let's try it again. So let me show you the code while it's loading. Like this. And what I noticed that it's going to take you longer
561:09
Speaker A
because this is a massive massive model. There's like 671 billion parameters I think. So it will be a little longer to generate response especially if you're not telling it to keep it short. All right. And now we got an answer. So all right mate you'll be asking fine questions for beginner Python swatch buckler. I have no idea what it means. So there is opent 43 and a5. So it's an older model. Then in cons cost dublons no local use the deep AI is better it's
561:40
Speaker A
free and open source good for coding can run locally then there are fewer guides smaller community obviously but in general it doesn't matter which kind of LLM you're going to begin with it just matters that you're going to start using it and produce results and later you can start fine-tuning and finding the better model for your specific use case because there are different LLMs for the coding for writing for general use for science research and so on so so on and I'm sure
562:03
Speaker A
you're going to find lots of guides online. I'm I'm not the one who recommend you specific models. All right. And now you know how to connect AI models with your Python scripts. And I want you to go and have some fun. Either create some simple chats like I did or maybe even go and push it to the next level. And I'm sure some of you already even have some cool ideas what you want to implement with AI in Python. And once you're ready, make sure to watch the next lesson where I will show
562:26
Speaker A
you how to create an awesome AI automation project that you will be able to use on daily basis in your computer. It'll be a great project to add to your portfolio and also make your life a little bit better. So, wish you happy coding and then I'm going to see you soon in the next lesson. Goodbye.
562:44
Speaker A
All right, welcome back. It's finally time for you to create your first Python app with AI. So, you can see that it's not that complicated and it's really rewarding. In this lesson, we're going to create a AI YouTube summarizer from scratch. And here's how it will work. First of all, we're going to grab the URL from the YouTube. Then we're going to get the video transcript from the video. We're going to send it to AI to generate the summary by using our special prompt. And we will save it as a
563:09
Speaker A
markdown file. And then later we will also turn it into cmd command. So it's much easier to use. And all of that will be done with our custom Python script. So you will have full control over each step and you can also adjust it to your own needs. And also before we begin, make sure that you watch the previous video where I explained how to add AI into Python script. It will help you follow along. And if you did so already then let's begin working on this cool
563:33
Speaker A
tool together now. So the first step we need to get the YouTube URL. So let's head over to YouTube and find some video. So I search for Python OP and I need to choose one of the videos. It's good to go for the shorter ones maybe like 15 minutes 10. We have shorter transcript and for example from Sakratica is going to be great or maybe Cory Shuffer. Let's go for Socratica. I really like her style. Now I'm going to grab the URL and let's make it a little bit more complicated. Let's go to shared
563:59
Speaker A
start add and then copy. This will also include this kind of timestamp in the end which will kind of mess up our code a little. So we better break it from the start. Now let's go back and here is YouTube URL equals this one. All right. Now we need to get the transcript and for that we're going to use one of the Python packages. Normally in the beginning probably you don't even know what packages you can use to get the transcript. So let's go to our friend Chajip and write I need to get a
564:28
Speaker A
transcript from a YouTube video using Python. What are the best packages? We're just going to ask and it's going to give us a list of packages and most recommended is going to be YouTube transcript API. That's exactly what we're going to use. Let's grab that one and see with peep install YouTube transcript. We can install it. There are some examples how to use it but and I'm not even sure if that's the correct one. So it's better reference documentation and see there are a few more examples.
564:53
Speaker A
We might use that one to actually get the name of the video as well but more on that later. Now let's just type this package in Google whatever you use. Then let's go for example here. And in here you can see examples how it works. For example I can see that I need to create this object. I need to make this import and then I can fetch uh from video ID.
565:19
Speaker A
But overall, you can get an idea how it works. Also, keep in mind that when you use this kind of packages, YouTube doesn't like bots scraping their website. So, you might get banned. And actually, in preparing for this video, I got my IP banned. But this ban lasts for like 24 to 48 hours. So, I'm good to go now. But just be careful. Do not try to run hundreds of times in one minute like to get the transcript. You're definitely going to get your IP banned. You can go
565:42
Speaker A
around by creating proxies, but this is not easy one. So, we're not even going to go there. Instead of that, we're just going to use it not so often and we're going to try to reuse our transcripts. Now, let's go back to the code and install this package. So, in PyCharm, go to settings, Python interpreter plus sign, and then search for YouTube transcript API. Here's the package. Click on install. And it was installed successfully for me. That's great. Let's click on okay.
566:10
Speaker A
And first of all, we need to get the transcript. So we're going to start with the import from YouTube transcript API. import YouTube transcript API like this. Now we need to create an instance of this uh API. So YouTube API is going to be YouTube transcript API. I hate saying this word over and over but that's what it is. Now once we're going to have it, we can get YouTube transcript and we need to use this instance. And then there's a method called fetch. And with control q we can check what it
566:40
Speaker A
takes. I can see we need to provide video ID and then there are some other things but we don't care. We're just going to provide video ID. And here's the challenge because this is our full link. You can see it's also shortened one. Let me actually go back quickly. I'm just going to reopen the same video but with the full YouTube.com URL because this is important because we want to make sure that we can support multiple URLs. So YouTube URL equals that one. And here
567:07
Speaker A
you'll notice that for the YouTube ID in this case it's right here and this case it's right here. Then in URLs normally when you see this kind of question marks it means that it provides there uh another parameter. Example here parameter video equals this. In this case this parameter whatever it means and then here another one is time and it's important because we need to parse this URL to get the video ID. In the beginning we can just of of course grab it and put it right here just to see
567:35
Speaker A
what we get. So I'm just going to print here print YouTube trans. Now let's run it. It might take a few seconds but eventually we're supposed to get some object here and you can see fetch transcript. We can go through there are bunch of different data. There is duration somewhere. There is a text. It's not very easy to read it in this format but don't worry going to show you how to break it down in a moment. But first let's focus on this URL so we can automatically get the video ID. We don't
568:01
Speaker A
have to manually always come back here and look, oh it's that part or that part. And for that we're going to write here get video id going to create a function uh extract video id here is going to be YouTube URL. And first of all you can see there are different rules. So if let's say YouTube.com in YouTube url then it's going to be rule number one or else if YouTube.b in YouTube url then it's going to be another rule. And to be honest it's actually not that
568:33
Speaker A
complicated. We could use the reg x but reg x is very confusing especially for beginners. I still always use chajp to kind of get the reg x and hope it works correctly. But we're going to simplify it. We can also add here like this. So we can look at that one and decide how can we get it. Obviously before the ID in the first case there are always going to be this symbol. So why don't we just kind of um video ID equals going to grab YouTube URL. We're going to split with this one.
569:04
Speaker A
And if you're going to split with that one, it's going to give you two parts. This is going to be one. This is going to be two. It's going to be in the list. And we want to get the second part. So, it's going to be minus one to get the last one. So, after we split with that one, we're going to get this part. We want to ensure we're not including this uh special kind of symbols here. And for that, we're just going to grab this one. I'm going to write here again split with
569:27
Speaker A
that one. At this time we want to get the first one because from that one when we extract this one the first part is what we need. I know it might not be the best syntax but that's going to get us the video ID. Can actually write here just return because it's a function. Now for the second one we're going to do something similar but here the first part is going to be this because it's always going to end B slash. Make sure you include the dot. It's going to help you avoid some issues.
569:56
Speaker A
And the second part is going to be so here's the symbol. It's going to be the question mark. So it's going to be like this. And again we're going to grab the first part. So this way we're supposed to get this. Let's test it. Video ID equals extract video ID YouTube URL. Print video ID. And then we also will be providing it here. But make sure you comment it out. We don't want to create extra API calls while we are debugging because we can get our IP banned. That's
570:25
Speaker A
what I did while developing this tool for the first time. I was just constantly running it through over and over and they just banned my IP. Now let's run. And you can see I'm getting the right URL from the second one. And from the first one exactly the same. So I think that works correctly. So we're getting our YouTube URL. Then we are getting our video ID. Then we're getting our transcript. So let's focus on that one again. before printed this object and you saw
570:56
Speaker A
that this object contains a lot of information and we need to get full transcript because this one is going to fetch some transcript object but we need to extract the information and for that we can write for text in uh YouTube transcript we can print the text like this let's run again and this time you can see we're getting all the transcript line by line we can also get the timestamps if we want to but it's not necessary so we're just going I'll kind of go like this D. Now
571:26
Speaker A
this is printing every single transcript we pre one by one. Let's make a list of transcripts with a list comprehension. For that I'm going to write I need t text for t in YouTube transcript. Exactly the same output as I showed you. And then we can combine this list into a single text because we'll need to send something to AI, right? And let me just see quickly here. I think there are no spaces. So it's line by line like this. So probably we need to add space in between because you
571:56
Speaker A
can see here you can see when we there is no space and then call. So if you're not going to add space then it's going to be together this way it's going to be correctly and then join list transcript. If you don't know how join works well if you have a list of objects like abc and d and I'm going to write here text equals something then join this list. What's it going to do? Whatever you put here, it's going to put everywhere in between all these characters and then
572:24
Speaker A
return you single string. So if I'm going to write here something like three dashes, it's going to take a put three dashes before b then three dashes between b and c and so on. And in the end, you're going to have a result like a dashes b dashes c dashes. If you're going to put here a comma, then between all items in your list, it's just going to put whatever you're going to provide there. In our case, we're just making space. So it's going to be a b and c like this.
572:51
Speaker A
Now this is going to give us a full transcript as a single text. And what and now we just can print the full transcript. Let's run it again. Now let's see music playing cookies when property made. They are crispy and delicious snacks. I know it sounds like gibberish but that's how the video actually starts. So we're getting our transcript correctly. Let's move further. But you can only pass last two and the method was defined with one parameter blah blah blah. So we're
573:18
Speaker A
getting something right. It's correct. Uh actually now what I want to do I want to grab this whole transcript like this. Let's just copy that one. And I'm going to create here full transcript. And then I'm going to paste it as a single string. I know it's going to be crazy long but here's the thing. I don't want to keep running these API calls over and over. I already got what I need. I'm going to activate it later when I will be uh running new URLs. But now we have the first transcript to work
573:52
Speaker A
with. We're in debug mode. That's totally fine. We're going to be a little bit conservative with our API calls. Now we're going to get to AI part. Let's add here emoji robot separator. Now that one is not complicated. The previous video I broke down exactly how it works. As you remember, we need to get our open AI key or whatever other LLM you decide to use. Maybe DC cloth, whatever. I have it right here. So, first of all, I need to get API key. For that, I need to from import uh load.enth.
574:28
Speaker A
Then I'm going to execute it. And then open AI key is going to be OS something like this. Let's just make sure I use the right variable name like that one. This is going to read whatever is written in your environment file. If you haven't watched the previous video, first of all, I recommend you do. But here's what's going on. You're not supposed to put your API keys in your Python code ever. They're called secret for a reason. Therefore, it's really good idea to store them in that end file
574:58
Speaker A
and then read them inside your code like this. This way, you're going to keep it secured and only local. It will also never go to GitHub. Uh now we also need to import OS and let's move this uh import on the top. So I'm going to write here imports and we're going to move make like this. Let's also move that one. So everything here nicely on the top. Now let's go down. So we have our API key. Now we can start using AI. For that one first of all we need to from uh open AAI import open AI
575:40
Speaker A
how we can create a client just create open AI and API key is going to be open AI key again let's move import all the way on the top now have the client and we can create a very simple response. There are different ways to create it since we don't need to keep the conversation going. We're going to use the simplest one where we send one question, we get one answer back. That's it. We don't need to keep it in kind of memory what we talked about. For that, we're going to write client uh responses create. Then we need a model and we need our input. So for the model um you can either go to open AI platform see a list of models
576:26
Speaker A
choose the most cost effective or maybe the better ones or with control space you can get a list right here. I know that for example 41 mini is very cost effective. It's very nice to run so I'm going to use that one but you can decide whatever you want. And for the input this is going to be our question to chat GPT or OpenAI GPT. So for that we're going to create our special prompt and then we're going to include our transcript in the end of the prompt. Now for this lesson I already prepared the
576:56
Speaker A
prompt. It's going to be something like this. So create a summary of YouTube videos based on provided transcript. The goal is to extract as much value as possible with clarity and insight over quantity. Then provide the answer in the row markdown syntax suitable for notion with no extra commentary or explanations. Then highlight the following sections clearly. The main message three to five key takeaways. short overall summary and I also want a clear steps to implement the ideas
577:19
Speaker A
taught in the video if applicable and important we're going to write here is the transcript and in the end we need to add our this full transcript to this message I'm going to put it right here so this is the message we're going to be sending to open AI and it's going to be a large one because of this full transcript but I think it's going to be fine now we're going to take this prompt I'm going to put it right here obviously you can create your own prompt your own
577:45
Speaker A
rules tools and so on. This is just the way I decided to do this. I'm quite happy with that one. It's quite short, so it will be fine. And now to read the response, we actually need to let's call it output response. And then there's output uh text like this. And obviously we're going to write here summary maybe AI summary and print output. So, display results.
578:15
Speaker A
Um, then let's make this one shorter. And this is ask AI. This case we're using open AI. Here's AI prompt and key. I think that's going to work just fine. Let's just make sure we this one is still commented out. We don't want to use this package for now. We have transcript right here. Let's maybe comment the print statement out. And instead, I'm just going to print the prompt. So, I'm going to print here uh full prompt. Then, we're going to print it. Let's also separate it with a bunch of dashes
578:47
Speaker A
between our full prompt and AI summary. So, we can see in the console what's going on. And now, the moment of truth. We're going to run it. Here is the full prompt. Create a summary of blah blah blah blah. Oh, and here is the message. So, it's in markdown syntax, which is great. Summary of YouTube video on Python object-oriented programming OOP. Then the main message, object-oriented programming in Python centers around creation and use of classes and objects
579:11
Speaker A
where class is a blueprint, object is instance with unique attributes and behaviors. That's correct. And see, it's not that long. Then the key takeaways, classes versus objects, initiations with constructors, methods versus class methods attributes then class attributes versus instance, and blah blah blah. Then short overall summary. It's going to be oh, it's actually not that long. and then clear steps to implement. Define a class, create constructor, create object,
579:39
Speaker A
create instance, blah blah blah. You can see that's a pretty good AI summary. Now, let's go grab some other video and then I'm going to grab one of my videos. I mainly create tutorials for architects and engineers using Ravit API. And actually, that one will be fine. It's also object-oriented programming, but it's going to be another angle. We're going to grab the link. Uh we're not going to change anything here. Just going to go here and we're going to change it somewhere on the top.
580:08
Speaker A
So YouTube URL equals this. So this is Eric Fritz. This was Socratica tutorial. Maybe I should have taken mine from the beginning, but well now I have it. Okay. So comment out the first transcript. This was like deaf mode. Let's put here a warning. I can also make it like todo. And it's going to make it like this because well, PyCharm highlights to-do comments like this. And we can comment out that one and that one. So now we're going to fetch the new transcript from YouTube. Then we're
580:43
Speaker A
going to combine it with our prompt and we're going to send it to chat GPT. Let's just run and see what we get. Now we get video ID which looks correct and create summary of YouTube video based on that. Here's our transcript. Now we need to wait a few seconds for OpenAI. And here is the summary. So the main message, understanding the basic principles of object-oriented programming is essential for effective working with Ravit API. You don't need to master all advanced concepts, but
581:10
Speaker A
getting grasp of fundamentals will significantly improve your ability to read, utilize, and navigate Ravit API classes. Then three to five takeaways and in there I talk about the four pillars of OOP such as encapsulation, inheritance abstraction polymorphism and practical applications. And here is like all the descriptions, then short overall summary, and then steps. Familiarize yourself with object concepts, explore classes and properties and methods, review inheritance,
581:37
Speaker A
relationships, and so on so so on. You can see it's pretty good. Indented even included credits, presenter, Eric Fritz. Well, it misspelled in both name and last name, but that's fine. But you see it already starts working. It's not perfect yet, but it's already something. Now we can save it as a markdown. For that one uh let's keep it very simple. We're just going to grab the output as it is. So we're just going to create here a file name um summary. We're going to add it with maybe video ID
582:07
Speaker A
and then should be MD for markdown syntax. Then with open then file name in the right mode as f and then we're going to write our output. Again we want to avoid to use this part right here. Let's create a new full transcript for testing. Uh it should be somewhere here as a single line right here. So the last line, just make sure we select everything. And I'm going to put it right here also in a single line. That's okay. And I think we can just run and see what we get.
582:56
Speaker A
So there's some syntax error. So let's use double quotes. Probably there's a quote somewhere in between. Okay. So here's our full prompt. Now we're waiting for reply for open AI and then we're going to try to write the file. Okay, AI summary. Here's the markdown. Then we wrote a file somewhere here. And here's our markdown file. Well, we need to remove the first part. Then you can see syntax is going to start working because if I'm going to leave it like this, it will treat it a bit different.
583:31
Speaker A
But if I'm going to remove it, now we can see the headings and so everything starts to work. And now, yeah, now it looks much better. So we have documented file of the video summary. It can be useful for later on. It can also be useful before you start watching the video because we can also make it much shorter. This case, it's a bit longer than I would want to, but I'm fine with that. I'm going to keep it like this. Now I have summary of this one. All right. And that's the code for
583:58
Speaker A
now. It works, but we can agree that it's ugly and it's not the best one. And here's the reason. When you start coding, at first you need to code fast and dirty. You don't care about how it looks. You just care that it somehow works. Maybe even not all the steps work properly, but you kind of get the result because once you're going to get to this point, then you're going to start refactoring and you can make this much better, work much better, address all
584:20
Speaker A
the cases, add error handling and so on. This is much faster way to create this MVP and then kind of build on top of that to make actually much better script especially if you're going to reuse it because otherwise you can spend like hours on first couple steps and then realize that you get stuck on the step number three and you cannot even finish the tool. This will save you a lot of time. Now to rewrite this code I definitely going to use classes. So we also going
584:45
Speaker A
to look into op during this lesson. If you if you don't want to, you can just add a few functions here and there, but class really going to make a huge difference here. So, here's what we're going to do now. It's going to be refactoring session. So, going to create a new class. We're going to call it YouTube samuriser like that. Let's start with init statement. As you know, this is how we create an instance of a class. And I would want to provide here YouTube URL.
585:13
Speaker A
And let's save it as YouTube URL like this. Now we're just going to follow exactly the same steps actually. Right click and split. Right. This way I can be in two places in the same file. So on the right is going to be our class. Let's make like that. And then here we can follow along what we're doing. So we have our YouTube summarizer. We're getting our URL. Now we want to extract the video. Let's actually insert it as a method. uh we actually want to grab exactly the same thing. Now since we are inside the method we
585:51
Speaker A
need to make here self then it takes YouTube URL it's not even necessary kind of can kind of just can look into self YouTube URL actually let's provide it like this let's also make it shorter okay and in we're also going to raise an error value error in case we kind of did not match any of these syntaxes. If there is no YouTube.com or YouTube.b then, then we're going to write that invalid YouTube URL and then we're going to provide whatever we provided. This is going to help with debugging. Now, we can also add here extract video ID from YouTube URL links. Works for both YouTube.com and YouTube.b uh formats. PyCharm also like that when you make
586:51
Speaker A
just empty one click on enter it's going to create it kind of this return and parameter this is going to be YouTube URL this is going to be video id from YouTube uh all right so next we can write here self video ID equals self extract video id this case we're going to provide it here but we could also kind of get rid of it all All right, let's hide it or maybe I'm going to keep it for now so you can kind of rewrite it at your own pace. So we completing this part. Now we're getting into transcript. So uh fetch transcript. This needs video ID. We're going to write it later. Now let's see what we need. We need these couple lines.
587:48
Speaker A
We're going to create this object. We're going to fetch the transcript and we're going to convert it into full transcript. And here we can actually write return. Now here it would be a good idea to add try accept statement in case our IP gets blocked. Don't remember the name of the error. So I'm going to leave it like this for now. Just know that it's it's a good place to actually add try and accept. But I'm doing it for myself. So I'm fine seeing the error. Later I can
588:13
Speaker A
address it. Now this is I think this is okay. So let's write self transcript equals self fetch transcript. Now we provide self video id. Now if you're new to classes the self refers to the class itself. I'm not really creating very reusable class. I'm making it more like uh one time use. So run it once and I'm going to generate all the results in in it. It's not really recommended way to use it. if you really want to make it reusable and properly but that's fine because I just
588:47
Speaker A
want to encapsulate everything inside the class so it's much easier to kind of maintain the code later I can make changes if I want to for now I'm fine with that okay we address that part now we're going to go to AI so define create AI summary so I'm going to write here self summary equals self AI create summary again I'm just going to provide hide it here but it's not necessary because we can get it either way. So summary um I'd like to create prompt as a separate property. I'm going to make it
589:26
Speaker A
here on the very top. So it's going to be prompt and it's just going to be return this statement right here like this. and we're going to turn it into property. Uh once you're going to use this decorator, what happens is you don't normally get your text like self uh prompt like this because this is a method. But once you put here a property, this becomes a property. So now you get it like this. It's just inside you have more rules how to define it and so on. I just like to
590:02
Speaker A
separate it. So I can also collapse it. And it's actually nice to have a prompt under its own kind of method which is property. Now this is okay. We have the prompt. It's without transcript. So we will we will add it here. Now we're going to grab that part here. Now let's also display results. Uh this is going to be something separate. Now somewhere here we need to create a prompt equals self prompt plus self transcript and this one now it's fine uh repair AI prompt
590:47
Speaker A
ask AI display results and let's also return output. Lastly, we need to find save uh MD. Going to take file name and we're just going to copy that part right here. So, save MD file name uh MD output. So, this goes here
591:17
Speaker A
and file name. Well, file name is okay like this. Very simplified. Now we can go and comment out the first part I think completely. We're going to keep our imports. That's important. I'm we don't have our key. So we need to find it somewhere here. So I got it right here. So, get API keys. I'm going to put it here like that.
592:01
Speaker A
So, we have our import, we have our API keys, uh, we comment out everything we did during the lesson. Maybe let's just go grab the URL because we need to test it. Class, I'm also maybe going to make like this. I'm using a special plug-in to make this asyard. I sometimes like to create this huge separators in my code. Very easy to navigate. Now here I'm going to create the same one for the main.
592:37
Speaker A
And now I can execute and see if my class even works. And then later we're going to go on and maybe refactor a bit more. So uh so we need a YouTube URL and then we're going to create a class instance. So YouTube summarizer it takes YouTube URL. You see when you work with classes there are different styles, different design patterns and so on. Um let's just go back to single script. In this case I'm currently running everything in init which is definitely not the recommended
593:10
Speaker A
way to do this but it's going to be fine. and it's going to give me a result. The reason is because for debugging and maintenance it would be nicer to kind of maybe do that in init statement maybe that one is also fine but then transcript and summary maybe that would be better to just say here the transcript equals YouTube summarizer and so on. This would make it much better for debugging and kind of maintenance and looking at that. I'm still debating if I should do that or
593:37
Speaker A
not because I just want to keep it simple. We already kind of combine it here for now. Then another bonus of using class is that if here you're going to click on structure, it's alt 7. Here you can see here's your variables. Here's the class. In class, you can see in it prompt extra. As you click, you also transport to this location. It's very very easy to kind of navigate in your methods. You can quickly Okay, fetch transcript this. Okay, save MD this. Oh, lastly, we're not making
594:09
Speaker A
here the file name. So let's make I don't know file name equals what we're going to do summary then here is going to be our self video id it's going to be md like that and then we can self save file name and here actually it should be save md and we need to provide here summary as
594:43
Speaker A
Uh, now the prompt. I'm fine. Extract video. That's fine. Fetch transcript. Uh, get the video transcript from YouTube with uh whatever the package was called. YouTube transcript API. Not a ben. Your IP can get banned by Google if you use it too often. usually 24 48 hours. Again, if you're going to get banned every day, probably you're going to eventually get banned permanently. Now, YouTube video ID returns full text transcript of YouTube video in English. Uh because this package by default it
595:29
Speaker A
works in English, but you can also get other languages, but I don't care about that. English is fine for me. Then that one is fine. Create AI summary. That's okay. And lastly, uh, save summary output as markdown file and doesn't return anything. That's fine. Now it works fine. Actually, maybe that one is not necessary. We're going to get rid of that one. This one is fine. And also this one we can actually include in this save MD. It's just going to be a bit cleaner syntax. So we don't need that. We also don't need markdown output. We can just grab self summary.
596:22
Speaker A
Then here we don't want to grab the summary. We're already kind of doing it here anyway. So, it was useless. In fetch transcript, let's get rid of that one. This one is going to be self video ID.
596:45
Speaker A
And for that one, I actually going to keep it. I like it. Let's also clean the dock strings. Didn't include it here. We don't need it here. All right. There's just one more step left and that's in case we already have a summary, we don't need to create it again. So we're going to create actually here cell file name. Uh we're going to grab it from here later. We're going to use here self file name and it's going to be like this. Then we're going to do something like
597:19
Speaker A
selfinsure new. We don't have it yet, but this way we're going to check if we already made files like that. For that, it's just going to be ensure new. And we're going to use if OS path exist file name then return false otherwise return true. We're going to write if insure new. Then we're going to create transcript summary and save it as a Mardon file. I wouldn't need that one here because otherwise if we already have a file right here, don't need to generate
597:57
Speaker A
it again. It already exists. Well, we could add the rules to kind of generate new, but I'm fine creating it just once.
598:07
Speaker A
Let's just see what happens. I'm just going to run it and see if we get any errors. Executed too quick because well, we don't make any print statements. So, that's the next step. Now first of all let's write here on the top generating summary for whatever YouTube ID we provided. Then inside this ones we're going to go and add some something here. Uh let's change it here a little like this. And this I'm going to write if video ID. I'm going to write extracting video ID. I'm going to
598:44
Speaker A
provide it like this. and then return video ID. This is going to help us with debugging. Then next uh file name. So here we need to ensure new uh here we're going to write something like a file uh video summary already exists. And we're just going to provide here the file name. So it's going to be self file name. Otherwise, we're going to go to the next one. It's going to be fetch transcript. Let's write here full transcript. Then we're going to make print statement. I'm
599:26
Speaker A
going to write here YouTube transcript. And it's going to be this one here. Don't forget to return it. Like that is good. So grabbing this one then we generating AI summary. So we already have here something generating AI summary. We're going to display it. And lastly uh we're going to write saved summary to file whatever the file name is. Ideally that supposed to be done everything with logging. So we can create levels for debug info. I have tutorial on that. But I just want to
600:20
Speaker A
quickly do that just as a kind of quick example. Now you can see we have the full class. So in in it we making the print statement. We're getting YouTube URL and then we're calling all the methods. We're extracting video URL. We're generating file. We're ensuring it's new. Uh maybe ensure new summary. Then we're creating transcript. We're getting the summary. We're saving it as a file. Then in the prompt, we define this prompt right here. In ensure new summary, we're just ensuring that file
600:51
Speaker A
doesn't exist yet. We're just returning true or false to decide if we continue or not. In extract video ID, well, that's very simple. We're just taking our URL and we're splitting before and after to get the video ID in between. Very simple way. Then to get the transcript, again, we're going to use YouTube transcript API. If you're not going to call it too much, you're going to be just fine using it. Otherwise, well, just wait a little bit before you start using it again in case
601:16
Speaker A
you get blocked. And if you're going to get blocked for more than 24 48 hours, just let me in the comments because I have no idea for how long they ban you. In my case, I think I waited around a day and it started working. So, I was very happy that I could continue with the tutorial. Otherwise, I thought that I would probably have to scrap the idea even though it was really nice. Uh, yeah. So we're getting our transcript and then we're creating our AI summary. We're just sending it a chat
601:40
Speaker A
GPT with our special prompt and we're getting an answer in the markdown syntax because somewhere here in the prompt we say that return in a markdown syntax with no extra commentary or explanations and we're saving it as MD file actually somewhere here in outputs.
602:05
Speaker A
I'm just going to quickly replace this um with this. And as you remember when we first wrote our markdown file here on the top, we had this uh syntax which kind of ruined a lot of things in a markdown. I'm just doing it very quick and dirty because I just want to end this tutorial. So I want to look for that one and remove it. And uh this is going to be the new output. So uh clean code block syntax. This is how uh chpt defines code block for itself. Uh we're displaying results
602:50
Speaker A
and we returning the output. That's fine. Lastly, we're saving the file and then in the main we're just running it. Now right click. Let's see. Generating summary for this video. Extracting video ID. This video summary already exists. stop tapping execution. That's fine. Let's go grab another video. Let's look for Python in 60 seconds. And Fire Ship is a great example. Let's grab that one and see if that works. So, our YouTube URL is going to be this. And I'm just running and seeing what I'm
603:30
Speaker A
getting. Generating summary for this video. Extracting. Here's the YouTube transcript. Now, we need to wait a little for the AI summary. If we're going to use the nano model, it's going to be even faster, but it's maybe going to be not as good. Now, here's our AI summary. We see it in the console. And in the end, save summary to file summary blah blah blah. Ideally, I would get the video title, but I'm going to leave it to you as a homework. Go to that one. And here we can see now generating MD
604:02
Speaker A
files for any YouTube tutorial we want to. This is great. So the main message Python visit our high level interpreter programming language known for its simplicity, readability and practicality making it the most popular choice for beginners blah blah blah blah. But this is great. Now we can get summary of any YouTube video we want to. All right. And lastly, we can also turn it into an cmd command. But I'm going to leave it for the next tutorial because I think that's
604:25
Speaker A
already a bit overwhelming. Let's just finish it here and continue in another lesson if you want to make it a CMD command. And I highly recommend because I think you're going to use this tool a lot. All right. And that's how you create a custom AI tool with Python. I hope that it will motivate you to create even more cool AI apps after this video. You've seen that it's not that complicated and it's also very rewarding because now you have your own custom AI summarizer with your own custom rules
604:50
Speaker A
and you can even extend it further. You can even save it to notion which might be another video, but we'll see. But anyway, I wish you happy coding. Make sure you follow along the video, try it out yourself, and then I'm going to see you in the next video where we're going to continue. Goodbye.
605:09
Speaker A
If you want to execute your Python scripts inside the terminal and instead of writing this Python, then full file path command all the time, you can create a shortcut that will do that instead. And also on top of that, I'm going to show you how to provide arguments to your Python files through the terminal, which is a tricky topic. There are many different options and I'm actually going to break down three different options how to do that. There's very simple one, there's medium
605:29
Speaker A
one and then there is most important one with arc parser. It's definitely something that you would want to know. So stick around and I'm going to show you everything inside this video. Let's actually dive in and begin this lesson. So for this example, I'm just going to use a very simple code example. You can see it's a simple function. It's just a single print statement. But it can be anything else, right? The point here is just how do we execute this Python file
605:50
Speaker A
through cmd command. So let me open command line interface on the side and you can do that by going to kind of start and write cmd like this open the command prompt and first of all I want to cover how do we execute Python commands through the terminal normally before we're going to create a shortcut because you need to understand this and as you know we can write here Python and then provide the full file path to any Python location for example I'm going to
606:12
Speaker A
go to copy path reference absolute path and can also be beneficial to use double quotes in case you use any spaces in your uh path path and this way you can see executing simple function. So we are making this print statement. This works however we cannot just write python file.py because it doesn't know where it's located. It tells me that cannot open file and it has a completely different path here because you can see I'm currently in this directory. So I could write here cd for current
606:38
Speaker A
directory and then go to another folder where this python file is located. In my case this is the path and then I could write here python file.py. Oh uh I made here a dash by accident. So let me try it again. And now you can see I'm executing this. And this Python file.py is actually right here. Here's in my folder. There's just a file.py. So first of all, this is how you run the Python files. And instead of writing this Python file.py, you can actually simplify this by creating a bad file
607:06
Speaker A
which is a file of commands which will be run inside common prompt. And I created it in exactly the same location. You can see it right here. At the moment, we're just going to put it here. Later, we're going to move it somewhere else. I just called it simple.bat. So we have a little different name. We can call it absolutely anything you want. This is going to be your shortcut however you're going to name it. In my case, let's actually call it, I don't know, EF. So it's actually a very short
607:29
Speaker A
shortcut. Now create this file. Then we're going to open it with a notepad. You can literally just new file here. Select text. Write here um test.bat. Then it's going to create it. I already have it. So I'm just going to modify that one. So edit in the notepad or you can also open it actually in the PyCharm. So let's do that. Show more options. There should be edit with PyCharm and it's right here. And you can see I already wrote a few comments here and it's very simple.
607:58
Speaker A
First of all, there's echo off and this is the command that's going to prevent showing all the commands in the console. And then the secondly I'm going to write Python and my command, right? I'm just providing here the absolute path. I could also write here CD to this location like this one. And then I would not need kind of the full path. I would just need this. I can try to execute it because we are in the same location. Right? If I'm going to write dear, you can see that
608:25
Speaker A
right here somewhere there's this EF butt. So if I'm going to just write EF, it's going to execute all these lines. And also notice that right now when I execute, I only see this because echo is off. But if I'm going to remove this line, have a look at that. I'm just going to write EF again. You can see that now it's going to print every command from this line. So every line by line is going to be written here and it's going to be much uglier. Therefore, it's really good to always keep here
608:49
Speaker A
echo off. This way it's much cleaner. It's just kind of you write EF and you execute your Python right away. So that's how we execute our Python file. Now here is the big problem. We can only execute this kind of EF shortcut whenever we are in the same folder where it's located in the cmd folder. If I'm going to go one folder outside with the cdouble dot for example and see I'm on a different path. Maybe let's do it once more. So I'm in documents. Now if I'm
609:15
Speaker A
going to write EF, obviously it's going to tell me EF is not recognized as internal or external command or any kind of program. And this is correct because there is no such files in the documents folder. So now what I need to do, I need to make this EF command global. So it's available everywhere. And for that you need to understand the system environments. I just go to start and write here uh environments until you see this edit system environments variables.
609:39
Speaker A
It's in control panels. So you need this uh menu in there click on environments variables and then in there there is path don't worry it's very common I'm going to explain exactly what it means. So what happens here is when you execute any command in terminal for example python it needs to check where this python.x file is located and for that it's going to look in this environments variable and you can see right there there is a path to my c python python
610:04
Speaker A
310 and this is where it's going to find python.x XA if I would remove that one from here and I would try to run Python command it would not run it because it would not find the executable file and the same way it finds the Python file can also add here new paths where we store ourbot files and then they're going to always know kind of uh our shortcut names for these files and let's add it I'm going to add here new and in here I'm going to paste uh this path
610:32
Speaker A
like this one so now this path is where my filepy is located. You can see right there is the path. I'm just putting exactly the same path right here. So, let's click okay. Okay. Okay. Now, I'm just going to close this command line and open it again. So, this time you can see that I'm going to just type EF and it's going to work no matter where am I. Uh, also at the moment there is a CD, which is fine, but you can see it changes the current directory every time you're going to run EF. You might
611:00
Speaker A
not want to do that. Therefore, let's just change it back to what it was before. Python file like this. We don't want to change the current directory. Now, if I'm going to go a few folders outside, write EF. You can see I'm staying in the same location no matter where where am I. I can execute my Python file. So, that's how you create a shortcut. This is the first step. Now, it doesn't matter. I can always come here, write EF. That's great. However, what if my function
611:27
Speaker A
needs some arguments? Maybe here I need AB and I always need it to be different. For example, in one of the previous lessons, we created AI YouTube summarizer and we just need to provide a YouTube URL. So maybe we would provide here YouTube URL. Then we're going to create our sum summary with AI and send it somewhere else. Let's keep it simple. Just AB is going to be enough. Then we're going to write here argument A and argument B. Then put it like this. That's it. So now
611:57
Speaker A
we need to get A and B. And there are three options. We're going to start with absolute basics. It's probably the worst option, but it's still worth mentioning because you can get it by just asking input. So, argument A is going to be this and then B is going to be argument B. Right? Now, if I would come here, write EF, you can see argument A is going to be 10. Argument B is going to be 20. It kind of works. It's very quick and easy to set up, but it's not ideal. If you're just
612:23
Speaker A
starting with Python and you really need to do something quick, you can use it in the long term. This is the worst option out of them all. I just wanted to show you that. Well, there is this kind of options. So, I'm just going to add here the comment and we're going to comment it out and move to the second option. The second option is going to be using the row system arguments. It's a little better, but it's still not ideal. But still, I think that it's worth showing
612:45
Speaker A
you cuz here we're going to use the import sys and then we can use print sys arg v. Now, what's going to happen here is that when we're going to write ef can also drop there a few arguments. Obviously AB is not defined. We're going to fix it in a moment. Let's comment it out for now. But the point is that I can execute here EF. You can see I'm getting the list at the moment. It's just the file that I am executing. But if I provide here EF, ABC, you can see I'm
613:13
Speaker A
not getting anything because I also need to fix something in my bat file. Not only I need to provide here the file path, I also need to provide here uh this symbol for arguments and then star for all of them. Now if I'm going to provide efc you can see it's capturing whatever arguments I provide after executing my python file. So I'm providing python whatever is my file and then I can provide all the arguments I need. I can literally write here 10 20 30. You can
613:41
Speaker A
see I'm getting this inside my python file. This is great because now I can modify the snippet a little. Since we need two arguments, we can make it very limited. If it's equal to actually free because the first one is always file name for some reason. And in this case, we're going to write if it's not free, then we're going to print that it takes two arguments a and b and otherwise exit. Or we're going to define an a is going to be this, b is going to be this.
614:07
Speaker A
And then we will be able to run our simple function. So let's try it. We cl to clean the command then ef 1020. I can see executing simple function argument A10 argument B20. So we're capturing our arguments and we're bring it into my file. But here if I'm going to provide more it's going to tell us the error. If I'm not going to provide enough it's going to also give us an error. And also uh if I want to get any help or something like this well you it's a bit
614:33
Speaker A
tricky on how to do this. So therefore it's also not recommended but I just want to show you the bare bones how it works before we start using the package cuz that's what's going to be happening in the background. Let's clean it up and actually go to the proper way of how to do this with uh arc parser. So that's a built-in package. Just going to write here import arc parse. And there are a few things we need to do. First of all, we need to uh set up arc parser. Going to write parser equals
615:01
Speaker A
then arc parse argument parser. Inside we can provide our description which is going to be u which is going to be a message for our help uh command. Going to see in a moment what it means. Let me just zoom out. Then we can define the arguments that we actually expect to work with. Right? We going to set up the positional and optional arguments. For that let's start with positional. So here we just say parser at argument. Then we need to define the name of their argument. We
615:35
Speaker A
can also specify the meta variable which is alternative name. Going to show you in a moment what it does. We can define here the help uh argument a should be an integer. Can also define here that the type should be integer. So if it's anything but integer it's going to kind of crash. Actually let's start just with this. So it's going to be a and here is going to be argument b. Now we're going to run arx. So I'm going to write parser parse arcs like this. Once you do that, you can get your
616:08
Speaker A
arguments from this by using uh names of your arguments. So argument A, argument B, right? So whatever you write here later you will be able to get like this. And now we can just go and test it. So I'm going to write EF. I can see it's an error. The following arguments are required A and B. Okay, not bad. I can also write ef-h or help. It's going to give me the kind of overview of all the possible arguments and how to work with them. And here you will see the message which comes from here.
616:43
Speaker A
And then this a and b positional arguments. We also can have description right here. So argument a should be an integer and blah blah blah. There's also a metavar which we can define a like this for example. And we can also do the second one here BBB. Right? Normally you would define it much better than this but the point is we replace these names. That's okay. Now let's clear it. I also don't like to I'm also going to remove this one. Let's keep it simpler. And this is the
617:12
Speaker A
positional arguments which are always required. We can also define some optional arguments. And for that I'm going to write here optional arguments. And it's actually going to be the same thing. However, instead of here just like this, we're just going to add here two dashes. This shows that this is a optional argument. We can also define the default value. For example, default is going to be I don't know five. And then the second one going to be D. Default is going to be 10. So we have
617:39
Speaker A
here optional arguments which can provide or we can ignore. Now in here we would write EF 1020 for AB and then let's say that D should be 50. Uh I also need to print it. So in here I'm going to change something like this. So it's all fstrings and we just can see properly what we actually printing. Try it again. So this time you can see that 10 20 then d was set as 50 and c is five. So this five it's actually a default value but this uh 50 we specified it right here. You can also
618:13
Speaker A
write here ef for example I want to just set up without specifying anything. You can see unrecognized 3030 because here we have to specify explicitly that C is going to be D. D is going to be this right here you can see unrecognized argument 2020. So uh we could also try to provide here something else like for example like this and you'll notice we're going to get an error argument a invalid integer value 10.2 because we specified right here that it has to be an integer.
618:46
Speaker A
So it's important that it should be like this and also there is an option for example we can specify here choices and let's say the choices should be one two or three for B argument this way let's say I'm going to write EF10 I'm going to get an error because it tells me here argument B invalid choice 10 choose from one two or three so it knows all the possible choices and it's going to report on that also if I'm going to use the help In you'll notice here that positional arguments
619:19
Speaker A
argument A should be an integer and here it changed to the choices one two or three because we haven't specified this meta variable. This is where it's really useful. Meta variable is going to be just keep it capital B. I run it again. Now I can see A and it became capital B. Argument B should be an integer. Ideally I would define my choices in the description as well. then different options then additional commands and so on. So overall it's not that complicated and now I can provide
619:50
Speaker A
1020 C is going to be I don't know 100 and D is going to be 200. And now you can see I'm capturing all these arguments. I can create optional ones. I can create positional ones. And there are also a few things embedded already kind of that shows the error what's going on. Did you provide the correct type? Did you provide the correct value? And so on. All right. And that's how you can bind any shortcut command inside your cmd terminal to any of your Python scripts
620:17
Speaker A
and even allow to provide arguments with arc parser module. And remember that in previous lesson we've created an AI YouTube summarizer. Now you can go back and turn it into a short cmd command. You can call it something like YouTube sum then provide YouTube URL and then let it create a new YouTube summary with AI. And also to extend this tool even more in the next lesson I'm going to show you how to use notion API to write any data inside your notion databases.
620:44
Speaker A
It's going to really help you to improve that previous tool we created. So wish you happy coding and I'm going to see you very soon in the next lesson. Goodbye.
620:56
Speaker A
So you're probably thinking to automate something with Python and then save your results into notion database. That's a very smart workflow but you would need to use notion API for that. And if you have no idea what is API and why developers use that, don't worry. I'm going to cover everything you need to know about APIs and show you exactly how to automate pages in notion database with Python. You can save your data in markdown syntax correctly inside notion.
621:19
Speaker A
It's a tricky topic, but I'm going to try to make it as easy as possible to follow along. So, let's start with the basics. What the hell is API? API stands for application programming interface. In simple terms, it's a set of instructions that developers leave so we can communicate with their apps or software using code. This way, we can create an automation and then connect to our favorite apps like Notion, for example, or create custom plugins for other applications using application APIs.
621:46
Speaker A
Now, when working with any APIs, it's really important to reference documentation. It's not always easy to read, but that's where usually all the answers are. So, let's start with that. We're going to go to developers.notion.com. This is the page for developers to understand how to work with notion API. Let's click on get started. And right here you'll notice there are three categories. There is home which is this page and there's guides where you can get a little bit kind of tutorials and
622:10
Speaker A
then API reference where it's more in detail how to communicate. In guides you'll find a lot of different information like like an overview how it all works some examples how authorization works, how build how to build your first integration. But keep in mind that this documentation is made mainly for the shell and JavaScript and Python examples are very limited. I saw a few of them but most examples don't have Python. Example, if I going to go to let's see where is this API reference
622:38
Speaker A
here on the left side there are different categories. Let's say that we want to look at the databases. You can see right here there is post, there is get and for example we want to query a database. And here's going to be an example in JavaScript or shell. and you can get more information how it works. And don't worry, even though it's different languages, it's really important to understand the structure of the JSON that we need to create. But don't worry, we're not going to rely so
623:01
Speaker A
much on the documentation. I just want to show you that it exists and you can find a lot of information here. But nowadays, asking AI is going to be so much more efficient because it knows exactly what's going on here and it can kind of spit out information exactly in the human language for beginners in Python. This is way better than reading it yourself, trust me. And first of all, we need to create a notion integration. So we have an API key and ID of the database we're going to work with. So
623:26
Speaker A
let's go to notion. And for this tutorial, I've just created a new page here. In here, I'm going to write something like notion API Python tutorial. And then let's create a database because I specifically want to work with databases. We're going to write here something like notion API tutorial. And that's our database. I'm going to add here another property like date. So we have the name of the page and the date. Example, we can create here a test then write that it's today's date and
623:53
Speaker A
then we can open it and on the side or maybe full screen we can write here specific information we want to for example we call the test and we can write here heading one here is some text then here is the heading two and so on. So now we put here some information again we can go back to notion API tutorial here is this entry and our goal is to automate that. Now as I mentioned we need to create a notion integration to connect it to Python. There are two steps. First of all we need to generate
624:23
Speaker A
kind of notion API key. For that click here on the three dots. Then scroll all the way down to connections and then look for this develop integration. Let's click on that one and it's going to open a new browser and it's going to be with your account and here are your integrations. Probably you have it empty if you don't know what's going on but don't worry. Just click on new integration. Then select here the associated workplace in case in my case I only have
624:49
Speaker A
one and then keep it internal if you want to kind of keep it private or public if you want to keep it public for some reason. I'm going to keep it internal. Now integration name is going to be Python course notion API. Again you can name it whatever you want. It's up to you. Then save it. All right. So it cannot contain notion name in the integration for some reason. Okay. Let's make something like this. It's definitely notion. Ah, come on. Okay, it was not possible. Let's just
625:20
Speaker A
call it API. Doesn't matter for me. I know what I'm doing. We created our integration. Now, we need to configure the settings. What's important here is this internal integration secret. This like an API key so we can communicate with notion and it has the access to our workspace and specific databases and so on. You can also configure what you can do and what you cannot do with this API key. In my case, I want to make sure that I can update and insert content.
625:47
Speaker A
I don't care about the comments and user capabilities. Well, I don't care about that one either. Then there's also we can specify the pages where it has access, but we're going to do it differently. There's also web hooks, but we working different way for now. We just want to show the API key and we're going to copy that. And make sure you keep it secret and don't show anybody because for example, if you use this key, you'll be able to write to my specific database, which I'm definitely
626:10
Speaker A
going to delete after this lesson. I don't want you to write any nasty stuff there. Now, for now, we're just going to take this key and I'm going to save it here as notion API key equals this. Again, as you know from pre previous tutorials, it's best to keep this kind of keys inside of that environment file and then import it here with that enthosing your keys in in the GitHub or anywhere else. But for this tutorial, that's fine. Now, next we need to get our uh database ID. That's also not
626:41
Speaker A
complicated. Let me also add here get notion key and database ID. Let's go back to notion. Then to get the ID of the database, we need to open it in a full page. You can see right here, open as a full page. Now when you click that, you'll notice that right here, you're going to see the name of your database. Example, if I'm going to go back, let's say that it's going to be called DB notion API tutorial just to help us navigate. When we're going to open it,
627:06
Speaker A
you can see here it's a DB view. We need to specifically get this view. Because once you're going to get it, you can either click right here and copy the link or right click here on the table. There's also copy link to view. That's also fine. Then we can go to browser, paste it, and right here in the URL, this part right here until the question mark is the ID of your database. And then this is ID of the view. So this is specifically for the table. We don't care about the view. We care about this
627:33
Speaker A
one right here. And again, if you are not sure, just make sure that this is the name of your database. In the beginning, just put literally DB in the name of the database. It will help you navigate. Now, I'm going to copy that one. Then, we can go inside the code and I'm going to paste it right here as well. Again, ideally post it in the environment file, not here. But for this tutorial, that's fine. Now, we have our notion API key. We have our database ID. We just need one more
627:59
Speaker A
thing inside of notion. So, let's go back in here. When you are in your database view, you need to make sure that the integration you created is applied to this page. Remember there was access page, but I told you we're going to set up differently. To set it up, click here on the three dots. Scroll all the way down to connections. And then we need to select here in the list our um uh API integration we created. In this case, I called it Python course API. Can see
628:27
Speaker A
there are different apps as well. We don't need we don't need to select them. We want to use our custom one. this one. The moment you're going to do this, it's going to ask, do you want to confirm that you give all this rights to this API key? And I click on confirm. Now you can see in connections, it says one right here. And here's the selected connection. Also, you can at any moment kind of come here and disconnect if you don't want this API connection anymore.
628:51
Speaker A
We can also verify if we're going to go back to the page where we created it. Let's just quickly update it. If we're going to click here on access, we're supposed to see here something. You can see there's one shared and one private. And there's our DB notion API tutorial database. This is perfect. This is what we need. Now I have set up everything we need inside of notion. Now let's go to Python code and continue here. Uh next let's define a simple example of
629:14
Speaker A
markdown syntax. I'm just going to paste here this example. And you can see that we're going to generate the page name is going to be test page 01. And then the content is just going to be very simple. It's going to be here heading one, heading two, regular paragraph, heading three, there's bolt and italic, bolt itallic text. In case you are new to the markdown syntax, just know that this is a markup language to format plain text. You can use various symbols to mark for
629:36
Speaker A
example here is the headings, there is a regular paragraph, this is the bold, itallic and so on. There are also code blocks, quotes and many other things. And then it will be rendered correctly in your app and actually kind of have very visual style. I'll put a screenshot on the screen so you can see the basics of all the symbols you can apply and what they create, how to create bullet points, numbers and all of that. Now we can start working on notion API to
629:59
Speaker A
create a simple page in notion database. And to do that with notion, first of all, we need to create header with our API key to authenticate. So we have the right to actually connect to our notion workspace. I'm just going to paste it. Here's how to set up header for notion API. We're going to create header dictionary. In there you need to write authorization and then with the bearer you provide your notion key. Uh let's just go and rename that one so it's a bit shorter.
630:24
Speaker A
So you take your key, you provide it here. after the bearer and space word. Then content type is going to be application JSON. This is the standard form for the API request. And for the notion version, we're going to use this one. This is the latest version. Even though now is 2025, it's still the latest version. If you're not sure, you can go back to notion docs. Then I think somewhere here in versioning, uh, I need to actually search for this. And I found it here. Changes by version. And you
630:52
Speaker A
will find all the versions here. You can see it started with 2021. And these are the latest version 2022 0628 even though today is uh the August of 2025. So you can use it exactly the same. Now we need to create a JSON to generate a new page in the database. And this is where it gets tricky and I highly recommend you to use chat GBT to help you generate correct JSONs because they're going to be a bit overwhelming. But let's take it step by step. For example, here's the
631:17
Speaker A
simplest data we need to set up data for notion API request. You can see right here in the parent, we need to specify the database ID. So connect to the right database and then we need to specify properties inside the name inside the title. Then we specify it's a text content and page name. It's very confusing. I know it especially if you never work with APIs or notion API. It looks like a mess. This is where AI comes in really really well. It can handle it pretty good. But for now this
631:44
Speaker A
is like the simplest syntax we need. So this is we just defining that we are creating new uh database page and it should have a kind of title name page name which we defined here earlier test page01. All right. Now we have the headers to authenticate and we have data to actually send to the notion server. Now we need to create a post request and for that we actually need to install a package. So let's go here to file settings integrations plus just look for the requests.
632:15
Speaker A
It's a very common package. Just click on install. You can see Python HTTP for humans. If you use some other ID, just go to terminal pip install uh requests. It's going to be exactly the same thing. Now, once we have it, we need to import it. I like to keep my imports on the top. That's right here. Import requests. We will also need JSON in a moment. So, let's import that as well. That's a built-in package. No need to install it. And I'm going to paste the code to
632:42
Speaker A
actually send the post request. As I mentioned, post means that we want to write something on the server. So, we're going to write response equals request post. Then, we need to specify a link where to send this request. This case, it's going to be the common links of the API. You can see API.com. This is the server that's going to handle it. Then, we provide our headers and we provide our data. If you don't remember, for the data we had this one and for the headers, we had this one.
633:09
Speaker A
Lastly, when you're dealing with web APIs and when you send any request or get something, you will also get the status of what's going on with your response so you know if it's worked or not. So, let's see if we managed to create it or not. I'm also going to paste this code snippet to verify the status. Just going to take the response which is right here. And there's a status code. If it's 200, it worked successfully. We're going to send the data and it could uh process it.
633:31
Speaker A
Otherwise, well, we failed to create it and then there's going to be an error message. Now, we have everything we need for now. So we verifying our status. We're sending post request. We preparing our data here. This is the simplest it can get. And this is the headers. Not so bad. And here's obviously the content. So, and we're not writing the content yet. I can even kind of comment it out. You can see it's not used anywhere. We're going to do it in a moment. First,
633:55
Speaker A
let's just try to create a page. So, I'm going to make it side by side. Then, right click, run. Can see page created successfully. We will get an ID. I just go to notion and well, we might need to update it. Let's go back to the page. You can see the pages here. I can open it. But can see now we have the first page here. Here's the test page 01. Exactly what we wanted inside. It's going to be empty because we haven't specified anything yet. But we already making progress. Now let's go back and
634:22
Speaker A
fix a few things. So for now, we're just filling in the name, but I also want to fill out the date. So somewhere on the top, we also need to import date time module because we want to generate today's date. Then somewhere here maybe after the page name we're going to generate today equals and we need date time uh now and has to be specifically in ISO format. This is what notion needs for the day times and actually I think yeah I made it wrong. So we need from date time import datetime.
634:54
Speaker A
I know it's confusing but that's just the way it is. And then it's going to work daytime dot uh now you can see now it can recognize the command. All right. So we're getting the date. Let's grab that. Then let's go to the data. And here we need to modify that. Again working with this JSON getting a little bit out of control but let me just paste. And then the start value should be today. So let's just break down. We're connecting to database. And then we creating new page
635:19
Speaker A
with these properties. This is going to be our name. Even though it's all confusing, that's just the way notion API needs to uh read the data to create it in the server. And then this is the date. Again, I know it's confusing. Just ask Chad to help you format this and just keep it one step at a time. It's going to make sense. The rest we're not touching anything. I just right click, run. Let's see if it worked. Again, I need to update my view. So, go back. You
635:45
Speaker A
can see now test page 01. Today, we have also the date and the time when we actually tried it. It's going great. Let's go back just to the code and this time we're going to focus specifically on the data inside these pages because now I want to write this content inside the page. That's also going to be the trickiest part and you're going to see in a moment why. But we need to go back to the data and right here you can see we had properties. We also need to
636:09
Speaker A
specify here a children and it has to be a list of other dictionaries. I'm going to call it children blocks. You can call it different as well. Don't forget the comma because we need to keep the structure of JSON correctly. Also here we can kind of collapse it to keep it a little bit simpler. So properties was exactly for the name of the page. So the name and date and children blocks is going to be the content. So let's create it somewhere. U define content blocks for the page
636:44
Speaker A
and it's going to be right here. Now, as you guessed it, our children blocks, they also supposed to have the same kind of JSON format with a little bit different properties. I'm just going to paste here the code. And I know it looks kind of weird, but bear with me. So, we telling here that we're adding this one object inside our content. There can be multiple objects. This is a block with a type of paragraph. Then we define that paragraph inside the rich text. Type is
637:11
Speaker A
going to be text and text is going to be whatever it is. Inside I provided here the content which is this one. Again, don't overthink it. You will use AI. I'm just going to show you what's happening and what is the issue using these children blocks. So, most importantly, now we're going to take that and we provide it right here. Now, we're going to go and execute. And with this response, we're just sending the same data, but now we also have childrens. You can see it was created successfully.
637:36
Speaker A
Let's go and check. Now, I already updated the page and here it is 544. Let's open that one. And inside we have a text. So, this is already a win. We've writing something inside the page. However, you can already notice that something is not right. Cuz normally in notion when we use this kind of syntax, if you want to paste it, it's not going to work. But with control shift v, that's going to paste exactly how we want it. And this is how notion supposed to read it. So notion uses the markdown
638:02
Speaker A
syntax and it's supposed to know what is heading, what is a bold text, itallic, and so on. But because we're using API, we told specifically that this is a single uh paragraph. You can see that for example here if I'm going to create heading one test you can see that this is object number one and then right here this is object number two so this is just the paragraph we're doing exactly what we're telling it this is the biggest issue of working with notion API
638:27
Speaker A
it's the most confusing thing probably because here how we actually have to do this and this is the trickiest part let's go back to the code and here we create the children blocks I'm just going to update it and just bear yourself it's going to look really really confusing Ready? Going to paste the new one. Oh boy. Yeah, that's how it's supposed to look. So, first of all, here we're creating heading one. We're saying create a object block. It should be heading one. Then we define heading one
638:53
Speaker A
should have this kind of properties, right? Pretty much we're just saying that this is the content of the heading one. Then we're creating paragraph. We're saying it's a paragraph. We're defining all these properties. And then right here, we say this is the content of the paragraph. It gets worse, trust me. So then we say this is the heading two. That's fine. But now let's say it's a paragraph with bold italic and bold itallic stuff. Say it's a blog paragraph
639:15
Speaker A
rich text and inside the rich text we can define this is the plain text text type. Then for the bold we need to say it's text type. And then we need to say that it's a bold text like with annotations true. Then we do the same for the italic. Then we do the same for the bold ital. It's becoming a mess. Obviously this is not the proper way to do it manually. First of all use chbt. It will help you a lot with that. That's where I got this one. I would never write it myself. But
639:39
Speaker A
secondly, it means that we need to create systems on how to parse markdown ourself into notion. Going to do this in a second. First, let's send this uh children blocks into notion. I know it looks weird, but just ask can I chat GBT to generate your children blocks where it's going to be heading paragraph um heading two and then paragraph with bold itallic bolt itallic. You're going to get this kind of example. Or if you have access to the course platform, I'm going
640:03
Speaker A
to leave all the code samples inside. Now, let's just run it and let's go have a look what we get. So, here's the new one. Again, I forgot to change the name, but that's okay. Let's open that. This time, look, we're getting our heading one, which is this part right here. Then, we're getting our regular paragraph, which is this thing right here. Then heading two is this one, which is this block. You can notice that it's separated by different blocks. This is kind of block block right here. This
640:30
Speaker A
is empty one. And inside this is how we need to specify inside each block we need to create this kind of dictionary to specify what's the type text and so on looks very confusing but it's just the way it is and it should not stop you because nowadays with AI that's super easy trust me we're going to push it even further because this is not ideal. I don't want to create this freaking children blocks myself manually for everything I want to send to notion. I would rather just copy paste and forget
640:56
Speaker A
about this. So, I hope you're not giving up so quickly because I know it looks a lot of effort and very confusing, but we're going to rely on AI and we're going to create a custom markdown parser. So, we're going to have our markdown syntax. We will automatically generate this kind of monstrous outputs for the children blocks without even seeing it. That's the beauty of using AI today. Also, there are a bunch of notion packages that kind of do this, but the ones that actually work with Markdown,
641:22
Speaker A
they're not supported anymore. They did not work for me well. So I decided to teach you the pure API workflow. Now we need to create our parser. The way we're going to do this is we going to well ideally you should exclude notion key and database. So let's just grab all of that. Let's go to chat GBT and make it work for us because well that's going to be the best way to get the result. So inside of here, ideally use as smart model as possible. I will try to use the
641:51
Speaker A
4 because I think it's the one on the free plan in case you don't have a chbt subscription. I'd like you to help me with notion API using Python. I have a code to connect to my database and create new items. But I need to create a markdown parser so I can relate the correct MD elements to notion uh blocks. Make sure it's a class and it can support and I'm going to provide the context so it can see my code. Oh, inside we're going to just write the children blocks. The idea is to replace
642:38
Speaker A
also generate a comprehensive markdown sample in the code. Lastly going to write provide the full code solution. Can also mark it some dashes so it knows it has a little bit more weight. And I think that should be fine. We're going to see. Sometimes you need to chat back and forward until you get the correct results or maybe it misunderstands something. But we're going to see how it works and if we need to kind of change anything. Also, a really, really big life hack. I haven't used it for that
643:06
Speaker A
one, but let's just grab my input and I'm going to show you how to be prompt engineer like a pro. Let's open a new chat. While it's thinking, I'm just going to write here, help me create a power prompt to get the best result from chat GPT. Here is my draft. That prompt alone is going to look at what you're doing and it's going to kind of rewrite it in way better way. It's not always as powerful, but if you're going to give it a little more context, it's going to work magic. For example,
643:40
Speaker A
you're expert Python developer with deep knowledge with notion API already have code that connects blah blah blah blah. The parser should support paragraphs, headings, paragraph with italic, bullet list, numbers, blah blah blah blah inline code, code blocks. I forgot those by the way. And here require you can see that's already more powerful. um uh prompt we could use. I'm going to copy that. It even told you why this prompt works. Some prompting insights, but we're going to keep it in case it
644:06
Speaker A
fails at the first try because first try whatever came to my mind I wrote and I don't care. Just try to write as much as possible and usually you get somewhat okay results. All right. Blah blah blah blah. Here's the full Python code with markdown parsing. So we can see we need to import array. This is for the reg x. Then there is this huge class. I'm not even going to try to go in and decide what's going on inside. I'm just going to kind of scroll through till I see all
644:35
Speaker A
the code for the class. Let's go into our code here. We need to import ray going to hide the contents somewhere. I want to define the class. Let's make it here after the content. So here is custom markdown parser class. So, I'm just going to paste it and I'm not going to even go inside, right? We're going to rely on AI 100%. So, I'm going to kind of scroll through so we can see. I don't know if you would want to rewrite it. I don't think so. There might be issues as well, but we're going
645:07
Speaker A
to fix it. So, so, so this is the class. It pretty much just looks at the text and parses line by line the markdown. If there is a hashtag in the beginning, it says, "Oh, that's a heading." If there are two hashtags, then that's a heading, too. if there are certain maybe symbols for the code, it knows it's going to be code and it kind of returns us correct um syntax of this dictionary. That's kind of the goal here. So, let's hide it. Let's go back to chat GPT and see what else. So,
645:37
Speaker A
it generated all that and you can see that it changed here the content. It made a little bit different. So, there's heading, there's paragraph, there's quote, bullet points, and so on. Uh we're going to also replace that. So let's find this content replace like this. You can see it's a little bit more stuff. Okay, let's go and see what else. So you can see we're creating parser like this. Then we're getting children blocks like this. Not exactly the syntax I would want, but hey, that's better
646:08
Speaker A
than doing it myself, so I take it. So I'm going to comment it out and then provide here the new version. Looks way better. We can agree on that. Then the headers are the same. Then the data is also the same because you can see it's the children page name today. Right? That's all the same. Response looks the same. This looks the same. Again, you can copy the whole code. I just like to kind of go through and see what actually changed. I could also ask Chad GPT to see what changed. Let's just
646:38
Speaker A
see if it works or get an error. Again, we're relying here 100% on Chad GPT. But you can see something is created. Now I can go and see what did we get. So it's supposed to be So it's supposed to be test page 01. Just delete all the previous ones. I can see just by the time this is the latest. So open. Now let's put it side by side and see if it works. So now let's compare. I can see heading one here, heading one here, heading two in paragraph here, then it's here,
647:08
Speaker A
then here's heading three. And notice there is bolt itallic and bold italic. And it's right here the same bolt itallic, bolt itallic. So it knows how to convert all this kind of markdown syntax into notion block elements which is a mess to do it manually but with chat GPT that was easy. Then the quote is also works. Then here are the bullet points and numbers. Same is here. Can notice that here it uses different one. You can either use minuses e the stars either think pluses as well. There are
647:36
Speaker A
bunch of different syntaxes in markdown that works but you choose whatever it can be the same. All right. Right. And lastly, I can see that the block codes and inline code doesn't work. We're just going to go back to straight ch and say I want more rules. Uh only update the class and make sure it can parse code inline code blocks. I think that's okay. Oh, and hyperlinks just in case. I don't know if it works or not, but just going to say hyperlinks also should work. So
648:08
Speaker A
this way you're just pretty much going to go back to Chad GPT and say hey this is not working just give me the answer wait and try it just go back and forth eventually going to make it if that doesn't work after like I don't know five tries just start all over again sometimes ch goes in this hallucination mode and it's really hard to get out of there it's much easier to start over again and also just remember this trick about the power prompts just literally that one line and write whatever you
648:34
Speaker A
want and it's going to rewrite it properly like you need to look at that that's like way better prompt and you're going to get way better result. Trust me. But now it updated the class. So now I just going to copy the class. I can scroll through and oh well first of all it still writes it. Just make sure it finishes it. Sometimes we copy the code before it even finished. All right. Now it finished. I can click here on copy. Um it's also going to provide you an example of what you want,
649:02
Speaker A
but I don't really care. I have what I need. So let's go back. We need to find our class which we hid right here. So, let's just delete it and paste the new one right here. Again, I can scroll through, but it's not necessary. Just make sure you ask Chad to generate it for you. If you're using something like cursor AI, it's even simpler. It's going to rewrite it for you, but we kind of keeping it a little bit simpler. Now, this time I remember to do this page
649:31
Speaker A
name. Now, it's going to be finally the proper notion summary pages here. And for the content, we're leaving the same thing. Now let's run it. It created. Now I can go into notion. Now here's our finally the proper notion summary pages here. Let's open that one. Heading headings. So on everything works now. Inline code works. Code blocks work. Uh block code. I don't know what's that. Uh for some reason this one is written here, but it's not a big issue. I think
650:02
Speaker A
if you just add here space, it's going to work fine. Honestly, I don't care. I could go back and forth and fix it. Now, let's test it. Remember, we generated these summary files with our uh AI YouTube summarizer. Let's just go in there. And for example, here is the markdown syntax. Let me just show you on the right side. You can see there's bunch of different text. This is how it looks. So, I'm just going to grab it blindly. I'm very lazy, so I'm not going to read
650:28
Speaker A
the MD file. I'm just going to replace it like this. Also, these three lines supposed to be lines. Going to test if it works. We're going to run it. While it runs, let's put it on the side. It finished. So, now we should have it again. All right. And here is the page. So, we have the summary and the heading, the text heading. Here are the numbers. Uh, this is supposed to be bulletin. So, we can just take that and say there are a few issues. I'm going to grab that
650:57
Speaker A
one. I think this is going to be the final time for this tutorial when we're going to ask Chad GPT. So almost there. Let's give it some motivation now. All right. And now here's the sample I tried. Now I'll just send and see what we get. All right. It realized what's wrong. Let's fix it. Now let's just wait that you update the class. Then we're going to copy that, bring to our code, and see if it works. That's the wipe coding of today's. And it's really insane how far
651:29
Speaker A
AI tools went. Because when I start coding with Python, I had to just spend hours or even days on forums looking for simple answers to simple questions. And this notion API, it would take me weeks to figure out all this kind of conversion of the markdown to make it properly and handle everything. Maybe not weeks, but you get an idea. It would take a lot of time. Here 5 minutes and I have amazing code which is still being written, but I don't mind waiting 30 seconds instead of days, you know.
651:59
Speaker A
Okay, it wrote the class. Let's copy that one. Oh, and by the way, remember that I'm using the model which is available on the free plan. Ideally, I would go for the O3 or maybe O4 mini high which is more advanced for the coding to generate even better response and also I could use my power prompt so it gets better results from the start and this is just the simplest way how majority of you would do. So, this already works well. Don't worry. Now let's go back to the code. Find the
652:25
Speaker A
class. Can select it and delete. You can see here's 10 lines and 200 lines. Now it's deleted and paste the new example again. I don't care what's going on inside. All I care about if it works or not. And if not, I'm just going to provide the error message like this key error. Something is wrong. Just copy that thing. I really hope that I would get an error. So you can see that I can just go back and just paste here an error without any explanation. It will
652:50
Speaker A
know what to do. Thank you for the trace back. The issue lies how we access the last item in blah blah blah. And here is the result. So luckily it decided to fix only one thing. Okay, let's grab that one. It's called handle list item. We can go to structure. Go inside your class. Then look here. Handle list item. Can see it's right here. Okay, let's just hope that we need to replace the whole thing. Hope that's fine. Also update stack handling in pars. Actually, maybe let's make it side by
653:30
Speaker A
side. So, so let's go to pars. So, here's the pars. Here's flash buffer. Let's start with that one. What does it want us to do? Self. Okay, I think it already did it. As you can see that here it's self flash buffer list buffer and says that it should become that last list list here. Okay, something is weird here. Let me just try to run and see. Maybe it already works and I don't even need to do whatever it tells me to. Yeah, looks like I go back to notion create a new page.
654:04
Speaker A
Let's see if it works. This way you can see the number at least are working. Nested bullet points are working. This is why it wasn't looking as good previously. Because here you can see it gave the number but then this was a little bit messed up. And now on this one it fixed the nested bullet points and numbers. So it looks a bit better. We scroll through and yeah the horizontal lines also work. Yeah, everything works here. So I'm happy with that. And now we have an
654:36
Speaker A
amazing script to work with notion API using Python. We can take our markdown syntax. We have our custom class from the AI which took a few minutes to write. We know how to connect to our database, create page and you know how to send the API requests to create new pages in your database and now you can also integrate it in other tools. For example, remember a few lessons back we created YouTube AI summarizer where we took the YouTube URL, we got the transcript with Python, we sent it to
655:02
Speaker A
OpenAI, we got our AI summary and we wrote it to markdown syntax. Now instead of saving it as a markdown syntax, we can come back here. We can create a new one to kind of save to notion. You would need to bring your class here. You need to set up your API keys correctly, but it's not that complicated. And again, ask AI is going to help you a lot. And this way, you would be able to create your YouTube summaries and send them directly to notion. I'm not going to
655:29
Speaker A
show it here. If you want me to just write in the comments, maybe I make another lesson, but I think you're going to be able to do it on your own. And then you would have amazing YouTube AI summarizer inside your notion. How does that sound? All right, and that's it for this lesson. And now you know how to use Notion API with Python. I hope you enjoyed this lesson. There are also much simpler API examples for Python out there to kind of get the weather or some
655:52
Speaker A
other things. But I wanted to teach you something really useful that you can use in your daily life to actually make it better. And now you know how to save information inside notion databases with Python. This will help you keep your data really nicely organized and easy to access from any device. Way better than saving in random files, uploading to clouds, and you know the idea. Now it's your time to practice with notion API. Just follow along this lesson and create
656:15
Speaker A
this awesome Python automation. I wish you happy coding. Then I'm going to see you in another lesson. We're getting closer to the end of the course. So see you soon. Goodbye.
656:29
Speaker A
Finally, we're going to try W coding, which means that AI is going to write code for us. And for that we need to install cursor AI ID. We could get AI assistance for PyCharm or VS Code, but it won't be as good as using an ID with an integrated AI features. So in this lesson, first of all, we're going to go and download and install Cursor ID. Then we're going to set it up and then we're going to try using AI chat and agents modes. Then in the next lesson, we're
656:53
Speaker A
also going to build a web app. But for this lesson to begin, head over to cursor.com website and find download for Windows button. Wait until it's downloaded and then run the installer as usual. Accept the terms. Then click next. Next. Then here we can click a few tick boxes so we can open files and folders as cursor projects. This is very common for any code editor. Then next and install. All right. And now we have our cursor. Once you run it for the first time, it's
657:18
Speaker A
going to welcome you and you will need to create or login into an account. You can also skip and continue, but to get the most out of it, it's best to create an account. So let's do that. So there are multiple options. I'm going to go with continue with GitHub, but you can also use Google or an email, whatever you prefer. This is just quicker for me to use GitHub for this. And here I'm going to claim my pro. And it will ask for payment details to get the 14 days trial. I'm totally fine
657:43
Speaker A
paying for cursor. So I'm just going to fill it with my details and continue. But you do as you wish. Then you'll be taken to the dashboard. Here on the side, you will see some kind of news introducing cursor web. Then you can connect to GitHub, Slack, whatever. But most importantly, you can activate your pro trial right here. So I'm going to click yes, upgrade me. And now it's activated. So now we have the pro plan activated. And we can start using bunch of different kind of AI models from
658:08
Speaker A
cursor. So let's go back to the cursor ID. And we're going to just log in again. It's going to take you to the cursor website just to confirm it. And now we're all set. We can return to cursor. Now I can customize the team. You can click around. See, there's also a lot of teams available. I think I'm going to go with that one. Then here's the quick start. And it's good to write it down. Ctrl + I to activate the agent. Cursor tab for predicting your next moves. And inline edit can be done with
658:34
Speaker A
Ctrl K. That's all fine. So let's continue. Then for the data sharing, it tells you that you need to deactivate it manually in the settings and we're going to do it in a moment. So for now, let's just click here and continue. Then language for AI. In my case, I'm going to use English. You select whatever you use. And that's it. Finally, we're getting inside our cursor. And that's how it looks. Let's go to file preferences and go through cursor settings right away cuz remember first
658:57
Speaker A
of all we need to deactivate this uh privacy data sharing. Well you can leave it but I prefer to go to privacy mode. The next we can check different settings here. There editor keyboard shortcuts and so on. You can import it from VS code because cursor is based on the VS code. They just kind of forked it changed it and now it's worth billions. It's crazy. Then in the chat there are different settings here. For example by default you want to have agent or ask.
659:20
Speaker A
I'm going to leave the agent and then in here we can just kind of go through and see if there's anything interesting to us. Then in the tab or models actually in models we can kind of choose which models we want to activate because first you activate it here and then later you can select them inside your chat. You can even kind of keep changing it as you talk to the agent. You can see there are lots of different models. There's even grog for let's activate maybe a few or
659:46
Speaker A
maybe I don't know what's sonic. Let's activate that one. Then there's new feature backgrounds agents. I haven't used it yet. Then for tools and integrations. Here you can set up your MCP tools. Then the rules and memories. This one is interesting because here you can define different rules for your cursor and for each individual project. For example, I can write something like Python code preference keep code simple and readable whenever possible. Obviously it's simple but it helps
660:10
Speaker A
cursor to know the style and so on. So then let's add another one. Write short dock strings inside all functions and classes. And this is you know very simple rules. Over time you're going to kind of fill it up. Also cursor will be automatically filling it in with memories because it will see what you kind of ask it to do and like oh you often ask this should I add it to the memory. It's going to be really useful. Then there are also project rules and you can also write it here but normally
660:34
Speaker A
you would create that cursor rules file inside your project and you would define there the brief of the project what you do this tag and to be honest the more you write in this file the better because then it knows exactly what you want to do. and we'll try to avoid different packages, different stacks and so on. So you can define really well what you're doing and what has to be used but we're not going to do that. I'm just going to skip it to keep it simple.
660:57
Speaker A
Then the next one is indexing and dogs. And in here you can provide links to different documentations for your projects. For example, we're going to work with Django. So let's just click here add docs. And I'm just going to paste here the URL for the Django documentation like this. And you can see right here we have Django. It index at this date. And now we can also index it while we're talking to chat GPT, which is great. Now network, we're not going to do anything here. And in the beta,
661:23
Speaker A
you can opt in into early access if you want to get access to the new features, but it might make it less stable, but it's up to you. That's it about cursor settings. So, let's go back to cursor and actually try start using it. So, first of all, we actually need to open a project. So, we're just going to click here. Then just go anywhere in your computer, create new folder. Already prepared one. And then click select folder. And now you're going to open this folder as a project in your cursor
661:46
Speaker A
ID. The moment it's absolutely empty. We can create file folder refresh. Then here we have search, we have changes, we have our agents. And here we have our extensions. And let's start by installing Python extension which is a must if you want to code with Python inside cursor. Also you might notice that there are multiple Python extensions here. One have 7 millions, other 8 millions. And it was confusing for me which one to install. But just go with the regular one and it's totally
662:11
Speaker A
fine because you will see once you start installing it, you also install the other one because it's just the static type hinting and some other things. So don't worry. Just wait a little and eventually you're going to have Python installed. We're going to close it and we're going to create new file. And here we can just test our environment by writing something like print hello cursor. And now we're going to execute and see. where they can right click and go to run Python and select one of the
662:35
Speaker A
options or there is a button right here on the top to run Python file and once you click it here's the terminal and you can see hello cursor in the console so everything works Python works and our cursor is ready to start working now let's get to the fun part right here on the side you will notice here is where your AI chats and agents live so let's write there something like create a new pattern file with a guessing game asking me what will be the color from mixing
662:59
Speaker A
color one and color two remember meet exercise during this course and let's see what cursor AI is going to do for this exercise. It's going to be quite fun. It's already creating here a new file. I can see it wrote 78 lines and it explains what it did really well. So there's quite a lot of information here, but let's click and see. I'm going to keep all and you can see there's quite a lot of code, way more than we coded. Let's just scroll through and actually let's just
663:25
Speaker A
run and see how it works. So I'm going to run. So round one of the five. What color do you get? Red plus red going to be blue. And it's wrong. The answer is red. Obviously in here. So it's going to be purple. Then orange. Yellow. Blue is going to be green. And red and blue is going to be purple. Good job. We guessed four out of five. And we know our colors well. But overall, it's really nice. We just wrote one sentence to our agent. And now we have this code. Obviously, we
663:54
Speaker A
can simplify to make it harder. It's all possible here. Now let's say that we are beginner just going to go here in the ask mode and ask something. Just going to write explain the code line by line like I'm absolute beginner. Let's just see what's going to happen. Also in here we can provide context by using this at symbol and choose this color mixing game pie. So it knows exactly what I'm talking about. So line one it's going to tell us about the import. Then line 3 to 13 it tells us
664:20
Speaker A
that we're creating a function. Def means this. This is the name of the function arguments parameters so on. Then from 14 to 25, this is called dictionaries. It's actually really nice that it kind of takes pieces by pieces, explains what's going on, gives you all the keywords, what they mean. I really, really like what we get here. So, print displays text on the screen obviously. And here's some variables. Then this is how you create comments. Really, this is a good tutorial like
664:47
Speaker A
because you just take your code, put it here, and then cursor going to break it down line by line like this. It's awesome. Okay, and that's pretty much how it works. Now let's go back to the agent and maybe we want to make some changes. Let's write here I'd like you to refactor the code using OOP principles. Create a single class cuz sometimes it can create multiple classes. So let's keep it simple. Now it's it will refactor it in a moment. Let's just wait while it's kind
665:11
Speaker A
of working and booms it made the changes. So we can scroll through and you can see red what was removed, green what was added. Here you can also read in text what's going on. For example, key op improvement single class constructor instance variables method organization and better encapsulation main function. First before even going through let's click on run and see if it works. So what color do you get by mixing red and blue and let's just kind of guess wrong to see what's going to
665:37
Speaker A
happen if you're going to give lots of wrong answers and says keep practicing you'll get better at color mixing. All right and let's see. So here is the class. Then there is an in statements where we define some attributes. Then a few different methods. There's everything is really well documented and it's actually nice and also you can use the cursor AI while you code as a inline assistant. Let's click on Ctrl K and this time create fun as our text saying colors. Let's see what's going to make.
666:06
Speaker A
Ooh, this is much nice. Yes, we take that. All right. And now let's play the game. Let's just kind of drop red. We guess nothing. Keep practicing and in the end we're going to get this color sprint statement which actually looks really awesome in the console. All right. And now you have cursor ID ready to start w coding anything you want. But just a quick note AI is really amazing but you should always stay in control of your own code. It's really easy to get lost in the AI chat and
666:33
Speaker A
agents and then have no idea how anything works in your own app. You will understand what I mean once you start working on your app and the code is going to start to grow really really big. And then even AI will eventually start hallucinating. It will start failing to help you fix any bugs and eventually it might even break your app. So experiment, push it to the limits and ensure that you always learn from AI so you can stay in control of your scripts. But for now, let's head over to the next
667:00
Speaker A
lesson where we're going to VIP code a web app together from scratch. It will be a fun exercise to test how far we can push cursor. So as always, happy coding. I'm going to see you soon.
667:15
Speaker A
In this lesson, let's use cursor AI and actually create a web app together from scratch. For that, we're going to use Django framework in Python, which is amazing to create any kind of website or web app you want. It will use Python as a back end running on the server. And then you also can use HTML, JavaScript, CSS for creating the front end of your website. And don't worry if you're completely new to Django. I used it in the past multiple times, but I'm not an
667:39
Speaker A
expert. I still have to Google different commands and the ways to do certain things. But in this lesson, we're going to pretend like we know almost nothing about it and we're just going to rely on cursor AI and see how far can we push it to actually create it for us. And without further ado, let's actually jump in and just begin this lesson right away. So here's the cursor ID and first of all, let's create a project. So click here, open folder, and go anywhere you
668:00
Speaker A
want and just create a empty folder. I'm going to call mine something like pi voting app. Now we created our empty project. You can see there's absolutely nothing. And before we even going to start using kind of cursor agent, we need to do a very important step. We need to create a virtual environment for this project specifically. And the reason is simple. Think of virtual environment as a box that contains all your Python settings, all your packages, the Python engine itself. And this way
668:25
Speaker A
you will use this box only for your project. And later when you're going to work on another project, you're not going to use this box. You're going to create a new one. This way all the packages and configurations, they're not going to kind of uh mess up with different projects. And it's not complicated. We're going to click right here on the view and we need to activate terminal. In the terminal, we need to write a command. It's going to be python-m then vin which stands for virtual
668:49
Speaker A
environment. And then we need to write the name of the folder where it's going to be contained. The default name is something like venth which is very common and it's going to just lay in your project. But you can also call it whatever you want even voting app whatever. I think I'm going to go with that one but using vanf is also fine. Let's click on enter and it's going to take a little bit. Then once this command is run, you'll notice that in your project you will see this folder v
669:14
Speaker A
voting app. This is your virtual environment. This is where your python leaves. You can see here is the python x like a copy. There also a library of all the packages where we will be installing and so on. And to use it we need to activate it. For that we need to go to scripts and then there is activate. But we need to run it through the terminal. So I'm just going to write here we need dot dash this is for local kind of uh project. Then we write voting app. You can also use tabs to
669:41
Speaker A
autocomplete in case it's a longer sentences. Then we go to scripts and then we need activate like this. And now you can see it's activated because it's green right here. However, if you're going to get an error and I'm going to show you a screenshot of what you might experience, then don't worry. You just need uh one more command because you're missing some permissions. I need to run specific command to kind of allow it. I'm going to show it on the screen. Just
670:04
Speaker A
follow it like letter by letter, execute it and then next time go back and try to activate it again and then it's going to work whenever you're going to see this kind of name of your virtual environment in parenthesis. Now what does it mean? We can also test it. We can write here pip list. You'll see right now we have only two packages inside and these are kind of default p packages. We could also kind of upgrade. You can see to upgrade we can run this command and
670:26
Speaker A
that's fine. We can just copy that one. Paste it here. Enter. Accidentally added a dash. Let's run it again. And we'll see it's just going to go and update it. Now we can clean it by cls. And we can also install Django since we will be working with Django. So let's write pep install Django. And here's the thing. If I'm going to open the cmd and write here pip list, that's going to be the global packages available to all projects. And you can see I try to keep it minimum and
670:53
Speaker A
simple. I don't install anything here cuz I always work in virtual environments. I can verify this by writing here p list. I'll see that these packages are specifically inside our virtual environment inside your box. And you're only going to use this box for this project. So you're not going to mess up any other projects. And trust me, it's going to be amazing way to execute it. We can also save our requirements. People pleas this kind of dash requirements.txt.
671:18
Speaker A
And if you're going to write it, you'll notice that here's the requirements file. It's very common to do it in the end in case later you would need to change from one machine to another. You have a list of all your packages there are specific versions and it can be really important to kind of recreate your project and keep it running because with some updates sometimes something breaks. So it's best to keep the same versions but this is not necessary but it's good to run it in the end. For now
671:40
Speaker A
let's just delete it. All right. Now you know enough how to create virtual environment and we are pretty much set and ready. Now we're actually going to start working with our agent. So let's make it a bit bigger. Make sure it's agent here. I can also select one of the models. Uh for the models, we can go to file preferences cursor settings. Somewhere here they're going to be models. You can kind of go through the list and activate whatever models you want. And for this lesson,
672:04
Speaker A
I'm going to be working with this cloth for set model. And notice that there are two of them. That's because of different reasoning. This one has thinking in parenthesis. This one does not. So this will be more kind of go in depth and kind of speak to itself before it's going to spit out the answers. So yeah, I think it's going to be great for this project. Now I prepared the prompt. So let me just copy that one and I'm just going to copy it here and quickly read
672:26
Speaker A
through. So I want to create a web app for online polls and voting using Python plus Jungle framework. The idea is I want to have the main page where I'm going to see the large heading and on the top there is going to be a very nice style of a large active poll in the middle with some shiny effect. There should be filter on the top to toggle between active and past polls. Let's say it should be multi- option pole. Then for the design, it should have the modern UIX design with minimalistic
672:53
Speaker A
approach, but make it pop with modern styles. Use rounded cards and make everything interactive to encourage user to vote. And you see it's very weak. I'm not saying specific colors because I just wonder what curs you're going to do on its own. Ideally, you would want to at least let it know, do you want a light theme, dark theme? Do you have any color preferences? Do you have any special styles? Maybe you want to make it like a bento layout. Maybe something
673:14
Speaker A
else, you know. I'm just going to keep it very weak and open just kind of so cursor can interpret it whatever they want. Then in the code note, I want to use tailwind for the front end by adding uh URL to styles. I don't want to install it locally. Then keep everything simple, short and readable when possible. Keep comments in the code for absolute beginners. And then use that ven virtual environment for this project. Actually, I called it ven uh voting app.
673:41
Speaker A
And this might be important because at first agent going to try to run it with a global and you don't want to and you don't want it to install anything globally. It can mess up a few things. So just make sure it uses virtual environment. So it's a good note. And yeah, that's it. We're going to keep it very simple. We could also drop it in CHBD. Ask it to refine it for some super prompt. It would make like multiple pages output. I'm fine with that. Just going to select my model right here and
674:05
Speaker A
we're just going to execute and see what we get. Also, when you start working with agents, ideally try to keep your request and features to the minimum. You don't want to kind of describe everything like Facebook app does and then say make a clone. You want to start with a prototype, then add one feature, another, and so on. It will be much easier to kind of handle what's going on. And in case you get any errors, it's easy to go one step back and repeat it again instead of kind of deleting and
674:30
Speaker A
starting from all over. And it will be asking us to execute various commands. So here first of all you can see it tries to activate our virtual environment which is great. Then tries to start the project. I'm just going to click on run. I don't like when it combines commands. It's often gives some errors but let's see what it does. See now it tries to do it separately. Try the first one. Then the second one Django admin can also modify here if we don't like
674:55
Speaker A
something. For example, Django ving app. I want to maybe do it differently. call the project uh pi bolting app and then all apps inside with app prefix. Just a little note of how I would like it to do. And if you're not familiar with jungo framework at all, it might be a little bit hard to keep control of what's going on because for example here it wants to start project. It means kind of create a folder structure of your project and naming it's not that important but I like to have certain
675:27
Speaker A
conventions. Now I think it gets stuck in the terminal for some reason. So I often click here on shift so it continues to the next step. I don't know why it happens. Let me know in the comments if you also experience this thing. You can see command was cancelled. Would you like me to do uh please continue?
675:50
Speaker A
So start the project. I mean it already started but let's try it again. Now you can see it ran very quick and I really like that it has this to-do list. You can see everything it's going to do. So create project create pole choice models foring system and so on. Now we can see kind of runs through creates a bunch of different files creates model settings. We can click through and see what's going on. And here it might be a little bit too much if you never work with Django.
676:17
Speaker A
But in a nutshell, example here is your app folder. You will see there's admin, apps, models, test, views. There are more other ones and not all of them are necessary. For example, we're not going to use tests. But in models, we define sort of the model for your databases, how you want to structure the data, what fields you want to have and so on. So we can later reuse it. In the admin, it's more interaction in the admin dashboard with your models and so on. You can see
676:41
Speaker A
here what's going on. It's not that complicated, but when you do it on your own, it's actually hard because there's kind of a lot to wrap your hand around. But with cursor AI, it's going to be so easy. Trust me. All right. Now, it again got stuck in the terminal because it should not be hard. This command runs really fast. It's kind of make migrations and migrate. You do this to kind of update the data in your database after you create the models and so on. I think we're going to click here.
677:09
Speaker A
probably it's going to ask us what's wrong. I know it continues on its own. So now it's fine. So we can see this time it migrated really quick. Now let's have a look at all the to-do list and we can also do it right here on the bottom. Maybe better because it's not going to disappear. So I created the project. We created poll and choice models. We configure Django admin. Then create views for poll listing voting and results. Then create more than template with Tailwind CSS.
677:34
Speaker A
This is the front end how it looks on the website. Make make migrations. configure URL routing. I think we close to the end. Just needs to make the templates. It's going to take a while because HTML can get longer. Actually, not that long. Here's here it is. And you'll notice that it's not a standard HTML. It's made specifically for Django because of the syntax because we can inject our kind of if statements, our different properties from Python logic as well. And here we can see for
678:03
Speaker A
example if messages, then do this, then end if. You get used to that. It's actually not that bad to combine HTML with Python logic. Then there are poll details, poll list. Okay, it keeps writing HTML. So, we just have to wait. Oh, now it's important. Notice that we're creating super user. This is like an admin for your Django website. So, it gave it username admin email for some reason this and at least it asks me to provide password. I'm just going to call it admin as well. Also,
678:34
Speaker A
it's very common when you start typing, you don't see any stars. This is so nobody can steal your password. My password is really bad, but I'm just going to override all these rules. All right, and I think we're coming to the end. I I see we're on the last step, which is great. Okay, I can see that server is running. So, one thing you need to know about Django is that you're going to keep working on your project. Then, when you want to run your server, you have to use
679:00
Speaker A
this command python manage.py run server. This will spin up the local instance of your server. Going to go to view terminal. We actually I thought we're going to see here running. I don't see it for some reason. Oh, but I think it still run some tests. So, let's wait a little longer. And now voting app is complete. So, I cannot wait to see how it looks. It tells me that it's already running, but I don't see it in my terminal. So, let's just open it up. And you can see
679:34
Speaker A
it cannot be reached but that's not not an issue. We're just going to come here write python manage.py run survey. This step is to kind of define on your website what's going to happen on different URLs. You're going to write www myebsite.com-poll maybe you're going to go to the website maybe you want to execute something what's going to happen on homepage and so on before the line. So we need to write what was it? um do this whatever I call my van scripts activate like this.
680:05
Speaker A
Now let's clean it up. And now if I'm going to run python manage.py run server. Now it's going to run it. So I think this was the issue that I also had to activate it even though I thought I did. Now we're not going to care about this one. Just hold control then you can open your hyperlinks because this way nothing happens. So this way it's going to open. That's how it looks. So what do you think so far? Let's see. Have our pass polls. We can view results.
680:35
Speaker A
Not bad. I I like the graphics. Let's go back. Active. There are a few things we can polish for sure, but that's a good start. So I don't like this voting hub. Looks a bit ugly. The colors. Maybe we're going to ask for better colors. And then let's see. How do you prefer to work? Vote. hybrid. This this this fully remote cast your vote. Okay, so I already see a few things that I don't like. First of all, it's huge. I have to scroll through and I cannot even see five options at the same time.
681:08
Speaker A
This is the 100% zoom. It's still really large. Secondly, I don't like to go to the new page. I want it like a popup model menu. We're going to ask Chad PT for that. And um yeah, I don't like this spacing as well. Again, when you fix issues, just try to keep it simple. Do not go overboard with too many kind of features at at the same time. This one is nice. It has the leading choice showing you what's winning. Share share results. So, we click here, share a link. Huh, interesting. Let's
681:42
Speaker A
say Outlook. Huh. And then it's going to open Outlook. Check out the results of the poll. What's the most important feature in the code editor? Did not include any URLs, but it's already a start. We could leave it. We just ignore it. So, where do we begin? Let's go back to the cursor. And for this, there are two options. One, we could use the same agent, but I like to create new agents for new features. It helps it kind of manage it a bit better. This case, it's
682:10
Speaker A
not as bad. We just kind of started. It did not use as many tokens. I think it says 23%. So, we can still run a few commands with that one. Then, we're going to create new agent. To create new agent, we're going to go right here. We can see all your agents. We can even run multiple agents at the same time, but they often need the same file, so they're going to stop and it might mess up things. So, keep it simple. Actually, let's go with a new one we already made it. Now, I'd
682:38
Speaker A
like to make some changes to my current Jungo project. Instead, I want a nice nice popup menu. Also, ensure the size is fitting on the screen. It's too large right now. This is feature number one. Also adjust the colors used. Again, I'm going to see what it's going to do. Let's just enter it. And now we have our new to-do list. We can read it. Convert poll apps to model pop-ups instead of new pages. Exactly. Replace purple gradients with modern appealing colors
683:19
Speaker A
here. Again, I did not define what color I want. Probably I would go for blue because psychologically it's a really good color, but we'll see. We're going to see what this chooses. Then replace emoji with SVG. Ensure models fit properly on the screen and add Ajax functionality for seamless voting without page refresh. That's also a nice feature. And now we just wait. All right. Also once in a while you might notice that you need some interaction with the agent. Can also
683:48
Speaker A
allow listed so it knows that it can run this command on its own. So we don't always have to say yes it's okay to run whatever. Now again I think it just got stuck in this terminal. I don't know what's going on. Just going to click here on shift. It's supposed to continue after that. And now you can see it starts working and it's really fast. So I don't know why it gets stuck. Okay. And now our changes are complete. Here's the new colors, enhanced header design, model popup system, Ajax
684:18
Speaker A
integration. So what it means for us is that you can go and test how it looks. Bing. It's still running. Can see here it is. It's ready for use. So with control, I'm going to click right here. and it's going to open it up. So, I like this colors way more. The icon is much nicer. Voting hop. It's okay. I would probably want it to be kind of centered and this one's smaller, but other than that, it looks pretty good. We have an issue with model. That's very okay. I'm going to f we're
684:54
Speaker A
going to fix it in just a moment. So, what we do, we're just going to copy one of them. It says page not found. So there's something about URLs or templates. Probably URLs in this case. So let's go back. And I get an error when I open my card as model. I'm just going to paste it like this. It will know what to do. Okay. It tells me that it fixed it. Yes, there was some issue with the URLs. Now let's go. And it's good to refresh it. Then vote and now way way better. So
685:33
Speaker A
which technology trend excites you the most? Whatever. Let's try that one. So cast your vote. That's good. Then fully remote cast your vote. Then let's say whatever it is uh Python. So the booting is pretty good. It tells me it's recorded and everything. I think I can vote multiple times. Yeah, this is not good because now people can come in and kind of influence the votes. But now it's good because it means that we can test what's going to happen if we going
686:07
Speaker A
to just do this. So we're not going to ask it to fix right away.
686:15
Speaker A
So I made a bunch of votes and it's pretty nice how it works. I really like it. The colors, it sorts the results. So if we're going to put this going to go too, you can see all this one is still leading that's great or that's okay. In the past if you're going to view results that's also fine. So I'm happy with that part. Next let's solve this alignment. I really don't like it. And we also going to make this one smaller and voting hop in the middle and really bold.
686:47
Speaker A
Maybe let's make it on the side so it's easier. Great. It works. A few more bucks. The vote now button should be aligned to the bottom otherwise it looks messy. Then another one. Then this text is too big. Make it smaller. And this text is too big. And actually uh remove it. It's not necessary. and move the subheading participate under active calls to lastly make voting hub logo in the middle. Actually, no. I'm going to leave the first one. Quite fine with that.
687:46
Speaker A
Just going to make these two quick changes. And uh yeah, here it's okay. I'm quite okay. What's going on here? So, we're going to do that and then we're going to go and look at something else. All right, I think it has finished everything. Here are the visual improvements on. So, let's go and test it. So, we can see right away the text removed. Much nicer. Also, this one aligned nicer. And uh overall, it's not complicated. You can see to make all these changes. That's nice. We would
688:15
Speaker A
also probably make the footer always on the bottom, but that's okay. I can just zoom in, pretend that's fine. Now, one really important thing is that while you work with agents and you have somewhat kind of the state where you like what you get, how it works, you should definitely back up. If you're new to Git, I have a whole tutorial during this course and there's a separate lesson for that. You can go there. I'm going to break down the basics. What is Git, how to use it, and how to start as a
688:39
Speaker A
beginner really, really quick. Let's come here. And now we can see it's much better. So the current folder doesn't have any git. Would you like to get it? Publish to GitHub. So first of all, we're going to initialize repository. You can see changes. The page might tells me that all the files in your project, they're going to be committed. That's fine. And let's write here uh by voting version 1.0 or maybe 01. We're still in beta. So commits. There are no stage commits.
689:11
Speaker A
Blah blah blah. Yeah, that's okay. Now you can see it was pretty quick. Now I can publish the branch. I don't know if I'm even logged in. Yeah. So we need to sign in. Let's click allow. Copy. Continue to GitHub.
689:28
Speaker A
So here is device activation. I'm going to go with my account. So here I need the code and I can see right here. Open this page and tab and paste one time password. So like this. Continue. Authorize this connection. That's okay. Authorize. Now my password. And we are all set up. Our device is connected. We can close this. Now I can click publish the branch. Publish to private repository. And uh yeah, I think we are ready to push it. Now look at that. It asked me
690:09
Speaker A
would you like cursor to periodically run g fetch and fetch is just pretty much check is there any changes on the github to kind of see somebody else updated your files for me I don't really care for now and now look if I'm going to go to my GitHub account go to repositories you can see there is pi voting app that's because what's the folder is called and here is everything we did so far here is the version and now we can work with g the beauty is that there's this feature
690:36
Speaker A
called branching and we can keep kind of updating ing all our changes kind of saving it here as a milestone and if anything goes bad we can always go back to the previous step and start it all over and it's really important when you work with agents because you probably have experienced how they can hallucinate either judge or even agent themselves they can start making so many bugs even they cannot fix it they just like well good luck this way will definitely help you keep it organized
691:01
Speaker A
and have kind of backup option now we have this set up it's okay I'm going to create a new agent and remove that one. And here for the context, now we're going to focus on the next feature. And this is going to be the admin dashboard.
691:20
Speaker A
All right. And the feature is ready. And I cannot really wait to see what it actually did. You can see here what it does. The statistics, quick tools, enhanced admin experience, and here's how to access their two links. So the main dashboard and enhanced admin dashboard. So let's click on that one. And it's not running because we can see our environment is not activated. So let's do that with arrow app. I can go until I can see this activate command. And now again I'm going to find this
691:50
Speaker A
python manage.py run server. Oops. Do not put any dashes. My bad. Now you can see it's running. So I can activate that one. So admin admin in my case in your case. Okay. So we have some stats on the top which is pretty nice. Then here are the quick tools. Create new poll, manage polls, view results, recent polls, top performing, currently act. It made really a lot. And then most popular poll. I feel like they're trying to sell me something here just just because of this icon.
692:22
Speaker A
Now let's start from the top. I cannot really click. It's just to see. Maybe I would make one less so it fits in the line. But now let's test if this works. Great. New poll. We can have here questions. Let's write test test end date doesn't matter. Let's just add a few hours. Now in here it says enter each choice on a new line. Example option one, two, three. So okay, test one. Option two. Hello. What about this? Now I'm going to create poll. Something was created thing empty. Yeah,
693:01
Speaker A
I can see it right here. And now if I'm going to go to local host like this, we can see here's the test. Here are the options. It ignore the empty line, which is also great. And we can cast our vote. And overall, this app works pretty good. We have a nice looking dashboard. Not everything works exactly like I wanted to, but with enough chatting back and forward, we could fix it. And it's definitely better than we could do manually in this short amount of time.
693:28
Speaker A
Cuz pretty much, think about this. We just gave a few prompts to agent, waited, and then it made a bunch of things. Then we said, I like that, I don't like that. Change the color, remove this, remove that, change tile here, then we waited a bit longer. And now we have this, a proper web app. You can see it all functioning right here. You can see all the votes, all the past votes. We can kind of move it around. We have our amazing dashboard, which looks pretty good, their top performing,
693:55
Speaker A
recent polls, and so on. And if you'll do it manually with Django, especially not knowing Django, well, that will definitely take you days if not weeks. But with cursor AI, we already started, we made it, and it's great for prototyping. It's not always the best for kind of creating production ready applications because with AI you you still have to know programming to make it proper to add security implement databases because using AI it can skip a few security features then people can
694:24
Speaker A
flood your database with random data and so on and probably you even seen a few post online where people like I'm building AI app in public and then like help somebody attacked me I don't know what I'm doing this is why it's not enough to just start using AI tools we still need the basics of Python and I still need to understand the frameworks you use. In this case, we use the Django framework. So you need to understand the basics how to create a project app, what's the difference between model
694:48
Speaker A
views, how to maybe create different controls in the admin dashboard, connect the models and so on. Once you know the basics and you know how to use AI, putting these two together will result in a really, really amazing apps. But until then, don't sweat it. Just start building crazy prototypes, make lots of web apps, and just make it fun. Like I'm sure you can build a lot of things and even one of them can really become something that many people want to use
695:13
Speaker A
and who knows maybe you're going to make a business out of it. Oh and also before we go I really forget that uh if you want to host this apps to actually be on the website like www. I don't know pyoting.com here's the thing I'm not going to go in detail now but you need two things. First you need domain can go to any website like namechepeep godaddy um cloudflare there there are a lot of them. So you're going to buy the domain. Then you need to create a server. And
695:38
Speaker A
for the server, I want to recommend you render.com. I'm not affiliated, but they are really, really good at what they do. And it's really quick and easy. Then you're going to create an account. You can see I have a few services running. Then add new. I'm going to click web service. Then you will select your GitHub with your project. You're going to spin up on the server. And it's very easy and quick to do. And find tutorials online for Django and render. You'll see
695:59
Speaker A
how easy it is. And now, well, that's it about this lesson. I want to wish you happy coding and hopefully you're going to create amazing apps with cursor AI and Python. See you soon. Goodbye.
696:16
Speaker A
Congratulations. This Python course is coming to an end and I want to share the most important advice for learning Python. You see, courses are great, but knowing something and never using it is the same as not knowing it at all. So, practice Python to get better at programming. And if you don't know where to start, I want to recommend three great options for you to choose from. And the first one is to solve small daily challenges. And it's one of the best for absolute beginners. There are
696:40
Speaker A
platforms like Czecho, lead code, code wars, and many others that have library of Python challenges that you can solve in 5 to 30 minutes. And it's amazing for building a daily habit and focusing on one small thing at a time. And you will learn a lot by solving those. And the best part, you can also compare your results to other people. And you'll often find that your 100 lines of code could have been written in 20, 10, or even a single line of code. And you will
697:07
Speaker A
definitely want to learn from these examples to improve your own coding skill. And actually, let's go over this examples and try to solve a few simple challenges on cheekio lead code and code words. So you can see how it looks inside and how it can help you improve your own Python skill on daily basis. Let's start with a cheek. It's on Czecho.org. org website. It's free. Here you can see you can select Python. Then you have to log in and either start a new game or continue the game. I
697:33
Speaker A
already have an account, I think. Let me try. I'm going to use the GitHub to connect. All right. And now I'm on the platform. You can see there are bunch of different islands. There are numbers. I think it's your points. And then inside there are going to be bunch of different challenges. And the kind of higher you go, the harder the challenge is going to be. You start here with the home. Then you open another challenges. Let's go here for example for O'Reilly and see
697:55
Speaker A
what kind of challenge we get. So you can see I already have some solved and we can scroll through and there are different levels and see there's elementary, there's simple, then there is uh elementary plus I think there are also intermediate advanced. Let's just scroll through for example moderate and yeah there's moderate plus and so on. So you can kind of select challenge with whatever you want. Example let's select something simple like here's the sum of digits. So we don't spend a lot
698:21
Speaker A
of time here and then we're going to see what is the challenge. So for this challenge the sum of digits uh here's the task. We're giving an integer like 38 here for example. If it consists only of one digits simply return it value. So it's going to be if it's two then we return two. If it consists of two or more digits like here's one two then we add them together and they show it here 3 + 8= 11 until the number contains only one digit and return it. So we do not
698:49
Speaker A
return 11. We keep adding until we have this kind of single digit. I think that's what they do in numerology but overall that's not a complicated task. Now here are a few examples of for example if you run this one this is should be your result. This is what assert does. It just checks does this equals to this and if and if not then it's going to give an error and scroll down and here is going to be our code block. To be honest used to be way nicer. I don't know why they decided to
699:15
Speaker A
make it worse but well let's just continue. You can execute code here. You can click on it. It's going to execute. And we can see here assertion error. We did not pass this test because it's supposed to be two. And we returning zero for now. So let's go and code it. What you need to know here, this one you don't care. It's just for you to kind of test your code. It's an example of the print statements and assertions, but you just need to fill in this function. So it's
699:39
Speaker A
very simple. Let's clean it and actually write our code. So we have a number and we need to add digits. For that we need to convert it into a string and convert it back to integers and then join them together. Now there are different ways to do this. I'm just going to use list comprehension. So actual numbers is going to be a list. I'm going to return number for number in string of number and since this is a string we need to turn it back into an integer and thanks to that we can create new
700:13
Speaker A
total. It's going to be just sum of all the numbers. You can also kind of make the print statements to kind of test what you're doing just to kind of better understand. You click here for example example 11. You get some results here. Uh also at first it tells here that if it's just one digit we return the value. So let's just write it. If number is less than 10 return number then let's see we're creating this total. And here we have to create recursive because we don't know how many
700:40
Speaker A
times we need to do this. probably in their example, it's not as many, like two or three, and we could just kind of copy and paste it multiple times, but we're going to do it the proper way. We're going to make recursive. I haven't covered recursive functions, but here's what's going on. It's a function that calls itself. So, here we just need to return some digits with total. What's going to happen? We're going to get a number. We're going to check is it less than 10? If yes, then we return it.
701:06
Speaker A
That's it. Otherwise, we're going to create a sum of all these numbers and then we're going to try it again. So this time, for example, let's say that we had 38, 3 + 8, 11, right? So it's 11 right here. We're going to try it all over again. Now with 11, we're still not returning it. 1 + 1 equals 2. Then we start it again. And then at this time, it's going to be two. Now, if I wrote everything correctly, it should execute. Yeah. So mission is complete. Click to
701:32
Speaker A
check result. So click here to check solution. It's going to check even more examples because this is not the only ones. There are probably hundreds of them and then hey task is solved and there's like lots of them and yeah they actually added huge numbers. So you cannot really copy paste the same solution. You have to use recursive here. This is solved. Now let me just see where I can compare now because they changed everything. I used it so long ago. Also right here is where you can
701:56
Speaker A
get the hint if you don't know how to solve the mission. Can click on it. It just tells you the idea is to convert the given numbers in string then convert a digits back into integer. Then you can continue and you get more and more answers. Oh, it tells you that you could also use the while number which actually would be much simpler solution. I just thought about recursive right away for some reason. And you can kind of keep asking for help and actually learn it one step without
702:20
Speaker A
actually getting an answer from chat GPT or something. Then I don't remember where to compare. Oh, you can see that here the best solutions will be open in a moment, which is strange. Let's reload it. Okay. And here we have to actually wait to see the results. So, I think they're trying to get more paid members, but and actually check used to be so much nicer than now because they well, first of all, they made this one uglier. It was really nice before. Well, it is what it
702:46
Speaker A
is. But anyway, now let's move to the next one, lead code, and it's amazing. I highly recommend you to try this one out. Let's just sign in and solve a challenge together. Again, I'm going to go with the GitHub option. Now, let's go to problems and we're going to select something here. Going to scroll through. Let's just go like Roman to integer. Why not? All right. So, here Roman to integer. And as you know, there are this Roman letters I, V, X, L, C, DM, and they
703:16
Speaker A
refer to numbers. M is for millennium is going to be a thousand. C is for century 100. X for 10 and so on. And the thing is that it's very simple. We're just going to kind of take whatever we get here. Here are some examples. For example, that one. Then we're going to convert each one into a number. So it's going to be 55 5 11 one. Then we're going to combine it all together. However, in Roman numbers, remember that we also sometimes subtract it. So there should be some example here. And the
703:44
Speaker A
thing is that here, so it's not going to be 1,000 plus 100 plus,000. It's going to be,000. Then because C is smaller than that one. Therefore, it's going to be this minus this. It's going to be so cm is 900. I hope you know what I mean with Romans. I don't really want to explain it. But just know that somewhere here there is like explanation if you want to read it. Just know that normally. But in general, just know that if next number is bigger than the current number, then probably
704:12
Speaker A
the current number subtracts from the next number. That should be it. So let's see what we do. Uh first of all we need to create a dictionary of all these things. Just give me a moment. I'm just going to fast forward. So here's my dictionary. All these letters equals to all these numbers. So far simple. Now we're going to come here and we just need to write our solution inside this method. So let's see what we're going to do. Uh first of all we need to convert it into
704:38
Speaker A
numbers. So number is going to be with list comprehension. I'm going to write what is it? dictionary Romans I uh for I in S. You can see here's the S string. So assume it's just going to be letters like this. This is going to give us a numbers. Now we need to understand how to uh add or remove them. So but for example we could just write here sum of numbers but this would be the wrong result. Let's just test it actually so you see what happens. Let's say that you think this
705:11
Speaker A
is the solution. Then somewhere here you click on run and it's supposed to say wrong answer. Actually for case one and two we made it right. But then this one we fail because here this is where subtraction starts. So we need to check the next number and so on. So it's not always that straightforward. Uh there are so many ways to get it but I'm going to use the while loop. Going to define here a few things. So we're going to say total is this and the current index is going to be this and
705:42
Speaker A
while current index is less than length of uh numbers then we keep going once it's going to be higher then we covered all the numbers and that's it. We try to make it very simple so we're going to write a current number and next number let's just call it next and current. It's going to be maybe like this cur. So this is the current. It's going to be nums of the i. The next one is going to be nums i + 1. Now, keep in mind that this one you might get an error because it's going to
706:19
Speaker A
be out of range. Let's say we have five numbers. We're on the fifth iteration. It's going to be six. Here it's going to say out of range. Normally, you would check it first, but I'm just going to kind of cheat and just add here try except statement. And next is going to be zero. It's not the best, but it's just a quick and dirty way to get to the result. So, one of these is going to be true, and I'm fine with that. Now, we need to check if current number is more than next.
706:51
Speaker A
Also, we need to name it something different. Uh because next is actually a function for iterators. Uh if it's more then we do something here. If it's less then we do something else. And in here we also need to if current is more or equal because it can also be equal 11 one and we do something. And then here we're going to say total plus equals uh what is it? Current number. And here we say total minus equals current number. Uh return total. I need to run it. I'm not sure if it's
707:33
Speaker A
complete or not. So I think this should be fine. Let's just run and see if we get any errors. So we run judging. No pressure. And then we hit an infinite loop because I'm stupid and I forgot to do this. Whenever you make a while loops, make sure you iterate, you increment your values. I often forget this, but let's see. And accepted case one, two, three. That's fine. So, our logic kind of works. Now, I think we need to submit it. And usually when you submit, it's going to
708:08
Speaker A
go through way more examples. And now we're going to see. Yeah, that works. So, in lead code, what I like is it's going to tell you how good is the runtime, how good is the memory. For example, in memory, you can see mine beats 84% cases. Not bad. In runtime, I'm kind of in the middle. So, I could optimize it and try to execute it much faster. You can see somebody made some crazy stuff there. Well, some people well just kind of takes a bit longer. That's okay. Again, we could go and
708:36
Speaker A
optimize it, but I think I just want to go and see examples. Now, I need to find where it is. And I can see here the solutions. Click on that one. And let's see. Here's the clean explanation. So, we can go to Python. Tells you, okay, here's how to convert it. Here's the logic and so on. So, you can find a lot of explanations here. people like to write it out and you're going to learn a lot. Let's go to the next one, Code Wars. And here we're just going to log
709:05
Speaker A
in. I think I also have a account with GitHub. Now here you can see suggested challenge Python. Yes, Python is good. And then convert string to camel case. Let's just click here and see what's going on. So complete the meta function. So it converts dash delimiters word into camel strings. So if it's death stealth warrior, it converts to death stealth warrior like this. If it's like this, it's like this. So there are different kind of dashes. That's okay. I think I just click here on the train.
709:37
Speaker A
It's not as intuitive, but it's also fine. Okay, here's the function. Here are the simple tests. And this is where we're going to write our code. So here's the task again. And just look at the examples. You can see if that was this, then this should be the result. So we're going to remove all the dashes or underscores. I'm going to replace uh the first letter of each word with a capital one. So it's like this. It's called camel casing. And the first one also doesn't change
710:02
Speaker A
here. It's uh lower case, here's uppercase. So first one stays intact. So let's begin writing this function. So first of all, we need to split into individual words. And there are like two symbols, this and this. To keep it simple, we're just going to write words uh text equals split. And we're going to split by this one. And to make it also at the same time, we're going to also replace all dashes with underscores. This way we're going to kill two two birds with one stone. So now we have a
710:33
Speaker A
list of words. Now we need to convert it into camel case. So words uh camel and we're just going to use list comprehension. So it's going to be word capitalize for word in words. And now we need to join it into a single camel string. So camel is going to be this. And we're just going to join all the words. Words camel. So that should work fine except we still need to address the first letter I think. First of all, let's try to run it. We scroll down. Here's like test
711:07
Speaker A
attempt. Let's click on test. And we passed. Passed. But this one did not return correct value. You can see it should equal the the kind of lowerase letter. So let's just fix it. Probably just going to separate the first word. So first equal words pop zero. And then here in the camel we're going to write first plus whatever is the rest. So let's do it like this.
711:42
Speaker A
Now we're going to test it and see what we get. Now, now I can see all the results passed. We can attempt it and it's going to just give more examples and we passed 200 assertions. So, we finished this code and again when you solve it, you can also go and try to make it even simpler. So, you can combine multiple things together and that's up to you or somewhere we can check the examples if only I remember where I think right here in the discussions going to click there and you can see how
712:11
Speaker A
people solve it, what they do. Some people going to use way more lines of code. Some people going to have oneliners. So yeah, I think there should be solution. I think that one right here. Click on solutions. Yeah. And here are all the examples. You can also vote best practices, clever and so on. So this is the simple one. There are oneliners. You're always going to see them and they are often very confusing. But I can see people solve it different ways. Some
712:34
Speaker A
some people take more lines, some people take less. That's all fine. As long as you solved it, that's great. Then you can learn how others do it. Oh, what's going on? What is this? That's that's a lot of effort. Okay, so this is the code wars. As you can see, there are lots of these small challenges. I think they call katas. I'm not really a code wars user. I just know it exists. I tried it a few times and I want to show you that there is this option and it's not bad honestly. Lots
713:00
Speaker A
of examples here. All right. And I hope you start to see how much you can learn by solving these challenges, especially on daily basis. And over time, you can increase the difficulty and even learn more. I highly recommend you to try it out for a few days and hopefully it's going to turn into a habit and then once you're going to get more comfortable with Python, you can try to create bigger projects and there are two main ways to think about it. Firstly, you can
713:23
Speaker A
create an app clone. It's very popular way to challenge yourself. Choose an existing website or an app and try to make it yourself with Python. Make it more limited and add your own twist to that. For example, you can take an Instagram and create your own version of photosharing app with a twist. This is a good option because you already have an example which is used by millions of people. So you know it's working well and now you can reverse engineer it and
713:48
Speaker A
try to make it yours. And it can also be really fun to kind of remake existing apps. And again you probably have no idea how to create any of the steps. Authentication how to store data in databases how to create accounts and so on so on so on. But don't worry once you're going to create a simple plan and break your idea in a smaller steps it won't be as hard to follow along and figure it out. one step at a time. And trust me, you're going to learn so much by actually doing it. And in the end,
714:14
Speaker A
hopefully, you're going to also have a really cool project. And especially today with AI, you can create prototypes so quick, even if you barely know programming. Just make sure you understand the code and you refactor it manually, too. So, you are in control of what's going on and you're going to learn a ton. And again, you're going to have a cool app by the end. All right. And lastly, you can also develop your own project. You might already have a cool idea and you should definitely try
714:38
Speaker A
to make that happen. Even if you have no idea how to do any of the steps, just try it. You will learn so much when you start developing your own app and that's the best way to learn programming. For example, when I was learning Python, at certain point, I wanted to challenge myself. It was around the time I used to listen this Reddit stories with robot voice. I know it's weird, but it was fun at the time. But anyway, I wondered, could I automate this? How hard can it
715:02
Speaker A
be to create this automation? So I decided to challenge myself and this was also before AI boom. So I had to figure it out the old school way with Google and Stack Overflow and a lot of trials and errors. So I sat down and made a plan. So I needed to parse Reddit for popular posts, get post content, then get all the comments, then create audio from text, then create images from comments, then sync images and audio so it looks like animated. Then how to edit
715:27
Speaker A
video with Python. This was much easier and more fun than I imagined actually. Then how to add various like background music, different elements, pop-ups, CTAs, all of that. Then how to upload it to YouTube automatically. And lastly, how to create a thumbnail and like title and so on. At the time, I didn't know how to do any of these steps, but I thought, well, I give it a try. And in less than a week, I had a prototype that made these videos. It wasn't perfect,
715:52
Speaker A
but I proved to myself that I could actually do this. And then I spent more time refactoring code and making it better. And eventually, it worked pretty good. I generated around 250 videos really quick in like one or two months and at the time the only step I couldn't automate is creating thumbnails to look good. So I had to do it manually. I could do it with Python but they looked ugly it was just worthless. But today with AI that would be so easy with all
716:15
Speaker A
these kind of Sora and other models. But eventually I got bored and abandoned this project. But it's still one of the most fun projects that I've made and I learned so much about development by actually doing it. So, I highly recommend you to challenge yourself and try to execute an idea that you have in your mind. You never know where it's going to lead you. All right, and that's it, guys. Just make sure you practice what you've learned during the course and start coding as often as possible.
716:41
Speaker A
This can really change your life because it has certainly changed mine in terms of how I work and how I think about problems. Cuz after all, programming is a problem-solving skill, not just writing code all day. So, it will make you a better problem solver no matter what you do in your life. And it's a really great skill to have, especially nowadays. So, I wish you happy coding and then I'm going to see you in the final lesson where we're going to finally wrap up this course. I can't
717:05
Speaker A
wait for that one. So, see you. Hello world. This is the final lesson of this Python course for beginners. And I would like to say huge thank you for staying this long with me. We've covered so much about Python and I hope that you've learned a lot. And most importantly, I hope that you actually started using Python. This would be the biggest win for me as an instructor. This course was a huge passion project for me and I wanted to create it for the past three years. But the time was never
717:34
Speaker A
right. But in 2025, I decided that this is the year that I'm going to make it happen. And well, I did so, but I didn't expect this course to grow this big. And I even had to put my business on hold for many months. But I guess I just got too excited and I wanted to share as much as I could so you could enjoy this journey. And I would appreciate if you could share this course with a few of your friends or maybe leave some feedback in the comments. I would love
717:57
Speaker A
to hear about your wins and what kind of projects you actually started working on and also how much you learned. This will also help this video in YouTube algorithm and hopefully it will reach 1 million views. That's my biggest goal for this course. And also a last important note, I want you to know that this YouTube channel is going back to teaching architects and engineers how to code with Python inside specific Autodesk Ravit software. That was the main goal of this channel all this time
718:23
Speaker A
and I want to go back to this. So don't get surprised when you see new videos coming on that topic. That's just what I do here. And finally, I want to wish you happy coding on your programming journey and I hope that you're going to enjoy coding as much with Python as I do and create many amazing projects. Again, wish you all the best and see you soon. Goodbye.
Topics:PythonPython for beginnersLearn Python 2026Python programmingPython projectsPython automationPython AIPython web developmentPyCharm setupProgramming basics

Get More with the SozAI App

Transcribe recordings, audio files, and YouTube videos — with AI summaries, speaker detection, and unlimited transcriptions.

Or transcribe another YouTube video here →