×

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

Python Set Methods

[et_pb_section][et_pb_row][et_pb_column type="4_4"][et_pb_text] Python Set Methods A set is a collection which is unordered and unindexed. Python has a set of built-in methods that you can use on sets. Methods ...

4 minutes read.

Excel Automation with Python

Data analysis is the upcoming technology in the IT sector; this data analysis can be performed easily with the help of data frames or by using excel sheets. The data...

4 minutes read.

How to Iterate a List in Python

As we know, a List is one of the four unique data structures available in Python; in this article, we will deep dive into understanding iterating through a list and...

3 minutes read.

List Assignment Index out of Range in Python

As we know, a List is one of the four unique data structures available in Python; In this tutorial, we will deep dive into understanding iterating through a list and...

3 minutes read.

List in Python

What is List in Python In Python, lists are used to store the multiple values in one variable. We can say that list is the collection of similar as well as...

3 minutes read.

Python getattr() function

Python getattr() function The getattr() function in Python returns the value of the named attribute for the given object. Syntax getattr(object, name[, default]) Parameter object: It is a required parameter which represents an object. name: This parameter represents the...

1 minute read.

Python program to display ASCII value of the character

Python program to display ASCII value of the character The full form of ASCII is American Standard Code for Information Interchange. This example explains a program in Python to find the...

1 minute read.

Python Keywords

If you are trying to learn a programming language, you need to have a basic idea of "What are keywords" and "How they are used". You can learn about keywords in...

7 minutes read.

How To Compare Two Strings In Python

In this article, we will discuss how to compare two strings in Python. So, before that, let’s have a quick revision on “What are strings?” Strings are a sequence of characters that...

5 minutes read.

Python Trigonometric Functions

Before jumping right into the functions and how to use them we need to know what do trigonometric functions mean and how many functions does python has. What are Trigonometric functions? Trigonometric...

3 minutes read.

Python data science course

What is meant by Data Science? When processing raw, structured, and unstructured data utilizing various technologies, algorithms, and the scientific method, data science is a detailed study of the enormous quantity...

4 minutes read.

How to check version of Python

How to check version of python The versions of Python come with different kinds of features and functionalities. It is not a herculean task to keep track of the updates these...

3 minutes read.

Google Python Class

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

How to read data from com port in python

Comport, the I/O interface is known as a COM port that allows the connection for a serial device to a computer. COM ports are sometimes referred to as serial ports....

3 minutes read.

Python Control Flow Statements

This article aims to introduce you to what control flow statements are in general and Control Flow Statements in Python programming Language, the Importance of control flow statements and look...

3 minutes read.

Python Arithmetic Operators

An arithmetic operator is a mathematical operator that is used to operate on two operands. Based on the operator used, action is performed on the operands, and output is delivered. Following...

3 minutes read.

Python String index() method

Python String index() method The string.index() method in Python finds the lower index of the first occurrence of the specified value or raise a ValueError if the substring is not found. Syntax index(sub[, start[, end]]) Parameter sub:...

2 minutes read.

Python Break Statement

In Python, loops are used to automate and repeat processes in an effective manner. However, there may be occasions when you wish to entirely exit the loop, skip an iteration,...

2 minutes read.

What is a Script in Python

Have you ever heard that Python is a scripting language? Or when people address a Python program file as a script? Besides programming, script generally means "a written story for...

3 minutes read.

Python Glob

Methods for matching files that include particular patterns in accordance with UNIX shell-related expansion rules are referred to as "glob" methods. Similar methods for finding, locating, and searching for all of...

12 minutes read.