Skip to main content

Posts

Showing posts with the label LISTS

List Loop

Looping in a list is used to access every element in the list. There are two tyes of looping. Normal for-in loop as specified in line 8 & 9. Range of length loop as specified in line 13 & 14. Try Yourself: #List looping example   colors_list = ["red", "green", "yellow", "violet"] months_list =  ["Jan", "Feb", "Sep", "Mar"]   print "Colours in the list: " # Normal Looping for i in range(len(colors_list)):     print(colors_list[i])   # Range Looping print "Months in the list: " for month in months_list:     print(month)         The lines 8 & 9 are like normal for loop; where each element in the list are accessed using the index value. The lines 13 & 14 are special case; where every elements in the list of months_list is taken. The iteration stops when all the elements in the list are exhausted.

LISTS in Python

One of the basic data structures in Python is sequences. There are six built-in types of sequence - strings, Unicode strings, lists, tuples, buffers, and xrange objects. The most common are lists and tuples.  A list is an ordered set of values, where each value is identified by an index. The values in the list are known as elements. Each element of a sequence is assigned with a number - its position or index. The first index is zero, second is one and so forth. Creation and Accessing List: The list can be created by putting values separated by commas between square brackets []. List is one of the most freuently used and very versatile data type used in Python.  Syntax for List:                <List_name> = [<Value_1>,<Value_2>,........,<Value_N>] NOTE:  Nested lists are also acceptable. List Operations: Two operations + and * is allowed in list. + for concatenation ...