• Interaction with Lists in Python - Part 2

    Interaction with Lists in Python - Part 2

     

    Creating Lists


    As in real life, before you can do anything with a list, you must create it. As previously stated, Python lists can mix types. However, it’s always a best practice to restrict a list to a single type when you can. The following steps demonstrate how to create Python lists.

     Open a Python Shell window.

    You see the familiar Python prompt.

     Type List1 = [“One”, 1, “Two”, True] and press Enter.

    Python creates a list named List1 for you. This list contains two string values (One and Two), an integer value (1), and a Boolean value (True). Of course, you can’t actually see anything because Python processes the command without saying anything.


    Notice that each data type that you type is a different color. When you use the default color scheme, Python displays strings in green, numbers in black, and Boolean values in orange. The color of an entry is a cue that tells you whether you have typed the entry correctly, which helps reduce errors when creating a list.

     Type print(List1) and press Enter.

    You see the content of the list as a whole. Notice that the string entries appear in single quotes, even though you typed them using double quotes. Strings can appear in either single quotes or double quotes in Python.

     Type dir(List1) and press Enter.

    Python displays a list of actions that you can perform using lists,. Notice that the output is actually a list. So, you’re using a list to determine what you can do with another list.

    As you start working with objects of greater complexity, you need to remember that the dir() command always shows what tasks you can perform using that object. The actions that appear without underscores are the main actions that you can perform using a list. These actions are the following:

    ·        append

    ·        clear

    ·        copy

    ·        count

    ·        extend

    ·        index

    ·        insert

    ·        pop

    ·        remove

    ·        reverse

    ·        sort

     Close the Python Shell window.


    Accessing Lists


    After you create a list, you want to access the information it contains. An object isn’t particularly useful if you can’t at least access the information it contains. The previous section shows how to use the print() and dir() functions to interact with a list, but there are other ways to perform the task, as described in the following steps.


     Open a Python Shell window.

    You see the familiar Python prompt.

     Type List1 = [“One”, 1, “Two”, True] and press Enter. Python creates a list named List1 for you.


     Type List1[1] and press Enter.

    You see the value 1 as output. The use of a number within a set of square brackets is called an index. Python always uses zero-based indexes, so asking for the element at index 1 means get-ting the second element in the list.




     Type List1[1:3] and press Enter.

    You see a range of values that includes two elements. When typing a range, the end of the range is always one greater than the number of elements returned. In this case, that means that you get elements 1 and 2, not elements 1 through 3 as you might expect.

     Type List1[1:] and press Enter.

    You see all the elements, starting from element 1 to the end of the list. A range can have a blank ending number, which simply means to print the rest of the list.


     Type List1[:3] and press Enter.

    Python displays the elements from 0 through 2. Leaving the start of a range blank means that you want to start with element 0.

    7. Close the Python Shell window.

    Even though it’s really confusing to do so, you can use negative indexes with Python. Instead of working from the left, Python will work from the right and backward. For example, if you have List1 = ["One", 1, "Two", True] and type List1[-2], you get Two as output. Likewise, typing List[-3] results in an output of 1. The rightmost element is element -1 in this case.


    Looping through Lists


    To automate the processing of list elements, you need some way to loop through the list. The easiest way to perform this task is to rely on a for state-ment, as described in the following steps.

     Open a Python File window.

    You see an editor in which you can type the example code.

     Type the following code into the window — pressing Enter after each line:

    List1 = [0, 1, 2, 3, 4, 5]

    for Item in List1: print(Item)

    The example begins by creating a list consisting of numeric values. It then uses a for loop to obtain each element in turn and print it onscreen.

     Choose RunRun Module.


    You see a Python Shell window open. The output shows the individual values in the list, one on each line.
  • 0 comments:

    Post a Comment

    Powered by Blogger.

    Tags

    Popular Posts