Transcripts
1. Introduction: Hello and welcome to a new class. This is Python for beginners and had the unions, and I'll be your instructor for this class. And as jump to the outline. So first of all, we'll talk about why we use python. And then we are going to setup by them and our computers. Then we are going to discuss python syntax than types, operations and functions. And addition to that, we are going to cover decision-making such as the if statement. And then we are going to talk about while loops and for loops and how and when to use them. After that, we're going to talk about how to read and write into files. Then on jump to strings in Python, moving on to data structures. And we're going to discuss less dictionaries, tuples, and sets. You also have the datetime, module and zip functions. And I thought they are important, so I included them here. And finally, we are going to discuss recursion and how to think recursively when solving specific problems. And finally, we have our project that would be to build a calculator. So this is it for the outline. Now let's move on to why she should learn Python. So basically, Python is one of the most popular programming languages in the world. And it is used for automation, AI, applications and websites. And this class, we are going to learn the basic concepts in Python as well as data structure. So welcome again and see you in the next video.
2. Set Up Python: So let's go ahead and download Python now. So you go to Google and type python. So this is the first Python page and we press on download, download Python. And at the time I'm recording it is the 3.9 virgin shot. Just press it and it will be downloaded. Now that we downloaded Python, let's go ahead and click on it. Let's wait a bit. Will get install. Now, just make sure to check this box at Python 3.9 and install now. And now you get setup was successful. Let's close this and check if price than a street installed on our computer. So just type Python. We get Python version, which is 3.9. Now let's go ahead and download our Visual Studio, which is an integrated development environment. It's where we're going to create and run our Python code so that you could download. And we'll get to this page and press on Windows, since I'm using a Windows ten and the 2P downloaded. Now, simply press on this and wait a bit, will get this license agreement simply accepted. Click on Next, Next, and then Finish. And this is our Visual Studio code, is as the welcome page. And here can create our files or our Python code. So now that we have our Python and Visual Studio downloaded on our computer, See you in the next video.
3. Values And Types : Let's start with values and types. In Python, we have types such as STR, INT, float, and bool. So for example, if we have a string of words, characters, anything that would belong to the type STR. And if we have an integer such as the number 101520, this, this belongs to the type i and t. And if we have, for example, the number 25.5 where we have a floating point number. This would belong to the type float. And finally, we have the bull. And in this case, the only consists of two cases and either true or false. And they both Boolean expressions and Boolean types. So they will belong to the type would. Let's go ahead and create our first program. So we'll go to File append folder. And let's create. We have here the Python folder, just select it. And here we have python folder. And here let's create our five, creating new file and save it in the Python folder in desktop named burst. But save. And here we have our first by Ethan file. So as we said, we have some types in Python. For example, a string such as hello world. And if we want to know the type of this, drain, can simply print. What we should print is the type of this. And lets run this code. So we get class SDR. Now, if we entered, for example, an integer here, let's say a 100 and undisclosed one more time. We'll get the type INT. Same thing with a float, a 100.5. We get float. And finally, let's try running this code we get. Now, remember that Python is case sensitive. And for example, if we typed true with a lowercase t, will get an error saying that true is not defined. And we'll get to the keywords or the reserved words in Python later on in the next videos. But for now, remember that we can try D, but with a capital L, with a capital T. Now let's go ahead and print some statements. So use the print method developed for us and Python. And let's print hello, world. Close the parentheses and this code will get hello world. Let's for example, the number one, and we get one. Now, let's assign these values to some variables. And this is called an assignment statement. So for example, if we don't want to print this number directly, we can simply assign it to a variable called number would be equal to one. And then if we want to print this number, we can simply refer to it as the name number. Run this code, we get the same result. And of course, the type of this variable is the same type of the value which refers to, for example, here we have an integer stored in the number and a variable called number. And if we go ahead and print the type of this variable number, we get I, N D, which is an integer. Now how do we write our variable names? First of all, a variable names can contain both letters and numbers. However, we can't start with a number. For example, if we name our variable one, Alex, Python will give us a syntax error saying that this is an invalid syntax. We can't start with a number. The same thing for example, if we use the add character after the variable name, that will also give us an error if it is legal to use up uppercases letters. But it's good idea to begin variable names with a lowercase letter. And it's good to mention that we can use the underscore character. For example, if we have a variable name that consist of two words, for example, hello world. And we don't want to put them together. And we can simply use the underscore between them. So let's go ahead and create it here. And the visual pseudocode, for example, if we want to name our variable Hello world, instead of writing a word like this, we can simply use the underscore. And it will be working correctly myself. And now we have our hello word variable. Now, let's go ahead and talk about some reserved words in Python. So for example, we have default n12 and as, assert and break. And these words are reserved in biofilm recant use them but for their specific purpose. And we'll talk about each and every one of them later on in the next videos. But for now you just need to know that we have some reserved word in Python that we can't use as variable names. That being said, See you in the next video.
4. Operations : Let's now talk about operators. So for example, we have the addition operator, subtraction, multiplication, division, and remainder. For example, if we have two numbers, x two is equal to one and y equal to two. And we need to add them and give them, assigned them to a variable called a. In this case, if you want to add them, we simply write x plus y. Let's go ahead and print a and see what we get. We get three. Now, the same thing with subtraction, minus sign, get minus1. Multiplication should be, will be the star sign. And this code will get two since one times two to the vision. And in this case, we get 0.05. and if we want to get an integer in this case, we simply add another division sign and we'll get 0. Now if we want to have the power of three to the power two, for example, let's say e to the power two x. And to start sine. And this code will get nine. Staying and going to talk about is the remainder. And in this case x remainder of two. This is Director of the remainder. And if we divide x by y, we get a remainder of one. And this code will get one. Turnout that we have our operations and operators. As spoke about expression. An expression is a combination of values, variables and operators. The value all by itself is considered an expression and so as a variable. So for example, if we say 12, this is an expression. And if we say x is also an expression, x plus 12, same. And for example, let's say that we have the following expression. And in this case, x would be equal to x plus three to the power of x plus x plus two. And multiply it by five divided by two. And let's talk about the other, that this operation is going to be executed. So as we know from simple math, first of all, we have the high priority quality parentheses, then followed the power, and then multiplication, division. After that, follow the addition. And the last thing is from left to right. So here, first of all, we perform this k to the power of x. And let's say that Ax is you could do in this case, first of all, three to the power of x, that will be seed superpower of 29. And we have x plus two, which is four multiplied by 5-20 divided by 21010 plus 919 to 21. So let's go ahead and print x. And we should get the number 21. Since this. And this will return a floating point. And if we want to have an integer, will simply convert this x, two and x. And this code will get, when you want as an integer.
5. Type Conversion: Let's talk about string operations and type conversions. So first of all, let's have two integers, x to be equal to two and y equal to ten. And in this case, if we go ahead and print x plus y, Python will automatically perform the addition operation here, and it will print the number 12. However, if we have x and y, S drinks and we sequence simply indicate that the last train by adding these ear. And if we run this code, we get 210. So the plus operator works with strings, but it is not as addition. In the mathematical sense. We said it performs concatenations, which means it joins the two strings and link them together. So as we can see here, if we have 210 and print them x plus y, we get 210. If we want to have a space between them, simply add one base and this code, we get to ten. Now we also have the star operator. And this operator works with strings also. And it multiplies the contents of a string by an integer. So let us suppose we have the string that is cold, one, which should be equal to a load. And in this case, if we've brand one multiplied by four, Let's go ahead and run this code. So we'll get hello, hello, hello and hello. So it will print hello four times. Now if we want a space between them, we could simply add here. And this code we get for hellos represented like this. Now we can also add integer and floating point in one expression. So let's delete these. Let's create an integer and a floating 0.90.5. Let's go ahead and print x plus y. So our code will print 30.5. And in this case, the integer is converted implicitly to afloat. And we can use the built in functions and, and float and Python. For example, if we want to have an integer, in this case, we can simply open the parentheses and add x plus y closed parenthesis. We'll get 30. And for example, let's suppose we print acts as a floating point. If we print x alone run this code will get. Then, however, if we use the float function, we'll get 10. And we can use these exact functions to convert between strings and integers and floating points. So let's suppose we have an integer string x, which is equal to ten as a string. If we print the type of acts, we get STR, indicating that it is a strength. However, if we want to store this number and a new variable, let's call it y. And in this case, we can use the lm function with the x as an argument. And let's go ahead and print the type of y. And we'll get end. And the same thing with the float. However, if the string does not contain numeric characters, who Python regenerate an error. So let suppose we have Hello. In this case, let's print discovered. We'll get an error saying that invalid literal for int because we don't have any number here. Now, also, if we have less, suppose we have number here. And even if we run the code one more time, we'll get the same exact result. So every time we have one character or one letter in the string, and we tried to convert it to an integer. We'll get an adder. And in the next videos we'll learn how to handle this adder, how to cut from the string, and how to ask the user for an input. So see you in the next video.
6. User Input: Let's move on to reading user input. How can we read? Uses input? It is very simple. We can use the Python built in function called input. And this function gets the input from the keyboard and returns a string. So let's suppose we need to ask the user for his name, and we need to store it in a name variable. And in this case, would simply write input, open the parenthesis, and write whatever we want to ask them. For example, what is your name? We can add this. And this represents a new line which is a special characters. And in this case, if we added here, Python will automatically jumped to a new line and let's see how it goes. So here we have what is your name? And the next line we can guide Adi. And let's suppose we don't have this. Lets run this code. We are obliged to write on the same line. Now that we have our input stored in the variable called name, we can use it and printed out, for example, let's say print hello. And the name, the user, right? And in this case, what is your name that's here. And this code again. So what's your name? At the it will print hello. Now let's go ahead and convert this input. So for example, if we expecting the user to type an integer, and we know that this input function only returns a string. So in this case, we need to convert this string to an integer. We already talked about the end function, built-in function in Python. So we can simply, let's ask the user to enter an age or his age. And in this case, we need to convert this input from a string to an integer and store it in the variable called age. And the number, let's say, and here we have the number. And if we go ahead and print this age plus one, just to check that this is a real integer and a string, for example, into their age than we get 11. So this is it for how to convert an integer as string to an integer. Let's now talk a little bit about comment. Everything after the hashtag character. This one is ignored by Python. For example, we can write here that our code will print the age of print. And normally we write comments when we have a very large good and we're working with other programmers. So to avoid confusing them or confusing ourselves, maybe later in the future, we can simply write the simple line or a simple sentence just so we can know what we did here. Now this code is pretty simple, but when we work with huge and large, that will be easier for us to just write these comments. So that being said, this is it for this video. See you the next one.
7. Logical And Comparison Operator : Let's move on to comparison and logical operators. First of all, let's start with the Boolean expressions. Let's start with something simple. For example. The footprint, then equal to that. And this expression will simply compare these two numbers, 1010, using the double equal operation. And let's run this code will get a Boolean expression and it says true. Now, if we have 11 in this case, would get false. So true and false are special values that belong to the class bool and then not strings. So for example, if we bring the type of this expression, simply get booed. So when is this? This is one of the comparison operators. And all the, of the comparison operators return a true or false depending on the expression given. So let's go ahead and try these comparison operation. First, we have the the not equal to. Then we have the less than, less than or equal. We have the greater than. And finally greater than or equal. So for example, let's go ahead and write some code using the not equal comparison operator. So for example, one. And that's assigned to variables a equal to ten, b equal to two, and a not equal to b. And in this case, the not equal. So we're going to get that a not equal to b. In this case it is true. So we are going to get true. Now let's compare a if it is less than b, and in this case it's not less than B, we get false, less than or equal to b. And the same here, a, which is ten, it's not less than or equal to two. However, it is greater and greater than or equal to b. Now, we still have some logical operators, such as not our intent. So let's try them here. And let's go ahead and delete these. So for the logical operators, let's use them. Some expressions. For example, x is equal to ten. And if we're not, x is less than, than. Here in this case, let's suppose, let's see, here we have 15. And in this case, let's work with this. X is less than 15, it is true. However, if we write, it will negate whatever we have next to it. So here we have True. And since we wrote, not really get false. So we have our false. Now let's go ahead and use the end and the logical operators. For example, x is equal to ten, x is less than five, or x is less than 20. And in this case, when we write the or logical operator or when we use it, we just need one case to be correct for the whole value to return true. And in this case, we'll set x is less than five. It's not less than five, however, it is less than 20 and it should return true. However, when we say and we mean that this X, it should be less than five and less than 20. So the two conditions need to be satisfied in order to return true. However, we only have one condition that is satisfied, which is Ax is less than 20. However, ten is not less than five. So we will get false. So this is basically at four logical operators. We still have one thing to add. For example, if we had anything with a true, it should return true. For example seven and through, print 17 and through we'll get true. And this is it for the comparison and logical operators. Let me just write here comparison and logical operators. So that being said, See you in the next video.
8. If-Statement : Moving on to conditional statements, we have the if statement. So let's suppose we have a code and sometimes while writing it, we need to execute a specific statements under a specific condition. So let's suppose researching for a specific number or a specific character in a string. And whenever we find this character or this number, we just printed out and say that we found it. So how do we do that? We will use the if statement. If we found the specific character, will print it out. Now, as we can see here, first we have our condition. If this condition is true, we need to execute a specific code and then move on to the rest of our code. However, if it's not true, then we'll do nothing and just jump and continue with our, with the rest of our code. So let's go ahead and write the if statement in Python and middle EDs. And let's suppose we have an integer, x can be equal to ten. And we are going to check if x is positive. So if is greater than 0, followed by a colon. And that indicates that we are now in the black in the body of our if statement. So after writing df and followed by the condition, we need to write our body. Now, every line of our code in the if statement must be indented. So for example, if we say print x is positive, and let's run this code will get an error saying that expected an indented block. So how do we indented with simply press down? And let's run the code one more time. We'll get AX is palatable. Now if we write something here for example. And let's run this code, will get access positive and the end, even though x is, if it's not positive, let suppose f x is equal to minus five. And let's run this code will get the and in both cases, since this print statement is not in the body of the if statement. We also have something called alternative execution. And in this case, sometimes we want to check for a number. Let's say if x is positive, will print x is positive. Otherwise, we print x is negative. So sometimes we want to do something if a specific condition is satisfied and another complete guaranteed thing if the other effect is otherwise. So here we have the alternative execution. How it works, we'll simply write out good will face a condition. If it's true. We'll do an action. If it's false, we'll do the alternate for action and then we continue with our rest of the code. So let's go ahead and implement this here. For example, we say that if S is positive, print x is positive, else print x is negative. And in this case, let's run this code. We have x is equal to minus phi, sorry, get axis negative. And of course, the end, since the end is not involved in any of these blocked. Now let's move on to changed conditions. So sometimes we might want to have more than two conditions. For example, we need to check if x is positive, this is negative and if it is equal to 0. So how do we do that? We will use the f and the abbreviation of this S, f is LF. So for example, we're going to check x is positive, print x is positive. And here we write LF, And in this case, print axis Netherland negative. And finally, if both are not satisfied with, just simply print that x is equal to c. And let's go ahead and non-discursive will get, actually, we didn't specify the condition. And what we going to do it as specified at x is negative, print x is negative. Otherwise, when tax is equal to 0 and in this case x is equal to minus five. So we get x is negative and the end, our profit is equal to 0. We get that x is equal to 0, followed by the end. Now we also have something that is called nested conditionals. And in this case, we have an if else statement. And inside the body of the f or the body of the else, we have another if-else statement. So let me demonstrate it here. Let's suppose we have x equal to two and y equal to 3. First of all, we're going to check if x or y are equal. And if this is not the case, then we're going to check if x is less than y or x is greater than y. So first of all, we simply check if x is equal to a. And in this case, we simply print that x and y, our equal. However, this is not the case. We'll get, we'll check if x is less than y. And in this case, we'll print x less than y. Otherwise, we'll print that x is greater than y. And in this case, x is two and y is three. So we get x is less than y. So we could have done the same exact code using chain conditionals. So instead of entering TLS here, we can simply, let me type it here. F x is less than y. Print x is less than y. And otherwise, we'll print that x is greater than y. So if I run the code, here, we have the column. And let's run this code on one time we get x is less than y, so we get the same output if we use chained or nested conditionals. Now, let's suppose that we have an integer x. And in this case, we're going to check if x is greater than 0 and less than ten. So we can write that if x is greater than 0, we're going to check if x is less than ten. And we'll print x is between 0. And then, and in this case, f x is equal to two. If we run this code, we get access between 010. So we wrote this code using one f statement. So we're going to check if x is greater than 0 and x is less than ten. So we simply use the and operator. And x less than ten. Simply print x is between 010. Let's run this code and we'll get the same exact result as before.
9. Try And Except: Until now, we've seen some exceptions. And in this video we'll learn how to catch them and using the try and except. So first of all, let's suppose we have an input from the user and stored it in the variable called m. And in this case, we'll ask the user to, for example, it's asked him to enter his age, age. And after that, we need to convert this into an integer and store it in a variable called age. And to do it that, simply write and give it a parameter of m. And let's suppose we need to print this out. And let's run this code. And let's suppose we have 20 and we get 20. Now, for example, if we write 20 and by mistake we also q. So here we get this exception saying that invalid literal for int, we cannot convert any character, any input that has a character in it into an integer. So to deal with that, we can use the try and except method. So first of all, you're going to try to convert this and store it into a variable called age. If anything occurs, will simply print that. Please enter a number. Since it is surely not a number. And if we succeeded by converting this string into an integer, go ahead and print it. Otherwise we print, press, enter a number. So let's suppose we entered by mistake the letter i. So what we're going to get is please enter a number. So what Python did here is that it tried to convert this input to an integer. And since this input contains a character that is the letter i, we cannot converted. So friends, please enter a number. So this is it basically for the try. And except. Now let's move on to some short-circuit evaluation logical expression. So let's suppose we have the variable an integer called x with a value of five and the other one, y with the value of n. Now, we print x is greater than or equal to two. It should print true. And x over y is greater than or equal to one to 0. And suppose, and in this case, five divided by ten is equal to 0. And if we run this code, we're going to get a true sense, true and true will get, give us true. However, let's suppose we have instead of ten, we have 0. And as we know, if we try to divide by 0, it is at the, the value or the result is infinity. And in this case, if we tried to divide by 0 in Python, we're going to get 0 division error, division by 0, since we cannot divide by 0. With that being said, See you in the next video.
10. Functions: In this video, we're going to talk about functions. So we have some built in functions in Python, such as the type function that we used earlier in the first videos. So let's remember it. And if we have an integer and we want to know the type of this value, would simply use the type and we'll get the class. And so this value is of type integer. Now the expression in parentheses is called the argument of the function. So whatever we have an inside the parentheses is the argument. And the argument is the value of variable that we're passing into the function as input. So let's see some other built in functions in Python. So for example, we have the max and min functions that gives the largest and smallest values in a list, or a list of words or characters, or even integers. So let's suppose we have a string called Hello World. So let's call it S. And inside the string we have hello world. And just like this, and if we print the max and this string, we're going to get w. So characters in Python have numbers. And for example, let's say that the letter a starts at the number 60, and it goes on until a, B, 61, C6, z2 and so on. And here, our letter w is the largest between all of the letters, even though we have this capital edge, or even if we add a capital D here, and let's run this code will also get w. So let's see how these characters are place. So here we have the ascii characters. So for example, we have the letters D capital letters says add 6590. And then we have the lowercase letter with starting at 97 all the way until a 122. So as we can see, that w has a 119, and in this case it is the biggest between all of the characters. Even it is bigger than all of the characters. And let us brand the minimum using the men function and men as. So what are we going to get is the space? And if we go back to this, so let's see, let's add this one, for example, to our code, to our string here. So I'm going to add this curly brackets and Let's run this code. So the maximum is this character. Now we still have the len function. That is, it returns the length of specific string. So let's say that we have hello world, and let's use this length LEN. As undiscovered. We get 12. So this is the length of our string, hello world setting at 012, ending at index 11. However, it only counts how many character we have, including this space. So this way is included and the counting. Now, let's see some conversion functions. We already used one before, and it is the nth function. And let's suppose we have a number x is equal to 15. And if we want to convert that, suppose we have a string called number, and it will be equal to 50. And we want to convert it into an integer. So x will be equal to n, this number. Let's run this code printed out before. Let's print x plus one. And so I'm going to get 51. Now, suppose we entered why you. And let's run this code as we said, that we get this adder. Now we also have a conversion from floating point into integers. So we already saw this one. We also have a float before. Let's convert a floating point into an integer. So here we have 5.5. Let's run this code. We get six, just because we are adding one here. Let me just erase this and we get five. Even if we have 5.9999 undisclosed, we'd also get five. So whatever we have as a floating point will just ignore everything after the decimal point and just store the number on the left. We can also convert an integer into a floating point to, to that. Let's suppose we have an integer called x and d value is five. And in this case, we need to store floating. Let's name it floating, and we use the float function. And then let's print loading, strung this code. And we get 5. We can also compare it to a string. So let's suppose we have this integer five and x as string. Let's use the STR function inside x and print the type of this. So type of X as rank. Let's run this code will get STR. So we have here the number x. Let's print, let's print the type of x and compare both types together. Let's run. This code will get end and SDR. So what we did here is simply convert the x from an integer to a string. So this is it for this video. See you in the next one.
11. Math Module: Let's move on to the math functions. So you have some built-in functions in Python. And to use these mass function math functions, we need to import math. Now, we can use every method inside this math. So one of the method is the seal method. So this function returns the smallest integer greater than or equal to whatever we enter. So let's suppose we have floating point x equal to five by two. And let's go ahead and print that seal. And let's run this code will get six. Now we also have the Math.floor and function. And this floor function returns the largest integer less than or equal to x. For example, here we have 5.2 will get five mass that floor. And underscored, we get five. So let's move on to other functions. Let's, let say we have also the factorial function. And as the name indicates, the factorial function returns the factorial of x. So let's print factorial of four. Mastered Victorian, I'm sorry. And let's run. This code will get 20-fold. We have also the exponential brand, Matt, exponential of three. And it will print E to the power of three. And in this case you will get 20 these numbers. Let's move on to other methods. We also have the power and it returns x raised to the power of y. So this, this function needs, needs two arguments and let's use it mastered bow. So the first argument, let's say three e to the power of two should return nine. Let's go ahead and undisclosed you get 9. If we want to convert it to an integer, would simply use the end function. We get nine. Let's move on to the square root. So the square root function of x, it returns the square root of X. Then run this code axes. Let suppose x is equal to 25. We should get square root, I'm sorry. And run the score it, we get five. We also have some other methods such as cosine and sine tangent degrees. And we have radians. And as their name indicates, they returned the cosine and sine and so on, a given x or a given value. Now, let me delete these and if we print what we're going to get is module math built. And so this is the module math and it is built and Python. So let's use some other functions inside math module. And suppose we need to get the log of a number. So we'll use math then. And the number is three. And that spring, the, let's run this code. We get 0.47 and so on. We also have the math.pi and it represents the pi. It is an approximation of pi actually, and it is accurate to about 15 digits. So let's print math, but pi, we get this number, and these are 15 digits after the decimal point. So this is it for the math class. We have so many functions in this module and you can use any of them whenever you want. And the only condition is that you need to import math module before using any of these functions. And whenever you need to use one of them, you will simply write mass and use the dot operator and dried the function that you want to use. And inside the argument of this function, you write your input. So this is it for this video. See you in the next one.
12. Random Module: Now we're going to talk about how to generate some random numbers. To do that, we can simply use the random module and we need to import it just like the math module. So instead of importing math will import random. And now we can use the functions available for us in this module. And it happened to be that we have a function also called random. So let's suppose we have integer x. And in this case we need to call the random module. I, use the dot operator and use the random function inside. And let's print run this code. And every time we run this code will get a different value of x between 01, but one exclusive, so we'll never get one using this function. Now, sometimes you might want to have a random number between two specific numbers. And we can use here the rand function. So let's suppose we have, we want to have a random number between and within a specific range of five to ten. So grand. And, and inside, we'll go, we'll enter 510. Let's print x, and we will get any random number between 510 inclusive. So perhaps if we stay running this code here, we got ten. So whenever we used the rand and makes sure that these numbers are inclusive. Now let's suppose we have a sequence and we need to choose a random element from the sequence. And we'll cover list later on. But for now, let's suppose that we have as equal to 123456. So we have an S6 elements. And let's use the random choice from s. Run this code. We're going to get five over three to five and so on. So it is going to generate a random element from these elements and d s. So this is it actually for the random module. Just make sure that when you use the rand function, that the element you enter here are inclusive. So for example, if you don't want to include d5 and simply move to six. And same here if you want to include, I don't want to include it, then move backward by one, decremented by one, and insert nine. So this is it for the random class, the random module. See you in the next video.
13. Create Functions: In this video, we're going to create our own functions. So to do that, we use the def keyword. And this keyword indicates that this is a function definition. So let's suppose we need to create a function and go to aim at Grand twice. And it will take my, take some arguments. So at the empty parentheses after the name indicate that this, this function doesn't take any argument. However, we can add something here. If we have an input, we can add it here and you can use it. Then in, inside the body of this function. But for now, let us print hello and print 11 time hello. So when we define this function, after using the column, we need to indent whatever we have in the body. So for example, if let's suppose we don't, we don't indent. And let's run this code. We get this error expected and then tented block. So as we did with the if statement, this is the same case here. So now we have our function. Let's use it here. So let's type print twice and run this code. See what you're going to get. We're going to get Hello twice. So let's be clear here. How do we create the function will just simply use the def keyword followed by the function name. And then we write whatever we want to do inside this function. Then when we want to call this function, we use the same exact name. For example, here we use print twice. So to use it, use dysfunction, we simply write print twice and it will work automatically. Now, once we define this function, we can use it in another function. So for example, we have the print twice and let me define a new function and call it print four times. And in this case, we'll simply call print twice. Two times, twice, and print twice. And a call for a time. Run this code. We're going to get hello four times. So what we did here is we call this function print four times. So we go ahead and look, what do we have in this print four times function, we have print twice. So we need to go to print twice. And what do we have here? Print hello, we print hello, first-time, print hello. So print hello another time. Now, we, after finishing from dysfunction, we go back to our print four times. We also have print twice again. So we go back to print twice. Grant allow two more times, and that is a total of four times. So this is how we can use a function inside the other. Let's now talk about fruitful and void functions. So here for example, these two functions print twice and print four times our loyal functions. We just used them to print something on the screen. Or for example, if we want to have some other effect, again, use devoid functions as long as we don't want a return value. So this is a void function. Let's suppose we need to create a fruitful function. And whenever we create this fruitful function, we almost always wanted to do something with the result and use this result. We need to have a return value. So let's suppose we have an integer inside this function and it is equal to ten. We can't use this integer outside of this function since this integer is a local variable inside. So for example, if I try to print x and let me run this code, I'll get name x is not defined. So how can we use x outside of this function? And here we can return. So for example, if we want to return x, we simply type return x. And in this case, whatever. Whenever we use this print twice method, we can store this x inside a variable. So let me delete these. And let's suppose that this function is called multiply or multiply X. And it, it should get an a value or an argument. And let's suppose that it is x. And in this case, let me just define x before. Ax is equal to two and x two. So what we're going to do is to create a function that takes as an argument x and multiply it. So x would be equal to Ax times two. And then we'll simply return x. Now, if we call this method multiplied by two and run, this code will get nothing since this method just execute. And now we have the value of x which is equal to four. However, we need to store this value in a variable. So let's create a variable called result and call the method here. And suppose we have at, now, if we go ahead and print the result, we're going to get four to what we did here is that we created a variable x with a value of two. And we created a function that multiplies this x with two and return this acts. Then printed out. For example, we can modify this code glass, the user to enter a specific number. So to do that, let's create a variable called x, and input leaves enter a number to multiply. And let's run this code. You're going to get, Please enter a number to multiply. And if we enter five, we get 55. So we can notice that there is about here. Now, what Python is doing is he's considering that x is a string and then multiply it by two. So whatever we have, for example, if we enter Hadi, will get Hadi twice. So it just concatenate two strings together. To deal with that as we did before, we can use the try and catch. But for now, let me just convert this x into an integer. So x will be equal to n of x because we are absolutely sure that we are going to enter a number. And in this case, let me try five. And we'll get five multiplied by two, which is ten. So this is it for the fruitful and for void functions. See you the next video.
14. While Loop: Let's now talk about wild loop and where do we use it? For example, let's take a situation where we want to print statement ten times. And in this case it will not be practical to use the print statement ten times n one code. So we would not try it. For example, print hello, Brent alone, and so on. So this is not practical. So one solution for this is to use the while loop. And in this case, this is can be considered as pseudocode. And this pseudo code is a code that you can read for any progress programming language. So different syntax. Every language has different syntax, but the basic or the algorithm of the code is the same for every language. So let's say we have a and this is the condition. So y a is equal to true. We keep on executing the same code. So we'll keep on doing B. And whenever a is equal to false, will just quit this for loop. So this is the very basic idea of the while loop. And let be demonstrated using an example. Let's go ahead and create our variable. Let's say it is equal to 0. And let's say we want to print out all the numbers between 0 and then it exclusive. So one way to do that is to create the while loop. And let's say y is, x is less than. Then we'll just say rank this number and increment x by one. And whenever we're done, just print done. So let me go ahead and run. This code. Will get 012 up until nine. And then we got the message that is done. So how did we do that? First of all, who created an integer, a value 0? And we set the condition in the while loop. So for x is less than ten, print x. So the first time the editors, the loop Ax is equal to 0 and x is less than ten, so we'll print it, then we'll increment it. So now Ax is equal to one. We're going to check again. X equal to one is less than ten. It is true. So we're going to print it out and incremented until getting to nine. Now, nine is less than ten printed. Now nine x will be equal to nine plus one, which is equal to ten. Now, if we go ahead and check here, ten is not less than ten. It is equal to ten. So this condition is not true anymore. So just quit the loop and continue with whatever we have after this loop. So here it happens to have a print. Dan, So we're going to get this message tank. Now, let's use LS statement with the Y loop. So Python allows us to do that. Let's say we have a count which is equal to 0 is an integer, and wide doubt is less than five. We just rent that discount is less than five. And when we get to five, we're going to print that discount is not less than five. So going to increment down, as we did before, God equal to plus one. And then whenever we get to a point where discount is not less than five. And here we're going to use the else. Going to print that discount is not less than five. And let me go ahead and run this code. See what, what's going to happen. We're going to get 0 is less than 51234 until we get to five. And in this case, five is not less than five. So this is it for using the else statement with a while loop. Now, we also have infinite loop. And this infinite loop is a loop that trans, for an infinite amount of time. One way to do that is to type while and just grew. And in this case, rent, let's say, and, and let me create a variable n outside. So n will be equal to 0. And let's increment by one. And whenever we'd done, we're going to print done. Let me run this code. And as you can see, you're going to get these infinite numbers. So let's examine what's happening here. First, we created our variable, and then we created our while loop, setting the condition to true. Then while true, we're going to print and then incremented. Now notice that here in the condition we have nothing to do with. And so every time it is going to check the condition, the condition will always be true since it will not be changed by anything inside this while loop. So we're going to get true every time we enter the loop and execute the code. Now, we may also do an infinite loop using another method. Let's say we have a variable x and it is equal to 0. X is equal to 0. Just print hello. So that make, run this code. And as you can see, you will get these infinite below. And let me just interrupt the execution of this code by pressing control C. So this is basically for the while statement and what wide loop without about the flow of execution for the while loop. And then we move to using L statement with the while loop. And then we talked a little bit about the infinite loop. So with that being said, this is the end of this video. See you the next one.
15. For Loop: In this video, we're going to talk about the for loop. And the for loop. The syntax of a for loop is similar to the while loop. We have the fourth statement and the loop body. So for example, let's suppose we have a string and we want to print out every character of the string on a single line. So one way to do that is of course to get every character from the string and print it out manually. However, this is not practical as we said before. And the alternative way, or the more practical way is to create a for loop. Let's suppose we have AX and inside this x we have hello. This is a string and four words, four letters, I'm sorry. And this acts were going to print the letters. Or we can say letter. So for every letter in this x, we're going to print, this letter, says pretty simple. And Python is very good at this implementations. And in this case we got edge e, l, l o. And what did he basically is that we took this x hello and Python reads it as a string. And we took every character and place it in letter, and then we print out letter. Now, we also have the range function. So for example, if you want to look through a set of code or specify at a specified number of times, we can use this strain function for each function. For example, if we wanna loop through a specific code for six times, we can simply use this function, let's say for x and use the function range, range of sex and brand X. Let me run this code and see what we are going to get. We're going to get 012345. So here we have six elements or six numbers, and that indicates that this loop run for six times. So what we did here is that we created a range and this range of sex from 0 till, and we gave acts these numbers from 0 to five as you said. And then we print it out. So we got 012345. Now, sometimes we might want to start at a specific index. We don't want to start at the 0. So one way to do that is to modify the strange function. We have an optional argument that we can add. For example, let's say that we want to look through the numbers 328. And in this case, we'll just type them here and we're going to get 34567 and excluding eight. Now, as we did with the while loop, we also have here the else in this for loop. So let's suppose we have this for loop that looped through the numbers between 358, I'm sorry. And in this case, when done, we're going to print out that we are finished. And in this case, just as Brent done. And what we're going to do here is that we are giving x D values between 38, print them out. And then after reaching the number eight, we're going to print that, we're done. So then let me run this code. And we're going to get 34567 and then the message done. Now, let's talk about nested loops. And nested loop means that we have 12 or more loops inside each others. So for example, let me create a string and the string hello. And the other one will be worse. And in this case, we're going to enter the first for loop. And for hello, let me name it. Hello, n x. We're going to enter a second for loop. And in this for loop, and we're going to print x, sorry, hello. And then we run this code and then I'll explain it to go back here. So here we have some, they may seem some random characters or some random, that is, however, they are printed and a specified format. So let me go ahead and talk about what we did here. First of all, we created the string that is x, and this the second string, y with the world. And it. Now, first of all, we're going to look through the axe, Hello, through the characters of this strength. And inside this for loop, we created another for loop. And we're going to look through the characters of the second word. And in this case, first, we entered this for loop. So hello as the first character. Now that is edge. And as we can see here, we're going to print edge, judge, five times, I think. And this is because we're entering the second for loop. And now after entering the second for loop, we have the hello that is fixed till now it is edge. And in this for loop, we're going to look through these characters W0 and d. And we're going to print edge with the A specific character from world. And here we have W, o, L, and D. Then after finishing with the first character, after finishing from this for loop. And then we move back to the second for loop, the outer for loop. And in this case, we're going to change the letter from edge to E. As we can see here. We're going to print E W E E R L. Then we're going to shift it to L two dimes since we have two L's. And finally o. What we're doing here is that we are going to enter the first for loop and fix the hello they character and entered the second one and work in this for loop until we finish from it. Then we can go back to this outer for loop. So in other words, the inner loop will be executed one time for each iteration of the outer four loop. Now, let us use this for loop and some algorithms or code. For example, let's suppose we have a string x, and inside the string we have these random letters. And we're going to count how many times the letter A occurred in the string. So let me create a counter and it is equal to 0. Now let's create our for loop. And for characters that at the end, that is X, we're going to check. First of all, we're going to check if this character is, a character will be equal to a. In this case, we're going to increment counter by one. Then after exiting default loop regarding to print count. And let me go ahead and undiscovered. Going to get a I'm sorry, not equal to a, to this letter. We run this code, we're going to get count five. So we have five a's in this string. And if you want to make sure this is the first 12345. So what did we do here? First, we have our string, and then we have our counter. Now, we created our for loop and loop through every character inside the string by using four character in x. And this is just in a name. We can name it c for example. And it will not affect our code. Just make sure that whenever you change here, you should change here. Now, for every character in the string x, we're going to compare it to the letter a. If it is equal to this letter, than we're going to increment the counter. And after exit, exiting this for loop, we're just going to print the count how many times a occurred in this tank. Now, the second code I'm going to write is to get a string that contains some numbers and compute the sum of these numbers. So let me create a string. And in the inside this tank we have some numbers. And four. Let, let me create this sum first. Sum would be equal to 0 for x or the number x. We're going to take this number and convert it to an integer so that we can use it for the sum. So going to integer to create an integer called integer, and we're going to convert the number and then add it to the sum, sum equal to some integer. And finally, we're going to print out some followed by our son. Let me run this code, going to get 71. So the sum of every number here, one plus two plus four plus three plus nine, and so on is 71. Now, the last algorithm we're going to implement is finding the maximum or minimum and a list of numbers. So let's suppose we have a list of numbers is equal to x, and we have 12423365519. And in this case, let me look through this list using a for loop. And let me create a variable outside. Let me name is Max, and it will be equal to 0 and y entering the for loop, the first time, mass will be equal to 0. And if this number is greater than max, then we'll simply assign this number to be equal to max. Max would be equal to this number. And we'll print that the current maximum is followed by max. Then after finishing from this word MOOC will print the maximum number is. And let's run this code. See what we're going to get. The first time we entered this for loop, we had max equal to 0. However, one is greater than 0, so Max is now equal to one. And we print out that this is the current maximum. Now we got to 44. And in this case, 44 is also greater than one. So the current maximum is 44. However, 23 is not greater than 34, so we'll just skip, it, will skip this condition since this condition is not satisfied. So just skip this line of code and then we're going to get to 5656 is greater than 34, so the current maximum is 56. Now, then 5519 are less than 56, so we don't have to do anything until exiting this for loop and print out the maximum number is and followed by the maximum. Now, this is the code for finding the minimum. So we have this list and we're going to check if number is less than minimum will go to assign minimum to be equal to the number. Print the current minimum, and finally print the minimum number. And here the minimum number is 0, I'm sorry. We need to assign a very large number, let's say 1000. And the minimum number is eight, then three, then one, and the minimum number is one. So this is it for the for loop. It is widely used and we're going to use it so much in our next lessons to see you in the next videos.
16. Break, Continue And Pass: Now we're going to talk about break, continue and past statements. So let us start with race statement. And what is breaks it. Raise statement is a statement that terminates the loop containing it. For example, if we have a foil or wide loop and we went to terminate immediately this loop, we're going to use this break statement. So that's implemented in our code. Let's suppose we have a while loop. While, true. So this is an infinite loop. Unless we're using a break statement. While true, we are going to accept an input from the user. And this is a number. So don't store it in a number. And ask the user to enter a number. And we're going to say done when finished. And in this case, we are expecting from the user to enter some number. And if he enters the, at the word done, we're going to terminate this code. So if this number is equal to done, we're going to break. And if this is not, the case, would just print this number. And after that, we're going to print done after exiting the loop. Let me go ahead and, and discard who don't forget, enter a number and two done when finished. So here we have it is going to ask me again until a number one, until I until done, and it is going to print out done and terminate the program. Now, let's use this race statement and another code. Let's suppose we have a list, xs and this list we have some positive and negative numbers. So let's say we have these numbers, minus 23, minus 46. So we're going to write the code that checks for us if we have a negative number in this list. So as long as we don't have any negative number, we're going to pass through this list. And when we're done, we're going to print no negative numbers. However, if we just find one net ID number, we're going to print out, we're going to first terminate this for loop or a while loop and print out that we have a negative number in this list. So one way to do that is to create a for loop for numbers. And we're going to check. Let me just close this and we're going to check if this number is less than 0. In this case, we're going to terminate this program. Print. This list, contains at least one negative number. We might have more than one, but the first number, the first number we find, we're going to terminate the loop and print out that we have at least one negative number, number and break. Otherwise we're going to just simply print the numbers. So let me run this code and we got these numbers. So we have 13425,543 for three and we reached minus two. So we have minus2 within an printed out. We just check if this number is less than 0 and this condition is satisfied. So we're going to print this list contains at least one negative number and break out from this for loop. Remember, would not just breaking out from this f. This is not a loop. This is an F Statement. And we're going to break out from the forward loop. And after breaking out, done, we can decode here saying that we've done, get angry, get the message that we're done. Now, let's use this break statement in the while loop. Let's suppose we have x be equal to ten and y, x is greater than 0 down to enter this while loop, and we come to print the current value. And the current value of x is equal to current value of x is. And after that, we're going to check if x is equal to r increments S implemented. X will be equal to x minus one. And then we're going to check if x is equal to five. And in this case, we're going to print that x is equal to five. And print out one more time. Let's print done and break out from this group. And we run this code, sorry, here we need to run this one more time. And we're going to that current value of axis 109876 until reaching five and will be equal to x is equal to five. And then going to print out. So what is this code is doing is decrementing X every time by one printing at, so we have 109876 until reaching five. And when we reached five, we'll just print out x is equal to five. Then we're going to print out done, and break out from this while loop. So now we have good idea of the break statement. Now we have the continue statement, and this is mainly used to skip the rest of the code inside a loop for the current iteration only. For example here, when we think that Ax is equal to five and print x is equal to five, then we're going to break out from the loop. We're going to terminate this block of code, as it is, doesn't exist and just continue for the rest of the code. However, let's use continue here. So we're going to check if x is equal to five, then going to continue. And if this is not the case, we're going to print out x. Let's decrement here also. Run this code. She was going to happen. We're going to have 109876, then we're going to skip 54321. So what did we do here? First created the variable x, which is equal to ten, and then we enter the loop while x is greater than 0. And check if x is equal to five, then we don't do decrement it. So x will be equal now to fall and we use the continue just to make sure that we're not coming to print x is equal to five. So after that, if this is not the case, we're going to print out this and decremented also since this documentation is inside this if, else if statement. And at no point in time, we're going to decrement twice, since it is either this or this. And in this case, we got 109876 and we skipped five. Then we continued until reaching one which is greater than 0. So this is it for the continue. And let's implement it in another code. So the next code, we're going to create a variable, and in this case is going to be a word. And let's say hello. So this is our world and, and our code. We're going to check if we have the letter E and F. This is the case, then we're going to just skip it. So four characters, character and world. We're going to check if this character or letter is equal to eight. We're going to continue. And this is not the case. We are going to print out the current letter. And let me run this code first. So we're going to get, we have here, current is not, sorry, here not current. And this is the character. Run this code one more time, we're going to get etched. So as you can see, we just skip the letter e by using the if statement. So here the character is equal to the letter E, and we're going to use the continue statement, skip the rest of the code in this for loop, and go back and changed the letter E to the letter L and follow the execution, the flow of execution normally after. Now let's move on to the bass statement. So the pass statement is in operation and nothing will happen when it is executed. So this path statement can be useful when we have some code. And for example, we have an if statement and we don't know what we're going to do in this if statement or we're going to type that later. We can simply use the past year. So that demonstrated the, let's say we have here the f statement. Will it delete this? And let's suppose I don't know what I'm going to write here. So I'm going to leave it for later. But however, if I don't write anything in this if statement, Python is going to generate an error saying that indentation expected an indented block. So as you can see here, we don't have anybody in this f stable. So we can use the pass. And now we'll get our code and to work normally. So this is one way to use the pass statement. And you can actually add here that we need to add something. So it is as a pass block, add something here. Just so we can remember that we need to add something here and our code is not finished yet. Let's run this code to get. This is a pass block at something here. And this is basically for the break continue and pass statements. With that being said, this is the end of this video. See you in the next one.
17. String in Python: Let's now move on to strings. So what is a string? A string is a sequence. And let's suppose that we have this word hello. And we have been using strings a lot in the previous videos. So let's suppose that we have the string hello consisting of letters at e, l, l, o. And we can access these letters separately using the indices. So here we have edge has index one, e has an index of one, I'm sorry, edge has an index of 023 and so on. So this is the positive indexing and we have the negative indexing, indexing starting with the last letter or the last character in the string as minus1, and going backward until reaching the first character or letter with the largest negative number. So this is it basically, let's go ahead and use this in our code. So let's suppose we have the string x and it will be equal to hello. And in this case, if we want to use the letter H, you can print it out. So x, and we append the square brackets. And we said that it is at index 0 and we typed 0, we're going to get the letter patch. Now let's suppose we want the letter O, can use t negative minus1. You're going to get o. And at the same time we can use 01234. So at index four, we don't get the same result as before. And this is how we access the, this, these characters in the string. Now we can also start this character and another variable, let's say a, it will be equal to Ax at position four. And then print a. And of course we're going to get the same result. However, if we use a number here and a floating point number such as 1.4, going to get an error saying that string indices must be integers, even if we use letters or strength to get the same. So this is basically about how do we access every character in the string. Now, let us move on to the multi-line strings. So let's suppose I have the piece of code. And as you can see, close it here and let me print it out. Print x. And if we run this code, we are going to get the same result. And the line breaks are in SETA inserted at the same position as in the code. So as you can see here, we have some space here, we have some data here, and so on. So it is the exact same structure as we wrote in our code. Now until now we've been using a small string so we can count its length. However, for example, if we're wondering what's the length of this, can use TLS, LEN method or function with the parameter x. And we're going to get the length of this string. And it is one u1, including these spaces here and here. Of course here also. Now, this is it for the length function. However, let's suppose we want to access the last element here. And we don't know the exact number or exact index of this character. So let's store the length and a variable called tanks plan acts. And let's try to print out the last character in the string as usual, x and length. However, if we run this code, we're going to get that we have another string index out of range. So as we said before, let's suppose that we have a string. Hello. It starts at index 01234. However, its length is five. So how do you access the last element? It is the length minus one. And now if we run this code, we're going to get the letter j as the last letter in the string. Now let's move on to the last idea we are going to cover in this video, and it is string slicing. So now we learned how to access one character or one letter in a string. How about if we want to access several letters or in range of letters at the same time. So let's suppose we have a string called X and it has the value of hello world. And in this case, let's suppose we want to print just hello with the comma. So it starts at index 012345. And if we want to print this, we can use print x from 0 until five. However, five is not included. So if we want to print the comma with, really should use sex. Now if we go ahead and print it out, you're going to get hello. Let's print out word alone. So we're going to start at seven and until the end. So how do we say that? We are going to need all of the rest of the string would just use this column and we don't type anything after that. Now, let's suppose that I want just one letter. For example, the letter W acts at 78. And in this case, we're going to get w. Now, the last thing we're going to cover is the negative indexing. And in this case, let's suppose that I want to print out the letter world. And in this case, this character starts at position minus1, minus2, minus three, minus four, and so on. So minus 1234566 since we have negative indexing. So we're going to start at minus six. And, and at minus1 is go ahead and run this code, going to get world. And let me use minus seven. And this code one more time. And we get this space right here. So this is it for string slicing. And we'll continue with some strings characteristics in the next videos.
18. String Functions 1: And in this video, we're still going to talk about strings. And one important dataset is that strings are immutable. So whenever we create a string that suppose we have the string Hello world. And whenever we attempt to change specific editor at the strength that say, we want to change this edge to a. And in this case, let's run the code. And we're going to get the error saying that SDI object does not support item assignment. So we can't change any character in a string after initializing it. However, what we can do is using the slicing we learned earlier in the previous video and add the letter a to it. So let's suppose we want to take these and add the letter a at the first, at the beginning of the string. So we can do that by simply saying that x would be equal to x from one, and so on. And before this would just simply going to add a plus x. And now we go ahead and print this acts. We going to get a hello world. So what did we do here? We just remove this character by using the slicing techniques we learned earlier. And we just added the letter a at the beginning of the string, making it a hello world. Now, let's suppose we have two strength and we need to merge them together. That suppose we have the string x equals two, hello, and the string y equal to o. And in this case, the fluid going to merge them together, you can simply create a new string, let's say a. And the string will be equal to x plus y. So this is how we can concatenate or combined the two strings using this plus operator. Now let's print a. And we're going to get Hello World as one state. So this is how we merge two string to string together. Now we also have the end at the end, but not an operators and the end and not end our Boolean operators that takes two strings to strings and return true. You have to first Chang is a substring of the second fourth otherwise. So let me demonstrate this. Suppose we have a string x and in this train we have the value hello. And let's use this end. So let's sprint. If a is n. And in this case, let's run the code. We're going to get false since we don't have the letter a and hello. However, if we search for e, we're going to get true since we have E here. And let me search for edge and lowercase edge, we're going to get force since Python is case sensitive, remembered this lowercase edge is not like the captain one. So if we use the capitalists were going to get true. Now let's suppose we have a string x, and in the string we have color black. If we search for the word color. So print n, x and run the code, we're going to get through. So not only one character, we can, only, we can always search for more than one character. So in this case, we searched for the word color, and it happened to be that we have a colour in the string x. Now let's move on to iteration. How can we pass to the characters of a specific string? So let's suppose we have the string colors and we'll call it x and colour. So this x, we have several methods. One method is to create a counter. So we have the counter will be equal to 0 at first. And while this counter is less than the length of this x, so how can we have length? We simply use the L-E-N method with acts as an argument inside this function. And while counter is less than length of x, we're going to just assign a new variable, Let's name it a. And in this case, the color R acts at down, would be equal to a, will be equal to x at counter. And we're going to print out this a. Then you're going to increment the counter as usual and plus1. And whenever we return, let's just print done. And let me run this code. We're going to get c, o, l, o, and followed by the message done. So what we did, what did we do here? First to create our counter? It was at 0, and the first letter was the letter C. Since x at 0 is this. Let us see. So a now is c. So we printed out. Then we increment the counter and check if it is less than the length of x. In this case, one is less than five. And we're going to keep executing this loop until we have counter equal to five, then we're going to exit. And print done. So this function, but this wild loop just prints out the letters of the string. And then finally print done. Now, we could have done this with a simpler code. So how can we do that? We'll simply use the AND function. So we'll use a for loop for a and x a. So this is very simple in Python, and Python makes it very easy for us. So what we did, what did we do here? Recreated a character and it is called a. And in this case, every character in x belong to pass through X, and every character in x would be stored in a onetime. So for the first time, it will be seen then 00 and r. And we're just going to print out this a. So if we want the same result, let's print done. And then we'd done. And this code will get too long to get the same result as before. Remember we used 45 extra lines. Now here we only have three lines in the for loop. Now, let's move on and talk about strings comparison. So let's suppose we have strain word. And in this case we're going to ask the user to enter a word, and we're going to compare this word with the word color. So let's say f, This word is less than color. Print that word. This word, plus is less than color. And otherwise, we go to check deferred is greater than color. Don't do print. Word is greater and color. And finally, if these two conditions are not satisfied, then this surely as that color. So we're going to print the word, a word, simply tapping, just say that the word is colored. And let me run this code, see what happen. First of all, buttons going to ask us to enter a word, and this case MI, Type, C, L, and C. Then we just add a space here. And this quote one more time. And in this case your l is less than color. Since how, let, let me just demonstrate how these strings comparison is working here. So first of all, it is going to compare the first two characters in the string. And it happened to be that we have c and c, So it is same. So Python is going to jump to the second character 00 and tapped to be that they are also the same L and N. However, here we have the space or the end of our string, and we still have some letters. And we're just going to assume that this is less than the color. Now, let me try another, another example. Let's say we have the AAA. And in this case, since we just have D and D is greater than c, then we're going to ignore everything after that. After this, even though we have three a's and they all less than 00 and r. But however, Python will simply just compare the first two directors, and in this case, one of them is greater than the other, happened to be the D, C. So we're going to get this string is created than color. And finally, let me just type color. We're going to get the word is colored. And by the way, as we said earlier, Python compared the characters as numbers. It uses the ascii. And in this case we have the letter capital a. It is equal to 65 and all the way to z. And it is also equal to 90. And then we have the lower cases a and starting from 97. And the latter is equal to 122. So let me just strike this and a 122. So Python is going to compare the letters as numbers and then give you the result as greater or less. So this is basically for string comparison. That being said, this is the end of this video. See you in the next one.
19. String Functions 2: Now let's talk about some string methods. First of all, we have the DIR function that lists all of the methods that is available for us in Python. For strength. Say suppose we have a string a, and in this case, let us print the DIR and run this code. Going to get all of the methods. For example, we have ADD, contains hash in it, lower l strip, may trans, and so on. So here we have all the methods and let's go ahead and use some of them. So for example, we have the capitalize, and in this case, let me just adjust this string. And this function will just convert the first character to uppercase and make the rest of the string as lowercases. So even though if, even if we add some uppercase characters here, we're going to get capital and all of the characters that are following ad should we just be automatically converts it to lower cases? We also have the methods upper and lower. And in this case, upper converts all lowercase letters and strength to uppercase and lower do the exact same, opposite. So it converts all uppercase letters in the string to lowercase. So let's suppose we have this drink and let's use the upper method. And I'm going to get this result to get Hello World and capital. And let's use the lower. And in this case, we're going to get Hello World and lowercase letter. Now, let's talk about the starts with method. And this method determines if the string is a substring of string, starts with a specific substring and it returns true if this is the case and false otherwise. For example, if we have this string hello world, let me just get back to normal. We have a little. And in this case we're going to check if the string starts with hello. And to do that, we simply just type S dot starts with. And we're going to type what? We are going to check. And in this case we're going to check if the string starts with hello. And if this is the case. Going to bring true. And since Hallowell starts with hello, hello. So it matches our condition, even if we just use edge e, we are going to get through, as we can see here. And however, if we use Hello with the space, you're going to get false since Hello is followed by the comma and we did not include it. So if we just typed gamma, going to get through. So this is how we check if a string starts with specific string or character. Now, let's move on to define method. And this determines if the string occurs and staying or a substring of a string. So to clarify this, it will return the index of the character of found and minus1 otherwise. So let's start with this. So let's, as usual, we have this Hello World string, and we're going to check if we have the letter O in this example. So S dot find. And what we're going to find is the latter. We're going to run this code. We're going to get four. So it will only print out the index of the first character found. And in this case, the letter O goes twice. However, to just print out the index for since the first 0 is at index. But now let's suppose we want to find the o comma. Let's run this code. We're going also to get the number four. So even though we have world here, let me run this code one more time. We're also going to get the number four. Now, if we search for the letter a, for example, we're going to get minus one since a is not found in the string. And the last thing we can talk about, and this Find method is, for example, as we did before, we're going to check the index of o. However, we don't want to check with the first word. So going to start at index five, and so on. Should we just add the this argument and this, using this argument, python will ignore everything before the index five. So it will start at 01234. It will start from here until the end. And in this case, o is at index eight. So this is basically for a defined method. So now let's move on to the strip method. And this method turns a copy of the string. And let's suppose we have some spaces here and here. And use this method S, that trip. And in this case. We'll return the function hello world without spaces from the left or right. Now, the strip will just remove all the characters from the right here. And the else drip going to remove all the characters from the left, from the right and from the left, I'm sorry. And we can also add an additional argument here. So let's suppose we have some characters here. Let's say we have hashtags. And he, we have also hashtags. And that's used the hashtag in the strip. And it is going to remove all of the hashtags, but the left side from the string. And do the same thing on the right side are stroke. And let me just fix this. And strip we remove all of the characters as hashtags on derived and district method, we're going to remove all of the hashtags from left and right, as we can see here. Now, we also have the method replace, and it is basically just to place one character by another, end the string. Now remember that we said that strings are immutable, however, but we're going to do here is to take a copy of the string and just replace some elements. So instead of slicing the string by ourselves, Python will do this for us. So let's suppose we have the string Hello, world, and we're going to change the letter O, two a. And in this case, we just rent x-dot could place to replace o by a. Let's run this code. We're going to get hello world. So this is it basically, it is not the same string. So he would not having the same string. But we're just creating a new string and slicing from 0 until four, then five until 678, and then from eight, from nine or ten until the end. And we're adding a here and here. And this is it basically for the replace method. Let's move on to the split method. And this method, as the name indicates, it just splits displaying into substring with it finds instance of the separator. So let's suppose we have this string hello world, use AX dot split and we're going to split by the comma, let's say. So here we have the camera and let's run this code. We're going to get a list. First. The first element in this list is hello, and the second one is world. Now we can also change this to o. We're going to get Hal W LD. So whenever the letter O occurs in the string, going to cut and separate these these characters. And so this is the first element, then this is the second one. And lastly, here we have the last element in this list. And if we don't add anything here, let's run this code. We're going to get hello world. So it will just split the words are using the space. So whenever it finds a space that will just split here. And of course we have so many other methods for strings and you can check them by yourself. And for now, we'll just cover these ones. So see you in the next video.
20. Format Operator: Now let's move on to format operators. And format method in string contains curly braces as placeholders, which can hold arguments according to position or keyword to specify the bowl. So let's suppose we have this string. X will be equal to, hi. My name is Adam and Eve. Out. We'll get the same line as usual. Hi, my name is heavy. However, we can modify this. So let's suppose we have ax and in this case, we don't want to type high here, can simply use a curly braces here. And we don't want also type Hadi. So this is another curly braces. And in this case we're going to add dot format and type, whatever we want here. So first you need high and then we need ID. And if we run this code one more time, we're going to get the same exact result. So this is the default. The first curly braces will take the first string in the format, and the second one we take the second. Now we also have the positional order. So let's suppose I type one here and 0 here. It will be Hadi, My name is high. So make sure you don't pass the indices. And otherwise you'll get an index error replacement index out of range for visit positional arguments. So what is this? It is basically that we are out of range since we only have two strengths here. And trying to access the string at index three. However, we only have 01. So the right way to do that is 01. We get hi, my name is Heidi. Now we also have the keyword order. And in this case, we need to give, assign these values are strings into some variables. Let's call them a and b. And in this case, we can use a here and b. Let's run this code going to get the same exact result as before. Now, we can use the format operator with the sign, the percentage, percentage, sign, this one. And in this case, we can use it for a decimal floating point and strength. So let's suppose I want to print out that. I, i ran for 4.5 kilometers. And in this case, let me just modify the string would read these and x would be equal to. And I'm going to say that n five days, I grant ten kilometers. So let me type here n and love to use this. And after that we're going to type d, indicating that this is a decimal. And days, I ran for 10.5 kilometers and in this case it is a floating point. And how do we access this floating point by two percentage and the letter g. And here we have 410 kilometers. And let me just take this days and days. And after that, we're going to use, thus, we have an Five days, then we have the string days, then we have the 10.5 floating-point. And that green underscored print acts press run. We're going to get in five days. I ran for 10.5 kilometers. And this percentage allows us to construct strings, you pacing pulse of the strength with the data stored in variables. So when applied to integers, we use e, d, indicating that this is a decimal and G for floating point, and finally, as pole strength. So this is it for the format operator. And last thing I'm going to talk about is if we have, let's say the SG And one more, run this code one more time, going to get not enough arguments for format string. So we have this one for 1234. However, we only have three arguments in this format. So there's no enough arguments. We need to add something here, VDD 0.3, let's say. And this COCOMO time, who get going to get this result? And of course, if we assigning ax equal to b. And in our format we only have a strength that say, hello. And let's print x. We're going to get at a saint that decimal format adder, an ampere is required, not strength Since we are asking for a number. And in our argument we have hello, which is a strength. So this is it basically for the format operator. We don't use them a lot, but it's good to know that they exist and we can use them whenever we want. That being said, this is the end of this video. See you in the next one.
21. Handle Files: Now we'll talk about file handling. So first of all, if we have a file and we want to access it in Python, how do we do that? We'll simply use append. And this append function takes two parameters, define name and the motor. So let's suppose we have a file called my fight and we want to access it. We have several modes. For example, we have the unmoored syndicates read, and this means that we are opening a file to read from it. And this is the default mode. We also have the w, right? It opens the file for writing and creates a file f. This file we indicate doesn't exist. We also have the a append and it opens the file for appending or create the file if it does not exist. And lastly, we have the x mode and it creates the specified file, returns an error LD50 exist. So let's go ahead and use this append method. For example, let's suppose we have, as we said, the file called myfile and we want to open it, let's say open up in the parameter. And our filename is my file, and we want to open it to read simply indicated by r. We can also specify is the file should be handled as binary or text mode. And we can simply do that by adding the letter t indicating the text mode. And it is the default value, or b indicating that is a binary mode as images or so. So for example here, if you want to deal with it as a text mode, which simply T. And now we're going to open this file and the name is my phi. We're going to read as a text mode. Now, I already created a file here, and it is called myfile that the same name as here. And divide should exist in the same directory as the python program, or it will give us an error. So here as we can see, I have my Python program first by and followed by my file.txt. So they're in the same directory and we're good. And if we run the code, going to open the file correctly without errors. However, let's say by mistake, I wrote lowercase f And something here, for example, lets run this code. We are going to get an error file, not found error, no such file or directory called my file with an e. So as we can see here, we don't have this filename and we're going to get an error. However, if we want to create this file, you can simply use the x mode. And let's run this code. Let's check here. Now we have my field, that text. Now let's check for the last two modes. We have the W and a. And as we said earlier, if we want to open a file using the W mode and the file does not exist. It will not give us an enter. It will create the file for us. So let's suppose we need to create the file called writing the TXT. And let's use the W mode, and let's run this code. We're going to create this file writing dot TXT. And now we can write into it. We can also use the append method. And in this case we have the writing the TXT file here. So for underscored, nothing will happen since we already have this writing the TXT file. However, we can now append into it. Let's suppose we need to create another file, writing the TXT. And we need to append to this fine now. And this file doesn't exist. So Python will simply create this file for us. And as we can see here, all of these are created. So all four modes, three of them will create the file for us if it doesn't exist. And only one, which is the default mode will return an adder since we have no file found in the directory. So this is basically for how to open files and which mode should we use? And one more thing to add is that when we open a file and the openness successful, the operating system returns us a file handle. And this phi is not the actual data contained in the file, but instead this the handle that you can use, the read data. So in other words, whenever we open a fine, we're not going to get d phi itself, going to get something that is called handled. And using this handle, we can read and use the data however we want. And with that being said, this is the end of this video. In the next videos we're going to learn how to read and dried in the files. So see you the next videos.
22. Read File: Now let's learn how can we read files. A text file can be thought of a sequence as a sequence of lines. And we already talked about loops such as wild loop and for loop. And we can use these loops to pass through every line in this text file and extracted and printed out, for example, in our Python program. So let's go ahead and create a method that retrieves for us the texts, all the lines in our text file. So for example, let's suppose we have the myfile that dxdy, and here we have hello, hi. How are you? And finally, I'm good. And let me just move this. We have hello, how are you? I'm good. So we have three lines in this text file. Let's go ahead and create a function here method. For example, we're going to extract from the file. We'd use the function append. And our file is called myfile dot TXT. And in this case, this is our file and we're going to read from it so we don't have to add the argument r Since the default mode is read. So we can simply type up and, and the name of the file that we want to read. Now we have our F and this F, we can read from it. So for example, and our text file here we have three lines. So let's create a counter to be equal to 0 at first. And for every line in this f, We are going to increment the count plus one. And we're going to print our line, whatever we have and the line NF. And then after finishing, we're going to just print the number of lines is, and we're going to print count. Let me go ahead and run this code. We are going to get Hello. How are you? And I'm good. And the number of lines is. And as usual, instead of having to write this code, we already have some built in functions and methods. And Python. For example, we can use the read method, and this method simply read the whole file into one string. For example, here we have our phi and this is our file handle f. And instead of writing this code, we simply just write the strength as dv will be equal to F dot read. And then we can simply print SD. Let's run this code. We're going to get Hello, how are you? I'm good. And this is one method. Another method would be is to specify the number of characters we want to return. So n, This f Madrid here can add an extra parameter. So for example, we just want to return hello and hello is 123455 characters. So we simply return the first five characters. And this code we're going to get Hello and the o is included. Now, sometimes we might want to have or extract the first line and we don't know how many characters there is in the slide. So instead of specifying the number of characters, we're going to extract. For example, if we have here a lone actors and we want to extract them, we can simply use the readline method and will extract the first line and print it out. And it might not just be the first line, it will create the lines of the files. For example, if I said one more time as t to be equal to the red line. And That's printers. We are going to get, how are you? So first of all, going to get this hello and these characters. And then we're going to get how are you. So it will be passing through all the lines and printing out every line at encounters. And this is it basically for the red line. We can also now search through a file and we can combine the patent for reading a file with string methods to build simple search mechanism. Now, let's suppose that in our text file we have hello one and Hadi, hello to Jerald, hello Adam, and so on. So for example, if we want to print out the lines that contains hello or that starts with hello, will simply use this algorithms. For example, first of all, we have our text file, myfile dot TXT. And let's create a counter just to check how many times we extracted if an aligned that start with Hello. So now to be equal to 0. And for every light in our file handle f, We are going to check if this line starts with the word hello. And if this is the case, simply print out this line and increment the count by one. And finally, after extracting these lines and ending default loop, we simply print the count. Is. Followed by the number of how many times we printed out a line that starts with hello. So let me go ahead and run this code. We're going to get hello, hello and hello three and d count as three. And notice that this algorithm or this code just ignore everything. That doesn't start with. Hello. For example, here we had each ad and Adam. Our code will simply pass through all of these lines. However, it will just print out add increments the count whenever we have aligned that stars with hello. And, and let me just do this over here. We have Hello, hello, hello three, and we have no space between them. Even if we do this, we are going to get tallow one hundred, two hundred three. So it's not just about the words, it's about the characters. And in this case, Python is only going to check if the line starts with H, E, L, L, O. And ignore everything after that, even if I add anything here. And let me save this and discard one more time, I'm going to get this line of code, said dislike of the texts. We have. So simply Python, ignore everything after the letter O and whatever we have here. It doesn't matter, it will just simply printed out. Now, notice here that we have an extra line here, here, and here. So we have three extra lines. And one way to get rid of these lines is to use the astype method. And this method will strip the white space from the right side of the string nodded to not skip these lines. So for example, before printing line, we are just going to say that line will be equal to line drip. And let's run this code. We're going to get the three lines without these extra lines. And now sometimes Alcott might get very complicated and it might be easier for us to use the continue. So instead of checking if our lines start with, our line starts with hello, we going to check if our law outlines. Do not start with this letter with this word Hello. This is the case. We are going to continue. And as we said before, the continue using the continue will simply jump over all of these lines and go back to our for loop. And if this is not the case and our line starts with hello, then will simply ignore this. And we are going to continue with our code as usual. Welcome to strip and printed out and increment the count. So this is basically for the continue. And here we can add one month, one more thing can use defined string method to. So let's suppose we have some emails here. Let's see how the US at something that come and Uranus at something.com. And here we have some lines, for example. I'm good. And lastly, let me just add when email something. So this is our file. Now. We have three emails and three lines inside this file. And we are going to print out these image and ignore everything else. So here we might want to use this and find method. And first of all, we have our file handle and we are going to enter the for loop. So for line in f by file handle, we are going to strip this line and check if this line, if in this line we can find the ad. So let me just type it offline dot find f. We found something that is at something that gun. And whenever we use this method, find, we're going to get the integer. For example, if we don't find this, we are going to get minus1. However, if we find it, it will be anything different than this minus one. So f line dot Find something that KM is not equal to minus one, will simply print this line and increment the count. Let me run this code and we got an error saying that syntax error. Here we have. Now we can just run this code one more time. We're going to get head units at something that come Yunus had yet something that gone. And the last email we entered this ad unit stand at something that gum. And the count of course is three. We have three image. And as you can notice, we just simply ignored everything that does not contain something that car. For example, here we can simply, we can simply know that our code is not powerful sense. Here, it is clearly not an email. However, if we add at something.com here and let me run this code one more time. We are going to get this line. It had the urine at this line of text had units at something that num, which is clearly not an e-mail. We have some extra space here and some extra space here. And the email cannot have these. And notice that our count is incremented. So it is better to modify this code and check if we have some space here, we will not printed out going to check for spaces and or when do you use this at something that comes? For example, if I write here something that come and followed by Hadi Uranus, lead can just delete this a stranded Xcode. And we're going to get this email. And this is an invalid email. So it is always better to not use, not just use this.
23. Read User's File: And this video, we're going to learn how to ask the user to enter the filename and use it and the append function. So sometimes we have, we might have so many texts. And we're going to ask the user to choose one of the available texts. So for example, let me just read this and let's cut. For example, we are going to ask the user for defining. So we're going to create a variable called filename and ask the user to file name. And simply, we are going to open this file using the append. And entering the file name that we, we have that the user has provided earlier. And then we are going to pass through this file, renting out every line. And of course, keeping it to our strip. And incrementing the count doesn't create account created now. So before default loop count to be equal to 0 and then simply printing out the count and count. So this is, it basically, lets run this code. Please enter a filename. And let's suppose I want to choose the file to dxdy, identified to TXT. I'm going to get these lines of code. So whatever we have in this mind, file.txt is going to be printed out here. And the count to be equal to six. We can perform anything here. So the same algorithm or the same poets we created earlier, you can use them here. Just, just, instead of entering defined name, we are going to ask the user to enter the file you want. Now, for example, let's suppose I ask the user to enter a file name and the user entered a FIFO that is filed to the day dxdy. And we can clearly see that we have no file to here. And let's run this code. We are going to get the file not found error. So that's no such file or directory called file to the extreme. And Python is going to take this name and place it here in the parameter and tried to open it. However, while searching here and the directory in the file or the folder is going to know that we have no file to existing here. So it is just going to compare define name, file 2.txt with every filename here. Wifi, myfile, writing, writing to have no file two. So after comparing it with every file, is going to return an error. Now, how do we deal with that? We already dealt with error at several times before using the try and except statement. And we can also use this here. So how do we do that? For example, the user entered a file. We are going to try to append it. And if anything occurred, we are going to accept this using the accept. And we are going to brand. And if we run this code, we going to get an intention errors since here we did not intend to let me just and he also didn't then. And this is it. Let's run this code one more time and to define name. And let's suppose I entered file two as before. Now this time we are just going to get the slide saying error. So Python checks and tries to open this file and add. An error occurs here. So it will just, and everything happening, simply going to the accept block and doing whatever we have here. And here we have the print adder, so simply it will print. Now we can also and always use this try and except here. However, in this case, we are just simply going to check if the file is valid. And if this is not the case, we are going to use the accept method. However, if I run this code one more time, I am going to get, let me just type file to the TXT. And I'm going to get f is not defined since I'm using f here in the for loop. And my F is not defined since I have an adder wide opening this file. So here we can simply use ii exit and this function just terminate. It will just terminate the program. So if I wrote TXT, I'm going to get added and the program will be terminated. So this is how we use the exit function. And one more modification we can add to predict that the user might, might do something by mistake or write a file that doesn't exist. Instead of just printing adder, we are going to print, please enter another name. And in this case, we are going to accept this filename b equal to input. Now, let me just take this and place it here. Remove this sprint. Now, if I run this code one more time, I'm going to get these into a fine name. And in this case, file 2.txt run this code error. Please enter another file name, and in this case my file, the TXT. And of course I'm going to get I'm sorry, he cannot open it. So I'll simply open it outside. F would be equal to this file name one more time. So in this code, file to the TXT and file myfile dot dx, dy, and everything will work correctly. Now let's see what God is doing. First of all, it's asking the user to enter a file name. And in this case, the user might get mistaken and entered another file. So I'm going to try to open this file. F something occurred. We don't print error and ask the user one more time to enter defined. And if the user enters another file that is not one of these files, is going to generate the same error as we did before using the try and except. So I will, I will only give the user two times or two chances to enter his good. And you can always modify or enhance our code by using the wild loop here, instead of just asking it, asking him one more time. So we can ask him so many times until he enters. She enters the file that is available and our folder. Now, the last idea would go to talk about is the closing files. It is always a good practice to close defined when we're done with it. So when using a find, the resources that are tied to this file are always opened. And, and when closing this file, and this will bring up all the resources that are tied with our file. So how do we close it will simply use the F After finishing from everything, we simply use the f that lows. And this will simply and automatically close our file after using it. So this is it basically to about creating files. In the next video we're going to talk about writing files and how to use the functions or methods available for us and Python. So see you in the next video.
24. Write File: In this video, we're going to talk about how you write into the files. So let's suppose we have our file, myfile TXT, and we have some lines here, emails and something just to hbase clients. How are you? I'm good. And in this case, let me just remove these. And we're going to open this file using the open. And we don't have to type the name of our file. And let's suppose we want to write in this file. So you use the second parameter w, indicating that we are going to write into the file. Let me just save these since they would disappear. And I am going to write into this file f dot tried going to try that, the content and then deleted. So whenever we use the W mode or the write mode, we are going to delete everything in the file and write our own the lines. And let's close this file. Let's run this code. We are going to get. Please enter file. I'm sorry. Let me just run this code one more time. And as you can see here, it did not get anything. However, if we go to our text file, my file.txt, we can see that we have the content has been deleted. So Python had deleted everything in our file and just added the line that we wrote here after tried to contact has been deleted. And I write something here. And we can see that we have high added to this line. So let me just delete this one more time and save whatever we had Lear. And let's run this code one more time. Go back. We have the content has been deleted and high. So this is how we write into the file. Now suppose we don't want to remove everything here. Suppose we just want to append here, for example, we want to add some image, and here we have our image. Let's suppose that we want to add some email here. So to do that, we are going to use the a indicating that we are going to append to the file. And instead of just deleting everything, we're going to just add an email after dried. Hello at something that come. Let me delete this close to fight. That we say it does run the code. We are going to get the first three lines that we had earlier and outfile and followed by the extra line that we just added. And our function F dot dr. As we can see here, we got hello at something that come added to our, to our first three lines. Now we are going to check for the last mode. We already used x. However, as just see if we entered acts and the file, my file.txt. And we said earlier that x is going to create for us a new file. And it is going to check if this file exists. And if this is the Says decays is going to get, we're going to get an error. So for underscored, we're going to get file exists. So saying that this file we entered here, so this, my file.txt is already existing, as you can see here, my file.txt, and we can't create another file with the same name. So let's just add the number two here. And let's run this code. We can see that we created a new file, mine file.txt. And see what we have here. We have hello at something that come, since we wrote hello at something, something that gum in our file F phi handled. And it's going to be automatically transferred to our myfile, to the text T. So this is it basically for reading and writing files and how we can append and create new files. So this is a general idea how to use files and Python. They are so powerful and we can use them to extract data, NFO or anything we want to add to our Python code. So for example, if we have a list of students and averages, numbers or anything, we can extract them from the file and work with them in our Python program. So this is basically for this failure. But that being said, see you at the next one.
25. Lists: Now we'll move on to less. We can think of a list as a sequence. And like a string. And list is a sequence of values. So we already talked about strength and we already know that a string is a sequence of characters. However, the list can be a sequence of any type. So the values in the list are called elements, are sometimes items. And we can store anything we want such as strings, elements such as integers, floating point numbers, or boolean, or even lists. Another list, so it will be a list inside the beggar list. So, and this is a general idea of a list. Now how we can create it. So first thing we're going to do is when creating a list is to use the square brackets. And in this case, let's suppose we want to create as cold. It will be equal to some elements such as car, house and TV. And the list is a collection which is ordered and changeable. So this collection is ordered from Chi house TV. If we print out this list, we're going to get car house TV. And we can always change the order of these items in this list. And this code, I didn't print it out. Let me just rent our list. Now run this code one more time. We are going to get DVM, sorry, here we have two square brackets. We're going to get car house TV. So this is our list and it will all it. We can always add something here, for example, an integer code, we are going to get the same. We can store both types here. Let me add a floating point number, 20.5 and another list inside. So it would be a list of numbers. Let's close it. And this is basically so he restored a car that is a string 20, that is an integer 20.514.1 to another list inside our list, and finally another string. And we're going to get the car 20.5120 to TV without errors. So using list, we can enter anytime we want without getting any error. Now, whenever we have a list within a list, this is called a nested left. And one more thing to add here is if we create an empty list, let's suppose we are naming it empty. We can simply use the curly brackets and it will print out empty. We're going to get an empty list without any elements inside. So we can create an empty list, will not going to get any error if we tried to print it out. So Python is going to just print out curly brackets indicating that we have no elements in this list until now. Now, let's suppose I have another list. For example, the list of numbers to be equal to 1357. And in this case, we are going to talk about how to access the items in this list. So here is our list containing four elements. And we are going to access one of the elements the same way we did using the strength. So the list and this is work the same way as string indices. So for example, if I want to use the number three, and this three is at index 21. And I can simply print out the list at position one. And the scold, I'm going to get less is not defined. I'm sorry. It is not Capital. And he forgot the equal operator. Let's run this code. We're going to get the number three, since three is at position one, remember that the indices work as 0123 and so on. And if we attempt to use the negative indexing, so let's run this code. We are going to get the number seven. We're going to start with the last element and going backward one element at a time. Now, let's suppose I want to use the index five, and we clearly do not have this element here. Sends our list contains only four elements and I'm attempting to use the element at index five, we are going to get the index error list, index out of range. So this is basically for accessing the items in the list. Now, what will happen if we attempt to change an item and this list? He already said that strings are not mutable and we cannot change any character in the string. However, we can create a new strain at whatever we want from the other string and add some characters of our own. However, the list, we can change any item we want. So lists are mutable. So unlike strength, less NSMutable, because we can change the order of the items as well as a reassigning an item in a list. So for example, here we have a list of elements 1357. And let's suppose I wanted to change the number five into six. So simply, I will reassign the element at position. In this case, five is at position two, so less at two will be equal to six. Now if we print out this list one more time, we're going to get 1367. So what does Python do here? Just modified whatever we have at position two. And here we have the number for it, change it to the number six. So this is basically to how we can change our reassign numbers and the list. Now let's move on to slicing. We can specify a range of indexes by specifying where to start and where to end their range. And a new list with the specified items will be returned. For example, if I have a list of elements such as numbers, in this case 1357, and I want to create a new list just of two elements. In this case, I'm going to pick up 35. So I want a new list starting with three and ending with five. So to do that, we can use the slicing. So for example, let's create a new list called and, and in this case it will be the same as lists. However, it will start at position one and, and at position two. And however, if I type 22 minus one, so I am going to only get this number three. So I will push it by one. And now if I print out, I'm going to get 35. So one, starting with one, this is the position one and ending with 33 excluded. So 12, we're going to get three. And now one more thing here, for example, if I type one and whatever we have after this will simply going to get 357. So just extracting one or deleting one from the list. So we're creating a new list, which will be having 357 as elements. And same thing here. If I typed two, I'm going to get the first two elements in this list, 12. And I am just ignoring everything after the number. And if I just type these columns, I'm going to get the whole list. Writing this will simply return whatever we have in this lesson. We're going to get all the elements available for us. And the last thing we're going to do is to change two numbers at a time. So here for example, if you want to change the number 34 in the list, less 20B at position 20 and I'm sorry, 12. We are going to change them too. Is 00. Let's print this list. We're going to get 1005, I'm sorry, here we need to add three and we are going to get 1007. So what did we do here? We just changed the list that is between 35 here, between 13. So the element at position one, the element at position two, excluding the element at position three. So we're going to change 35 to 00 and then we print out for this, is it for this video? In the next video we're going to talk about the N operator as and how to traverse a list. See you in the next one.
26. Traversing Lists: Lists, how we find an element inside enlist. Now let's suppose we have a list. Strings, say we have a key and a TV. So you have three elements in this list. And we want to make sure that the key is in this list. So we wanna make sure that this element key is in this list. How can we do that? Simply by asking if d N list. So this statement should return true since we have the element in the list. So let me print it out, print key and list. And if we run this code, we are going to get to. So Python checks if this string in the list and returns true if this is the case and false otherwise. However, if I typed key with a capital K, it will give us false since Python is case sensitive. So now, this is how we can check for one element. Now, how can we check for all elements are how can we pass through the list? It is quite simple. We can pass through the list using a for loop. Now let's suppose we have the same list, Sharkey dV, and we want to pass through every element in this list. So use default for element. And now this is just a name. You can name it whatever we want to connect a element by anything. Now, to work with elements and four elements in this list, we're simply going to out this element. And let me run this code, see what we'll go through that we don't need to get Qy and TV. So Python passes through every element in this list and print it out using this for loop and this code you just created. Now, what will happen if we have an empty list? So let's suppose our list here is empty. And as you can see for underscored, nothing will happen. We're not going to get an edge because Python checks in this for loop. Python checks if the list it contains an element, then it will print it. If this is not the case, it will just never execute this body of the for loop. Now, let's move on to nested lists. So let's suppose we have some elements here. Let's say TV and the number 55. And another list inside this list, and it will be a list of strengths, let's say t. And finally, we have a number, five. So let me underscored. And as you can see, we're going to get dV 55 and this list as one element. So although a list can contain another list, the nested list, this one that contains the key and Kurt, still counts as a single element. So Python will not execute this as a list and print out every element inside this list. However, it will treat it just as every other element and print it out as it is. Now, let's talk a little bit about how to add, remove, and update the items in the list. So let's suppose we want to add an item to this list. Let me just did this list using dv, e And God. And let's suppose we want to add the laptop for example. And we will simply do that by using the append method. So this method add an item to the end of this list. So if you want to add here laptop, we will simply use list.append and add whatever we want here. Now if we go ahead and print this list, we are going to get TV, key, car, and laptop. So this method as this element to the end of this list. And let's suppose we don't want to add laptop. At the end of the list, we want to add it between TV and key. We can use the method insert. And this method add an item at the specified index. So it should take two parameters. One that is the string or the element we are going to add, and the other is the index. Where are we going to add it? So here, let me delete this and we're going to use lists, but insert. And in this case, we are going to insert at position number one. Here we have this position is number one. And we're going to add laptop. And let me print this list out. We're going to get tv, laptop, key, and car. So we just added this element and inside this list at a specified index. Now, this is our new list. How can we remove an element from this list? We can simply use the method a remove to remove an element by calling it by its name. So let's suppose we have here tv, laptop, and p and q. We're going to remove the element key. So simply use lists, but remove, and we simply type, gave me print this list. We are going to get tv laptop. And so we can remove the specified item by calling it by its name. So this is the first method that we can use. The second method is pop, and this method removes this specified index or the last index if it is not specified. So let's suppose. I want to remove the element curve and in this case, I can use the box method. Gentlemen, Brent, this list. And let's see what we are going to get. We are going to get tv, laptop, and key. So this method just remove this element, the last element if we did not specify the index. Now if we want to remove the element key, we know that Python starts at index 01 and decay is at index two. We can simply use list.pop and at index two, non-disclosure and more time, we're going to get tv, laptop, and car. So you can see that we use this pop method with and without an argument to delete or remove an element at a specified index or at the last index. Now we can simply also used the number three and we're going to get the same exact result as before, since this curve is the last element. So we simply doesn't matter if we added the number three or not. We are going to get tv, laptop, and K in both cases. Now, we also have the del keyword for d, l. And this skewered just removes the item in the specified index. So let's suppose we have this list of four elements and we want to delete the element at position 0. So we simply use DL and list at position 0. Then let me print this list. We're going to get the list laptop, key and car. So just deleted the element dv using d l keyword. However, if it did not specify the index, what we're going to delete. So I suppose we just said the list and print it out. We're going to get a new empty. And the last method we're going to see in removing items is the clear method. And this method empties the list. So for example, here we have this list and let me use the clear list that year. And in this case, we're going to get this empty list. Sometimes we want to remove two or three items. So it's not very practical to use the list dot remove this item or this item twice. And in this case, we can use slicing. So to remove more than one element, we can use the DLL with Slice index. So let's suppose we want to remove the element laptop and key and at position 12. So it simply use dia between 133 excluded. And this case, if we print the list, we're going to get. Dv and car. So just deleted two items at the same time. And it is important to remember that the slice select all the elements up to but not including this one. So for example, if we say one to five, it is from one to five minus one. So we're going to get the element at position 1234 and not including the last index. So this is basically for removing items. Let's move on to updating items and updating items. If we want to write or update the items, we need the indices. So let's suppose we want to update the item laptop. And we know that laptop is at position one or index one. So we simply use list at position one and change that to do whatever we want. Let's say we want to add house. And in this case, we print this list. She was, we're going to get run this code and we're going to get TV, house and car. So as you said before, lists are mutable and we can change every element in this list. And by using this numbering or this index, using the list at one, we just changed the laptop to house. Now, let's create a code that takes a list of integers and multiply every number in this list by two, then return this list. So for example, let's suppose we have a list of numbers, 2345678. And in this case, we're going to pass through all this list. How do you best through a list using the for loop for number in a list, we are going to multiply each number by two. However, if we just use for number in the list, we are going to get every number. So how can we update it? If we just got this number, we can't updated. So we need the index of this number. How would we do that in Python using the L-E-N method and the range method. So let's suppose we don't want the number, we want the index of this number two, Let's emit index or index and the list, however, we are going to use the range. And then the length of this number, i'm sorry, the length of the list. Now, let us see what are we doing here? First of all, we are taking the length of the list, and in this case, the length is 1234567. So we have seven and the range. And we are going to take the numbers between 0 and the strange. And in this case, we are going to get 01234 excluding the last element. So we are going to get 01 up until seven minus 16. And we are going to pass through this list using the list at position or list at index. We are going to change list at index two. Let's add index times two. So for every element, we are going to daycare and multiply it by two, then stored this result in this same exact position as before. So we are updating every element by taking it, multiply it by two, and then place it and the position before as before. So this is basically, let just this list after finishing from this code and see what we are going to get. We got 46810121416. So this is exactly what we are expecting, sends to multiply by 246 multiplied by 212. And now that we've done with this example, this is the end of this video. See you the next one.
27. Lists Functions: Now let's move on to list operations. For example, if we have two lists, a and B, and we want to merge them together, we can use the plus operator to concatenate these two lists. For example, if we have a equal to a, B, C, and D, and we have another list that is named B. And in this lesson we have five integers, 12345. And we want to concatenate these two lists and a new list called C will simply use the plus operator a plus b. And let me print C, C. What we going to get discouraged, and we're going to get a, B, C, D, One 2345. So this plus operator will just concatenate two lists, a and B. And notice that we said a plus b. And we're going to get the elements of a first, followed by the elements of B. Ok, so this is it basically for the plus operator. And we can do the same effect using the extend method. However, we don't want to create a new list and we just want to add these numbers and B to a. We can simply use the extend method. In this case we use a dot, then we're going to extend all of the elements in B. And if we go ahead and print a now, we're going to get ABCD 12345. So we're going to get the same exact result as before. However, now we're printing a. So let me go back here and create the new list a plus b. And in this case a footprint a. We are going to get a and a, b. And b. And in this case, we're going to see that a and B are still the same. However, the C is the one that we are creating. So let's run this code and we're going to get a dis ABCD be 12345, and c is both of them concatenated together. However, and the extend method, if we print out a, we're going to get this result. So this is it. This is how we can merge two together, either new list or simple or the first list we have. Now the second thing we can use as this character and this, we will use it to repeat a list of a given numbers, given numbers of time. For example, if we have a list of characters, ABCD, and we want to repeat these characters four times. We will simply use a times four. And if we go ahead and print this, let's see what we are going to get. Let me save this and we're going to get ABCD followed by the same exact. Abcd characters four times. So here we have the first time, second time, and third time, and the last four time. So this is basically, for example, if I multiplied by two, I'm going to get ABCD, ABCD. And it's not that I'm, I'm multiplying every element by two. It's not even as I added some numbers here, I'm not going to get the multiplication of every number. For example, thrown onto skirt. I'm going to get 12341234. I'm not multiplying every element by two. I'm not going to get 1468. I'm going, I'm going to get the same elements twice, or how many times I multiply it. Now, let's move on to 13 plus. So for example, if I have a list of numbers, let's say I'm not having this list and an ordered, for example, I have 3417510 and I want to sort this list. I can use the method so that arrange the element of the list from low to high. So let's suppose I have the list a, and by using this method, I am sorting the elements in this list. If I print out this list a, and due to that 1345710. So they are the same exact elements, however, they are now sorted. Another thing is that this sort method also work on strings, characters or anything. For example, if I have here some datasets, a, a, c, b, and let's say I have eight and I'm going to get abc H. But this is basically for sorting the list. Let's talk about some functions. First of all, we have the LEN function and this just returns the how many elements we have in this list. So we already used this method or function LEN and strength. And when using it with strings, we got the length of the string. Here we have elements in a list or going to get how many elements we have in a single list. So let's suppose I have this list of four elements and I want to print out how many elements. I'm going to use the alien method of a. Let me run this. I'm going to get four, indicating that this list has four elements. And another function is the maximum. And as the name indicates, when using the maximum, I'm going to get the maximum element in this list. And in this case we're going to get H. Since H is the maximum element, it is not the last element. This d maximum, even if I don't sorted, even if h is here. And let me deleted from here. Surroundings, codon wartime, I'm going to get h. And we also have the function minimum. And in this case, we're going to get the minimum element in the list. I'm going to get a. Now let's change the values here. For example, two integers. And let's say we have 134, two, seventy, seven, thirty four. And in this case, we're going to compute the sum instead of creating a for-loop and passing through every element in this list, we can use the sum function. It is a built-in function in Python. And in this case, I simply sum of a. And Python automatically compute the sum for us. And the sum is 141. And as you can see, the length is six, the maximum numbers 77 and the minimum one is one. Now, if we want to compute the average, there is no method and function to have the average of a list of numbers, some numbers. So we simply use the sum function divided by the length or how many elements we have. Some of a divided by how many elements we have in this list is going to give us the average of this cluster and is 23.5. Now let's move on and create another example. Compute the sum and the average of some elements entered by the user. And we're going to do it using two methods. One with a list and the second without it. So let's start with the first one that is without a list. And in this case, we are going to compute the total. The total is equal to 0 at first. And we need to set counter equal to 0. And we're going to enter a while loop. And it is always true. While true. Then we need to get the input from the user. So let's name it m. And we are going to ask the user to enter a number. And in this case, please enter a number. And we're going to get this number converted and added to the total. So first of all, how do we check if the user is done? And this is the last element and we will finish now, we're going to compute the average and print it out. So simply by entering the letter of the word done, and this case done, finished. So we are asking the user to enter a number and enter the word done. Finished. Now, without to check if this input is equal to one. So f is equal to the work done for them to break out of this loop. If this is not the case. When we are going to convert this input from string to integer or float. So let me convert it to a number. So the number will be equal to. And then we're going to add it to the total, total equal to whatever we have in the total plus this number. And though to increment the counter by one to counter equals counter plus one. Then after the, after finishing from this while loop and after the user enters the word done, we are going to compute the average by dividing the total of a counter. And in this case, when this average, average will be equal to the average we created that we underscored. And see what we got to get, going to get Please enter a number than a finished. So I'm going to write 102025.313.744.5. And now I'm done. I simply type down in order to get average 26, 0.1. so Python gets all these numbers, stored them in the total. And every time I enter a number, Phyton or this program, we'll increment the counter by one. Then finally, after writing down, it's going to exit this while loop. Compute the average by dividing the total the counter and then printing out this average for us. Now, this is the first method. Let's write the same exact code using another method. Where will you be using a list? First of all, let me create a list and let's name it list. And in this case, w t to be equal to a new list. And same as before. While true, we are going to take an input from the user and store it in M, enter a number. And then we are going to check if this M is equal to done. If this is the case, we are going to break. Otherwise, we're going to convert this into a float and store it in the number. Then we are going to append this into the list. So list dot append and we're going to append this number. So we adding, every time we have a number, we're adding it to the list. Then finally, after finishing, we are going to compute the average by using the methods or functions we have and the list using the sum of this list divided by the length of this list. Then finally, printing out this average. Check me on this code. I'm going to get into a Number 2020.423.712.66 and then done and I'm going to get this number 19.189 and so on. As you can see here, we just created a list. Use the functions available for us or the built-in functions in Python. And in this case, we didn't create a total or a counter to check how many times the user inserted or added a number. And as you can see here, using a list is much simpler than creating some variables outside. And this would be very helpful for us when we have some complicated or large amount of code. So this is it basically, in the next video we're going to talk about lists and strings. So see you then.
28. Lists And Strings : Hello. And in this video we're going to talk about Les and strength. As we said before, a string is a sequence of characters and the list is a sequence of values. But a list of characters is not the same as a string. So to convert from a string to a list of characters, we can use the list. So let's suppose I have a string that is called ST, and inside the string I have Hadi. And in this case, I want to create a list of characters. And in this lesson I'm going to place the die. And in this case, I can simply create a list, let's call it list WT. It will take lists with SD. So if I go ahead and print this list, I am going to get the letters in the string as the elements. So I have age a, the array. So what did we do here? First, we created a string that is Hadi, and then we converted this into a list of directors. Now, let's suppose I have a string of words. In this case, let's say I have a string and it is, how are you? And in this case, I can use the split method to break the words and place them into the list. So here I'm creating a new list and I'm going to work with SD, that split. And in this case, I'm going to split the words in this list. Print this list. I'm sorry, split these words and this train and place them in the list. And if I go ahead and run this code, I'm going to get you as elements in the list. Now, I can use these elements as I want. So for example, if I want to print the second element that is at position one, I'll use list at position one. And in this case I'm going to get the word, since this word as the second element in this list. Now let's move on to splitting using a delimited. And this case we can call the split with an optional argument called a delimiter that specifies which character to use as word boundaries. So let's suppose I have some elements or some words. And they are at this, like this. How are you, how are you? And in this case, I want to split this string into words. So before using the split method, I am going to specify the delimiter. In this case, I am going to specify it as the dot. And I'm using this as the optional director or the optional parameter argument. And in this case, after. Using this delimiter, we're going to print this list and see what we are going to get. We are going to get the same exact result. However, now we are using this terminal. If we did not use this one, that suppose I deleted. And this case, let me delete this and print out this list. We're going to get how that R, the du as one element. And this is because we don't have any space between the words in this list. However, we have a dot operator. And to use this, we need to use the limited and specify it as the dot operator. So this is basically for the database. Now let's move on to the joint. And this method, join is the inverse of the split. It takes a list of strings and concatenates the elements and placed them in a single string. So that suppose I have a list, and then this list, I have four elements. The first one is alone. The second is how? Then we have the and finally, we have u. And in this case, if we just add a space here, I wanna concatenate these elements and stored them in one single strength. So I'm going to create the delimiter and in this case is going to be the space or nothing. Actually. Since we added some spaces here and here. So we don't, we don't need to add a space delimiter. And I'm going to use the delimiter dot join. And in this case we're going to join the elements in this list and store them in a string SD, then printed out at me underscored, we're going to get Hello, how are you as a single string? So what did we do here? We created a list of elements, and this list contains strings, recreated a delimiter than we join the, all the elements in this list into one strength and print it out. Now, let's suppose we don't have here some spaces. So let us delete this. And what are we going to do in this case, if we run this code, we're going to get Hello, how are you as a single word? Now, to fix this, we can simply add space here and run this code. We're going to get Hello. How are you remembered that this delimiter is the thing that we're using to join these words or elements together. So whatever we add here, let's suppose I had this, I'm going to get Hello, how are you? And let me add that. We are going to get hello at how, at, at u. And in this case, if I add a single or a white space, I'm going to get Hello. How are you as words and not as one word? So this is it for less and strings and biting, it is very easy to play with lists and strings. However, in other programming languages, you might want to create our own functions or our own method to link or concatenate some words in his analyst and place them in a string. In Python, as we can see, it is very straightforward. And with that being said, this is the end of this video to you. The next one.
29. Object Values and Aliasing : Now let's move on to objects and values. First of all, let's take an example of two strings. In this case, a would be equal to hello, and B will also be equal to Hello. In this case, let's check if a is b. For example, let's print a B. Save this, and run this code. We are going to get true since we already said that strings in Python are immutable. And in this case we cannot change the strength once we created it. It is allowed to have two variable names that are pointing out at the same strength. In this case, a is equal to hello and b is also equal to hello. And this case we are going to get ASB true. However, this is not the case and less. So for example, let's suppose I have a list of three numbers, 122 and also b to the same exact numbers 12. And this case a fire and I'm going to get false. So since a is a list, and all we already said that lists are mutable so we can change the values in this list. So once we create a list, it is not going to be equal to another list. And this case if I change a at 0 to be equal to three to three for example, and I run this code, we are going to get false also sends a is different than B. So once we changed anything in a, it will not be changed in B. So this is the general idea about objects and values. Now let's move on to aliasing. And in this case we have two lists. And we already said that lists are mutable and we can change any item in this list. However, I wanna make sure that B is equal to a. I can simply type b equal to a. And now if I run this code, I'm going to get through. So what did we do here? A refers to an object and we are also assign b to be equal to a. And in this case, B will also refer to the same object a is referring to. And this case, the association of a variable with an object is called a reference. And for example, here, the two references are pointing out and to one object. So here B is pointing out at this object and also a is pointing out as we can see, to the exact same object. Now, remember, lists are mutable. So these two lists, a and B, are pointing out to the same exact object, one to two. So if we make a change and specific, for example, a at 0 is equal to four in this case, this will affect also the second list. So for example, if I print out now a to be equal to the a list, and then print out b, followed by b. And let me run this code. We are going to get the same less for two to four to two. And keep in mind that we also, we just changed the first element in the list a. However, it also changed in the list B since b is equal to a and they are both pointing out at the same object. Now, for immutable objects, like strength, aliasing is not as much of a problem. So it almost never makes a difference whether a and b refer to the same string or not. So if I have a equal to hello and b equal to hello, we can't change any item or any character in the string. So it doesn't matter if they are both pointing out at the same objects or not. And now we reach the end of this video. See you in the next one.
30. Dictionaries: Moving on to dictionaries. In Python, we have something that is called dictionary, and it is written with curly brackets and it has keys and values. So let's suppose I want to create one. For example, I want to create a dictionary of a student. In this case, I can write students equal, barely races up and, and I want to add the student's name. In this case. I want to save it and name to be equal to the h, to be equal to 20. And I'm sorry, here we need to add a comma. And, and finally, the country I'm living in, and it will be equal to Lebanon. And in this case, let me save this and print student. I'm sorry. I need to add column that we're under. I'm going to get name had the age 20 and country, Lebanon. Now that we created our dictionary, how do we access our items? So we can access the items of a dictionary by referring to its key name inside square brackets. So let's suppose I want to access the name. In this case, I can use Student square brackets name. Let's run this code. I'm going to get, I'm sorry, he willing to add describing run this. I'm going to add the if I want to access the age, I can simply type age Ramdas going to get 20, and so on. Now, we also have the get method and this gap takes a key and a default value. If the key appears in the dictionary, it returns the corresponding value, otherwise, it returns that default value. So let's suppose I have name, age, and country. And I want to use this method. For example, I am student dot get. I am searching for the age. If I find the age, it's okay. Just get it. However, if I don't find it, print out 25. And this case, if I underscored, I'm going to get 20 since our program search and this dictionary and finds this age and print it out. However, let's suppose I am searching for the address. And in this case, if we don't find it, we are going to print 25. So simply, this get method just checks and this dictionary for this address, and we have no address here, so we simply return 25. However, if we just use this without this additional argument, we're going to get none. So Python or our program will return none, since we don't have any address to the same side and safe side, we're going to add something here. Well, let's move on to changing the values. So for example, and the less we changed the value using indices. However, if we want to change the value of a specific item in the dictionary, we should refer to its key name. So let's suppose I want to change the name from Hadi to Joe. And in this case, I will simply write student at name equal to Joe. And in this case, let me print out the whole student. And, and I'm going to get named Joe age 20, country, Lebanon. So it is very simple to change the name. We simply use the name or the key. Now how do we add items? We can add items by adding square brackets with the key name inside it. For example, here, we already have an aim. However, if I type here name two equal to jo Ramdas, I am going to create another name, this name to Joe and place it inside the dictionary. That is a different name than Hadi and as a key and value different than these ones. So this is basically for how we can add items. Now let's talk about removing items. As before, we have debug method that removes the item with the specified DNA. So since we can add at the specified key name, we can also remove. So let's suppose I want to remove named two. I simply type student that pop and I want to name two. And in this case, if I run this code one more time, I'm going to get name Heidi, age 20, country Lebanon without name to job, since I added, then remove it from this dictionary. Now, let's suppose I want to remove the last item or the last inserted item in the dictionary. So it is quite simple. I don't want to really know the last element or the last item. I can just pop item and I'll simply pop out the item here. In this case, this is the last item, FI up one more time. I am going to be removing the element. Did the country and devalue Lebanon. So I still have named Heidi and age 20. We also have the DL keyword as before, and this removes the item with the specified key name. For example, if I don't want to use pop item, and let's use this DL keyword D, student name. Let me run this. I'm going to get age 20, country Lebanon without the name. Since I used the delete. Now, the last one is declared and this clears the whole dictionary. And in this case, I will type student that clear. And run ongoing to get an empty dictionary. Now let's create a code that checks for us how many times a character in the string. So it should count Africa actor and then print out the dictionary would recreate. And in this dictionary we're going to have every character with how many times it occurred in the strength. So let's suppose we have a string called SD, and inside the string I have Hello. My name is Heidi. And in this case, first, the first thing we're going to do is we're going to treat you create the dictionary and name a D. And how do we create a dictionary to simply assign D to be equal to d ICT and empty parentheses. Now, we're going to pass through every character in the string as t. How do we do that? We create a for loop and for every character lastName at C. And the strength st, we are going to check F desk character c is not. And the dictionary D, we are going to place it. So how do you place it? The position C is going to be equal one. So what are we doing here? As first thing is to create the for loop and passing through every character in the string. Then we are going to check if this character is and the dictionary, as it is not in the dictionary, we are just going to add it as a key and the value will be equal to one. And this is not the case. So we are going to increment the at sea by whatever we have at TLC plus one. So remember, we are counting how many characters or how many times the characters occurs and the strength. So if it is not in this dictionary, so this is, this means that we did not count it yet. So we'll just assign the value to be equal to one. However, if we have a specific value such as one or two in the dictionary. So this means that this character's occurred before. And we are just incrementing the number or the counter of this character. So let us then print D after exiting the for loop. And excuse me, here we have not n d. And finally, and out the, the, let's run this code. We are going to get H2, e2, f1, 01, and so on. So this is at basically, however, we can use the get method and this method automatically handles the case where a k is not in a dictionary. So it can reduce the number of lines. So as you can see here, instead of writing F and L statements, we can simply write d. C will be equal to d dot gut. However, whatever we have and see if we have nothing in C return 0 and incremented by one. So let me run this code and we're going to get the same exact result as before. However, let me explain what did we do here. First thing, we created the foreloop. So we're going to pass through every character in the string. And then we are going to take whatever we have and the dictionary, the add key value C and incremented by one. So for example, if we have to, we are incrementing by one to get three. And if we don't, if it is not in the dictionary, we are just going to return 0, indicating that this value or this, It's not in the dictionary until now. And we are going to get 0 plus one to d, c is going to be one. So this is basically, this is how we can use the get method to minimize or to decrease the number of lines and our code entities. Very helpful. So this is a small example, however, we can use it in a much larger codes. Now, this is a general idea about dictionaries. With that being said, this is the end of this video. We continue talking about dictionaries and the next one. So see you then.
31. Dictionary Functions: And this video, we're going to continue our discussion in dictionaries. And I believe that we are now familiar with itr nested. So we're going to talk about nested dictionaries. We have already seen nested loops and nested if statements. So we also have nested dictionaries. For example, if I have the dictionary school, and in the inside the dictionary, I'll open the curly braces. I have glasses. So let's name the first class to be equal to plus one. So class one will be equal to I, open the curly braces and here, I'm sorry, not equal paren. And inside these cars I have students. So students one equal to Addie, student t2 equal to Joe, and so on. So this is it for example, this is class one. Let me copy this and this here. And shouldn't one will be alex, Student two will be john. So basically, this is how we can create a dictionary within another dictionary. So here we have the first or the outer dictionary, and we have two dictionaries inside class one and class two. So let me go ahead and print this goal. If I type School, and here we have class two. Let's run this code will go into that class one, we have student one, student two Jo, Class two, Student one, Alex and student to John. Now, let me print out the school at class one and see what we get. We're going to get the enter dictionary, that is class one dictionary. And inside of class one will have students. One had the student to John. And same thing if I wrote two, will get certain. One, Alex and student two, giant. So this is it basically for creating dictionaries. Let's move on to looping through a dictionary. First thing is that we are creating a dictionary. How can we loop through every element or every key and value inside, inside it? So one way is to work with a for loop. For example, let's suppose I have a dictionary of names. And this, inside this dictionary, I have the name ID. And this name will have a value of ten, followed by the name Alex with a value of 20. And finally the name John with a value of 30. And in this case, this is a dictionary of names. So let's print out these names using a for loop. So for For example, in the dictionary names, we're going to print out this key followed by the add this value. So the values of every name, we're going to get added ten, Alex, 20, Jiang, 30. So this is the key. Key are the names had the Alix and John, and the names at the values of these keys. And in this case we have 102030 for each t we have. Now let's suppose we just want to print out the names that have value of 30 or above. So before printing these names, we're going to check if the values of these keys are above 30. So f names at this kid is greater than or equal to 1030. We're going to print out this key and value. And we're going to that John 30. So the, only, the only key that has a value above or equal to 30 is John. So this is how we can use f statement using dictionaries. Now, let's suppose I want to solve these keys. One way to do that is to store them in a less dense resulted and finally print them out alongside with their values. For example, if I have this list of names, I can store analyst as Neyman cluster novelty as usual. And we are going to store the names, but is, so I'm going to store all the keys from this dictionary in the list I created just now. Then I'm going to sort this list using the list dot sort method. And finally, I am going to pass through every key and this list and print out the key itself and the value of this key in the dictionary. How do we access that using the dictionary? Here? Let's run this code and we are going to get, I'm sorry, here we got an error saying that type error built-in function or method object is not iterable. And this is because we forgot the parentheses here. And let's run this code one more time. I'm going to get a alex H.pylori heavy than John 30. So this is the names in alphabetical order. So a, h, and g, followed by the values. So here we have 20201010, and they are matching the dictionary we just created and our code. So this is it for dictionaries. With that being said, this is the end of this video. In the next video, we're going to talk about tuples to fuel them.
32. Tuples: Hello and welcome back. In this video, we're going to talk about doubles. And doubles are basically comma separated list of values. For example, if we want to create a tuple, we will simply type T indicating that this is a tuple. If this is just the name, can dry DU, tuple, e or anything. But just to make it clear, will drive team. And let's say I want to add some values. So number one, number two, number three, followed by number four. And lastly, the last element, that is five. And in this case, although it is not necessary, it is common to enclose the tuple in parenthesis. So this will help, help us quickly identify that this is a tuple. And whenever we look at the Python code and we have a parenthesis inside these parentheses, we have values separated by commas. We directly know that this is at Apple. Now, to create a tuple with a single element, we have to include the final comma. So for example, if I want to create a tuple and it is just one element, for example, the number one and I added in parentheses. Let me print the type of the steam. So for example, if I ran this code, I'm going to get STR indicating that this is a strain. However, I wanna have a tuple. So what do I do? I simply add become a here. Let me run this one more time. I'm getting the type tuples instead of string. So as you can see here, if we do not add the comma at the end, type, Python is going to treat this as a string and not a tuple. Now, let's move on to creating tuples. And this is another way of creating or constructing the tuple. And this is by using the built-in function tuple with no arguments, we can create a new empty tuple. So for example, if I want to create the tuple t, it is known to be toppled and, and the parentheses. So if I print now this tuple D, I'm going to get empty parentheses. And if I want to add something while creating this, I can add it here inside the arguments. I'm going to get the number one with a comma. Remember that if we do not include this comma, Python is going to treat this as a strength. Now, let's move on to accessing items and defiled assignment. So let's suppose I have a tuple T, and in this, inside this tuple, I have some numbers and strings, 1234. And I want to access the first element inside the tuple. So we're going to use the square brackets as before, the square brackets at index 0. And if we run this code, we're going to get the number one. Now. If we use the negative indexing minus1, I'm going to get the last element in this table. And that is the number for the string for in this case, since we are storing strings in this tuple T, we can also use the slice operator, and this slice operator selects a range of elements. So for example, if I want to take these two numbers, are these two strings, I can simply type 1-2-3 and underscored, I'm going to get 23. $0.01 is to the string two is at position number one and the strength three as position number two. And here we are. And taking all from one until three excluded. So this is why we just get two numbers, 23. Now, unlike lists, tuples are immutable. So if we try to change one element in this topic, we are going to get an error. So let's suppose I want to change the element at position 0 from one to five as strength. And let me run this code. I'm going to get error type error. It tuple object does not support item assignment, so we can modify the elements of a tuple, but we can replace one tuple with another, just like what we did with strength. Remember that we, when we want to change something in a string, we are creating a new strength and removing some characters and replacing them with others. So here we can do the same exact thing. For example, if I want to change this number one to five, I can easily do that by simply assigning T to be equal to the number five and plus whatever we have at the Proposition one until the end. And if I go ahead and print the steam, I'm going to get 5234. So what did we do here? We just this double D and slice it using the slice operator. Then add the number five into it before adding the rest of the elements. So we just remove the number one and number five using the slice operator here. Then we print it out, regard 5234 as if we replace d number one by the number five. However, this tuple is not the same as this one before. Now let's move on to topple assignment. So we have a unique feature in python. And this is the ability to have a tuple and the right side of the assignment statement. And this allows us to assign more than one variable at a time when the left side is sequence. So let's suppose I have the tuple T, and then this tuple I have had the units, and in this case, tuple ID, and followed by another string units. And in this case, I want to store them in x and y. So I will simply write x and y to be equal to this double D. And now if I print out x and then print out y. I'm going to get the and units. So this is how we can assign to the character or two variables at the same exact time. However, we have the condition. And this condition is that the number of variables on the left must be equal to the number of variables on the right. So if I have here had units and for example, number four, and I tried to execute this program, I'm going to get a value error, too many values to unpack. So it is saying that it expects to, however we just added this. And total is now three. Even if I remove this and add variable here, let's say a. And I run this code, I'm going to get another error. And this is not enough values to unpack expected 32. So here we are trying to assign three variables at the same time. And in side this puppet, we only have two variables. Now, sometimes we want to split a string, for example, and it doesn't need to be just a tuple, for example, here we have a strength had that unions. And we want to split these into two separate words. We can use x, y equal to ST string we just created. And using the split method and the dot operator as a delimiter, we can split these two words and store them in x and y respectively. So if our underscored, I'm going to get the same exact thing as before. Hadi and Eunice separated by using the delimiter here. Now, let's move on to compare tuples. So the comparison operator works with tuples and other sequences as we have seen before. And let's suppose I have two tuples of numbers or integers. And this k, In this case, tuple one will be equal to 123. Tuple two will be equal to also 123. And in this case, let's print is t1 less than t2. And let's run this code. We're going to get false since T1 is clearly equal to t2. So if i tried d1 equal to T2, I'm going to get through. Now, how does this operator work? Let's suppose I have three elements in each tuple. However, here I have the element four and a I have instead of three, I am have, I'm having a 100. And in this case, if I run this code, one, T1 is equal to t2, know. And if I try, T1 is less than T2, it is going to give me, give me the boolean true. So here we are comparing the first two elements. Here we have 11 and they are both equal. So we are jumping to the second element. Then we are comparing the element two with the element four. So two is less than four. We end everything and print out true. We don't look at the other elements even if they are too big. So here, for example, a 100 is way bigger than three. However, we are at the start here. At the second element, we found two different elements. And in this case, we are just ignoring everything after this element. Suppose we have so many elements after the element or the integer two, however, always get to, that is T1 is less than T2. And this is just because we have 24 at position number one or the index number one. So to wrap it up, Python is going to check for the first elements. And in this case 11, they are equal. So it is going to jump to the second one. And we here we have 242 is clearly less than four. So we'll just ignore everything after this and print true since T1 is less than T2. So this is it for the tuples. And in the next video, we're going to compare and talk about dictionaries and tuples. So see you then.
33. Tuples And Dictionaries : Now let's talk about dictionaries and tuples. So in dictionaries, we have a method called items that returns a list of tuples where each tuple is a key value pair. For example, if I have a dictionary and let's suppose I have dictionary called d, and in this case, inside this dictionary, I have the key. And it is equal to 300, became TV with a 220. And lastly, a key k. And inside this k We have 500. And in this case, let's use the item method, as we said before. And this method will convert this dictionary into a list of tuples. So we create a tuple and we have inside this list the items. And let me bring this team. And as we can see, we are going to get a list of tuples. So inside this list, we have a list by square brackets, as we can see here, we have square bracket and square bracket. So this is a single list, and inside this list we have tuples, and this tuple is a key value pair. So as we can see here, we have the key and its value. So this is one tuple. This is the first apple, and this is the second tuple. The tuple, as, as we can see, they are in pairs and value, and they are not in a particular order. So here we just created them, card, TV gave. However, we can always solve them. For example, if I use data as a list and then print out this t, and I will get here, sorry here, I have the astronomers go one more time. I'm going to get 300 TV to a 120 K 520. But after sorting, I'm going to get car 300, 500 TV to a 120. So as we can see here, the sorted by the key and not the values, and they are sorted. C is less than t and less than t. So this means that they are sorted in descending alphabetical order by the key value. Now, let's move on to multiple assignment with dictionaries. For example, if I have a list of items, and in this case, the items are tuples, as we can see here. So he would just created a list of tuples. And I wanna use every key and A value in the Stapel. For example, you can use default loop of key and value and this t. And we'll simply print out the key and the value. So let's print out value. I am going to get, let's run this code one more time. I'm going to get 300 car, 220 TV, 500 K. So what are we doing here? We are passing through every element in the list we just created here. And it happened to be that the elements inside this list are tuples. So we taking the key and value inside the Stapel and print, get them out. Now, let's talk about multiple assignment with dictionaries. So let's suppose we have a dictionary that is the TV and k and f stored them in a list. And I am going to sort them by the key. However, if I want to sort them by their values, how do I do that? So for the first thing we going to do is to create the list that is an empty list. And now we're going to use the items method or function. So for example, for key and value inside this needle items. And as we said before, this would return a list. And we are going to pass through every key and value. And in this case, we are going to append to the list we just created. That append. Not the key and value are going to append the value followed by the cave, and in this case value. And if we just want to see what was going to happen here, we can simply bring L and we're going to get, I'm sorry, he will have an error. Takes exactly one argument and argument are given. And in this case, we are going to sort them as a couple. And we're going to get 300 car, 220 TV, 500 K. And in this case, we just flipped the keys and values inside this dictionary and store them in a list, as we can see here. So we have 300 car 220 deviates by a 100 K. Now let's sort them by the values that is storing them by three hundred and twenty hundred to a hundred and twenty and five hundred. And we can simply do that by using the L but salt. And if I run this code, print out run, we're going to get 220 TV 300, car, 500 K. However, if I went to stored them, sorted them in the descending order, you should use here an extra argument inside this parameter using the reverse, and it will be equal to true. So what we're saying here is that we are changing the reverse from false to true. So by default, we have the reverse equal to false. And this reverse would simply sought every list or the thing we're going to sort by the descending order. And in this case, if I underscored, we're going to get five hundred, three hundred. And lastly, 220. And this is basically for multiple assignment with dictionaries. That being said, this is the end of this video. See you the next one.
34. Datetime Module: Let's move on to datetime. First of all, why do we use date-time in Python? Let's suppose, for example, you want to send an invitation length for someone, or you want to set up a meeting after today's. So here you can use data. And there's plenty of examples where we can use date-time, but for now, let's learn how to use it. So first of all, to work with data as data objects, we need to import the datetime module. And to do that, we will simply type import and followed by date. Now, we can use the classes and functions. And this module is built in module and you don't know, you don't need to download. It, is already downloaded and installed when you download the Python. So let's now use one of the functions here. For example, if we want to know, know the date. Now, we can simply create an object xx and use date, time, dot datetime. That now. And then let me print x. We're going to get, Let's run this code. We're going to get this line. So as you can see here, the date contain contains the year and month and also the day, then followed by the hour, minute and second, and then the microseconds and the DateTime modules has many methods, many methods to return information about the date object and we're going to see some examples about them. So the first thing we saw is to get the date. Now. Now let's see how can we create our own data. So instead of saying datetime, that datetime dot now we simply create the function x, datetime, date time. And inside the parameter of this function, we will simply at the year, month, and day. So let's suppose we want the 20-20 and the month 11, and then the day for. So now, if I run this code, I'm going to get 2020114. And since I didn't mention the minutes or hours or seconds, I'm going to get zeros. So we can add optional three extra arguments here and specify the hour, minutes, and seconds. Now let's move on to the STR f time method. And this method takes one parameter and specify the format of the string we are going to return. So let's suppose we have our object here. And we want to get the weekday. We can simply use acts that STR f time. And inside this, we are going to place our format that so that we can get. The weekday, and in this case, to get the weekday, will simply add centered a. Let's run this code. And we are going to get the weekday, Wednesday. Now, let's suppose we want to get the short version of the weekday. You simply use the lowercase a. And let's run this code one more time. I'm going to get WBD, and this is the shortcut of Wednesday. Now, let's move on to the month. And we have also a shorter version and the full version, short, short version is b, lowercase b, November and novae. And the full version is the capital B. And in this case we are going to get November. Now, let's suppose we want to get the number of the day from 0 to six and starting with Sunday at 0. And this case we can use the character or the letter w. And in this case we are going to get three, since Wednesday is at position number three, setting at Sunday 0, Monday, Tuesday, and Wednesday would get the number three. Now let's move on to the character d. And in this case, if we use the D, we are going to get into get the day of the month. So we are going to get $0.04 an hour code. We already wrote that this is the fourth day starting from one till 31. We also have the y and to indicate the year, and in this case, we also have two versions. The lowercase version would get 20, year 20-20. And in this case, the version we're going to get 20-20. Now let's just make it clear here. If it is 2018 and we used this, we're going to get 2018. And if we use the lowercase, we talked to get 18. And so there are plenty of these functions are these shortcuts. And let's move on to getting the date or the local version of the data. And to do that simply you use the letter x. Astronomers were going to get 11418. And if we want to get the local version of the time, we are going to get to write capital X and we're going to get 0 since we didn't specify the time. And last thing we are going to do is to use the letter C. And in this case we are going to get the local version of data time together. So this is it for the STR time method. And we have so many shortcuts and so many characters we can use to get different things from this method. And let us move on to the DIR, D-I-R. And in this case, if we use DIR on datetime, like this, DIR date time. And let me run this. We are going to get all the functions that we can use here, such as Maxine says datetime, date, and so on. Now, let's move on to getting date from a timestamp. And this timestamp is a number of seconds between a particular date and January 1970. So we can convert this timestamp to date using the from timestamp method. And in this case, let's suppose that I have a specific time stamp and I want to get the difference between this date and January 11970. And in this case, let me write this as timestamp. It will be equal to date. And let's use the timestamp method. And let's suppose I have this number of seconds, and let's print the timestamp and done this. And here we got an error saying that date is not defined because we forgot the input, the date from time, date. And this case, this one more time. I'm going to get this output. And this state we just impose it here is very helpful. We can also create our today's date from this. And we can simply do that by simply creating an object. Let's call it today. And we are going to use this date and the function today. And if I want to print out our current year, I can use today that year. And let me run this. I'm going to get 2020 phi one d month and day will come to today, that month. And today. And in this case, I am going to get 20-20. Demand is ten and the day is 20. Now let's move on to the time class that represents the local time. And in this case, if we want to use the time class, we are going to need to import it like the Date class. And in this case, from DateTime, we're going to import time. And let's use some of the methods here. So for example, if I want to create a time starting at 0, in this case, I can use the time method. If I go ahead and run this code. I'm going to get all zeros since I did not specify the parameter here. What I want from hours and minutes and seconds. And in this case, I got all zeros. However, if I create another variable object and in this case I will name it be. And I will give the time some parameters such as two and forty four and fifty six. Let's print B and underscore it one more time. We are going to get our two and minute 44 and lastly, seconds 56. We can also add the microsecond in this case, let me create a new object. And our one minute 34 and seconds 55 and some microseconds. And let's print C. Restaurant. We are going to get our one thirty four, fifty five point zero, zero, five, eight, four, seven. And in this case, this optional for this last argument is going to be as the microsecond of the second part. Now, as with the date function here also, once we create the time object, we can easily print the attributes such as the hour, minute, second. And in this case, if I have a time and let's name it b, a, and in this case I have three, 45, 55. And let me print the hour. I simply type a dot d dot operator and type the hour. Let me run this code and I'm going to get three grand a dot minute. And lastly, print a second. And that Miranda's on no time I'm going to get three forty five, fifty five. And I can also print the microsecond. And in this case, since I did not specify the microseconds, I am going to get 0. And finally, our last method is the SERP time. And this method creates a datetime object from a given strength. So let's suppose we have a string that is a date. And let me just create the strength. Let me name it as and in this case, I have five. July 2020. And in this case, let me use the STR method here. So let me create the date. It will be equal to datetime. And in this case, we need to import the datetime and then use datetime that use the STR time method, giving this string date as string as an argument. And we need to some extra argument here. And the second argument will be the format. And let me write it and I'll explain it. They be. And finally, y. So what are we doing here? Here? This spring will constitute the format in which we are going to create our date. And in this case we have dd today, be it as default month, say this is d capital V, we already talked about it. And finally, the y constituting the capital Y is the year before. And in this case, if I print this date, I am going to get 20275. And this is our minutes, seconds, and they are all zeros since I did not specify them. And the data string here, and this is it basically for the DateTime. And with that being said, this is the end of this video. Over towards beneficial. See you in the next one.
35. Zip Functions: Moving on to zip functions. And when you think about a zip function in Python does not like where whenever you zip, unzip a file or a folder on your desktop. So in Python, and zip function takes a parameter and iterators. And by iterative as we mean something that we can pass on its characters or elements using afforded loop, such as strings, dictionaries, and lists. And in this case, it will take, for example, tourists and take every elements by pairs. So for example, it will take the first two elements and create a tuple, stored them in a tuple, and then stored this tuple analyst and so on. So for example, the first two elements alone, then we'll move on to the second, the second elements. And each list stored them in a tuple and then store it and invest. So for example, to justify this, let's suppose I have a list, scarlet list1, and in this case it will be some, some characters or letters a, B, C, and D. And in this case I have another list called L2, which would be capital a, capital B, capital C, and finally capital D. And in this case, if I use this step function here, so let's create a new list. So this is the list zip list. And in this case, I am going to use the method with the last one as the first parameter and the list through as the second. And then I will store it in a list. And this list will be called subclassed. So if I go ahead and print this list and run this code, we are going to get a AA, BB, CC, and finally need the. So we took the list1 and list2. They took every element. So for example, the first element a and the capital a will be stored in the tuple, the first tuple here. Then the second element of the list will be the tuple that contains b and capital B, and so on, C, capital C, D, capital D. Now let's suppose we have five elements in list, 14 elements in L2. And let's say we have the additional element e. In this case, if we run this code one more time, we are going to get the same result. So it is important that we have the same size or the same number of elements and the two are less. And in this case, we have here five elements here for we are going to get. Only the first four elements of each list. And after that, whatever we have in this list, for example, in list one will be just ignored. So Python or this zip function, zip method will ignore whatever we have after the fourth element. And in this case, if I also have here, for example, a capital E. And let me run this as expected. We are going to get the same results. So it doesn't matter what list contains more elements. And this is it basically, this is how we use this function. However, let's suppose we already have dysfunction and this list we have tuples. And in each tuple we have, for example, the lowercase a and the capital case a. And in this case, if we want to go back from the zip to unzip, we can simply create a new object called unzipped. And in this case, we can use this zip function. However, we can add the star and use the simplest recreated earlier. And that's print unzipped. Let's run this code. We are going to get. And I'm sorry, we need to store them in a list. And we are going to get two different tuples and one list. And this top of the first tuple will be the first list we entered. So as you can see here, we have the four element ABCD. And they are the elements or items. And list1 and a capital a, B, C, D are the elements in L2. Now let's suppose we want to store them and a and b, we can simply use this and store them here, such as a dharma we. And if we go ahead and print now a, we are going to get ABCD. Let's print B. We are going to get the capital a, B, C, and D. Now let's see how we can use this zip function. And our code says suppose I have four loop. And in this case, let's say I want to pass through list1 and list2 together. So how do we do that? We, for example, can use for i in range of four. And in this case, we are going to print whatever we have, at least one at position i. And same thing at this position, I surrenders. We are going to get these letters, a, capital a, B, capital B, and so on. Now, this is the traditional way. However, weekend US a more fancy way. And in this case, we can use the zip function here. So for example, we can create a tuple, in this case, misnamed L1 and L2. This is how we can extract a tuples from tuples. A for loop. And in this case, we are going to extract for L1 and L2. And these, and this case the function will have list1 and list2 as parameters. And now if I go ahead and print L1, L2, and lets run this, we are going to get the same result as before. However, this is a better way since, for example, if we have ten lists, for example, or ten dictionaries, and we want to use each and every element and the each dictionary or each list. So it is better to use the zip function as we can use or take any number or any elements from these lists at once. And for example, here we took L1 and L2 at once. Now let's move on and create real example unzip. And in this case, let me delete this. And suppose I have three lists, and this case we have a list of list1. Let's name it. And in this case, I have an list1, some products that suppose I have banana, apple, and orange. Then we have the count. In this case. Let's suppose we have 345. And then the price in this case, let's say 0.06, 0.20.7. So what we're going to do here is that we are going to print banana. I bought the count three banana, three bananas at $0.6. And then I bought four apples at $0.2 and so on. So this is the idea, and let's go ahead and create our for loop. And as you remember, we can use the tuple here and extract from the tuple. So it will be the fruit counter and the price. And in this case, and the zip, we have list1, count and price. And in this for loop we are going to print the sentence. I vote. Then we are going to add STR of the price of the count. And this case we have the count. And then I bought three. Now I need to add the bananas. So plus. Sdr is already as a strength so we don't need to convert it. And I add one, I'm sorry, fruit. And then I bought three bananas. I need to add the ad, and then I need to add the price. So I need to convert it to string price and the donor side. So it is basically a string. This code I'm going to get, I bought three bananas. I'm sorry, here we need to add after fruit.'s and let's run this one more time. We're going to get, I'm both three bananas at 0.06. I bought four apples at the 0.2, and I bought for five oranges add $0.7. And in this case, we accessed all three lists. List one, count and price at the same time and draw this a sentence using these function. Now, it looks like it is a little bit messy and deal with that, we can simply move this STR counter and create hear counter and price. They will be equal to STR counter followed by STR price. In this case, I can simply remove STR from the sentence. And same thing here. We can give over STR also here. And the parameters code I will still have the same, I'm sorry, E and different this one more time we're going to have the same exact result as before, but we'll move the STR just to make it look like a better version of the print. We can also create a sequence outside. Let's name it as q, and this will be a list and then store every element here in this list. So we can use as equal to this. And then we use SQL that apparent and append this as. And then after finishing from the for loop, we can print skew. And let's run this. I'm sorry, here we need to do with the indentation. And one more time you are going to get the three sentences, sentences in one list. And so the first sentence, I put three bananas at 0.06, habit for uppers at 0.2. And I bought for five oranges at 0.7. each sentence is considered as one element and this sequence or this list. So this is it basically for the zip function. We learned how to use it and when. So for example, here we have three lists and we need to access them at once. So it is very beneficial here. And sometimes we might have ten different lists. And we need to perform some mathematical operation and use every element in this list. So it is very beneficial to use the zip function. Otherwise, we need to write very large or complicated line of code. So this will make it easier in terms of understanding and how to use. And with that being said, this is the end of this video. See you in the next one.
36. Sets : Moving on to sets. And in this case, we already wrote fantasies indicating that this is a tuple. We wrote square brackets and this is a list. And finally, the only thing that is missing as the curly braces. And in this case, this is how we create set. To use curly braces to create a set. And a set is an unordered collection datatype. And it is iterable, mutable. And the most important thing that it has no duplicate inside. So for example, if I want to add some numbers from one to five, I'm trying to add the number 51 more time. So this just ignore this number as it doesn't exist since I already have this exact number inside the set. And one useful way to use this set is, for example, let's suppose I have a list and I wanna remove all duplicates from this list. I can simply add them to asset and then move them back to this list. So this is how I can get rid of all the duplicates simply by adding them to the set. And the set automatically remove all the duplicates and side. And whenever I convert them back to a list, there will be no duplicates left. So this is the idea of a set. Let's use it. And first of all, let me name this set S. And in this case, if I want to add some elements here, I can simply open the curly braces and let's add some integers, 123. So this is our set. Let me print it out. And let's run this. I'm going to get the set 123. Now suppose I want to add the number 31, madame. So I simply use the dot operator and the add method unless adds the number three and print it out. One more time, we are going to get the same exact results. So I'm going to get three elements in a set, 123 in both cases. Since I already have the element or the number three in my set, it will not be counted. So let me remove this and add the number four. And in this case, I will get two different sets. So the first one has three elements and the second has four elements in it. And let us now, I use this set to remove duplicates from a list. So let's suppose I have a list and call it list1. And in this case I have some number in this list. So let me say 1223345677. And in this case, I want to remove all duplicates in this list. So I simply put them in a set. So let's create our set and I name it as. And how can I move my elements from the list to a set? I'll simply use set and give it a parameter list. So now if I go ahead and print this out, I'm going to get 1234567. So the set removes all the duplicates and this is how they will be printed. Now, let me convert them back to a list as usual. If I want to convert them back, IE use list1 to be equal to and convert this set to a list. Let me print my list. List one run, we're going to get 1234567 as a list. So this list one, this is the say, same variable or the simplest recreated here. However, really moved some items and elements without doing anything just from placing them in a set and then moving them back to our list. And we have something that is called a frozen set. And this person said is immutable and it has me immutable objects. And it is only used to support methods and operators in the decode. So for example, if I want to have is said to compare it to another set, and I'm sure I don't want to change anything inside the set. I can simply use the frozen set. And if by mistake I tried to change any element, it will generate an error. So let's suppose I want to create a frozen set. I will simply call it s. And in this case, use the president said, I'll type processes inside this set. I will use a list here inside this frozen set, and we can compare it to the set using this frozen sets. So as before, when we created as and it was equal to set. Inside this set, I place list1 and this case, I will place it diety here. So let me place 123. And if I go ahead and print out this frozen said, we're going to get 123. Now, we already talked about how to add elements to the set. For example, if I have a sad and in this case, I want to add some animals. I can add them either here or I can use the.add method and add the element. And if I print this, we're going to get 1234. Now, let's suppose I have two sets and I want to merge them together. I can use the Union. And in this case, let's suppose I have a set s1 will be equal to some letters a, B, C, and D. And S two will be equal to. The capital letter a, B, C, and finally P. And in this case, if I want to merge these two sets together, I can simply use the union method. For example, if I want to merge them in a set called S, I can simply type S1 that union and place the second set inside the parameter of the union method. Let us print s and c12. Down to that, we are going to get B, CC, BB, AA. And this set is an unordered. So it doesn't matter how the other is. We are just going to get the elements, all of the elements of S1 and S2 together in a single set. Now we also have another method to add these two sets together, and this is this character. So for example, if I want to add S1 and S2, I can simply use this. And let's print as one more time and run. We're going to get the same exact result. So we have two methods here. We can either use the union R, this operator. And let's move on to the intersection. Suppose we have two sets of numbers and that we create them here. Different set will be s1 will be equal firstly to an empty set and as to the same thing as two. And then let's add some numbers to S1. So let's suppose by wanna add the numbers from one to ten. So I'll use a for loop. So for i in range of ten, in this case, I'm going to add two Aswan. This i. And the same thing for S2. And in this case I want to add all of the elements between 26. So for i in range of sex, I want to add these numbers to S2. So if I go ahead and print s1, now, I am going to get from 0 until n minus one and the same or as two. I'm going to get from two until six. Minus1 should do 345. Now, let's use the intersecting intersection method here and do get the intersection of these two sets. So let stored them in a set called S3. It will be equal to S1, that intersection. And inside the diameter of this intersection method, I will add my second set. And let's run this. We are going to get, I'm sorry, I didn't print it out. So let's print S3. Run one more time. We're going to get 2345. And this is the same as set to. However, this is because all of the elements of set to our already existing in set one. So let's suppose I add here, for example, in set to do that, add the number 15. We have here 234515. However, if I run this code, I will only get 2345 since 15 is not N, The first set. So set one contains all of the elements between 010. Another method, find the intersection of these two sets is to use the and operator. And in this case, I can create S3 and S1, S1, and S2. If I print this, we're going to get the same result as before. So we are also here have two methods. The first one is to use the intersection method and the second is to use the and operator. Now we still have some methods and we go over them in the next video. So see you then.
37. Sets Functions: Welcome back, and we are still discussing sense. Let's move on to the difference between two sets. And I suppose we have the two sets are the same two sets as before. The first set contains all of the elements between 09 and the second set against the elements 234514. So we'd added 14 year. Now let's suppose I want to compute the difference between SATB1 and is true. I can simply use the method and difference. In this case, let me create S3 and use S1, that difference as two and as rain. And in this case, we are going to get 016789. So notice we have here 14, but it is not showing up and the set S3. So what did we do here? First of all, we took the set S1 and compare all of the elements inside the set with the set S2. So we start with the element 0, and this case we tried to find the element 0 in S2. We did not find that, so we store it in S3. So this is a difference between S1 and S2. Same thing for element one. We started here. However, we have 2345. So we are not going to put them since they exist. And both sets, S1 and S2. Now we jump to 6789 and they do not exist in less than two. In this case, we are going to place them and says three. However, this number 14 is not. And the different set because we are comparing the elements in set 12 elements in set two and not vice versa somewhere not comparing the elements here with the elements in set one. Now, let's suppose I want to know the difference between two nd S1. I will simply use as to the difference of S1 and not like this. So instead of typing S1, S2, that difference and difference of s1 has rung. We are going to get the number 14. And in this case, what did we do? We search for number two. In set one, we found it, so we don't put anything. And the same thing, 345, they are already in set one. So as you can see here, 345. So we ignored them and finally we reach the number 14. And the number 14 is not inset S1. And in this case we added to the difference. So S3 will only be a one element, that is the number 14. Now, let's suppose I want to clear these sets. What do I do? So the method clear, we used this method a lot of times and the list and dictionary in everything. So for example, if I want to clear all the sets, I can use clear for S1, S2, clear, and finally, S3, that clip. I print them out one more time. S1, S2, and S3. Let's run. We're going to get an empty set. And this is how Python indicates that this is an empty set that will give you sat with no parameters inside the parentheses. So this is how we can delete all of the elements at once and a single set. Now let's suppose I have two sets and I want to know if they have anything in common or not. And in this case, I can use the, is this joint method and this will give me the Boolean indicating that true or false. So if set for, for example, is set and set one and set two have nothing in common that will grant true. So for example, as you can see here, we have two sets, S1 and S2. And this case, we can clearly see that they have 2345 as common elements. And in this case, let me delete this and create this Boolean. So I simply print out s1. That is this joint as two. In this case, since they have common elements, it will print false. However. Let's suppose we have two different sets. And in this case, as two will start at ten and finish at 15. And as we can see now, they are different. And in this case as two contains the element 1011121314, even who added one more time, as you can see here, we add the number 14. It will not be showing up in the set since we already have this number here. And they are two different sets that have no common elements. In this case, if we use the SDK method, we will get true indicating that yes, set one and set two have nothing in common. Now let's go back to our previous range. So between 26 and, let's see if set to as a subset of set one. In this case, I can simply use the method subset so as to a subset of S1. And if I run this, I'm going to get false since I have 2345 and all of these elements are ends at one. However, this number 14 is not in set one. So if I remove this line and print it out one more time. So we can clearly see that all of the elements in set two are in set one. In this case, we are going to get true since set two can be considered as a subset of set one. Now if I convert this S1 that a subset of S2, we can clearly see that we have more elements here. And it is not possible to have this set to be a subset of a smaller set. In this case, we are going to get false. We then do the same exact thing using the plus operator. Instead of typing a subset, I can simply use as less than S2. In this case, I'm going to get false. However, if s1 is bigger than S two, and this is, this means that S1 is upper set, considered two as two. And we are going to get through moving around to removing items or elements from a set. And let's suppose I have this set one that contains all of the animals from 0 to nine. And I want to remove the element 0. I can simply use the discard method, in this case as 1.The scarred 0 and print out s1 run, we are going to get 1-2-3 up until nine, ignoring the number 0. However, let's suppose I added here some argument of a number ten, and this number doesn't exist in set one. Let's rank does. As you can see here. We get, we got 0123456789. So as you can see, it doesn't remove any element from the set. However, if we use, we also have the method removed. And in this case, the phi attempt to remove the element at the item 0, I'm going to get a set that is, that does not include the AMC. However, if I try to remove the Amitabh Dan, For example, I am going to get and Arabs. And remember that when we used the discard method with argument than we did not get an a, we just got the set itself. However, if I use this method, remove, we are going to get a key error. And that is at line sex, as you can see this because of the removed method. Now, let's do an example on sets. So let suppose I have three Les and I wanna know the common elements in this list. And for example, I have the list L1 will be equal to some numbers 1234567. L2 will contain also some numbers, 1580. And finally, less three would contain 123789. And in this case, let's check for the intersection, first of all, between list1 and list2. So the first thing we are going to do is to put them in sets. Set one will be equal to set of list one, set two, set of L2, and finally, set three to be equal to set of lesson three. In this case, let's compute the intersection between the first two sets, S1 and S2. So enter. I will name it, enter will be equal to S1, intersection of S2. Then we will compute the intersection between S3 and this intersection we just computed here. So do that final intersection, I name it equal to enter that intersection of S3. If I go ahead and bring the final intersection, now, as you can see, we are going to get one. So the element e1 is the only element, but the number one is the only number three. And this three lists. And if we just check, so we have here 111 and here we have two. However, this too doesn't exist unless two and so on. We can only find, for example, three exist here and e, eight exist here and here, and so on. Now, to make it clear, I will just print out the inter friends and run. We're going to get 105. So when we computed the intersection between S1 and S2, we got just the numbers 15. And we can clearly see that 15, the only common elements between list1 and list2 Do we have 80 and not included and list1? And after that, we computed the intersection between our result 105 with less three. And in this case, only the number one is enlist three. So as we can see, five is not included in list three. And this is why we just got the element one. Now let's do another example on set. And in this case, we are going to create a code that gives us the number of vowels and strength. So first of all, let's ask the user to enter a string so the string will be stored in t. In this case, we are going to ask and rank. And then we are storing it and esteem. Now to check the violence, we need to create a set of hours. How do we do that? We simply named our set by word. And in this case, I will create a set inside this parameter, I will add some vowels in a string. So a E I, O U, and the capital a, E, I, O, U. And now that we have our string ST and our vowel set, I can go and create our for loop. So we're going to pass through every character in the string. So I name it for C and our SD strength. And this case, we are going to check if this letter C and the vowel sounds. So if it is n vowel, we'll increment the count. So I am going to create the outside. Then I will incremented here to be equal to count plus one. Then after that, I am going to print out the number of vowels. So the number of vowels will be and it is the count itself. Let's run this. So we asked him enter a string. So for example, I will type hello. World. In this case, I am going to get number of vowels is three. So as you can see here, we have 00. And let me run this one more time and I'll just enter three a's. I'm going to get the number of hours is three. However, if I enter something that does not contain vowels, and i press enter, we are going to get the number of hours is 0. So this is at four sets. And they are very useful when it comes to removing duplicates, as you can see. And with that being said, this is the end of this video. See you the next one.
38. Examples 1: Now let's solve some exercises using Python. And the first exercise is to find a specific number is a prime number. And by definition, a prime number is a natural number greater than one. That is not a product of two smaller numbers. So for example, if we have the number five, suppose I have n equal to five. And in this case, and is prime, since the number five can only be written as one times five, or five times one. So a prime number is only divisible by one and the number itself. So let's suppose I have four in this case, I can write for equal one times 44 times 12 times two. And this is clearly not a prime number since we have the number two here. And this indicates that this is a natural number. And let's go ahead and try it out code. So first thing we are going to do is to ask the user to enter a number and let's store it in an input. In this case, I am going to ask the user entered and number. And then I'm going to convert this string into an integer and store it in the variable called num and n, m. Now we have the number. First of all, we need to check if this number is positive. So the first rule is to have a positive number. And in this case, I can simply check if numb is greater than one. Then we can continue with our code. Now, how do we find if a number is a prime number? We are going to pass through all of the numbers between two and the numbers. Let's suppose I have the number equal to five. And in this case, to check if five is a prime number, I'm going to pass through all the numbers between 15 excluded. So I'm going to pass through the number two and check a five is divisible by two. This is not the case. Then I am going to number three and so on until I find one number, that is, this number n is divisible by, let's suppose I have here the number six, and I'm going to pass through all of the numbers. I sat with the number two. In this case, six divided by two is equal to three. So this number six is not prime number. And this is the general idea of how we are going to find if a number is prime or natural. And of course, if we don't find any number that we can divide this. And with, we are going to say that this number is a prime number. And in this case, num is greater than one. We are going to pass through all the elements, as we said, from i in range of, As we said, starting with two. And all the way up to now minus one. And this is how we can write it. So numb, excluded. And then we are going to check if this number is divisible by two b equal to 0. So number divided by I equal to 0. This is the case. I am going to just print out that this is not a prime number. So now is not a prime number. And I am going to break out of this loop. And let me break here. However, if I run all of the for loop from i equal to two until num minus1. And I didn't find any number in which NUM divided by this number is equal to 0. So this is how we found the prime numbers. So this is not the case. I'm going to print that number is a prime number. And then we also have the condition if numb is greater than one, then we are going to execute this code. However, if num, It's not greater, we are going to simply print out that num is not a prime number. And we'll go ahead and run this code. I'm going to get answer a number. Let's suppose 55 is a prime number. And let's suppose I entered sex. Sex is not a prime number. And finally, if I enter minus phi, in this case, transfer is less than one and I'm going to get minus five is not a prime number. Also. Now, we can modify our code to make it a little bit more clear. And in this case, if I enter a number that is not a prime number, my program might print that sex is not a prime number and give me why sex is not a prime number. So how do you do that? When we arrive to num of i divided by i is equal to 0, we are going to print out the I and NUM divided by R. So how do do that? Simply here, print i times. In this case, I'm going to divide the pie and then I'll add s. And finally number. Let me run this ad sex. In this case, I'm going to get sex is not a prime number and y, since two times three is six. And if I want to make it an integer, simply add. One more here and run this. I'm going to get two times three is six. And now we have the reason why sex is not a prime number, since we already have two numbers. And which if we divide six, we are going to get an integer. Now, talking about prime numbers, let's create another program that displays all of the prime numbers between a specific interval. And in this case, I am going to ask the user to enter two numbers, the lower bound and the upper bound. And first of all, into the first number. Then I'm going to ask the user to enter the second number. The second number. Then I'm going to convert both numbers. So numb one equal to n and put one and num two will be equal to input two. Now we have our two numbers and we are going to pass through all of the numbers in between these two boundaries. So how do you do that for? And let me name it. And range of the lower or the number one and number two plus one. So remember that whenever we use a range method, this number two is not included. So if you want to include that, we simply add one. And I want to indent, of course, so I'm going to indent all of these. And I think that here we need to also the if-else. And I think we're good. Unless we have to also indented. Now we're good. And so I deleted and is not a prime number since we are only searching for prime numbers in a specific range from num1, num2 denoted by n. And in this case, we are going to take every number. And so I suppose I start with 500. I'm going to take 500, check if n is greater than 1500 is clearly greater than one. So I'm going to execute this for loop followed by the ALS. So first of all, I'm going to check if we have any number between 2500 and which if we divide 500, we are going to get 0. If this is the case, we are going to simply break out of this loop and go to check another number, 501 and so on. However, this is not the case. So we are going to get that 500 is a prime number or this number is a prime number. Now let me run this. And they're asking us to enter the first number. Let's suppose I enter 500. The second number would be 600. And if I run this code, we go to get 13818450000 four forty one, forty seven, fifty seven, and so on. And if you go ahead and check for yourselves. So these numbers are prime numbers and they cannot be divided by any other numbers except one and the number itself. So to simplify things, we first of all ask the user to enter two numbers. And these numbers are the boundaries. For example, we entered five hundred and six hundred. So we are going to pass through all of the numbers between five hundred and six hundred included. Since we added plus one here, just to say that we need to include 600. And we took every number between this interval and search as this is a prime number printed out otherwise, don't do anything and go back to search for another number. So this is basically for the prime numbers. It is a general idea. And that being said, this is the end of this exercise. And see you in the next one.
39. Examples 2: Now let's solve another simple example, and this is to find the factorial of a specific number. So we're going to ask the user to enter a number. First of all, we store it and then put M and then converted to a number using the nth method. And now we are going to get the factorial of a specific numbers. First of all, all of the negative numbers don't have factorials. So first thing we're going to check if the number is less than 0, then we simply going to print that the factorial does not exist. So factorial doesn't exist. And after that, if this is not the case, and if number is equal to 0, then we're going to print out the factorial of 0 is one. So the factorial of 0 is one. And finally, if both of the cases we are not satisfied, then it is surely that this number is a positive number and we need to compute the factorial of this, a number called DOM. So we are going to pass through all of the elements between one and the number itself. And to do that, we can simply use the formatted for loop and for i in range of. We said that starting at one and finishing at this number plus one to include the number itself, then we are going to multiply each number. So let's just create a number factorial cold outside. We can't create it as zeros since we are going to multiply every year with this number. So again, say factorial factorial, factorial times by sends. The first factorial would be equal to 0, and then this will lead to having zeros anytime we enter a number. So to fix that, we will simply add one here. And then let's print out this factorial. So let me just make it a little nicer. So d factorial. And then we're going to add the number. And then as. And finally add factorial. And this is basically a strong discord. And to a number, let's suppose I enter minus five, is going to print factorial doesn't exist. Sorry, one more time. And I'll type 0, going to get the factorial of 0 is one. And lastly, let me type six and d factorial buff sex is 720. This is simple code or simple exercise. Let's move on to. A little bit more complicated one, and this is to find the Fibonacci sequence. First of all, let me explain what Fibonacci sequence is. And basically this sequence is the summation of each two numbers. First of all, we have the numbers 01. And how the sequence works is that we are going to add the previous two numbers. In this case we have 010 plus one will give us one. And then we are going to add one plus one. We are going to get two, then 123, then two plus 35, and then three plus 588 plus 513, and so on. So this is how we are going to compute the Fibonacci sequence. And let me start with our code. So the first thing we're going to do is to check if the number of terms the user is going to provide us with is negative. And if this is the number of terms is negative, then we have an error. So we're going to ask him to enter the number of terms again. So we are going to expect from the user to enter a positive number to indicate how many elements should we print from the sequence. And this is an infinite sequence, since every time we can add the two numbers and we're not going to. And since we can simply add eight plus 1321, then 21 plus 13, and so on, so forth. And we are going to ask the user to enter a number. In this case, let's suppose he enters the number five. So we're going to print out the first five elements of the Fibonacci sequence. So the first five elements are 011, 23. And in this case, let me start by asking the user to enter the number and enter the number of elements and then convert this string into a number. So as usual. And first thing we're going to do is to check if this number is negative. This number is negative or equal to 0. We're going to print that. Please. Enter a positive, the number. And in this case, let me just put it in a string using quotations. And then if this is not the case, we're going to do something else. So first of all, let me create the previous element, and in this case the previous element and name it priv. And it will be equal to 0. So this is the first element. And then we have the 3D screen is, I'm sorry, this is the first element. It will be equal to 0 and the second element to be equal to one. And you'll understand it and a bit. So here we have the first two numbers and let me add a counter outside to be equal to 0. And now this is not the case and the number is not less or equal to 0. It might be equal to one. And in this case, the number is equal to one. Then we're going to simply print out the first grief, grief. And of course, this is also not the case. We are going to create a for away while loop and work in it. So first of all, since number is not less or equal to 0 and number is not equal to one, then it is surely greater than one. And in this case, each number greater than one, we're going to pass through the while loop we're going to create. And this while loop will hold and tell. Counter is greater than the number of terms. And while counter is less than this number, we're going to print the number as here. So we're going to print the result. And let me just break before the number grief. Grief. Then print out the number priv and go to this while loop to print the result. And this result will be equal to previous, previous, previous. And after printing this result, now, our previous previous will be equal to previous. And let me just talk about it in numbers here. So first of all, we have 0 and this is the proof brief. And then we have the number one. And this is the previous. And of course we have the result that is denoted by result and it is equal to one. Now, after printing previous, previous and then proved the printing press and entering this while loop to print out result. Now what should we do next? Now the previous will be, previous, previous will be equal to this one. Previous would be equal to one. And then the result as the summation of these two numbers. And it will be equal to two. And this is, this will hold until the counter is less, is greater than numb. And if we want to do it here, for example, after printing the result one more time, after printing two, we're going to remove or adjust these variables to be equal to one. Previous would be equal to two, and the result will be equal to the summation of these two variables. And how do we do that? We simply assign previous, previous to be equal to the current previous. Then assign the result of the previous two be equal to our result. And after that, simply, you will simply go back and print the result, then adjusted. So here we have our result to be equal to previous previous breathes. And of course we should also increment the counter, counter equals counter plus one. Lets run this. We're going to that, enter the number of elements less. Suppose I just want five elements. You're going to get 0112, 358. And as you can notice here, that we got seven elements instead of five. And this is because we are ready print out previous and previous, previous outside our while loop. And to fix that, we simply subtract two from the number here. And let me write one more time. Five, we're going to get 01123 Fernandez, for example, for the element 20. So this is a very big number support because we just entered 20. However, if we add, for example, the number 10 thousand. And in this case you can see how big the numbers can easily get. And this is the power of the Fibonacci sequence. Whenever we add two numbers. And for example, a considerable amount of time, they will get really big fastly. And let me just stop this code. And this is it for the Fibonacci sequence and d factorial. But that being said, this is the end of this video. And the next video we'll see some other examples. Also. To see you then.
40. Example: Calculator: Now let's move on to another exercise. And this exercise we are going to remove all of the brackets and racist. So for example, if we have the square brackets and curly braces and parentheses, NSString, we're going to remove them and return a spring and a string that is AMT from all these brackets, parentheses and braces. And to do that, let's just delete this and ask the user to enter a string. In this case, instead of entity number of animals. And the strength. And first thing we are going to do is to create our own string. In this case, I name it b. And in this string we are going to have all of the brackets, parentheses, and curly braces. And after that, we are going to pass through every character in the string from the user and check if this character is in B, that this is not the case, then we are going to add it to our newly created strength. And let's create this drink. So let's name a title and it will be emptied. And we're going to pass through every character in input. And we're going to check that if this here, so if this character is not in our string b, then we're going to add it to the final. So finding V final plus c. Then after exiting this whole loop, we're simply going to print our output and this is our final string. And in this case, astronomers enter a string and suppose I enters Hadi. Hello. Hi. And good. And let me run this. We're going to get the hello. Hi, good, without all of the characters we used and the string b. So what did we do here? We created our string B and which we stored all of the elements we don't want in our new strength. And then we pass through all of the characters and our input. And if this character is not in B, then it is surely not. One of the elements are one of the characters, such as parentheses, square brackets, and curly braces. So it is safe. The final string. And we keep on adding at the exiting this for loop and finally, printing out our result. Now speaking about strengths, let's create another program that takes as an input a string of words, and then print out the words in an alphabetical order. And to do that, first of all, we are going to ask the user as usual to enter the string. Then. And we are going to store every word of the string and a list. And to do that, we simply use the words. This is our new list. And inside this list, we are going to use the split method on the input we just got from the user. And as we said before, this split method like pre-print words, and you can't see it for yourself if you don't remember it. And let's run this. And suppose I add two words at the high, is going to split these words using the whitespace here and stored them in a list. And the elements of this list, Strengths. And in each string we have exactly one word. Now, after solving these words, any analyst, and let us just go back to our code. So since we have a list of words, we can simply use the sort method that we have within the list. So we'll simply use words that sort and then print out the sorted words. So print out words. And if I run this and let me enter high ABC and low units. In this case, if I type, if I pressed enter, I'm going to get ABC as the first element. Second Hadi. Hello, hi, how you Uranus? And this is our list. Now, what do we do if we want to get them back and strength? And as you remember, we can use the join method. And this will simply give us our less bad our string back. We need to create the perimeter. And in this case, that we created as an empty space. And the delimiter B equal to just some white space. And then we are going to use the delimiter that join. And we're going to store the words. Then we'll simply print out our MP, a string, the fundus. Let me just copy this and paste it back here. And we're going to get abc are the hello, hi, how you Uranus. And in this case, we just converted all of the elements and put them in a list. Then sort this list. And finally, put them back in our string that is called mp, and then print it out. So this is our result and this is our string. And of course, this is our list called words. And as you can see here, it is denoted by the square brackets. And this is it basically for this exercise. Let's move on to another one. And in this last example of this video, we're going to create a simple calculator by using the functions. So first of all, in this class or in this program, we are going to create our own functions. And we'll simply focus on the four basic functions. They are addition, subtraction, multiplication, and division. So let me go ahead and create the first function that is the add. And it will be taking two parameters, x and y and add them together. So it will simply return x plus y. And the second function will be the subtraction. And let me name it subtract to also take two parameters and x minus y. Then the third function will be multiplication. So multiply x by y, and we'll simply return x times y. Finally, our last function is the division. So divide x by y, and we'll simply return x divided by y. So these are our, our full Functions. And let's start with asking the user to choose between for operations. And these are the four operations. So let me just ask him to choose between the operations. And of course I am going to print one. That is addition and subtraction. Then 33 is multiplication. And finally, four. And it is division. Now after two thing, we are going to keep working and until the user enters something. And let's suppose we're just going to ask the user every time he enters, until he enters the word w1. And in this case, so while true, I will simply sort the choice, he shows a choice. And in this case, your choice. As 1234. So I already gave him in the beginning to choose between operations. And I said that addition as one subtraction to multiplication three division is four. And then we'll go into store. The input enjoys. And now we're going to check if the user enters one of the four numbers, 1234. So how do we do that? We simply check if choice is end this dipole. So we have 12, 34. And if this is the case, we're going to ask the user to enter, to enter two numbers. And in this case, they would be num1 and restore it and a fluids or input and enter first number than numb two, same thing, float input and the second number. So now that we have our numbers, we're going to check if our choice is equal to one, will perform the addition. So f choice is equal to one. We are going to print the addition of using the function at, at numb one with num two. And if this is not the case as choice I equal to two, then we're going to subtract. So print, subtract num1 and num2. Then this efficent, this also is not the case. Then going to check if choice is equal to three and perform the multiplication. So multiply num1, num2. And then finally, this is also not the case. Then x would be equal to four. And we're going to multiply, sorry to divide. So print Divide by num two. And of course, after finishing From this, we can break. However, if our choice is not one of these four numbers, we are going to ask the user one more time, too, enter a number by printing out that this is an invalid. And going back to this while loop and then asked him again to enter your choice between 14. So I've run this code, I'm going to get, run this one more time. Sorry. Let's run. We're going to get choose between the operations, addition, subtraction, multiplication, division. And in this case, if I enter five, this is an invalid number. I'm going to get into choice one more time. In this case I'm going to choose one. So the first number will be for the secondary B7, and I'm going to get 11. Now if I want to have, the multiplication will choose three. So 44 times six, I'm going to get 264. Let me try one more. So the division is at number four. So 12 divided by four. I'm going to get the number three. Now, our code would only perform, as you can see, will only perform one operation at a time. So we can't run this. We can't use it until we run this again. So how do you fix this? We can simply create a while loop or we have this existing wild loop and we can't modify it, so will not exit it until we have the user to enter some character or some letter. And in this case, you can modify it and work with it and see if it works. So this is it basically for the calculator. It is a very simple one. And you can create much more complex ones using the functions and methods we learned earlier. And our class. With that being said, this is the end of this example. And see you in the next video.
41. Recursion : Now let's discuss recursions. And recursion by definition is the process of defining something in terms of itself. And we already know that in Python we can call a function inside another function. So in other words, function can call other functions. And it is even possible for the function to call itself. And this is recursion. Now, recursion is very beneficial when we are writing R code. It makes it look clean and elegant. And also, when writing a complex, a complex task, we can break it down into subproblems and use recursion. However, on the other side, sometimes the logic behind recursion is hard to follow. And we see that in the next examples. And recursive calls are expensive and take up a lot of memory and time. And finally, this recursive function is hard to debug. So if we have a function and it is not working, it will take us more time to debug it. So now lets demonstrate the use of recursion and this new concept in Python. For example, Let's create a function that computes the factorial of a specific number and any recursion, we have always a base case. And we learn about now, for example, if we have the function, let's call it factorial. And this factorial will take parameter x and we'll find the factorial of this number called x. Now, one method to do that is simply multiply each number from one till x. So for example, if I have a number, the number four, and I want to compute the factorial of this number. It is equal to four times three times two and times 11. Way to do that is to simply multiply this number x by d factorial of the second number. So I can remove all these and type three factorial. And also, if I want to compute the three factorial, here we have four factorial. And if I want to compute the three factorial, I am going to type three times two factorial and so on. So this is the general idea and how can we implement it in our function called factorial. So this method or this function factorial will just, and whenever we reach the number one, in this case, one factorial is equal to one, and then we're done. We don't have to do anything after reaching this number. So to simply start with this, we are going to set the initial conditions or the base case to be fx is equal to one, will simply return one. However, if this is not the case, we are going to I'm sorry, you will indent. And if this is not the case and x is greater than one, then we are going to return this number x, as we said. So here we have four multiplied by the x minus one factorial. So I am simply multiply by the function itself. And as a parameter, I will enter x minus1. And here the factorial dysfunction will do all the work for me. I will simply just right that I want x times factorial of x minus one and it will go to four times three times, three times 22 times one and so on until reaching the number one and then return. So if I now let say I have the number equal to four and I use d factorial, so I'll print it out, print factorial, and we have our number as a parameters. Let me delete this and run. Because and we are going to get 24. And if we check four times 312 times to 24 times one, stage 24. So we've got the factorial of this number using recursion. And this is the general idea of recursion. However, we can solve much more complicated problems and using simple tasks as we did here. So we just understood the basic of this factorial, how it works, and then divide us into a simple task as just multiplying the number by the factorial of the following the previous numbers. And of course, there might be some other easier ways to do to compute the factorial. However, we are just looking at the recursion and we're going to dig deeper into this concept in the next problems and exercise. Now let's solve another example and recursion. And this will sum, that will get us the sum of a list of numbers. And in this case, let me just create our function and call it the sum list. And of course it will take a list. So let's name it list WT. And, and in this case, let's suppose we have the following list. So I have 12345. So I have a list of five elements and I want to have the sum of every element. I want to compute the sum of all of the elements inside this list. And of course, if I have this list and let's call it L1, and I can use the sum method and to compute the sum of L1 by renters. I'm sorry, not amend this. So let me just add a comment here and then we get back to it later. And if I run this, I'm going to get 15. So the sum of all the numbers. As 15. However, we need to solve it using recursion. So we need to think about a method that will allow us to call this sum list one more time. And to do that, we are going to set the base case. And in this case, the base case of this function will be whenever the less is, the length of this list is equal to one. So f length of l is equal to one. Then we are going to simply return the, the only element of the list and that will simply return and at 0. However, if this is not the case, let me just rewrite our list here. We have 12345. And if this is not the case and we have more than one element left in the list, we are going to use slicing here. So let us think about this recursion method. First of all, we are going to take one and then add to it whatever we have of the list. And in this case we still have 2345. Then after that we are going to take this list 2345, and let me paste it here, 2345, that will be equal to two plus the list 345, and so on. So how do we do that? We'll simply return the element at position 0 plus called the method itself one more time. And inside the perimeter of this method will not go into enter the list itself. However, we are going to enter the list sliced from one until the end of the list. So now after returning this, we can simply print L1 and ASUM list L1. And let me just be deals. And we have our list L1, we use the sum function here. And the firearm deaths. I'm going to get 15, the same exact result as before, but let us just make it look more simple. So what did we do? First of all, we created our function sum list. It will take the list as a parameter. And our base case will be if the length of this list is equal to one. So whenever we have five and in this case the length of this list, it is one. We simply return the number five itself. And how do we do that? We simply called The first index the list at position 0, and we're going to get this number five. However, if this is not the case and you still have some elements, for example, let's suppose I have three 4-5 left and my list. Then I'm going to return, as I'm saying here, I'm going to return the first element, that is three plus some list of l one to the end till the end of this list. And this list, L1 till the end is 45. And I'm going to call the same method on DES. Two elements in this list. And check again, flanked is equal to one and so on until reaching the length equal to one. And here our function will finish and we get our result as an addition of all of the previous execution of this function. And this is basically for the summation of elements inside a list. Let's now move on to another example in which we are going to compute the value of a specific integer to the power of another one. So we are going to compute a to the power. And in this case, let me write another function and I name it power. And it will take a and b as parameters. Now here we will have multiple base cases. So now let's suppose that we are entering a and a is equal to 0. So 0 to the power of anything will simply return 0. So the first thing we're going to check, if a is equal to 0, then we simply return 0. This is not the case. We are going to check if b is equal to 0, and in this case, a to the power of 0, we return one no matter what a is. So b equal to 0, just returned one. And finally, if b is equal to one, so if this is the case, then we'll simply return a sense. Any number to the power of one will return the exact same number. So simply return a. And the final step is to compute the power by ourselves if all of these conditions were not satisfied. And how do we do that? We simply get, for example, let's suppose I want to compute the two to the power of five. And we already know that two to the power of five, S2 times two times two times two times two. So two times 25 times. And in this case, how can we do that? We simply return the number itself, a, and we multiply it by the power of a and B minus1. So every time we compute this, we call this function, we are going to return two times, two to the power of four this time. And after that, we are going to call this power function one more time on this parameter. And this will be two to the power four will be equal to two times two to the power of three, and so on until reaching two to the power of one. And in this case, B will be equal to one and will simply return a. And this is how we can compute the power of a specific number a and B, some specific numbers a and b. And let me just use it here in our code. So I'll simply print power of two to the power of five. And in this case, let me run this. I'm sorry, here we took and we forgot heat to type returned as from the summer time two to the power of five will give us 32. And these are general exercises. And to just demonstrate the use of recursion and how to think. Whenever we need to solve a problem using recursion, they are pretty simple and they can be solved using other methods. But we are going to solve them like this just to make sure that we understand how recursion works. With that being said, this is the end of this video. In the next videos, we are going to solve more problems on recursion. And from now on, they will get much more harder. So the bye and see you in the next video.
42. Towers Of Hanoi: Now let's move on to another more complex example. And this is called the Towers of Hanoi puzzle. And first of all, the Tower of Hanoi is a mathematical panel where we have three roads, as you can see here, 123 and, and desks. And in this example we have three disks also. So the objective of this puzzle is to move the entire stack to the other row that is here. So we need to move all these disks from one to three. And as you can see here, we accomplished it and the last step. So we have some simple rules. The first rule is that only one disk can be moved at a time. And the second one is that each move consists of taking the upper desk from one of these tags and placing it on top of another stack. And the desk can only be moved if it is the uppermost desk on a stack. So what does that mean? So for example, if we have this orange disk and you move the two boys three, we can't move this purple disk from one to three because we'll be having a bigger disk on top of a smaller one. And this is again the rule. And let's go through this example. So first of all, we move the first desk, the orange 1243. Then we move the purple tuple two. And of course, then we remove this orange to point to that way we have red, orange, purple here, and this is an empty road. And finally, we move the red 12 by three. Then we remove this orange from pole to pole one. And that way we have the three disks on different posts and now we can replace them on the third row, as you can see here. So red, purple, and orange. So as you can notice here, the first thing we are going to do is to take all of the tests except the final desk and place them on road to and to do that, we perform some operations here. And now that we have all of the desk here in row two, we only have the last disk left, so will simply move it from this from node 13, and we'll simply redo everything and passed through every step as we did before for this stack. So we have two desk left and we are going to do the same thing with this two disc. Now suppose we don't have three desks. Let suppose we have sexless. And in this case, the same algorithm and the same concept will apply. We are going to take all of the desk, placed them in CO two, and leave the last desk here. And then after that we're going to move this disk from a road one to row three and perform the same operation on the left. So how do we do that using recursion in Python? Let us go ahead and create our function. And let's name it Tower of Hanoi, Tower of annoyed. And it will take the number of desks. And for example, let's suppose that we have these three rods, AB and C. So the first one will be called start. The second one will be extra. Or let me just type and, and the third one will be the extra wrote. And let us create our base condition or the base case. That is, if the number of this is equal to one, then we'll go ahead and simply move disc one from our source to our destination. And simply do that by printing. Move disc one from the source, that is the start here, and start to destination. And this destination as the. And. So let's see what are we doing here? So the key to solving this problem, I recursively is to recognize that it can be broken down into a collection of smaller sub blocks have problems. So as we said here, we have our roads, a, B, and C, and we are going to denote the desk starting with one, the smallest and the largest. So here we have 123. Now what we're going to do is to move em. So all of the desks minus1 from the source, the destination volatility extra. So we're going to move all of them minus P Last one to the extra wrote. As we can see here, we removed all of the disks minus the last disk to Paul to. And then after doing that, we are going to move this desk, the last disk in this case, to both three. Now, let's suppose we have more than three disks in this case, let's suppose I have five desks and I want to move them all from 0123. And I just got to a point where I have my four desks here and the last paul One. So I moved it from one tuple three. However, I should repeat the same exact procedure for the four steps. Now. Our 444 deaths and our four desk now are at port two. So we are going to use this as the starting position and Baldwin as the extra and poetry as the end. So we are going to move the last disk from B2 to poll three. And you're going to do the same thing until we finish from all of the desk desks. Now let me go back to our code. So our base case is to have only one disk. So we simply move it from the source startled and now, and we'll simply return. So now we've finished from our function f. This is not the case. We are going to simply use this tower of hanoi, Call it one more time. Now, this term we are going to move the n minus1 desks. And we are going to start with the start and end with the extra. And the extra one in this case will be r. And so what, what we're doing here is that we are starting with our and minus1 desks. So here we have, for example, today, we are going to move them from pole to pole to using poultry. So our starting point as Paul one. So this is the start, our ending point is equal to and this is the extra. And finally, our, we're going to use poetry to help us move this disc. So as you can see here, we used it to store the orange disk. Now let's go back. And after using this tower of Android, I will simply print that move disk and we call it n. And we're going to move it from source. And in this case the source is the same as before, so it is start to destination and our destination is as usual. And now after moving this disk from the source to the end, we are going to move the n minus one. So I'll call it this method one more time. And minus1 starting with start, I'm sorry, with this extra. And we have the destination that is the end. And using that as our extra hour, we use it to help us move them from extra two. And so what did we do here? We first move n minus one disks from the first one to the extra one. Then we moved the last desk from the first wrote to the Android. Then after that, we moved all of the desk from the extra to the end. So if you want to see it here, we just moved all of the desk from position one to position two. And then we move this disk from one to three. After that, we moved all of these from two to three. And we got this. Now, the same procedure applied to every time we call Tower of Hanoi. So here we're not just moving them from two to three. We are calling this method tower of an hour. And this tower of hanoi function will simply do the same procedure again and again until reaching the results we have here. So now if I create my function here, I will simply uses so I have four disks, and in this case, tower of Hanoi. And I'll simply add this and I'll call them a, B, C. And let's run. Save this. I'm sorry, here we need to add three. So let's suppose I have three desks. And just to visualize, and we got move disc one from source a to B. So let me just open this here. And let us see, first of all, we move the disk one that is the orange from a to B. I'm sorry, I just change these letters from a to ACB just to visualize it better. And we got move disc one from source a to destination C. In this case, we moved one from Paul 123, as you can see here. Then we moved the disc two from source a to B. As you can see, we remove this and place it and B. Then after that, we moved the desk one from source to destination B. So move this orange from here to B. And we got this result. And finally, we moved the desk three from a to C. So we got this and we are now at this step. And now we just perform the same operations again. So we move the source B, the desk one, so the orange from B to a. And then we moved B, f, d disc two from B to C as, as we did here. And finally we just moved back the disc one from a to c again. And now we got our result here. Now, if we have Fordist and let's run this one more time, we are going to get much more bigger result. Now, let's suppose I have 20 desks. And in this case, if I run this code, I'm going to get so many lines. And as you can see, they will keep just appearing. Since 20 is too much, you can only imagine. And with three does it took us 123456. So it took us six steps to just move three disks from 1 to another. And actually it is two to the power of three minus one. So to the power of three, it is eight minus one, so we have seven positions. So you want to 34567. So just imagine to the power of 20 minus1 how big this number is. And these lines, we're not finished until, I don't know, maybe ten minutes. And this isn't for the town Tower of Hanoi. It is a little bit complicated. However, when you know the idea behind it and why we're doing each and every step here. You come to just see it as a simple algorithm. And in this case, look at how a small desk definition as, and just imagine what we're going to write if we're not using recursion. And this is it for this video. See you in the next one.
43. Project: Now let's move on to our project. And in this project we are going to build a calculator. However, we already built one and our previous examples. Yet, our previous calculator was just to choose between additions, multiplication, division, and subtraction. And we used to write add or multiply or divide and so on. However, this calculator here, as you can see, we have our addition, subtraction, multiplication division. However, we can simply, let's run this code and you see it for ourselves. And if I run this code, I'm going to get the four operations, addition, subtraction, multiplication, and division. However, using this updated calculator, I can, I design of my addition, for example, one plus 35. Press enter, I get four. Now, if I want to continue and simply say no. And the program will ask me again, if I want the classroom, for example, then minus five, I'll get five. Now let's suppose I want multiplication. So six times eight. In this case I'll get 48. And lastly, let's try the division. So 50 divided by six, I'm going to get 8.3 and so on. And now if I IPS and I want to add to this program, I simply type yes, and I'll be done. Now. What did we do here? We just created our four here. Functions. Add, subtract, multiply, divide. So these functions are available for you. You know how to create them since we learned how to use our previous examples, however, what you should do is to create a while loop to perform as many operations as the user once. And in this case, you'll keep using this desk calculator until the user types yes, that he wants to exit this program. And this is basically just one more thing is that you are going to take the input as a string. So for example, here we are taking six times eight, then you are going to check for this side. If it is available in the string, then you can know that this is a multiplication. And if this sign doesn't exist, for example, if we have a minus sign, as in this case, ten minus five, then we can know from this sign that this is a subtraction. And we can convert these then and five integers and then subtract them using the subtract method we created here. So this is the general idea about this project. It is not very hard. However, you should be familiar with the concepts and functions and methods we presented earlier in this class. And this is it basically for the project and for this class. I hope it was beneficial. And finally, don't forget to add your project into the project section so that I can correct it. And don't forget to leave a review if you feel like it. And with that being said, this is the end of the whole class. I hope you enjoyed it and see you in future classes.