BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Set clear() Method with examples

Last Updated: March 30, 2019 by Chaitanya Singh | Filed Under: Python Tutorial

The clear() method in Python removes all the elements from a given Set.

Python Set clear() method Syntax

set.clear()

Parameter: None. This method doesn’t take any parameters.
Return value: This method doesn’t return any value, it just removes all the items from the set.

Python Set clear() method example

In the following example we have a set of numbers and we are calling clear() method to remove all the elements of this set.

# A Set of numbers
numbers = {5, 8, 10, 9, 1}

# Displaying set before executing clear()
print("Original Set is:", numbers)

# removing all the elements from the set
numbers.clear()

# displaying set after calling clear()
print("Updated Set is:", numbers)

Output:
Python clear() method example

As you can see in the output that there are no elements in the set after we have called the clear() method. If you want to remove a specific element from the Set then using remove() or discard() method.

Top Related Articles:

  1. Python Recursion
  2. Python Keywords and Identifiers with examples
  3. Python Data Types
  4. Python Constructors – default and parameterized
  5. Python Numbers

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap