Beware of nan!
😱#python
#pythontip
😱#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
Forwarded from Information for IT Students
#PythonTip
—————-
Use _ in #python console to retrieve the result of the last statement
>>> 3 + 4
7
>>> _ * 3
21
—————-
Use _ in #python console to retrieve the result of the last statement
>>> 3 + 4
7
>>> _ * 3
21
Forwarded from Information for IT Students
#PythonTip
Fraction Calculation
——————————
>>> from fractions import Fraction
>>> a = Fraction(1,3)
>>> b = Fraction(5,4)
>>> print(a+b)
19/12
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
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
#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