Francais

Francais


Home


Products


Download


Tutorials

    Python Section
  • Python Quick Reference

  • Web Ring/Links


    Contact

    if elif else

    They are the base elements to create conditional block of instructions. elif creates a new condition inside the previous if else branch. Use several elif simulates the command case or switch found in other language.

    Examples:
    Without elif With elif
    >>> if a>0:
    …     print "Positive number"
    …   else :
    …     if a== 0:
    …        print "Zero"
    …     else :
    …        print "Negative number "
    >>> if a>0:
    …     print "Positive number"
    …   elif a==0:
    …     print "Zero"
    …   else :
    …     print "Negative number"

    for

    All elements in a list can be manipulated one by one with this command. If a loop changes the list, a copy of the list must be used to iterate. To do it, add at the end of the variable's name the characters [:] (a colon between square brackets).

    Examples:

    Normal iteration Iteration with copy to modify original
    >>> list=[ 1,3,5,7 ]
    >>> for A in list :
    …     print A

    1
    2
    3
    4
    >>> print list
    [1, 2, 3, 4]
    >>> list=[ 1,2,3,4 ]
    >>> for A in list[:]:
    …      print A
    …      list.append(A+4)

    1
    2
    3
    4
    >>> print list
    [1, 2, 3, 4, 5, 6, 7, 8 ]



    Previous Page
    8/13

    Next Page