To fully equip ourselves on our Python odyssey we will take a leap into Operators and their Precedence. Python is undoubtedly a Homeric programming language, epic in its possible applications and vast in its potentials. Operators within this language are tools to implement operations on variables and data values.

Transcript

Hey gang! I’m Tim from Core Electronics and welcome to our next step on our Python excursion today we will be hurtling into operators within python and precedence too.

Operators are used to perform operations on variables and values depending on what they are, they can be a method of comparison, give tools for calculations or even assign values to identifiers, furthermore they can be applied to all types of data such as binary, decimal and hexadecimal values. Now order is very important when it comes to calculations and without consistent precedence, predictable reliable results would be impossible Python has a fondness to perform certain calculations before others when they are written on a single line of code, this precedence will be talked about at the end of the video.

If a line of code is beneath another line of code the above code will always execute first, jumping to the computer here is the online tutorial for operators in Python which you can find on our Core Electronics website, scrolling down you can see tabulated every single operator in tables based on their type, furthermore these tables also show the special symbol allocated by Python to each of these operators. Also, a description of the operator, a code example and when applicable equivalent codes, the result of these code examples are also presented.

Let's get that complete understanding of operators within Python, starting with comparison operators. Comparison operators are used to compare two values these operators give you tools to determine the difference and the similarities between data types, leaping into the code on the screen I’ll run through each of them and a simple example to start I set up simple variables A equals 10 and B equals 5. The first operator we're going to look at is this double equal sign, this is an equals so if the two values outside the operator are equal then the condition becomes true, all of these operators will return Boolean results true or false so number one when this code is run through it will print false because A does not equal B, the next one A exclamation mark equals B so this is not equals so because A does not equal B this will print true, the third one, this is A greater than B because A is greater than B this will result true next one is A less than B because this is not true it will respond false, print that to the idle shell screen, print A greater or equal to B because A is greater or equal to B that will result in true and print A less than or equal to B that will result in false so if we run this up here you'll see all of these results come back.

Next are Arithmetic operators for decimal data, Arithmetic operators are used with numeric values to perform common mathematical operations hopping into the screen, I'll run through each of them and give simple examples so here you can see we set up the same A equals ten B equals five variables right now the operator we're looking at is plus so print A plus B that result in 15, second one is subtraction so print A take B that will result in 5, print A times B so that will result in 50 print A divided by B that will result in 2.

So, some of the more complicated mathematical operations are as follows, so this is modulus. This symbol represents modulus, this percentage sign so modulus divides the left side of the operator by the right side of the operator and returns the remainder value, so in this particular case because the remainder value of 10 divided by 5 is 0 it will result in 0, up to number 6 so this double star symbol represents the exponential calculation, so this will perform an exponential calculation on the left side variable based on the value of the right hand side value variable so this will be A exponential B which in fact is 10 exponential 5 and finally is floor division and floor division returns the answer or the result of the division in which the digits after the decimal point had been removed so in this case it would be 10 divided by 5 which results in 2.0, it will remove that 0.0 so we'll run this right , and the results come back as you'd expect.

Now Assignment operators, Assignment operators are used for assigning values to variables whilst also performing common mathematical operations, these are shorthand versions but because of how common it is to retain the same variable identifier and also perform mathematical operations on it Python deemed it valuable enough to have a bunch of operators set up to facilitate just this, so let's jump into the code on the screen so I can run through each of them and explain to you what's going on.

We set up the variables just as before A equals 10, B equals 5. Now looking down here we can see A plus equals B so this is exactly the same as typing A equals A plus B but it's being shorthand version. Print A, so this will result in 15. Now because A is being altered for each of these separate operators, I've reset A to equal 10 that's just so it's consistent with the beginning so over here we have A equals minus so over here we have A take equals B so this take equals is effectively the same as A equals A take B which equals 5 and then that result A will then become 5 so that will print 5 to the Python idle shell so you can see the pattern occurs over and over and over so we have A times equals B which is the same as A equals A times B, we've got a divided equals B so this is exactly the same as A equals A divided by B, we have A equals A modulus B which can be represented as A modulus equals B we have the exponential so this is the same as A equals A exponential B, the floor division which is a equals A floor division B which can be represented as A double forward slash equals B, so if we print this you'll see that the results are exactly the same as before I’m running the code you can see it right here.

