On Sat, 17 Nov 2012 10:01:01 -0800, chinjannisha wrote:

> Hi I had one doubt.. I know very little bit of python .I wanted to know
> when to use list,tuple,dictionary and set? Please reply me asap

Use a list when you want a list of items that should all be treated the 
same way:

list_of_numbers = [1, 3, 5.1, 2, 6.5]

total = sum(list_of_numbers)


or when you need a collection of items where the order they are in is 
important:

winners = ['george', 'susan', 'henry']  # 1st, 2nd, 3rd place

print('The winner is:', winners[0])


Use a tuple when you want a collection of items that mean different 
things, a bit like a C struct or Pascal record:

a = (23, "henry", True, 'engineer')
b = (42, "natalie", False, 'manager')
c = (17, "george", True, 'student')


Use a dict when you need a collection of key:value mappings:

config = {'name': 'fred', 'pagesize': 'A4', 'verbose': True, 'size': 18}
address = {'fred': 'f...@example.com', 'sally': 'sally_sm...@example.com'}

if config['verbose']:
    print('sending email...')
send_email_to(address['fred'], "Subject: Hello")


Use a set when you want to represent a collection of items and the order 
is not important:

failed = {'fred', 'tom', 'sally'}  # set literal syntax in Python 3 only
# use set(['fred', 'tom', 'sally']) in Python 2

if 'george' in failed:
    print('George, you have failed!')



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to