×

Python Set clear() Method

Python Set clear() Method

The set.clear() method removes all the elements from the set.

Syntax

set.clear()

Parameter

NA

Return

None

Example 1

# Python program explaining
# the set.clear() method 
# initializing the set
fruits = {"watermelon", "banana", "apple"}
# printing the set 
print("Actual set:",fruits)
# clearing off all the elements from the fruits set.
fruits.clear()
print("After the clear() method...")
print("Set:",fruits) 

Output

Actual set: {'apple', 'banana', 'watermelon'}
After the clear() method...
Set: set() 

Example 2

# Python program explaining
# the set.clear() method 
# set of week
week = {'Monday', 'Tuesday', 'Wednesday','Thursday'} 
# locating the elements of week in newWeek
newWeek = week
# clearing all the elements
week.clear()
print('Removing all the element using clear() method') 
print('Set: ', week)
print('New set: ', newWeek)
# again initializing the set
week = {'Wednesday','Thursday','Friday','Saturday'} 
newWeek = week
week = {}
print('Removing all the element by assigning {}')
print('Set: ', week)
print('New Set: ', newWeek) 

Output

Removing all the element using clear() method

Set:  set()

New set:  set()

Removing all the element by assigning {}

Set:  {}

New Set:  {'Friday', 'Thursday', 'Saturday', 'Wednesday'}

Python Set copy() Method

The set.copy() method in Python copies the set .

Syntax

set.copy()

Parameter

NA

Return

This method returns a shallow copy of the set.

Example 1

# Python program explaining

# the set.copy() method

# initializing the set

fruits = {"watermelon", "banana", "apple"}

# prinitng the set

print("Actual set: ",fruits)

# copying the set elements in new set.

newFruits= fruits.copy()

print("After the copying the dictionary...")

print("Copied set:",newFruits)

Output

Actual set:  {'apple', 'banana', 'watermelon'}

After the copying the dictionary...

Copied set: {'apple', 'banana', 'watermelon'}

Example 2

# Python program explaining

# the set.copy() method

vowels = {'a', 'e', 'i', 'o'}

# copying the vowels set to the secondSet

secondSet = vowels.copy()

# before adding

print ('Before adding... ')

print ('firstirst vowels set: ',vowels )

print ('Second  vowels set: ', secondSet )

# Adding element to second set, the first set does not change.

secondSet.add('u')

# after adding the last vowel 'u' in the set

print ('After adding... ')

print ('First vowels set: ',vowels )

print ('second  vowels set: ', secondSet )

Output

Before adding...

firstirst vowels set:  {'e', 'a', 'i', 'o'}

Second  vowels set:  {'e', 'a', 'i', 'o'}

After adding...

First vowels set:  {'e', 'a', 'i', 'o'}

second  vowels set:  {'e', 'a', 'i', 'o', 'u'}


Related Topics

Spyder (32-bit) - Free download

Spyder is a free and open-source scientific environment created by and for Python engineers, scientists, and data analysts. It is a robust scientific environment created in Python by and for...

3 minutes read.

Python BS4 Code

Python BS4 Code The BS4 stands for BeautifulSoup version 4.x. The BeautifulSoup is a Python library which is used for pulling out data of the HTML & XML files using the...

14 minutes read.

Python Pickle

The pickle is a module that enables serialization and de-serialization of the structure of the object in python. Pickling is the process that uses the protocols to convert the Python...

5 minutes read.

Python comment symbol

Python programmers frequently use the comment system because, without it, things may quickly become very perplexing. The developers' helpful information is provided in the comments, which helps the reader understand...

3 minutes read.

Python isinstance() function

Python isinstance() function The isinstance() function in Python returns a Boolean value ‘True’ if the given object is of the specified type, otherwise it returns False. Syntax isinstance(object, classinfo) Parameter object: It is a required parameter which represents an object. classinfo: This...

1 minute read.

One Liner If-Else Statements in Python

Before learning one liner if else Python, let us first know what Python is. Python is a broadly helpful, object-oriented and dependable language having a large number of utilizations from...

4 minutes read.

What is the re.sub() function in Python

There. sub () function is used to return a string by replacing the occurrences of a specific character or pattern with a replacement string. To use this function, import the...

3 minutes read.

Is Python Case-sensitive when Dealing with Identifiers

Yes, Python is a case-sensitive language while dealing with identifiers. Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. It is a case-sensitive...

6 minutes read.

Sentiment Analysis using NLTK

Introduction Data is being produced at an astounding rate and volume in the field of the internet and other digital services nowadays. Researchers, engineers, and data analysts often work with tabular...

7 minutes read.

Python JSON Schema

Python-jsonschema JSON: JSON stands for JavaScript Object Notation. It is a text-based format that represents structured data and can be used to interchange data among various applications. This is self-defining language...

5 minutes read.

How to plot a Histogram in Python

A Histogram is used to represent a given data provided as a chunk. This histogram is a graphical representation that uses bars to indicate the ranges of the data. In...

4 minutes read.

Python Program to Generate a Random String

The term "random" refers to a group of information or data that can be accessed in any chronological order. To create random strings, a Python program called random is utilized....

6 minutes read.

Applications of Python

Top 10 Applications of Python in 2020- 2021 Python language is famous for its general-purpose nature, which enables developers to use it in every field of development. Python can be found...

6 minutes read.

Python Set update() method

Python Set update() method The set.update() method in Python updates the current set, by adding items from another set. If an element is common in both the sets, only one appearance of this item...

1 minute read.

Python | a += b is not always a = a + b

a += b in Python doesn't always behave the same way as a = a + b; the same operands can produce different outcomes depending on the circumstances. But we...

3 minutes read.

Operator Overloading in Python

In any programming language, + is an arithmetic operator; it adds two numbers to give the sum. Have you ever tried to concatenate two strings? Concatenating two strings is adding...

5 minutes read.

Index Error in Python

The index errors are the run time error that is raised in Python when we try to access an index that does not exist. This might seem very trivial but...

4 minutes read.

Best Database in Python

In this tutorial, Firstly, we will understand what the term database means. Further, we will see the various databases supported in Python and when to use which one. Further, we...

7 minutes read.

Python while loop

Loops are essential in Python or any other programming language, as they help to execute a block of code repetitively. Sometimes, situations arise where you would need to use a piece of code...

2 minutes read.

Python vars() Function

Python vars() Function The vars() function in Python returns the __dict__ attribute for a module, class, instance, or any other object with a __dict__ attribute. Syntax vars([object]) Parameter object: This parameter represents any object with a __dict__attribute Return This function returns...

1 minute read.