Results 1 to 5 of 5

Thread: How to use Listings and Tuple in Python?

  1. #1
    Join Date
    Sep 2010
    Posts
    19

    How to use Listings and Tuple in Python?

    I have recently started learning the Python programming language in deep. Now I am stuck at the listings and tuple commands that are used in Python. I am just having an idea that it is part of expressions that are used python (I don't know more than this, that's why I am here). I am expecting that some of you members must be knowing about the python language and will provide some useful information for me. But important thing now for me, is the using Listings and Tuple in Python.

  2. #2
    Join Date
    Apr 2008
    Posts
    242

    Re: How to use Listings and Tuple in Python?

    I would like to discuss about the listings. As we have sequences of characters, we could have generated sequences of objects of different types. There is a primitive type in python that does the trick, called the list. In other languages like Pascal or Java, the lists are not primitive types. In Python. And as such it can be expressed with constants. Can I assign a list to any variable, simply as follows:
    x = [1, 'two', 3.0]
    In this way, I got a list, which contains the first element as an integer, the string as the second two, and as the third element of the floating-point number 3. The lists similar enough to the strings in the sense that you can access the various elements simply by using an index. So we can pull the string 'two from the list with the syntax:
    >>> X [1]
    'Two'

  3. #3
    Join Date
    Apr 2009
    Posts
    64

    Re: How to use Listings and Tuple in Python?

    I would like to comment more on the "afidelino" post. Along with that I am trying to explain in bit deep, taking same example. I am hoping that will help you to understand more. Pay attention to the fact that the indices of a sequence starting from scratch, and then to extract the second element must use the index 1. This has nothing to do with its content, which is confusing in the example 'two. Lists under the rules of the sequence (slice) that we have seen with strings. So for example we can take the last two elements:
    Code:
    >>> X [1:3]
     ['Two', 3.0]
     >>> X [-2:]
     ['Two', 3.0]
    Similarly we can get the length of the list using function len (). However, unlike strings, lists are mutable. We can then replace an element simply by assigning the value:
    Code:
    >>> X [1] = 2L
     >>> X
     [1, 2L, 3.0]
    What is also true for the sequence below:
    Code:
    >>> X [-2:] = ['two', 3, 4.0]
     >>> X
     [1, 'two', 3, 4.0]
    For example we can see that a slice can be assigned and replaced by another list, which may also have different lengths.

  4. #4
    Join Date
    Nov 2008
    Posts
    89

    Re: How to use Listings and Tuple in Python?

    You must have got an ideas about the lists, we also have tuples in python. They behave very much like the lists, but have one thing to be immutable. Or once it is created, a tuple can not be changed. To build a tuple, it is sufficient to specify a sequence of objects to put in a row, separated by commas. Here is a sample, including the demonstration of its immutability.
    >>> A = 2.3
    >>> A [0]
    2
    >>> A [0] = 1
    TypeError: object does not support item assignment
    A special case of tuple is composed of a single element. To distinguish one item from this case, using syntax is not particularly beautiful: the comma is "pending". That is, an object followed by a comma, is a tuple of one element.

  5. #5
    Join Date
    Dec 2008
    Posts
    87

    Re: How to use Listings and Tuple in Python?

    Tuples are exploited well by itself (they are convenient collections of elements) and especially for multiple assignment through the so-called "packing" and "unpacking". I am providing an example so that you can understand much better :
    Code:
    >>> X = (4.5)
     >>> A, b = x
     >>> A
     4
     >>> B
     5
    We first of all the tuple assigned to a single variable, and this is "packing". Then we put a tuple of variables on the left of an assignment. That is, where it normally is a single variable, we asked a series of variables separated by commas. Technically it is just a tuple used an assigned value. In practice, in this case, the assignment operator makes the 'unpacking': take each element of the tuple assigned, and assign to variables in the tuple "assigning". Usually, the greater usefulness of this mechanism is to deploy across multiple variables the results of a procedure or function call.

Similar Threads

  1. Manage wireless networks has no listings
    By TheHibiscus in forum Networking & Security
    Replies: 4
    Last Post: 14-01-2012, 11:54 AM
  2. How to insert value into tuple?
    By MKAIF in forum Software Development
    Replies: 5
    Last Post: 22-02-2010, 07:20 PM
  3. What is tuple variables in SQL?
    By Shophia_D in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 12:51 PM
  4. SQL problem if tuple is already in the database
    By Macbeth in forum Software Development
    Replies: 4
    Last Post: 17-11-2009, 09:02 PM
  5. Download Python 3.0 / Python 3000
    By Amaresh in forum Software Development
    Replies: 6
    Last Post: 24-02-2009, 09:28 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,290,479.04686 seconds with 16 queries