Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does if random.randint(0,1) mean? [closed]

Tags:

python

I was reading "Foundation of python network programming, 2nd edition". And I found in page 22 a sentence which confused me. It's simplified version is like below:

import random
# blah blah blah #
if random.randint(0,1):
    print "blah blah blah"

what does random.randint(0,1) do here? Does 0 equal False and 1 equal True here?

like image 620
Mario Avatar asked Mar 21 '23 07:03

Mario


1 Answers

random.randint() produces a random integer in the range specified, boundaries included.

Because it produces 0 or 1 at random, and numeric 0 is False and every other number is True, yes, it randomly produces a false or true value.

In Python, empty containers, empty strings, None and numeric 0 (integer 0, float 0.0, etc.) are all false values.

like image 52
Martijn Pieters Avatar answered Mar 31 '23 17:03

Martijn Pieters