×

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 globals() function

Python globals() function The globals() function in Python returns the value of the named attribute of object where the ‘name’ must be a string. Syntax globals() Parameter NA Return This function returns the global symbol table as a dictionary. Example...

1 minute read.

Python Tricks: The Book

A Buffet of Awesome Python Features Dan Bader is the author of the book called Python Tricks . he is the owner and editor of Real Python and one of the...

3 minutes read.

Accuracy_score Function in Sklearn

A crucial stage in data science is measuring our model's performance using the appropriate metric. In this article, we will examine two methods for calculating the accuracy of your predictions:...

13 minutes read.

Python pycountry

What is Pycountry? Python programming language: 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...

4 minutes read.

Python List sort() method

The list.sort () method in Python sorts the items of the list in place. Syntax list.sort(key=None, reverse=False) Parameter reverse: If a Boolean value ‘True’ is passed, the  sorting will be done in the descending order else for ‘False’...

2 minutes read.

Python System Requirements

Introduction As we know, Python is a popular programming language usually used to write scripts for operating systems. It’s handy adequate for utilizing in web development and application design. In this article,...

2 minutes read.

How to set font for Text in Python

As a tuple with the font family as the first component, a size in point as the second, and possibly a string with one or more of the style modifiers...

5 minutes read.

Python for Data Analysis

Data analysis uses various techniques to read, illustrate, manipulate and evaluate a particular data. You can have access to the data and keep the data updated regularly. You can append...

4 minutes read.

How to Fix an EOF Error in Python?

Introduction An EOF (End-of-File) error occurs when a program tries to read beyond the end of a file or a stream, causing it to return an error message. It can happen...

8 minutes read.

Python Lists vs Tuples

The difference between lists and tuples is one of the most frequently asked questions in an interview related to python language. Lists and Tuples are two of Python’s built-in data...

4 minutes read.

Nested for Loop in Python

"Loop" is one of the foundation concepts to learn programming in any language. Nested loops are significant steps in solving many types of problems, ranging from basic to complex scenarios....

9 minutes read.

How to run a Python file in CMD

Introduction Today, let us learn about how to run a Python file using cmd. Cmd is nothing but command prompt. So first let us know how to run a Python code...

4 minutes read.

JWT Decode Python

Python's PyJWT package makes it possible to encrypt and decrypt JSON Web Tokens (JWT). JWT is a public, recognized global benchmark for safely expressing demands between two parties (RFC 7519). JSON...

7 minutes read.

Python len() function

Python len() function The len() function in Python  returns the number of items in an object. Syntax len(s) Parameter s: This parameter represents a sequence (such as a string, bytes, tuple, list, or range) or...

1 minute read.

Best app for python

What is python? Python is a prevalent, general-purpose, and high-level programming language. Python Programming Language is appropriate for experienced programmers and also for beginners. Python programming language is being utilized by...

19 minutes read.

Python Set issubset() method

Python Set issubset() method The set.issubset() method in Python returns True if all items in the set exists in the specified set, otherwise it returns False. Syntax set.issubset(set1) Parameter set- This parameter represents the set to check whether...

2 minutes read.

Python Set union() method

Python Set union() method The set.union() method in Python returns a set that contains all items from the original set and all items from the specified sets. Syntax set.union(set1, set2...) Parameter set1- This parameter represents the first set...

2 minutes read.

Python sklearn train_test_split

Sklearn: One of the most beneficial open-source libraries for learning algorithms in Python is, without a doubt, Sklearn or Scikit-Learn. The most effective tools for statistical modeling and machine learning are all included in...

6 minutes read.

Python str() function

Python str() function The str() function in Python converts the specified value into a string. Syntax class str(object='')           or class str(object=b'', encoding='utf-8', errors='strict') Parameter object:  This parameter represents any object to convert the given...

1 minute read.

Convert Float to Int in Python using Pandas

Introduction To play with huge amounts of data, in python we require a tool. The tool which is available in Python is pandas. A panda is an open-source library. It is...

4 minutes read.