Teachnique
      CourseRoadmaps
      Login

      OverviewCommentsUser InputNumbersBooleansHistoryHello World ProgramEnvironment SetupSyntaxVariablesData TypesType CastingUnicode SystemLiteralsOperators

      Control FlowBreak StatementContinue StatementPass StatementNested LoopsDecision MakingIf StatementIf-else StatementNested IF StatementMatch-Case StatementLoopsFor LoopsFor-else LoopsWhile Loops

      FunctionsBuilt-in FunctionsDefault ArgumentsKeyword ArgumentsKeyword-Only ArgumentsPositional ArgumentsPositional-Only ArgumentsArbitrary ArgumentsVariable ScopeFunction AnnotationsModules

      StringSlicing StringsModify StringsString ConcatenationString FormattingEscape CharactersString MethodsString Exercises

      ListsList ExercisesAccess List ItemsChange List ItemsAdd List ItemsRemove List ItemsLoop ListsList ComprehensionSort ListsCopy ListsJoin ListsList Methods

      TuplesAccess Tuple ItemsUpdate TuplesUnpack Tuple ItemsLoop TuplesJoin TuplesTuple MethodsTuple Exercises

      SetsAccess Set ItemsAdd Set ItemsRemove Set ItemsLoop SetsJoin SetsCopy SetsSet OperatorsSet MethodsSet Exercises

      DictionariesDictionary ExercisesAccess Dictionary ItemsChange Dictionary ItemsAdd Dictionary ItemsRemove Dictionary ItemsDictionary View ObjectsLoop DictionariesCopy DictionariesNested DictionariesDictionary Methods

      ArraysAccess Array ItemsAdd Array ItemsRemove Array ItemsLoop ArraysCopy ArraysReverse ArraysSort ArraysJoin ArraysArray MethodsArray Exercises

      File HandlingWrite to FileRead FilesRenaming and Deleting FilesDirectoriesFile Methods

      OOP ConceptsDynamic BindingDynamic TypingAbstractionObject and ClassesEncapsulationInterfacesPackagesInner ClassesAnonymous Class and ObjectsSingleton ClassWrapper ClassesEnumsReflectionClass AttributesClass MethodsStatic MethodsConstructorsAccess ModifiersInheritancePolymorphismMethod OverridingMethod Overloading

      Feedback

      Submit request if you have any questions.

      Course
      Booleans

      Python Tutorial

      This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.

      Booleans

      Boolean Values in Python

      In Python, bool is a sub-type of int type. A bool object has two possible values, and it is initialized with Python keywords, True and False.
      >>> a=True
      >>> b=False
      >>> type(a), type(b)
      (<class 'bool'>, <class 'bool'>)
      A bool object is accepted as argument to type conversion functions. With True as argument, the int() function returns 1, float() returns 1.0; whereas for False, they return 0 and 0.0 respectively. We have a one argument version of complex() function.
      If the argument is a complex object, it is taken as real part, setting the imaginary coefficient to 0.
      a=int(True)
      print ("bool to int:", a)
      a=float(False)
      print ("bool to float:", a)
      a=complex(True)
      print ("bool to complex:", a)
      On running this code, you will get the following output
      bool to int: 1
      bool to float: 0.0
      bool to complex: (1+0j)

      Boolean Expression in Python

      Python boolean expression is an expression that evaluates to a Boolean value. It almost always involves a comparison operator. In the below example we will see how the comparison operators can give us the Boolean values. The bool() method is used to return the truth value of an expresison.
      Syntax: bool([x])
      Returns True if X evaluates to true else false.
      Without parameters it returns false.
      Below we have examples which use numbers streams and Boolean values as parameters to the bool function. The results come out as true or false depending on the parameter.

      Example

      # Check true
      a = True
      print(bool(a))
      
      # Check false
      a = False
      print(bool(a))
      
      # Check 0
      a = 0.0
      print(bool(a))
      
      # Check 1
      a = 1.0
      print(bool(a))
      
      # Check Equality
      a = 5
      b = 10
      print(bool( a==b))
      
      # Check None
      a = None
      print(bool(a))
      
      # Check an empty sequence
      a = ()
      print(bool(a))
      
      # Check an emtpty mapping
      a = {}
      print(bool(a))
      
      # Check a non empty string
      a = 'Tutorialspoint'
      print(bool(a))
      

      Practice with Online Editor

      Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
      Remember to save code(Ctrl + S Or Command + S) before run it.