Roshan Book

My Tech Notebook

Python tutorial 14- Tuples and Lists


Tuple

t=1,2,3 or t=tuple(range(25))

t[-1] returns last element

len[t] gives length of the tuple

min[t] gives minimum value of tuple

max[t] gives maximum value of the tuple

Tuple is deifned by ,

 

Lists are created with square bracket

same methods as tuple

You can also change things

x=list(range(25))

to change a position x[10]= 45

Why to use tuple

smaller class, simpler, faster, don’t allow you to change stuff

Operations in tuples and lists

t.count()

t.index(5)

list_x.append(100) – put 100 at the end of it

len(x) – length og lust x

x.extend(range(20)) – range 20 added to end of list x

x.insert(0,25) – insert 25 at positon 0

x.remove(12) – remove element with value 12

del x[12] – delete index 12

x.pop() – remove last value of the list and return it

x.pop(0) – pop from beginning

similarly x.pop(index) – remove that index value

# Dictionaries

Python verison of associative arrays

Created with curly braces

d={‘one’:1,’two’:2}

another way

d=dict(one=1,two=2)

x=dict(four=2,five=5)

you can say d=dict(one=1,two=2,**x)

This addds dict x into dict d

# to iterate

for k in d : print(k ) – prints all keys

for k, v in d.items() : print(k,v) – gives key and value

x.get(‘three’,’not found’) – gives three key value or give some default value if not there

del x[‘four’] – del key four

x.pop(‘five’) – remove and returns index five

You could store dic , tuple inside dictionary

8 responses to “Python tutorial 14- Tuples and Lists

  1. subliminal messaging October 19, 2011 at 8:50 am

    There are definitely a whole lot of particulars like that to take into consideration. That could be a great level to convey up. I provide the thoughts above as common inspiration but clearly there are questions just like the one you bring up the place the most important factor shall be working in honest good faith. I don?t know if best practices have emerged around things like that, however I’m sure that your job is clearly recognized as a good game. Both boys and girls feel the affect of only a second’s pleasure, for the remainder of their lives.

  2. Daniel Tan October 31, 2011 at 7:47 am

    Have you heard of this new wordpress plugin from online marketing guru Daniel Tan known as SEO Pressor ? I have already been using it for the past couple weeks and I already have 7 of my latest blogs on the first page of google for very competetive terms! I recommend you try it out here: SEOPressor

  3. Mobile Money Machines oa November 2, 2011 at 6:28 am

    How’s things, I see a 504 Gateway Timeout error when I browse your website. This sometimes indicates the server did not get a response. I figured yuo may want to know. Regards Jim

  4. Mobile Money Machines ue November 2, 2011 at 8:41 am

    What’s up, I receive a 504 Gateway Timeout error when I view this page. This usually indicates the webhost did not receive a response. I figured yuo may like to know. Thanks Jim

  5. Binary Options Trading System STRIKER9 November 2, 2011 at 5:07 pm

    How’s things, I get a 504 Gateway Timeout error when I view this website. This usually indicates the webhost did not get a response. I figured yuo may want to know. Best wishes Mike

  6. Pingback: Django revision 2 « Roshan Book

  7. moncler jackets for men December 16, 2011 at 11:13 am

    Hi there,effectively-penned web-site publish. Infos are fairly usefull and saved me many time that we could expend in a thing else as a replacement of hunting Im waiting around for even more, bye

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: