Python3 Flashcards

1
Q

How to compare floating point values in python3?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

math.isclose()

python3

A

python3: use math.isclose() to compare floating point values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to generate random integers in [1, 2]
python3

A

import random
random.randint(1,2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does this python3 code do?

import random
random.randint(1,2)

A

It generates random integers within [1, 2]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In Python3, how to randomly choose a value from a list?

A

import random
myList = [1,2,3,4,5]
random.choice(myList)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In Python3, how to set positive and negative infinitive?

A

float(‘inf’)
float(‘-inf’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly