The document discusses various Python operators like arithmetic, logical, comparison, bitwise and assignment operators. It provides examples of using operators like addition, subtraction, multiplication, division, modulo, floor division, exponent and logical operators and and or. The identity and type operators are also covered with examples.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
19 views3 pages
2.7 - Operators - mp4
The document discusses various Python operators like arithmetic, logical, comparison, bitwise and assignment operators. It provides examples of using operators like addition, subtraction, multiplication, division, modulo, floor division, exponent and logical operators and and or. The identity and type operators are also covered with examples.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3
Now let's look at some basic operators. In python.
Operators basically help us do basic
arithmetic and logical operations. We look at a bunch of operators and let's say if I say two plus three, this is called an operator, and the operator is operating on two and three, right? So these are called operands. These are called operands, right. This is called an operator letter, and both two and three are called operands, just to get the terminology here, right. Okay. Having said that, let's go and look at a bunch of examples. Right? So we have all these arithmetic operators. We'll go and see what each of them are. I would prefer to execute some of this just to show you. This is assigning ten to X and 20 to Y. And I can just say this is simple addition, as you would expect. I can of course do subtraction, very simple, straightforward. Again, of course, ten -20 is minus ten. As you can see, the output here. Let me just highlight the output for you. This is where the output is. Just FYI. Next, I can of course do simple multiplication. Ding dong ding. Straightforward. And of course division. Of course, ten by 20 is half. Here is an interesting operation. I'll just not use the same things here. Suppose if I have 15 percentile two, this is your percentage symbol. So what it does is. So for those of you who do not know what this percentage operator is, it's also called a modulo operator. What it does is it divides 15 by two. So two sevens of 14, it gives you the reminder that you get, which is one in this case, and that's what is the output of 15 modulo two. It's nothing. But what is the reminder that you get when you divide 15 by two? Those of you who have some familiar with any programming language would immediately recognize this. Okay, the next thing is a floor operator. For example, if I just say double, if I do double division symbol instead of using a single division symbol. Okay, let's do 15 by 215 by two is 7.5. So what is floor? Floor basically is if I have any real value. So 15 by two is 7.5, floor of 7.5. Mathematically, it's often written as this. Floor of 7.5 is basically the integer which is closest to 7.5 and less than 7.5. So on the number scale, if this is your 7.5 and this is your eight and this is your seven, the closest integer to 7.5 is seven and it should be less than 7.5. So you get seven. That's what you got here as an output. Right? This is what floor division means. Now the next interesting question is what happens to floor division if I do - 15 floor division? Two. Right? So this is interesting, this is a fun hack, because what is -15 by two? -15 by two is -7.5 on my number line. Here is my minus seven point minus, of course, here is my zero. Here is my minus seven. Here is my minus eight. Right. In floor operation, what should happen is you should find a number. An integer could be negative integer, which is closest to my number, but still less than this. That's why you get minus eight and not minus seven. This is important when you're doing a floor operation. So the floor of -7.5 is not minus seven, it's minus eight. Lot of people get this wrong. So the other interesting operation is called exponent. So what is exponent? Exponent is basically x power y. So I can represent it with the two stars. So two power four. Any guesses? 16. Right. So two power 36, 34. This will be harder, of course. I couldn't have guessed it. Okay, so what it's doing is it's basically computing x. So if this is x and this is y, it's doing x power y. So if you have x star star y, this is nothing but x power y. Right? So we also have the exponent function, sorry, exponent operator in Python very nicely and useful for us again. All these mathematical operators will come in extremely handy for us again. Then we have all these basic logical operators and what the output is basically boolean. They give us boolean outputs. For example, if I just run this snippet of code here, of course A is ten, b is 20, and so is print a less than b. Of course A is less than B, so it prints true here. Right? This is your greater than operation. This is your less than operator, this is your double equal to is a equal to B. This is is a not equal to b greater than, equal to and less than equal to. If you have any familiarity with basic mathematical comparison or any programming language, this is straightforward for most of you. Having said that, let's go to logical operators, which are fun, right? So you have your logical and. And logical, or so what is a logical and so let me just give you a quick table here. So, true and true is true, right? True and false is false, false and false is of course false and false and true is false. Right? Similarly, what about your r table? Your r table is, okay, true or true is true, true or false, same, false or true, both of them are true, only false and false is false. Okay, these are your simple logical operators that you would have learned in basic logic in your high school. And. Exactly, that's what it does. So this logical and is exactly like double ampersand in C. Your or is like your double line in C, not is exactly like that. Okay, so instead of using these symbols in python, you can just explicitly call them out with names. Next comes a very interesting topic called bitwise operators. These are a little tricky. Only computer science folks typically understand what these bitwise operators are, but they're very, very straightforward. Let's go and understand. So a equals to ten, b equals to four. So how is ten represented in binary representation? So a equals to ten. So what is 1010 is, sorry, ten is basically, you can write it as two power three plus two power one. Right. So this is eight and this is two. So that's ten. So this is two part zero, two power one, two part two, and two power three. Right. So zero here because there is no value here, one here, zero here, and one here. This is the boolean representation or the binary representation of ten. Right. Similarly, so this is what your ten is represented in binary. What about four? Four can be represented as 0100 because two part two is four. Right. So this is how you represent it. Now, when you say, and this symbol is a single ampersand, when you do it, it does a bitwise. And so ten and four is, now you do, okay, zero and zero is zero, one and zero is of course, zero, zero and one is zero. One and zero is zero. So what you should get here as output, you should get zero as output. And that's what you get. Sorry, that's what you get here. Right. Similarly, what about R? Let's just quickly see that. Okay, let's just quickly see R here. When you do r, you get 14. Why? Because again, simply ten or 40, or zero is zero, one or zero is 10, or one is one. One and zero is one. And this is 14 because two part zero, two power one, two part two, two part three. Eight plus four plus two, that's 14, right. Works out similarly. You can do all these operations like or not, not basically does the exact. So basically not of ten, not of ten would basically not of ten would just say, would just flip these into 0101, right? So these are very simple bitwise operators. Those of you who have studied computer science or have any familiarity with programming languages will quickly understand this. Next comes the interesting assignment. Operators, of course, we have seen equal to. So what does this do? It assigns the value of ten to a right. Now comes the other interesting operations like plus equals to ten. So those of you who have seen C would easily understand this. Okay, let me scroll up a little. Okay, so a plus or equals to is nothing but. So this is nothing but a equals to a plus ten. So minus equal to is nothing but a equals to a minus ten star. Equal to is nothing but a equals to a multiplied by ten, and so on and so forth. We have seen the floor division, the modulo operator, exponent operator, et cetera. It's just exactly the same thing. So whenever you have plus and equal to, basically a basic operator, and an equal to this will just unroll it into this format. This is a more concise representation of this statement, except that there is not much difference here. Okay? So those of you who have some familiarity with c would recognize this immediately. So a plus or equal to ten should be equal to 20, because a already has a value of ten here, ten plus ten plus ten will be 20, right. So again, very basic stuff here. Now here comes an interesting thing called identity operators in Python. So there are two such operators. One of them is called ease, and other is called ease, not right. So let's say I assign a value of five to a. Similarly, I assign a value of five to B. Now, if I say print a is B, surprisingly, you'll get it true, because what's happening internally is. I think we discussed this in one of our previous videos. There is a memory location where five is stored and a points to it, and B also points to the same object. Right? So when you say, is it true that a is b? Yes, of course it's true, and hence it prints it, right? Now, the same thing is not true for lists. So here, if you look at it, I have a list, l one, which is one two, three. I also have a list, one two three, you might say. Okay, probably this list, one two three is being created in a memory location, and both l one and l two are pointing to it. No, this is not true. It's only true for variables like this, for numbers, for simple variables, not for complex variables like your lists and dictionaries and tuples, more complex data structures. It's not true. Okay, so this returns false for you. What about strings? Even for strings, this is not the same. Even though, even though it's the same string, I have s one and s two, you might assume that AAIC is a string being created in memory and s one is pointing to it and s two is pointing to it. And that's true. That's exactly how strings work, right? Because here, let's just go here. So I told you, right? Only for complex data types. So if I say, if I run this, I get true. I could say ease not, then you get false, right? So even for strings, everything works just like your numbers there, your numbers up here, okay, only for slightly more complex data types like list, tuples, et cetera, it doesn't work. There is another very interesting operators in python, called in and not in, which are called membership operators. Let's look at it. Suppose if I have a list LST with four numbers, 1234, I could say one in list. If one is there in this list, it would return me. True, that's what it does. Very, very useful, right? Because now you can say whether an element is there in a list or not. You can do this for strings, for lists, for tuples, dictionaries, sets, et cetera. So let's do another example, right? Let's say I have a dictionary here. This is a dictionary. This is my key one, this is my value one. This is my key two. This is my value two. So I can search in the keys very easily. If I say is print one and d, since one is present in the keys, it would print true. Let's go to this code snippet and see what happens. If I say is a and d, right, it says false. I can only check in the keys, not in the values, right? Can I say two indie? Of course it's there. What about three? Indeed, of course three is not there should be written as false. So in dictionaries, the in operator is only used to check if a given value is in the keys. In the set of keys here, this is k one value one, k two value two. It only works in the keys and not in values, right? That's a membership operator.