Lists, Python Programming

 

Python has  a built-in list data  structure that  is easy  to use  and  incredibly convenient.  So, for that point, you can say

>>> y = [1, 2, 3]

>>> y[0]

1

>>> y[2]

3

>>> y[-1]

3

>>> y[-2]

2

>>> len(y)

3

>>> y + [4]

[1, 2, 3, 4]

>>> [4] + y

[4, 1, 2, 3]

>>> [4,5,6] + y

[4, 5, 6, 1, 2, 3]

>>> y

[1, 2, 3]

 

A list is written using  square brackets, with  whole code separated by commas. You may get components out  by specifying the  index  of the  element you  want  in square brackets, but  note  that,  like for character or string, the indexing initiate with  0.  Note  that  you  can index  elements of a list starting from  the initial (or zeroth) one (by using integers), or initiating from the last one (by using negative integers). You can add elements to a list using '+', taking  advantage of Python operator overloading. Note that this method does not modify  the original list, but creates  a new one.

Another useful  thing  to know  about  lists is that  you can make  slices of them.  A part of a structure is sublist;  you may get the basic idea from examples.

 

>>> b = range(10)

>>> b

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> b[1:]

[1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> b[3:]

[3, 4, 5, 6, 7, 8, 9]

>>> b[:7]

[0, 1, 2, 3, 4, 5, 6]

>>> b[:-1]

[0, 1, 2, 3, 4, 5, 6, 7, 8]

>>> b[:-2]

[0, 1, 2, 3, 4, 5, 6, 7]

 

Posted Date: 8/9/2012 7:20:13 AM | Location : United States







Related Discussions:- Lists, Assignment Help, Ask Question on Lists, Get Answer, Expert's Help, Lists Discussions

Write discussion on Lists
Your posts are moderated
Related Questions
Basics of python-introduction Python is designed for easy interaction between a user and the system. It goes with  an inter­ active function  known as  a shell or listener.  T

how to make a python programme for van der waals equation of state with surface discontinouty

Python Programs You must submit the source code and samples of output for each program.Please do not provide python file (eg. Filename.py). Copy all source code to one word fi

Finally! After years of adding unimportant sports, the IOC has finally added Shoe Tying to the Olympics. And you’ve been selected to write the software to handle the judging of t

from urllib2 import urlopen var= ('' response">http://placekitten.com'') response = kittens.read(body) body = response[559:1000] print ''Kittens:'', response

Python  Style Software  engineering courses  often  provide very  rigid  guidelines on the style of programming, generally the appropriate value of indentation, or what  to us

Imagine a "particle" located on the centre square of a two-dimensional grid of dimensions 11 by 75. The particle can only move one square at a time, either up, down, left, or right

Procedure calls When you compute an expression of the form ( ,  ..., )   the Python interpreter treats  this as a procedure call. It will be simpler to talk about

Write an algorithm for the sum of the given series 1,-1/2,1/4,-1/8.....

Interacting with the environment Computer systems have  to communicate with  the world around them,  getting information about  the external world, and  taking  actions  to cha