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
Rewrite the area.py program (shown below, or in the Creating Functions section of the tutorial) so that it has separate functions for the perimeter and area of a square, a rectangl

Python programs Every  general-purpose computer has a various detailed design, which  defines  that  the way  its program requires  to be specified is different. The "machine

Perimeter of a polygon Now, let's consider the problem of computing the length  of the perimeter of a polygon. The input is a structure of vertices,  encoded as a list of list

Write an algorithm for a program that shows the use of all 6 math functions. Write, test, and debug the program using Python. SAMPLE OUTPUT (not including author/program infor

A program to count how many files are on the file system which displays summary information regarding the total number of bytes used by all files and a breakdown of the number of b

what are the issues assciated with top-down analysis

1 Low Level Exploits 1.1 Savegames Jimmy is becoming increasingly frustrated at the computer game hes playing. He has a save right before the levels boss but he needs either more

Print vs Return Here are two different method declarations: def f1(x): print x + 1 def f2(x): return x + 1 What happens when  we call them? >>> f1(3) 4 >>

Range Very frequently, we will want to iterate through a list of integer values, often as indices.  Python gives a useful function, range, which gives lists of integers. It ma

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