Python3 Flashcards
How to compare floating point values in python3?
python3: use math.isclose() to compare floating point values
The function is part of the math module and is available from Python 3.5 onward.
Useful for comparing floating-point numbers where exact equality is unreliable due to rounding errors.
Numbers close within default tolerance
print(math.isclose(1.000000001, 1.0)) # True
Numbers not close within default tolerance
print(math.isclose(1.0001, 1.0)) # False
math.isclose()
python3
python3: use math.isclose() to compare floating point values
How to generate random integers in [1, 2]
python3
import random
random.randint(1,2)
What does this python3 code do?
import random
random.randint(1,2)
It generates random integers within [1, 2]
In Python3, how to randomly choose a value from a list?
import random
myList = [1,2,3,4,5]
random.choice(myList)
In Python3, how to set positive and negative infinitive?
float(‘inf’)
float(‘-inf’)