bool.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # True and False are the only values for the bool type.
  2. assert isinstance(True, bool)
  3. assert isinstance(False, bool)
  4. # The bool function allows other data types to be converted to bool types.
  5. assert bool(1) == True
  6. assert bool(0) == False
  7. assert bool([]) == False
  8. assert bool([1, 2, 3]) == True
  9. assert bool("") == False
  10. assert bool("hello") == True
  11. assert bool(None) == False
  12. # Boolean values are also integers
  13. assert True == 1
  14. assert False == 0
  15. # Boolean arithmetic
  16. assert True + True == 2
  17. assert True - False == 1
  18. assert True * True == 1
  19. assert True / True == 1.0
  20. # Boolean and bitwise operators
  21. assert (True and False) == False
  22. assert (True or False) == True
  23. assert (not True) == False
  24. assert (not False) == True
  25. # Using bool in a if condition
  26. if True:
  27. assert True
  28. else:
  29. assert False
  30. if False:
  31. assert False
  32. else:
  33. assert True
  34. # If statement with string
  35. def is_string_empty(s):
  36. if s:
  37. return False
  38. else:
  39. return True
  40. assert is_string_empty('Hello') == False
  41. assert is_string_empty('') == True
  42. # If statement with list
  43. def is_list_empty(lst):
  44. if lst:
  45. return False
  46. else:
  47. return True
  48. assert is_list_empty([1, 2, 3]) == False
  49. assert is_list_empty([]) == True
  50. # If statement with string comparison
  51. def compare_strings(s1, s2):
  52. if s1 == s2:
  53. return True
  54. else:
  55. return False
  56. def is_string_not_empty(s):
  57. if not s:
  58. return False
  59. else:
  60. return True
  61. assert compare_strings('abc', 'abc') == True
  62. assert compare_strings('abc', 'def') == False
  63. assert is_string_not_empty('abc') == True
  64. assert int(0xFFFFFFFF) == 0xFFFFFFFF
  65. print("PASS")