Here I make a calculator program, however, it's just the divide button where the obelus sign isn't showing up, can someone tell me what's wrong with it? All the other buttons work because I simply use "text = "+"" for example.
bttn_div = Button(calc, text = chr(246)) bttn_div.grid(row = 1, column = 3, pady = 5)Btw, here's an example of a working one, for reference.
add = Button(calc, text = "+") add.grid(row = 4, column = 3, pady = 5)Division Operators allow you to divide two numbers and return a quotient, i.e., the first number or number at the left is divided by the second number or number at the right and returns the quotient.
There are two types of division operators:
(i) Float division:
The quotient returns by this operator is always a float number, no matter if two numbers are integer. For example:
>>>5/5 1.0 >>>10/2 5.0 >>>-10/2 -5.0 >>>20.0/2 10.0(ii) Integer division( Floor division):
The quotient returned by this operator is dependent on the argument being passed. If any of the numbers is float, it returns output in float. It is also known as Floor division because, if any number is negative, then the output will be floored. For example:
>>>5//5 1 >>>3//2 1 >>>10//3 3Consider the below statements in Python.
Python3
print (5//2)
print (-5//2)
Output:
2 -3The first output is fine, but the second one may be surprised if we are coming Java/C++ world. In Python, the “//” operator works as a floor division for integer and float arguments. However, the division operator ‘/’ returns always a float value.
Note: The “//” operator is used to return the closest integer value which is less than or equal to a specified expression or value. So from the above code, 5//2 returns 2. You know that 5/2 is 2.5, and the closest integer which is less than or equal is 2[5//2].( it is inverse to the normal maths, in normal maths the value is 3).
Example
Python3
print (5.0/2)
print (-5.0/2)
The real floor division operator is “//”. It returns the floor value for both integer and floating-point arguments.
Python3
print (5//2)
print (-5//2)
print (5.0//2)
print (-5.0//2)
See this for example.
The division is a standard mathematical operation in any programming language, and Python is no exception. However, in Python 2, there is only one kind of division called integer division.
In general, the python definition of division(/) depended solely on the arguments. For example, in python 2.7, dividing 11/4 was 2 because both arguments were integers.
However, 20.0/7 will generate 2.857142857142857 as output because the arguments were floating-point numbers.
The above definition of ‘/’ often caused problems for applications where data types were used that the author hadn’t expected.
Python 3 has two kinds of division.
- Integer Division( // )
- Float Division( / )
The standard division symbol (/) operates differently in Python 3 and Python 2 when applied to integers. For example, when dividing an integer by another integer in Python 3, the division operation x / y represents a true division (uses __truediv__ method) and produces a floating-point result.
Meanwhile, the same operation in Python 2 represents a classic division that rounds the result down toward negative infinity (also known as taking the floor).
Python float division
Python 3 provides ‘/’ operator that does floating-point division for both int and float arguments.
print(42 / 2)Output
21.0You can see that the output is in the floating-point. This is because, during the time of Python 2, when you divided one integer by another integer, no matter what, the result would always be an integer.
Now, let’s see the following code.
print(41 / 2)Output
20.5Now, we have divided 2 with an odd number, so we got the floating-point value.
Python 2 division
In Python 2.2 or later, in the 2.x line, there is no difference for integers unless you perform a from __future__ import division, which causes Python 2.x to adopt the 3.x behavior.
To clarify for the Python 2.x line, / is neither floor division nor true division. The currently accepted answer is not clear on this. The / is floor division when both args are int, but is true division when either or both of the args are float.
from operator import truediv, floordiv print(truediv(10, 8) == 1.25) # equivalent to `/` in Python 3 print(floordiv(10, 8) == 1)Output
True TruePython integer division
Since Python doesn’t declare data types in advance, you never know when you want to use integers and when you want to use the float. Furthermore, since floats lose precision, it’s not advised to use them in integral calculations.
To solve this problem, future Python modules included a new type of division called integer division given by the floor division operator (//).
Now, / performs float division and // performs integer division.
In Python 3, you can perform integer division using (//) operator.
print(42 // 2)Output
21You can see that the returned value is an integer and not float.
Now, let’s divide odd value with 2 and see the output.
print(41 // 2)Output
20Here, you can see that it rounds off to 20. This is because float division rounds down to the nearest integer.
Python division with negative values
Some other programming languages use rounding toward zero (truncation) rather than rounding down toward negative infinity as Python does (i.e., in those languages -3 / 2 == -1). This behavior may create confusion when porting or comparing code.
print(-3 // 2)Output
-2If we try float division, then you will see the different results.
print(-3 / 2)Output
-1.5Conclusion
If you want a floating-point number in your division result, then you can use float division ( / ), or if you wish to integer-based division, then you can use ( // ) operator in Python. You have to take care of data type conversion in the long program to avoid any error or unexpected behavior.
See also
Python floor()
Python ceil()
Python sum()
Python square()
Python sqrt()