×

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

Abstraction in Python

What is meant by abstraction generally? A very general notion of a thing or work is known as abstract. To be more precise, the process of having a brief idea but...

4 minutes read.

Python Tutorial

Python tutorial is a widely used programming language which helps beginners and professionals to understand the basics of Python programming easily. Python is a high-level, easy, interpreted, general-purpose, and dynamic programming...

19 minutes read.

Add in Dictionary Python

Dictionary in python: Dictionaries are a useful data configuration for storing information in Python because they can mimic real-world data arrangements where a particular value exists for a particular key. The...

4 minutes read.

How to Convert String to List In Python?

How to Convert String to List In Python? We all are familiar with what strings and lists are, let us have a quick revision on them- Strings are a sequence of characters...

4 minutes read.

Drop() Function in Python

Drop() Function: The python programming language consists of various libraries; some of them are pandas and matplotlib. Data scientists mainly use the pandas library to analyze the data more easily and...

3 minutes read.

Python Random Module

The tutorial for the Python random module demonstrates how to produce pseudo-random integers in Python. Random Number Generator (RNG) The RNG (random number generator) generates a series of values with no discernible...

8 minutes read.

Python String rindex() method

Python String rindex() method The string. rindex() method in Python returns the highest index of the substring inside the string (if found). If the substring is not found, it raises an...

2 minutes read.

Working with files in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has an...

5 minutes read.

_dict_ in Python

An unordered collection of data values known as a dictionary can be used in Python to store data values similar to a map. Dictionaries can also store a key: value...

6 minutes read.

Python Variables

Variables are one of the most important terms we should be familiar with if we want to be good programmers. In simpler words, we use variables to store a value....

4 minutes read.

How to Program in Python on Raspberry pi?

Introduction to Python A popular programming tool with simple, complete novice syntax is Python structure of paragraphs, phrases, and words. Due to its widespread use, this has a large community that...

4 minutes read.

What is online python free IDE

An IDE is an acronym that stands for “Integrated development environment.” It is a software environment that has a software development package that consists of the code editor and builds...

4 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 and its advantages

What is Python? Python is an object-oriented programming language, a general-purpose programming language with a high level, and also an interpreted language that has an easy syntax and dynamic semantics. Python...

7 minutes read.

Genetic Algorithm in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The source...

4 minutes read.

Difference between Package and Module in Python

What are Python modules? A file with the “.py” suffix that includes Python or C executable code is known as a module. Multiple Python commands and expressions make compose a module....

3 minutes read.

Python program to find the greatest among two numbers

Python program to find the greatest among two numbers We have many approaches to get the largest number among the two numbers, and here we will discuss a few of them....

2 minutes read.

Python setattr() function

Python setattr() function The setattr() function sets the value of the specified attribute of the specified object. Syntax setattr(object, name, value) Parameter object: This parameter represents any object. name: This parameter represents the name of the attribute you...

1 minute read.

How to make API calls in Python

Information is extremely critical these days since it drives applications and organizations. It is subsequently critical to figure out how to get this information to serve your application. In fundamental...

4 minutes read.

Python Set difference_update() method

Python Set difference_update() method The set.difference_update() method in Python removes the items that exist in both sets. Syntax set.difference_update(set1) Parameter set- This argument represents a set (minuend) set1- This arguments represents a set(subtrahend) Return None Example 1 # Python program explaining # the set.difference_update()...

2 minutes read.