Subtitles section Play video Print subtitles If you use numbers in your python code, then you'll need to learn the rules of arithmetic. How to add, subtract, multiply and divide. Arithmetic may seem trivial, but there are some tricky details when combining numbers of different types or when dividing in. This video will cover arithmetic and python version, too. If you're using version three, then you'll want to watch our video for that version. Let's go inversion, too. There are four types of numbers in Long's floats and complex numbers and an arithmetic. There are four operations addition, subtraction, multiplication and division. To use arithmetic in python, you first need to understand the idea of narrower and wider types. Any end can be converted to a long by adding an L at the end. Another way to convert it into a long is to pass it to the long constructor. But some longs air too big to be converted into ins. For example. On my system, 10 billion is too big to be an end. Python treats it as a long see how it puts in L. A. The end. So we say that in our in our type lungs, you can also say that longs are a wider type than ends. Similarly, any long value can also be represented by a float, but not the other way around to convert along toe afloat, just at a 0.0 at the end. Another way to do this is to pass a whole number two the float constructor. But not all floats can be converted to longs. For example, the number 3.14 is a decimal. It's not a whole number if you try to change it to along. The number is first rounded and then made into a long. It's no longer the same number, so long as they're narrower than floats. Floats are wider than lungs. Lastly, any float could be made into a complex number, but not vice versa. To convert afloat to a complex number just at zero J to the number or past the number to the complex constructor. If you try to convert a complex number two afloat, you get a type error, so floats are narrower than complex numbers. We can also say that complex numbers are wider than floats. Let's recap ins are narrower than longs. Longs are narrower than floats, and floats are narrower than complex numbers going in reverse. Complex numbers are wider than floats. Floats are wider than longs and longs are wider than its. This is similar to how in mathematics integers are a subset of the real numbers, and the reels are subset of the complex numbers in Python Insane Long's both represent integers. Let's now explore the four arithmetic operations in Python. We'll start by creating four numbers, one for each. Type A is an end, B is along, See is afloat and D is a complex number. The rule of thumb is when combining two numbers of different types. Python will convert the narrower type to the wider type, then perform the operation audition. If we add a and B, we're adding an end and along Longs are wider than ins, so python converts a toe along than ads. The result is five as a long and not five as an end subtraction. If we compute C minus B, we're subtracting along from afloat Long's air narrower than floats, so python widens, be toe afloat, then subtracts. The result is 3.0, which is afloat. Multiplication. If we multiply A and C were multiplying an end on a float floats are wider than its, so a is changed to afloat, and then the numbers are multiplied. The result is 12.0 afloat division. Next divide D by sea floats are now worth an complex numbers, so see is wide into a complex number. Before the division, we get two plus zero J. While this is the same as the intruder to Python stores it as a complex number you can tell because it has an imaginary part. Don't forget that Python uses J as the square root of negative one, not I. In addition to knowing what happens when combining numbers of different types, you also need to fully understand division. If you were dividing two whole numbers in python, get ready. The answer may not be what you would expect. Look what happens if you divide 16 by five. The mathematical answer is 3.2, but the Python answer is three. This is because when dividing with two whole numbers, python returns the quotient, not the actual value. Recall that when you divide one number by another using long division, you get two numbers. A quotient on a remainder dividing whole numbers returns the quotient. If you want the remainder, you use the percent operator. If you do want the real number value and not just the quotient, you can convert either the numerator or denominator to afloat. Don't forget the big exception in arithmetic. You cannot divide by zero. In math. It is undefined, and in python it throws a zero division error. Be careful when dividing two numbers. Unless you were certain the denominator is not zero. You'll have to be prepared for this possibility when combining two numbers. Using addition, subtraction, multiplication or division python will wide in any numbers to make sure all values are of the same type. On dividing two integers. Python returns the quotient. And don't forget what happens when you divide by zero.
B2 longs complex arithmetic wider dividing division Arithmetic in Python V2 || Python Tutorial || Learn Python Programming 25 0 林宜悉 posted on 2020/03/09 More Share Save Report Video vocabulary