clear() function in python by R4R Team

-clear() is a python in-build function which are applicable on list and set to clear the content in list and set.

Syntax-

seq.clear()

- Here seq is may be list and set.
- clear() function are not applicable on string,tuple and other data type



program-

l=[1,2,3]
print("list is ",l)
l.clear()
print("Now list is",l)

s=set([1,2,3])
print("Set is",s)
s.clear()
print("Now set is",s)

t=tuple([1,2,3])
print("Tuple is ",t)
#t.clear()
print("Now tuple is ",t)


output-

list is [1, 2, 3]
Now list is []
Set is {1, 2, 3}
Now set is set()
Tuple is (1, 2, 3)
Now tuple is (1, 2, 3)


-In this program, we have a list and set then we clear the content by using l.clear() or s.clear().
- Here if we try to use clear() function for the tuple datatype then will give an error like AttributeError: 'tuple' object has no attribute 'clear'. so that's by we don't compile that line to ignore the error.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!