Programming in Python (Part-5)

0 Comments

Collections in Python are containers that are used to store collections of data, for example, list, dict, set, tuple etc. These are built-in collections. Several modules have been developed that provide additional data structures to store collections of data.

So far we have learned about how to save a value inside a variable, but what if we have to store multiple values inside a variable.

Well the answer is we have to use collections for that. It has 4 main types

List:

  • It can store any data type value in it. It can be changed

Tuple:

  • It can store any data type value in it. It can’t be changed

Sets:

  • Collection of no duplicate members. It can’t be indexed

Dictionary:

  • Collection which can be change and indexed as well but with no duplicate members

LIST:

  • Use square brackets [] to define list
  • List is mutable… it can be change after we define
  • List can hold values of different types
  • It can hold duplicate values as well

Code:

lst = ["one", 2, "three"]
print(lst)

TUPLE:

  • Use square brackets () to define tuple
  • Tuple is immutable… it can’t be changed after it defined
  • Tuple can hold values of different types
  • Tuple can hold duplicate values as well

Code:

tup = ('BMW', 'Suzuki', 'Nissan',2)
print(tup)

SETS:

  • Use square brackets {} to define sets
  • It can be changed after initializing
  • It has no duplicate members and It has no indexes
  • It can hold values of different types

Code:

set = {"Nissan", "BMW", "Honda",2}
print(set)

DICTIONARY:

  • Use square brackets {} to define sets
  • It can be changed after initializing and it has indexes in form of Keys as well
  • Two terms to remember

                 ->Key
                ->Value

Every item requires a key and its values

  • It has no duplicate members
  • It can hold values of different types

Code:

dic = {'Name': 'Uzair', 'Age': 24, 'Education': 'Masters'} 
print(dic) 

You can also check and learn this part through video tutorial as well https://www.youtube.com/watch?v=vFh_3cBcYBQ

You can find code with examples as well on my github page…
https://github.com/uzairaj/Python_Programming

Thank you for reading…


Leave a Reply

Your email address will not be published. Required fields are marked *