basic question about random array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeremy78517
    New Member
    • May 2012
    • 1

    basic question about random array

    Hi, I just started learning Python, here is an assignment I wrote.
    Code:
    numbers = [54, 5, 81, 70, 36, 3, 44, 13, 99, 60, 51, 26, 73, 18, 9, 82, 83, 67, 63, 53, 15, 9, 29, 68, 45, 21, 33, 10, 5, 52, 85, 52, 57, 99, 84, 42, 31, 74, 56, 31, 5, 79, 42, 13, 15, 35, 73, 34, 40, 98, 7, 70, 70, 2, 82, 21, 62, 19, 50, 33, 26, 84, 28, 49, 25, 67, 80, 12, 82, 4, 67, 19, 67, 57, 83, 67, 22, 17, 36, 77, 6, 59, 3, 4, 39, 90, 24, 28, 78, 7, 46, 8, 1, 47, 91, 20, 0, 56, 65, 75]
    
    for X in numbers :
        if (X >= 30) and (X < 60):
            text = " age- " + str(X)
            print text
    I would like to print the values in order from smallest number to largest number. I know it's probably very very basic but I really just could not solve it. Thanks for all your help.
    Last edited by bvdet; May 9 '12, 01:18 PM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    List method sort() will sort the list in place.
    Code:
    >>> numbers = [54, 5, 81, 70, 36, 3, 44, 13, 99, 60, 51, 26, 73, 18, 9, 82, 83, 67, 63, 53, 15, 9, 29, 68, 45, 21, 33, 10, 5, 52, 85, 52, 57, 99, 84, 42, 31, 74, 56, 31, 5, 79, 42, 13, 15, 35, 73, 34, 40, 98, 7, 70, 70, 2, 82, 21, 62, 19, 50, 33, 26, 84, 28, 49, 25, 67, 80, 12, 82, 4, 67, 19, 67, 57, 83, 67, 22, 17, 36, 77, 6, 59, 3, 4, 39, 90, 24, 28, 78, 7, 46, 8, 1, 47, 91, 20, 0, 56, 65, 75]
    >>> numbers.sort()
    >>> numbers
    [0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 6, 7, 7, 8, 9, 9, 10, 12, 13, 13, 15, 15, 17, 18, 19, 19, 20, 21, 21, 22, 24, 25, 26, 26, 28, 28, 29, 31, 31, 33, 33, 34, 35, 36, 36, 39, 40, 42, 42, 44, 45, 46, 47, 49, 50, 51, 52, 52, 53, 54, 56, 56, 57, 57, 59, 60, 62, 63, 65, 67, 67, 67, 67, 67, 68, 70, 70, 70, 73, 73, 74, 75, 77, 78, 79, 80, 81, 82, 82, 82, 83, 83, 84, 84, 85, 90, 91, 98, 99, 99]
    >>>

    Comment

    Working...