by BehindJava

What are Sets and Booleans in Python with Syntax and Examples

Home » python » What are Sets and Booleans in Python with Syntax and Examples

In this tutorial we are going to learn about Sets and Booleans in Python with Syntax and Examples.

There are two other object types in Python that we should quickly cover: Sets and Booleans.

Sets

Sets are an unordered collection of unique elements. We can construct them by using the set() function.\ Let’s go ahead and make a set to see how it works.

Example:

  • Create a list with repeats.

    list1 = [1,1,2,2,3,4,5,6,1,1]
  • Cast as set to get unique values.

    set(list1)

    Output:

    {1, 2, 3, 4, 5, 6}

    Booleans

    Python comes with Booleans (with predefined True and False displays that are basically just the integers 1 and 0). It also has a placeholder object called None.
    Let’s walk through a few quick examples of Booleans.

Set object to be a boolean
a = True
#show a
result-True
  • We can also use comparison operators to create booleans.

    # Output is Boolean
    1 > 2
    Result-false
  • We can use None as a placeholder for an object that we don’t want to reassign yet:

    None placeholder
    b = None
    print(b)
    result-None