Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python String
Next article icon

Python Boolean

Last Updated : 05 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Python Boolean type is one of the built-in data types provided by Python, which represents one of the two values i.e. True or False. Generally, it is used to represent the truth values of the expressions.

Python Boolean Type

Boolean value can be of two types only i.e. either True or False. The output <class ‘bool’> indicates the variable is a Boolean data type.

Python
a = True print(type(a))  b = False print(type(b)) 

Output
<class 'bool'> <class 'bool'> 

Let’s take a detailed look at Python Boolean:

Table of Content

  • Python Boolean Type
  • Evaluate Variables and Expressions
  • Boolean Operators
    • Boolean OR Operator
    • Boolean And Operator
    • Boolean Not Operator
    • Python Boolean == (equivalent) and != (not equivalent) Operator
    • Python is Operator
    • Python in Operator

Evaluate Variables and Expressions

We can evaluate values and variables using the Python bool() function. This method is used to return or convert a value to a Boolean value i.e., True or False, using the standard truth testing procedure. 

Python bool() Function

In Python, the bool() function is used to convert a value or expression to its corresponding Boolean value (True or False). In the example below the variable res will store the boolean value of False after the equality comparison takes place.

Python
# Returns False as x is None x = None print(bool(x))  # Returns False as x is an empty sequence x = () print(bool(x))  # Returns False as x is an empty mapping x = {} print(bool(x))  # Returns False as x is 0 x = 0.0 print(bool(x))  # Returns True as x is a non empty string x = 'GeeksforGeeks' print(bool(x)) 

Output
False False False False True 

Note: It’s not always necessary to use bool() directly. Python automatically evaluates expressions in terms of their Boolean values, especially when used in conditions like if statements or loops.

Integers and Floats as Boolean

In Python, integers and floats can be used as Boolean values with the bool() function. Any number with a value of zero (0, 0.0) is considered False while any non-zero number (positive or negative) is considered True.

Python
var1 = 0 print(bool(var1))  var2 = 1 print(bool(var2))  var3 = -9.7 print(bool(var3)) 

Output
False True True 

Boolean Operators

Boolean Operations in Python are simple arithmetic of True and False values. These values can be manipulated by the use of boolean operators which include AND or and NOT. Common boolean operations are –

  • or
  • and
  • not
  • == (equivalent)
  • != (not equivalent)

Boolean OR Operator

Boolean or operator returns True if any one of the inputs is True else returns False.

Python
a = 5 b = 3 c = 8  if a > b or b < c:     print("True") 

Output
True 

Explanation:

  • The condition a > b or b < c uses the or operator, which returns True if any one of the conditions is True.
  • a > b is True because 5 > 3 and b < c is also True because 3 < 8.
  • Since at least one condition is True, the if block executes and “True” is printed.

Boolean And Operator

Boolean operator returns False if any one of the inputs is False else returns True.

Example:

Python
a = 0 b = 2 c = 4  if a > b and b<c:     print(True) else:     print(False)      if a and b and c:     print("True") else:     print("False") 

Output
False False 

Explanation:

  • In the first part, the condition a > b and b < c evaluates to False because a > b is False, causing the else block to print False.
  • In the second part, a is 0 (which is considered False), so the entire condition evaluates to False and the else block prints “False”.

Boolean Not Operator

Boolean Not operator only requires one argument and returns the negation of the argument i.e. returns the True for False and False for True.

Example:

Python
a = 0  if not a:     print("False") 

Output
False 

Explanation:

  • The not operator inverts the Boolean value of the expression.
  • Since a = 0 (which is considered False), not a evaluates to True.
  • As a result, the condition if not a is true and the program prints “Boolean value of a is False”.

Python Boolean == (equivalent) and != (not equivalent) Operator

Both operators are used to compare two results. ‘==’ equivalent operator returns True if two results are equal and ‘!=’ not equivalent operator returns True if the two results are not same.

Example:

Python
a = 0 b = 1  if a == 0:     print(True)      if a == b:     print(True)      if a != b:     print(True) 

Output
True True 

Explanation:

  • The first condition a == 0 is True because a is indeed 0, so it prints True.
  • The second condition a == b is False because a (which is 0) is not equal to b (which is 1), so nothing is printed.
  • The third condition a != b is True because a (which is 0) is not equal to b (which is 1), so it prints True.

Python is Operator

is keyword is used to test whether two variables belong to the same object. The test will return True if the two objects are the same else it will return False even if the two objects are 100% equal.

Example:

Python
x = 10 y = 10  if x is y:     print(True) else:     print(False) 

Output
True 

Explanation:

  • is keyword checks if two variables point to the same object in memory.
  • In this case, both x and y are assigned the value 10. Python often optimizes memory for small integers, so both x and y reference the same object in memory.
  • Since x is y evaluates to True, the if block is executed and True is printed.

Python in Operator

in operator checks for the membership i.e. checks if the value is present in a list, tuple, range, string, etc.

Python
# Create a list a = [1, 2, 2]  # Check if lion in list or not if 1 in a:     print(True) 

Output
True 

