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
Environments in Python Generally, Python establishes the following binding environments: 1.  builtin     : the mother of all environments: it contains the de?nitions of

Programs and Data Object-oriented programming is a popular way  of managing programs, which  groups together data  with  the procedures that  works on them,  thus  facilitatin

#ques Write the pseudocode (use a word processor please) and the Python 3.0 program for the following problem. A monkey is being fed some food. Read in the starting weight in lbs:o

This assignment involves writing a program to solve word search puzzles. For example in the following word grid it is possible to find the words active, stock, ethernet and java. N

Structured data We will often  want  to work  with  large  groups of data.   Rather  than  providing  each  number its own  value,  we need  to manage the data  into natural s

Structured assignment Once  we have  tuples and lists, we may use  a nice trick  in assignment expression, based  on the packing and unpacking of tuples. >>> a, b, c = 1, 2

Displaying random geometric graphs in an uniform square, unit disk, dense rim unit disk, and uniform sphere

Every programming language has to have  some method for indicating grouping of operations. Here is how you execute an if-then-else structure in Java:   if (s == 1){ s = s

Worked example 1   Let's examine what  happens when  we compute the following Python code:   def p(x, y): z = x*x - y return z + 1/z   >>> p(1.0, 2.0) -2.0

Analytical models Analytical tools  are very important.  It may  be hard  to check  the  correctness of a machine by trying it in several possible  initial  conditions with  al