Saturday, December 21, 2019

set in python


s={10,20,30,40}
print(s)
print(type(s))


output:
{40, 10, 20, 30}
<class 'set'>

example 2:
s={10,20,30,40}
s1=s
print(s)
print(type(s))
print(id(s1),id(s))

output:
{40, 10, 20, 30}
<class 'set'>
1861259155048 1861259155048

In above case both s1 and s representing same id
but copying is not done


example 3:
by copy method

s={10,20,30,40}
s1=s.copy()
print(s)
print(type(s))
print(id(s1),id(s))

output:
{40, 10, 20, 30}
<class 'set'>
1861259156840 1861259157288

both s1 and s address are different

No comments:

Post a Comment

python class topic video