Python (Scripting) - Division (/)

by
Jeremy Canfield |
Updated: April 15 2024
| Python (Scripting) articles
Here is the most basic example of how to divide two integers in Python.
#!/usr/bin/python
print(10 / 5)
It is much more common to store the result in a variable.
#!/usr/bin/python
result = (10 + 5)
print(result)
Be aware that if you are using Python version 2, a whole number should be returned, like this.
~]# python example.py
2
Whereas Python 3 should return a decimal number.
~]# python3 example.py
2.0
You can divide decimal numbers.
#!/usr/bin/python
result = (10.5 / 2.4)
print(result)
Which should return the following.
4.375
round can be used to round to whole numbers.
#!/usr/bin/python3
result = round(10.5 / 2.4)
print(result)
Which should return the following.
4
Did you find this article helpful?
If so, consider buying me a coffee over at