Check if the denominator is zero before dividing. This avoids the overhead of catching the exception, which may be more efficient if you expect to be dividing by zero a lot.
def weird_division(n, d):
return n / d if d else 0
More Related Contents:
- a * a//b doesn't work properly in Python (// Operator)
- How can I force division to be floating point? Division keeps rounding down to 0?
- Why does integer division yield a float instead of another integer?
- Division in Python 2.7. and 3.3 [duplicate]
- Negative integer division surprising result
- Integer division in Python 2 and Python 3
- negative zero in python
- What is the reason for having ‘//’ in Python? [duplicate]
- Floor division with negative number
- How to manage division of huge numbers in Python?
- Python 3 integer division [duplicate]
- Removing Trailing Zeros in Python [duplicate]
- How to retain leading zeros of int variables?
- In Python, what is a good way to round towards zero in integer division?
- list to dictionary conversion with multiple values per key?
- Convert UTF-8 with BOM to UTF-8 with no BOM in Python
- Efficient calculation of Fibonacci series
- using requests with TLS doesn’t give SNI support
- Simulate Python keypresses for controlling a game
- Spark DataFrame: Computing row-wise mean (or any aggregate operation)
- How to download a file from Google Drive using Python and the Drive API v3
- How do I set sys.argv so I can unit test it?
- Running a bash script from a cronjob fails with “No such file or directory”
- Serializing a Python namedtuple to json
- Non-lazy evaluation version of map in Python3?
- Why does using multiplication operator on list create list of pointers? [duplicate]
- Using Properties in Python classes cause “maximum recursion depth exceeded” [duplicate]
- Asking “is hashable” about a Python value
- How to set the axis limits
- problems with python and virtualenvwrapper after updating: No module named virtualenvwrapper