Bring Your Logic using Python
341 subscribers
109 photos
1 video
4 files
544 links
Logic building using Python : Materials, links, e-books and Code snippets
Download Telegram
Beware of nan!  
😱#python

>>> sorted([5.0, math.nan, 10.0, 0.0])
[5.0, nan, 0.0, 10.0]

>>> sorted([math.nan, 1.0])
[nan, 1.0]
>>> sorted([1.0, math.nan])
[1.0, nan]

>>> min(3, math.nan, 1)
1
>>> min(math.nan, 3, 1)
nan


#pythontip
#PythonTip
—————-

Use _ in #python console to retrieve the result of the last statement
>>> 3 + 4
7
>>> _ * 3
21
#PythonTip

Fraction Calculation
——————————

>>> from fractions import Fraction
>>> a = Fraction(1,3)
>>> b = Fraction(5,4)
>>> print(a+b)
19/12
As a rule of thumb, do NOT modify a collection / array / list while iterating over it.

For example, this code will NOT print all elements in the list before deleting them. #python #PythonTip
#Python #Tip

#pythontip:

In python, comparison operators can be chained:

>>> x = 3
>>> if 2 < x < 4:
... print("x is between 2 and 4")
...
x is between 2 and 4