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
write a program that creates s dictionary containing course

how do you make an if and else statment work in pytho?

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

You should use for whenever you can, because  it creates  the structure of your  loops clear. Sometimes, however, you require to do an operation various times, but you don't want t

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

Use the function to compute and print the sum of first n prime numbers.

Programming models Just  as there  are several methods for organizing entire  software systems, there  are  different strategies for formally expressing computational processe

Define a function that draws a pentagon of a size you provide when you call the function. Include a ''''''docstring'''''' as the first line of the function''s body — it tells the p

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

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