hey guys welcome to the next blog in Python tutorial for beginners in this blog I'm going to show you how to use mathematical operators and how to do basic math in up so first of all we will open the idle IDE so just search for Python once again I have shown you this method in the last blog also so just search for the Python once again and in the last blog I have shown you how to install a Python and with the Python installation this ideal is also installed right so we are going to open this idle IDE now some of you who are familiar with Python and have some experience with Python may think that why am I using this Python idle IDE because there are many better IDE s and editors available for the development of Python so don't worry I'm going to show you everything how to install other IDE s which are preferred in the development of Python step by step but for now we will start with this idle IDE so the first thing which I want to do here is I want to increase the font size first of all in the idle IDE because this may be not visible to some of you you can change the font size by just changing this number and I'm going to change it to for example 18 and bold so we will be able to see our fonts in a better way so now everything is set so let's get started with the programming so let's start with the basic math operations so let's say I want to add something so I can just write number plus which is operator and the other number and then press ENTER and you will get the result of this expression now let's try the subtraction so let's say I want to subtract 60 minus 50 and then press ENTER and it gives the result let's do the negative subtraction so I'm going to just write 60 plus 50 and then press ENTER and it's going to return me the negative value now let's do the division so for example I want to divide 50 by 3 I'm going to press ENTER and you can see it gives me a float value or a decimal value and if you are familiar with other languages like Java or C++ this may appear a little bit strange to you because when you do the same operation in Java or C++ with this division operator you will get the different answer which is 16 and not these decimal float value so if you want to just get an integer value and not the decimal value out of the division then you can write this expression which is 50 double forward slash 3 okay and then press Enter and now you get 16 which is a whole number or an integer this type of division is also called the flow division in which you get the whole number as the answer and the decimal value is truncated out of your answer so if you want to get the exact value out of the division in decimal values also then you can use this expression which is single forward slash operator and if you want to do the flow division then use these two forward slash symbols now it's also possible to divide two decimal values so decimal values in Python and most of the other languages are called the float values so here instead of 50 I can just write 50 point 0 divided by 3.0 which is also valid or I can write 50 point 0 divided by 10 and then press Enter which gives me 5.0 and when I write the integer which is 50 divided by 10 it will also give me this value which is 5.0 now let's try some multiplication so if you want to do multiplication in Python you use a number and then this asterisk symbol and then second number and then Center and this is going to multiply these two numbers so this is the multiplication operator which we can use to multiply two numbers now this is what we are doing here is for example multiplying two numbers or dividing two numbers or addition of two numbers or subtraction of two numbers in Python it's called expression okay so 50 multiplied by 10 is an expression and I have already told you that these symbols for multiplication or division are called operators now the number on which you are doing these mathematical operations are called operands so 50 here is an operand then here is an operand or 3 here is operand and 50 here is operand now here 50 and 10 are also called literals because they are constant values so what we have learned till now this is an expression this is an expression or 50 divided by 3 is an expression the number on which you operate on are called operands and these symbols which you can use to multiply divide add or subtract are called operators and these numbers are also called literals when they are constants now let's try some modulo operations so for example I'm going to just write 10 modulus 3 here what it's going to give us is the remainder of the division of these two values so 3 multiplied by 3 is 9 which leaves the remainder 1 and that's the result we are getting ok let's try it once again so for example I will divide 21 divided by 5 it will give me the answer 4.2 but when I do 21 a modulo 5 and then press Enter it's going to give me the remainder of the division of two values now there is an another interesting operator which is called Expo and operator which you can use for the exponent values so for example I want to get the result of 2 to the power 5 how can I do it so I can just write 2 and then SS tricks and then 5 which means 2 to the power 5 and then when I press ENTER it's going to give me 32 or I can just write 10 and then I can just write SS tricks and then for example 10 once again and then press Enter it's going to give me the result of 10 to the power 10 now it's also possible to do these mathematical operations on more than one number so for example I can write 5 plus 5 plus 6 plus 9 and it will give me the result of the addition of these four numbers and it's also possible to use different type of operator in one expression so I can write 5 plus 9 minus 6 multiplied by 10 for example and it's going to give me the result of this expression now there is one question which may arise here is for example are once again I'm going to do 5 plus 9 minus 6 x 10 and divided by 20 and press ENTER here it's going to give me this value but how this value is calculated what operation is executed first is the multiplication is done first or is the division done first or the addition took place first or the subtraction took place first so I have this table and here you can see the precedence of these operators so in this table highest precedence is at top and lowest is at bottom that means parentheses have the highest priority and then the exponents have the priority and then the multiplication and division operators have the same priority here and at last the subtraction and addition have the same price dirty but whenever you use these operators which are in the same box in a same expression then whatever operator you use on the left have the highest priority and whatever operator you use on the right have the lower priority so in this table parentheses have the highest priority and the addition and the subtraction have the lowest priority and for example if you use multiplication and division in the same expression then the priority is decided which operator is appearing on the left so if the operator is appearing on the left it has the highest priority then the operator which are on the right side so let's take an example of that so for example so first the addition of these two number will happen and then the division of these two numbers will happen because parentheses have the highest priority and then the result of these two numbers will be multiplied by six because multiplication have the highest priority out of these two operators which are minus and multiply so multiplication operator have the highest priority and at the end the subtraction of the result will take place so let's press ENTER and let's see what's the result so the result here is 11 and how we get this result here so 5 plus 9 is 14 and the division of these two number is 1 by 2 so 6 will be divided by 2 which is 3 and then at last 3 will be subtracted from this result which is 14 which will give you 11 once again for example you use 60 multiplied by 20 divided by 30 and then press Enter it will give you 40 but in which order this calculation will happen first of all this multiplication will take place because in the table we have seen that multiplication operator and division operator have the same priority and that's why the priority will be decided from left to right now if you want to learn the order of precedence between the operators then you can use this keyword which is PE m da s P stands for parentheses Easton's for exponents M stands for multiplication d stands for division instance for addition and s stands for subtraction okay so you can either remember this name which is M Dass or you can use this sentence which I have written here so please excuse my dear Aunt Sally and you need to remember the first letter of each word here the first letter of every word for example P here stands for parentheses eise here stands for exponent M and D here stands for multiplication and division a and s here stands for addition and subtraction so for remembering you can either use the sentence or you can remember this keyword so that's it for this blog I hope you have learned something new in this blog and please keep reading these blogs and see you in the next blog
Post a Comment