Operators in Python | Python Operators | Python Operators Tutorial | Intellipaat

Operators in Python | Python Operators | Python Operators Tutorial | Intellipaat


So in the last session under python token we covered till literal part Right. So in our today’s session we will be learning about our last Python token “operators” So what are these operators. well these are some specific character which have
a specific task to perform. And based on the function and the task the
operators are further segregated into Seven different types of operators. Automatic operator, assignment operator, companion
operator, logical operator, bitwise, identity and membership operator. Fine. So let me discuss them one by one starting
with arithmatic operator. With arithmatic operators are the one which
is used to perform some arithmetic calculation. So what it does, it takes two operand to perform
operation on them. For example two plus three. So here two and three are operand and the
character plus over here is nothing but an arithmatic operator. So there are other automatic operators too. Like plus, minus, multiplication, division. All these characters are a part of an arithmatic operator. Let me just show you one more example. Like one plus two. So you have 1 & 2 are the operand and plus here is the arithmatic operator. One minus two again minus is the arithmatic operator. 2 mod 1, mod is the arithmatic operator. Let’s move ahead. Next we have is the assignment operator. Well this assignment operator is used to assign
a value to a variable the character which comes under this operator are.=, +=, -=, *=, etc For example you declare a variable as var=10. So this=is your assignment operator
and is used to assign value to a variable. Let’s get back to our Jupiter notebook and
learn more about it. So. For example I’m defining a variable var=10. So this=is nothing but the assignment
operator. Var +=10 Then again an assignment operator. What does this mean. So what does this mean. Well it means that Var equal. var Plus. 10. So if you print the value of VAr What do you
think the output should be. Well you’ll get the output as 20 since the
value of var is already 10 so 10 plus 10 it’s 20. So the updated value of var is 20. Similarly you can perform VAr -=10 And again get your value printed. So again the updated value of VAr would be. 20 minus 10=10. So this +=or -=it is generally named as a shorthand. Which means add or subtract and assign value to self Same example as mentioned over here like a=10, a*=10 So it means that a=10*10. When
you print a, you get the output as a hundred. So next we have as comparison operator. Well it is used to compare two values and
it returns True or false as the output. the operators which are part of it are Less then, greater than, less than equal to,
greater than equal to, or not equal to. For example. Let’s see what example we have up here. a=10, b=20. a is greater than b. Obviously
10 is not greater than 20. Right. So it is giving me false as the output. Fine. So let’s move ahead. Next is the logical operator. Well these logical operator are used to perform
some logical calculation and the operators are the keywoads which are a part of it are and, or and not. Example we have , a=10 less than 10
and two is greater than minus one. Then print a. So you are getting output as false. Okay let’s jump back to our Jupiter notebook
and see why we are getting the output as false So you had a equal. 10 is less than 10. and 2 is greater than minus one. So if you see we have two comparison operator
up here. So where is the value of 10 less than 10. Since it is a comparison operator. So it will give me a false right? 10 is not less than 10. two is greater than minus one. this is true So you can summarize this as. False and true. All right. So since it’s false and true therefore it
equals to false Well how? well do you remember the binary
calculation that you had learned in your college. It’s like one dot one. Equals one or one dot zero equals zero or
one and one equals one or one and zero is zero So consider true as one and false as
zero So false and true is false. That’s why you got the result as false. Now if you print the value of a. You’ll get the output as false. So next we have is the bitwise operator This operator is used to perform the bitwise
calculation. This operator includes. And. Or. Left shift, right shift, not Alright. So let’s see their use one by one. Let’s see the example what we have up here. So we have seven or five. The output is seven. Seven and five the output is five. So how do you think we are getting this output. Let’s see the calculation. So how it is calculated. So if you convert seven into its binary form
it’s triple 1. And if you convert five into its binary form
it’s 1 0 1. Now if you’re performing or so basically you’re
performing a binary addition on it. And if you’re performing and you’re performing
a binary multiplication on it. All right. So let’s see. So if you’re performing a binary addition
on it. So one plus one is one. One plus zero is Again one. And again one plus one is one. So that’s why you’re getting the or of
seven and five is seven. Even you can verify this using the calculator. open a calculator It’s a programmar calculator. So seven or five. So you’ll get the output as seven. It’s got. Fine. Next. Operator that we have got up here is till day operator or not operator. Well this operator is used to performance a
not operation. So what is not of 7. What is not of 7. It’s minus eight right. How it is minus eight. Let me just show using a notepad. let’s open notepad So. 7 and it’s binary form with four bits, what is it as 0 1 1 1. If you perform a not of 7 i.e. Not of. 7. What you will get All the bits would be switched like 0 would
be converted to 1 again 1 2 0 again 1 2 0 So you’ll get 1000 and automatically and negative
sign would be added at the beginning. So you’ll get minus 1000 and buying reform
which you convert into decimal you’ll get it as minus of it. That’s why. The output of nought of seven is minus eight. Next as the left shift and each shift operator. Let’s see what is the output first. For example. I’m playing for the. Right shift. And then I shift to what is the output. Let’s see how we got the output as to. Open our notepad. Let’s move this. Certain and it’s binary from what it is it’s
1 0 1 0. Well when you’re performing the right shift
you’re telling the interpreter that you want to shift your books to a diet. And how many books you want the shift is basically
two since you have mentioned 10 and I sure do. All right. So 1 0 1 0 shifted two bits to the right 0
0 and move. 1 0 from the end. So this is how you are shifting two words
towards the light. So you’ve got the desert as 0 0 1 0. Knife you convert this into decimal form you’ll
get the output as. To. Fine. So it’s like you’re trying to push to bits
from the left towards the right as a team. Next we have is. Then left shift 2. Or does the output it’s 40. Let’s see how we got that output again 10
in its binary form is. 1 0 1 0. So when you’re performing a left shift it’s
like pushing the banks from right towards the left. So then left shift 2 is like 1 0 1 0 and you’re
adding 2 because in the end. Since you are shifting two bits to the left. So you’re getting 1 0 1 triple 0. So let’s convert this into its decimal form. So 1 0 1 triple 0 and it’s decimal form is
what 40. So that is why you are getting that as early
as 40. So there is a shortcut trick. Well the shocker trick is Doc just started
zeros equal to the number of books that you want to shift towards the left. For example I want to perform ten left shift
three so my output should be. 1 0 1 triple 0 and 1 Mozart. I like since I’m moving three zeros. I like since I am moving three zeros towards
the left. So lets converted. So it’s a B. So you’re getting the output
as a B. Let’s verify you’re taking. Value if you perform n. Left Shift 3 you get the output as AP fine. Well I hope the big operators clear duvets. But in case your slightest doubt please add
your doubt to the comments section below and we’ll try to reply them at the earliest. For now we can just move ahead to the. Identity operator. Will these operators are used to test if two
Opens shares an identity. The operators which are a part of it is and
is not. These are basically two keywords like. What we have appeal. X equal then existent it’s true X equals then
X is not then it’s false. It is just used to best of the orbit and an
identity or not. The next and the final operator in Python
as the membership operator. This hot potato as used to test whether the
value is a member of a sequence or not. The sequence may be a list. It can be a string or it can be a pupil. I know some of you guys might be thinking
What is this list or does the stupid. Guys. I’d say that. Just be patient. I’ll teach you about them in detail during
this session. For now you can just understand that this
list is almost same as adding that she had already started in C C++ or Java. It’s almost same as that. And this membership operator it is used when
you want to check if a certain element is present in a list or not. So let’s see what other operators which are
a part of it. So well we have in and not in keyboards which
are a part of this membership arboretum. Let’s see an example. So lets the example appeal. So we have defined a list or you can see we
define an addy which consist of dog got involve. So we are checking for line and pets sends
back to is the name of our list. Ordinary you can see so we are searching for
line impact. So since there is no line in it. So the result is false. Next what you are doing was seeing for Bluefin
pets. So you will first present and pet. So we are getting our as true. So this was all for this session. In our next session we learn about various
data dives which we use in Python. Thank you.

One thought to “Operators in Python | Python Operators | Python Operators Tutorial | Intellipaat”

  1. Got a question on Python? Do write it in the comment and you will get a response immediately by our expert. For Python training & certification, call us at US: 1800-216-8930 (Toll Free) or India: +917022374614. You can also write us at [email protected]

Leave a Reply

Your email address will not be published. Required fields are marked *