Francais

Francais


Home


Products


Download


Tutorials

    Python Section
  • Python Quick Reference

  • Web Ring/Links


    Contact

    Function

    Predefined

    len()

    This function returns the number of element contains in a string or a list.

    Examples :

    String List
    >>>A=""
    >>>print len(A)
    0
    >>>A="toto"
    >>>print len(A)
    4
    >>>A=[]
    >>>print len(A)
    0
    >>>A=[1, 2, 3, 4]
    >>>print len(A)
    4

    range()

    This function creates a list with 3 parameters. The first contains the first number of the list, the second contains the last number of the list and the last is the increment to use between each number generated.

    Examples :
    >>>range(0,10)
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
    >>>range(5)
    [0, 1, 2, 3, 4 ]
    range(10,40,5)
    [10, 15, 20, 25, 30, 35 ]
    This function, used with the len() function, can create a loop to travel all elements of a list.

    Examples :

    Iteration with range() and len() Simple iteration
    >>>list=[1, 2, 3, 4]
    >>>forindex in range(len(list)) :
    ...   print index, "=", list[index]
    ...
    0 = 1
    1 = 2
    2 = 3
    3 = 4
    >>>list
    >>>index=0
    >>>whileindex<len(list) :
    ...  print index, "=", list[index]
    ...  index+=1
    ...
    0 = 1
    1 = 2
    2 = 3
    3 = 4

    split()

    This function splits a character string in a list of words. By default the separators are the space and the tabulation. All contiguous separators characters are removed. The maximum number of split is give by the second optional parameter.

    Exemples :
    split() split() with separator
    >>> a="This sentence, becomes,, a\tlist"
    >>> b=a.split()
    >>> print b
    ['This', 'sentence,', 'becomes,,', 'a', 'list' ]
    >>> a="This sentence, becomes,, a\tlist"
    >>> b=a.split(",")
    >>> print b
    ['This sentence', ' becomes', 'a\tlist' ]
    split() with string as separator split() with maximum
    >>> a="This sentence, becomes,, a\tlist"
    >>> b=a.split(",,")
    >>> print b
    ['This sentence, becomes', ' a\tlist' ]
    >>> a="This sentence, becomes,, a\tlist"
    >>> b=a.split(None,3)
    >>>print b
    ['This', 'sentence,', 'becomes,, a\tlist' ]

    splitlines()

    This function splits a text in list of lines.

    Exemple :
    splitlines()
    >>> a="Line1-a b c d e f\nLine2- a b c\n\nLine4- a b c d"
    >>> b=a.splitlines()
    >>> print b
    ['Line1- a b c d e', 'Line2- a b c', '', 'Line4- a b c d' ]



    Previous Page
    10/13

    Next Page