From 5d543d4221d8e4a67b026aad2bf64d3b5aa75850 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Fri, 3 Jul 2009 23:06:29 -0400 Subject: [PATCH] added sets-in-a-boolean-context --- native-datatypes.html | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/native-datatypes.html b/native-datatypes.html index 3edd3b7..50a2b35 100644 --- a/native-datatypes.html +++ b/native-datatypes.html @@ -426,11 +426,11 @@ IndexError: pop from empty list ... else: ... print('no, it's false') ... ->>> is_it_true([]) +>>> is_it_true([]) no, it's false ->>> is_it_true(['a']) +>>> is_it_true(['a']) yes, it's true ->>> is_it_true([False]) +>>> is_it_true([False]) yes, it's true
  1. In a boolean context, an empty list is false. @@ -650,6 +650,29 @@ KeyError: 21
+

Sets In A Boolean Context

+ +

You can use sets in a boolean context, such as an if statement. + +

+>>> def is_it_true(anything):
+...   if anything:
+...     print('yes, it's true')
+...   else:
+...     print('no, it's false')
+...
+>>> is_it_true(set())          
+no, it's false
+>>> is_it_true({'a'})          
+yes, it's true
+>>> is_it_true({False})        
+yes, it's true
+
    +
  1. In a boolean context, an empty set is false. +
  2. Any set with at least one item is true. +
  3. Any set with at least one item is true. The value of the items is irrelevant. +
+

Dictionaries