Explanation:

  • in keyword checks if an element exists in a list.
  • In this case, the element 1 is present in the list a = [1, 2, 2].
  • Since 1 is found in the list, the if block executes and True is printed.

Suggested Quiz
10 Questions

What does bool(0) return?

  • A

    True

  • B

    False

  • C

    Error

  • D

    None

Explanation:

In Python, 0 is considered a "falsey" value, which means bool(0) evaluates to False.

Which of the following is considered a "truthy" value in Python?

  • A

    0

  • B

    []

  • C

    {}

  • D

    1

Explanation:

In Python, any non-zero integer is considered "truthy," so 1 evaluates to True when converted to a boolean.

What is the result of bool(None)?

  • A

    True

  • B

    False

  • C

    None

  • D

    Syntax Error

Explanation:

None is considered a "falsey" value in Python, thus bool(None) returns False.

In Python, which operator checks if two variables point to the same object in memory?

  • A

    ==

  • B

    is

  • C

    in

  • D

    !=

Explanation:

In Python, the is operator checks if two variables point to the same object in memory. This operator returns True if the variables are indeed referencing the same object, indicating they are identical in terms of memory location.

What is the result of not True?

  • A

    True

  • B

    False

  • C

    None

  • D

    Error

Explanation:

The not operator negates the boolean value, so not True is False.

How does Python evaluate the expression bool(0) and bool(1)?

  • A

    True

  • B

    False

  • C

    Error

  • D

    None

Explanation:

bool(0) is False and bool(1) is True, but the and operator requires both operands to be True to return True.

Evaluate the result of False and not False?

  • A

    True

  • B

    False

  • C

    None

  • D

    Syntax Error

Explanation:

not False is True, but since False and True evaluates to False, the overall result is False.

Which of the following statements is correct regarding the comparison operators in Python?

  • A

    The `==` operator checks for object identity only.

  • B

    The `!=` operator checks if two values are equivalent.

  • C

    The `==` operator checks for value equality.

  • D

    The `!=` operator checks if two objects are the same.

Explanation:

The == operator is used to compare the values of two objects for equality. It assesses whether the contents or data of the objects are equivalent, regardless of whether they are the same object in memory.

What is the result of the code snippet: not any([False, False, False])?

  • A

    True

  • B

    False

  • C

    Error

  • D

    None

Explanation:

The any() function returns False if all elements are false. not False then converts this result to True.

What will all([True, True, False]) return?

  • A

    True

  • B

    False

  • C

    None

  • D

    Syntax Error

Explanation:

The all() function returns True only if all elements in the iterable are True. Since there is a False in the list, the result is False.

Quiz Completed Successfully
Your Score :   2/10
Accuracy :  0%
Login to View Explanation
1/10 1/10 < Previous Next >

Next Article
Python String

J

jeeteshgavande30
Improve
Article Tags :
  • Python
  • Technical Scripter
  • Python-datatype
  • Technical Scripter 2020
Practice Tags :
  • python

Similar Reads

  • Python Tutorial | Learn Python Programming Language
    Python Tutorial – Python is one of the most popular programming languages. It’s simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. Python is: A high-level language, used in web development, data science, automat
    10 min read
  • Python Data Types
    Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
    10 min read
  • Python Numbers
    In Python, numbers are a core data-type essential for performing arithmetic operations and calculations. Python supports three types of numbers, including integers, floating-point numbers and complex numbers. Here's an overview of each: [GFGTABS] Python a = 4 # integer b = 4.5 # float c = 4j # compl
    6 min read
  • Python Boolean
    Python Boolean type is one of the built-in data types provided by Python, which represents one of the two values i.e. True or False. Generally, it is used to represent the truth values of the expressions. Python Boolean TypeBoolean value can be of two types only i.e. either True or False. The output
    7 min read
  • Python String
    A string is a sequence of characters. Python treats anything inside quotes as a string. This includes letters, numbers, and symbols. Python has no character data type so single character is a string of length 1. [GFGTABS] Python s = "GfG" print(s[1]) # access 2nd char s1 = s + s[0] # updat
    6 min read
  • Python Lists
    In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s
    6 min read
  • Python Arrays
    Lists in Python are the most flexible and commonly used data structure for sequential storage. They are similar to arrays in other languages but with several key differences: Dynamic Typing: Python lists can hold elements of different types in the same list. We can have an integer, a string and even
    10 min read
  • Python Tuples
    A tuple in Python is an immutable ordered collection of elements. Tuples are similar to lists, but unlike lists, they cannot be changed after their creation (i.e., they are immutable). Tuples can hold elements of different data types. The main characteristics of tuples are being ordered , heterogene
    7 min read
  • Dictionaries in Python
    A Python dictionary is a data structure that stores the value in key: value pairs. Values in a dictionary can be of any data type and can be duplicated, whereas keys can't be repeated and must be immutable. Example: Here, The data is stored in key:value pairs in dictionaries, which makes it easier t
    5 min read
  • Python Sets
    Python set is an unordered collection of multiple items having different datatypes. In Python, sets are mutable, unindexed and do not contain duplicates. The order of elements in a set is not preserved and can change. Creating a Set in PythonIn Python, the most basic and efficient method for creatin
    11 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences