Follow

Python Set

set it use to store multiple values in single variable, it one in 4 data types. set are unordered, unindexed and do not store duplicate values. set are created insert items(value) in curly brackets {}. cannot access item using index number like a list.
Set cannot store duplicate values.

Example

set : {'mango','guava','papaya'}

Python set (not all duplicate)

x={"apple", "banana", "banana", "guava"}
print(x)

Try it Yourself »

Output

{'apple','banana','guava'}

sets

sets store multi datatype items
set={'hallo",True,2}
print(type(set))
#length of set
print(len(set))

Try it Yourself »

Output

<class 'set'>
3
var1 = {1,2,3}
var2 = {'hi','when','why'}
var = {'hi','hallo','who','what'}
Function Example output
len len(var) 4
type type(var) <class 'set'>
add var.add("what's up") {'hi','hallo','who','what','what's up'}
update var.update(var1) {'hi','hallo','who','what',1,2,3}
remove var.remove('hi') {'hallo','who','what'}
discard var.remove('hi') {'hallo','who','what'}
pop var.pop('hi') {'hallo','who','what'}
clear var.clear() {}
del del var
union var.union(var1) {'hi','hallo','who','what',1,2,3}
intersection_update var.intersection_update(var2) {'hi'}
intersection var.intersection_update(var2) {'hi'}
symmetric_difference_update var.symmetric_difference_update(var2) {'hallo','who','what','when','why'}
symmetric_difference var.symmetric_difference_update(var2) {'hallo','who','what','when','why'}
copy x = var.copy() {'hi','hallo','who','what'}
isdisjoint var.isdisjoint(var1) True

Python arrays

  • Tuple:tuple is written in Parentheses (), collection of immutable and ordered values. allow duplicate.
  • List:list is written in square brackets [], collection of mutavle and ordered values. allow duplicate.
  • Dict:Dict is written in curly brackets {}, collection of unordered and mutable values. not allow duplicate.
  • Set:sets is written in curly brackets {}, collection of unordered , unindexed and immutable values. not allow duplicate.

No comments:

Post a Comment

Tell us how you like it.