Operators can be keywords too, we talked about these next keyword operators in a previous video but perhaps you didn't realize that they were operators as a quick refresher a keyword is a combination of letters usually forming a word that has a special meaning, this special meaning can be a command or parameters which will be executed as soon as the computer device encounters the keyword when the code is run.

So vaulting into the computer screen let me run through these keyword operators starting with Logical operators, these are used for conditional statements and return results of either true or false, the three Logical operators in Python are the keywords “here”, “or” and “and not” so like before we set up A and B, the two variables but we've also set up a couple more variables we set up C and D which both equal 4 and we set up a list variable E, with the values 4, 5 and 4 making up the elements inside the list, so here are the Logical operators starting with 4 so we're going to print to the idle screen, true or false depending on whether A greater than B which in this case it is or C less than D which isn't true but because A is greater than B it will print true. The “and” operator A greater than B that's true C less than D that's false because both of these need to be true, this will result in false printed to the idle shell, print not A greater than B now not is effectively the inverse of whatever the Boolean response is so A greater than B is true however because we've used the Not operator it will reverse this and display false so let's run this right now just so you can see true for the first, false for the second and false again for this one.

Now Identity operators, these are used as a test to compare objects to see if they are actually the same object with the same memory location, these operators compare the class the type and the data values of variables to determine this the two Identity operators are the keywords “is” and “is not” which you can see being used in the statements on the screen so we got print C is D in this case because C is the exact same value as D, this will result in true, which you can see down here print C is not D because C has the exact same value as D, this will result in false.

Now finally Membership operators these are used as a test to see whether a value or a sequence of values is present inside an object the two membership operators are “in” and “not in” which you can see being used in the screen over here so print C in E, so what we're doing is the variable C is being looked for inside the elements of E, now because 4 is represented twice, and C is 4 when I type C in E that will respond in true which you can see down here, now print A not in E because A is value 10 and that never appears in the list E that responds in true, because the value of A does not ever appear inside E.

Now worth noting, Bitwise operators and Bitwise Assignment operators exist and are a very worthwhile conversation, these are different operators for use with binary numbers, a binary number is a number expressed in the base two numeral system. Ergo, there is lots of ones and zeros however for a beginner these operators are unlikely to be encountered or utilized. They are worth knowing about nevertheless and the information exists for the curious on our Core Electronics page even the little bit of knowledge these particular symbols represent bitwise operations put you in safe waters if you ever encounter them on your coding meanders.

Now order a procedure is very important when it comes to calculations, predictable and reliable results would be impossible without consistent precedence Python has a fondness to perform certain calculations before others when they are written on a single line of code, so flying into the computer you can see a table listing all the operators within Python ignoring the Assignment operators they are ordered with a descending value of precedence this means operators higher up in the table will run before those operators lower down in the table, as a higher operator has a higher precedence if all the operators on a single line of code have the same precedence value the flow of operations will be from left to right.

Also, I’ll introduce the final operator within Python which is the Parentheses operator which uses normal bracket symbols. This is the ultimate method of ordering operations as anything inside the Parentheses will be the first to run in a line of code if multiple levels of Parentheses are used, the operation inside the most Parentheses will be run first, now worth noting if a line of code is beneath another line of code the above code will always be run before it.

Leaping into the computer I’ll demonstrate a script all about precedence so setting up similar values that we've seen before A equals 10 and B equals 5 we're now going to create a new variable C and what will occur first is this. This will occur first because it's inside parentheses then the A minus that value will occur next this exponential will occur after that, then this times will occur to all of this value and then the plus then all of this value and then the minus, so if I’ve run this right now you'll see a very interesting result, but that is what has occurred to produce the variable C and the order that it's occurred if I was to delete this comment and turn it into the function delete “del A” because this function is occurring before this line, this will happen before any of this occurs now delete A, this function effectively deletes this variable so if I was to run this code now we would end up with a syntax error which I’ll demonstrate so precedence matters when it's a single line of code but when it's multiple lines of code, the above code will always run first.

That's it, this tutorial has illuminated every operator type, summarized their purposes and demonstrated to you how they can be useful in your coding ventures I hope you feel ever more bolstered in this Python programming language feel free to use the online guide as a future reference point and as always if you have any questions queries or things to add please let me know your thoughts, until next time stay cosy!

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.