list and tuple in python

tuple and set in python

list and tuple in python are both collection data types those are used to hold multiple values in a single variable.

tuple

a collection data type used to store or hold multiple data or values.

tuple is ordered, unchangeable, and allows duplicate values.

syntax:

var_name=(value1,value2,value3,....)


example:

data=("sam", "Delhi",34,110034,"sam")


#how to a tuple

print(data)


#how to print the first element of the tuple

print(data[0])


#how to print first 2 elements of the tuple

print(data[0:3])


methods of the tuple:

1. count 

    #how to print the occurrence of a specific element in a tuple?

     print(data.count("sam"))

    output:

    2


2. index

    #how to print the index of an element in a tuple?

     print(data.index(110034))

    output:

    3



set

a set is the opposite of a list. set is a collection data type used to store or hold multiple values.

set is unordered, unchangeable, and doesn't allow duplicate values.   

syntax:

var_name={value1,value2,...}


data={3,4,3,4,34,5,"sam",True}

print(data)

#print(data[0]) this will throws an error because set in not subscriptable


output:

{True, 34, 3, 4, 5, 'sam'}

there are several methods to handle the data of set elements :

1. add

add method is used to add an element to a random position in a set.

example:

data={3,4,3,4,34,5,"sam",True}

print(data)

data.add("hello")

print(data)

output:
{True, 34, 3, 4, 5, 'sam'}
{True, 34, 3, 4, 5, 'hello', 'sam'}

2. pop

pop is used to delete a random element from a set.

example:

data={3,4,3,4,34,5,"sam",True}

print(data)

data.pop()

print(data)

output:
{True, 34, 3, 4, 5, 'sam'}
{34, 3, 4, 5, 'sam'}

3. discard

discard is used to delete a specific element from a set. if the element does, not exists ,it will not give an error.

example:

data={3,4,3,4,34,5,"sam",True}

print(data)

data.discard("sam")

print(data)

data.discard(43)

print(data)

output:

{True, 34, 3, 4, 5, 'sam'}
{True, 34, 3, 4, 5}
{True, 34, 3, 4, 5}


4. remove

discard is used to delete a specific element from a set. if the element does, not exists ,it will give an error.

example:

data={3,4,3,4,34,5,"sam",True}

print(data)

data.remove("sam")

print(data)

data.remove(43)

print(data)


output:

{True, 34, 3, 4, 5, 'sam'}

{True, 34, 3, 4, 5}

KeyError: 43



5. clear

clear method used to delete all elements from a set.


example:

data={3,4,3,4,34,5,"sam", True}

print(data)

data.clear()

print(data)

output:

{True, 34, 3, 'sam', 5, 4}

set()


6. update

The update method updates the current set, by adding items from another iterable.

example:

data={3,4,3,4,34,5,"sam",True}

print(data)

data.update([235,234,3])

print(data)

output:

{True, 'sam', 3, 4, 5, 34}

{True, 'sam', 3, 4, 5, 34, 234, 235}


8. union

union method returns a set that contains all elements of the original and specified iterable.

example:

data={3,4,3,4,34,5,"sam",True}

print(data)

new_data=data.union((3,4,667,32))

print(new_data)

output:

{'sam', True, 34, 3, 4, 5}

{'sam', True, 34, 3, 4, 5, 32, 667}


9. intersection

intersection method returns the common elements from original and other specified iterable.


example:

data={3,4,3,4,34,5,"sam",True}

print(data)

new_data=data.intersection([3,4,667,32])

print(new_data)

output:

{True, 34, 3, 4, 5, 'sam'}

{3, 4}


10. difference

difference method returns a set of elements from calling a set object those are uncommon between the set and a specified iterable.

example:

data={3,4,3,4,34,5,"sam",True}

print(data)

new_data=data.difference([3,4,667,32])

print(new_data)


output:

{True, 34, 3, 4, 5, 'sam'}

{True, 34, 5, 'sam'}



0 Comments