Start Working with Python : List and Dictionary

Like all other programming languages python also have similar facilities of data structure, file handling and string handling etc.

Data structure in Python:

There are 3 types of variables in python

  1. Numbers : 1,2,3 etc
  2. Strings :    “ABC” , “CDE” etc
  3. Booleans : TRUE and FALSE

LIST:

List is a data structure which is similar to array to store the data (character/ number) sequentially.

LISTNAME = [] will create a blank list.

LIST functions :

Append : to append an item to the list.

Syntax : LISTNAME.append(“item1”)

Remove : to remove an existing item from the list.

Syntax : LISTNAME.remove(“item1”)

Here is the example of LIST in python:

To print whole list just run print command like: Print LISTNAME

Python_list

Dictionaries:

The dictionary allows you to associate one piece of data (a “key”) with another (a “value”). The concept came from real-life dictionaries. Instead of straight bracket (‘[]’)in LIST here we will use curly (‘{}’) brackets to define a dictionary.

You can create a dictionary with {}

Dict1 = {} ß will create a blank dictionary named as Dict1.

To add entry in the dictionary you need an index like below

foods[“banana”] = “A delicious and tasty treat!”

foods[“dirt”]   = “Not delicious. Not tasty. DO NOT EAT!”

Here “foods” is the dictionary name. banana and dirt are the indices which have assigned to some description (entry).

If you want to print the whole dictionary (which will obviously give an unreadable output) use below command: print foods ß foods is the dictionary name

But in contrast if you want to get value (description) for particular index below is the command

foods[“banana”]  ß where ‘banana’ is the index

to remove any element from dictionary :

del foods[“dirt”]

Python_dictonary

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

3 thoughts on “Start Working with Python : List and Dictionary

Leave a Reply

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