• Interaction with Lists in Python - Part 1

    Interaction with Lists in Python - Part 1


    A lot of people lose sight of the fact that most programming techniques are based on the real world. Part of the reason is that programmers often use terms that other people don’t to describe these real-world objects. For example, most people would call a place to store something a box or a cupboard — but programmers insist on using the term variable. Lists are different. Everyone makes lists and uses them in various ways to perform some or the other tasks. In fact, you’re probably surrounded by lists of various sorts where you’re sitting right now as you read this book. So, this Post is about something you already use quite a lot. The only difference is that you need to think of lists in the same way Python does.


    You may read that lists are hard to work with. The reason that some people find working with lists difficult is that they’re not used to actually thinking about the lists they create. When you create a list, you simply write items down in whatever order makes sense to you. Sometimes you rewrite the list when you’re done to put it in a specific order. In other cases, you use your finger as a guide when going down the list to make looking through it easier. The point is that everything you normally do with lists is also doable within Python. The difference is that you must now actually think about what you’re doing in order to make Python understand what you want done.


    Lists are incredibly important in Python. This Post introduces you to the concepts used to create, manage, search, and print lists (among other tasks). When you complete the Post, you can use lists to make your Python applications more robust, faster, and more flexible. In fact, you’ll wonder how you ever got along without using lists in the past. The important thing to keep in mind is that you have already used lists most of your life. There really isn’t any difference now except that you must now think about the actions that you normally take for granted when managing your own lists.



    Organizing Information in an Application


    People create lists to organize information and make it easier to access and change. You use lists in Python for the same reason. In many situations, you really do need some sort of organizational aid to hold data. For example, you might want to create a single place to look for days of the week or months of the year. The names of these items would appear in a list, much as they would if you needed to commit them to paper in the real world. The following sections describe lists and how they work in more detail.


     

    Defining organization using lists


    The Python specification defines a list as a kind of sequence. Sequences simply provide some means of allowing multiple data items to exist together in a single storage unit, but as separate entities. Think about one of those large mail holders you see in apartment buildings. A single mail holder contains a number of small mailboxes, each of which can contain mail.


    ·        Tuples

    ·        Dictionaries

    ·        Stacks

    ·        Queues

    ·        Deques

    Of all the sequences, lists are the easiest to understand and are the most directly related to a real-world object. Working with lists helps you become better able to work with other kinds of sequences that provide greater func-tionality and improved flexibility. The point is that the data is stored in a list much as you would write it on a piece of paper — one item comes after another. The list has a beginning, a middle, and an end. the items will be numbered. (Even if you might not normally number them in real life, Python always numbers the items for you.)


    Understanding how computers view lists


    The computer doesn’t view lists in the same way that you do. It doesn’t have an internal notepad and use a pen to write on it. A computer has memory. The computer stores each item in a list in a separate memory location. The memory is contiguous, so as you add new items, they’re added to the next location in memory.






    In many respects, the computer uses something like a mailbox to hold your list. The list as a whole is the mail holder. As you add items, the computer places it in the next mailbox within the mail holder.

    Just as the mailboxes are numbered in a mail holder, the memory slots used for a list are numbered. The numbers begin with 0, not with 1 as you might expect. Each mailbox receives the next number in line. A mail holder with the months of the year would contain 12 mailboxes. The mailboxes would be numbered from 0 to 11 (not 12, as you might think). It’s essential to get the numbering scheme down as quickly as possible because even experienced developers get into trouble by using 1 and not 0 as a starting point at times.

    Depending on what sort of information you place in each mailbox, the mail-boxes need not be of the same size. Python lets you store a string in one mailbox, an integer in another, and a floating-point value in another. The com-puter doesn’t know what kind of information is stored in each mailbox and it doesn’t care. All the computer sees is one long list of numbers that could be anything. Python performs all the work required to treat the data elements according to the right type and to ensure that when you request item five, you actually get item five.

    In general, it’s good practice to create lists of like items to make the data easier to manage. When creating a list of all integers, for example, rather than of mixed data, you can make assumptions about the information and don’t have to spend nearly as much time checking it. However, in some situations, you might need to mix data. Many other programming languages require that lists have just one type of data, but Python offers the flexibility of using mixed data sorts. Just remember that using mixed data in a list means that you must determine the data type when retrieving the information in order to work with the data correctly. Treating a string as an integer would cause problems in your application.
  • 0 comments:

    Post a Comment

    Powered by Blogger.

    Tags

    Popular Posts