DEV Community

Cover image for Python #list #tuple #set #dictionary
Rafeek01
Rafeek01

Posted on

Python #list #tuple #set #dictionary

1. List :
It is used to store multiple items in single variable.
In list,

  • Items will be in order.
  • We can change items.
  • It allows duplicate of items .

mylist = ['dog','cat','monkey','cat']
mylist[0]='cow'
print(mylist)

Output:

['cow','cat','monkey','cat']
Enter fullscreen mode Exit fullscreen mode

2. Tuple :
It is used to store multiple items in single variable.
In tuple,

  • Items will be in order.
  • We can not change items.
  • It allows duplicate of items

mytuple = ('dog','cat','monkey','cat')
mytuple[0]='cow'
print(mytuple)

Output:

TypeError: 'tuple' object does not support item assignment
Enter fullscreen mode Exit fullscreen mode

3. Set :
It is used to store multiple items in single variable.
In set,

  • Items will not be in order.
  • We can not change items.
  • It do not allows duplicate of items -(in below example cat will be showed only once when you print myset)-.

myset = ('dog','cat','monkey','cat')
myset[0]='cow'
print(myset)

Output:

TypeError: 'set' object does not support item assignment
Enter fullscreen mode Exit fullscreen mode

4. Dictionary :
It is used to store multiple items in single variable.
In dictionary,

  • Items will be in order.
  • We can change items.
  • It do not allows duplicate of items .

mydictionary = {'dog':'moo','cat':'meow','cat': 'meow''lion':'roar'}
mydictionary['dog']="bark"
print(mydictionary)

Output:

{'dog': 'bark', 'cat': 'meow', 'lion': 'roar', 'cow': 'moo'}
Enter fullscreen mode Exit fullscreen mode

-------Thank you-------------
-------have a good day------

Top comments (0)