Sets

 Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32

Type "help", "copyright", "credits" or "license()" for more information.

>>> ##Almost similar to sets in maths

>>> a={1,2,3,4,5}

>>> type(a)

<class 'set'>

>>> ## remember this below

>>> a={}

>>> type(a)

<class 'dict'>

>>> ##set

>>> a=()

>>> type(a)

<class 'tuple'>

>>> ##above fo tuple

>>> ##below for set

>>> a=set()

>>> type(a)

<class 'set'>

>>> ##sets are immutable

>>> ##sets doesnt have any index , no order

>>> a={10,90,100,3,5,40,70}

>>> set(a)

{3, 100, 5, 70, 90, 40, 10}

>>> set(a)

{3, 100, 5, 70, 90, 40, 10}

>>> set(a)

{3, 100, 5, 70, 90, 40, 10}

>>> set[0]

set[0]

>>> a[0]

Traceback (most recent call last):

  File "<pyshell#20>", line 1, in <module>

    a[0]

TypeError: 'set' object is not subscriptable

>>> ##meaning we cant extract values from set, as it has no order

>>> a.add(14)

>>> aet(a)

Traceback (most recent call last):

  File "<pyshell#23>", line 1, in <module>

    aet(a)

NameError: name 'aet' is not defined

>>> set(a)

{3, 100, 5, 70, 40, 10, 14, 90}

>>> ## above was adding elements to it.

>>> a.remove(70)

>>> set(a)

{3, 100, 5, 10, 40, 90, 14}

>>> a.discard(90)

>>> set(a)

{3, 100, 5, 40, 10, 14}

>>> a.pop(5)

Traceback (most recent call last):

  File "<pyshell#30>", line 1, in <module>

    a.pop(5)

TypeError: set.pop() takes no arguments (1 given)

>>> a.pop()

3

>>> a.pop()

100

>>> ##pop removing elements one by one

Comments

Popular posts from this blog

XAMPP, SQL BASIC COMMANDS

The Minion Game Hackerrank Solution

Arrays - DS | HackerRank Solutions