Python Array Example - Insert and sort the array items
Published November 16, 2021In this Python array examples Ask the user to Enter five integeres, then Store them in an array. After that sort the list and display them in reveres order.
Let's write the program for the above
from array import * nums =array('i',[]) for i in range (0,5): num = int(raw_input("Enter a Number: ")) nums.append(num) nums = sorted(nums) nums.reverse() print(nums) |
- In the above example we created array as nums
- Then looping 5 times to ask the user to eneter numbers to array by nums.append(num)
- Then sort the created array with sorted() method sorted(nums)
- Then reverse the array with array reverse() method
- Finally Print the array
Output:
Enter a Number: 21 |
Article Contributed By :
|
|
|
|
871 Views |