SoFunction
Updated on 2024-11-14

Python input a number of positive integers, ascending order output

Python input a number of positive integers, ascending order output

Broad Format:

Enter the number of lines, separated by spaces:

When lined up, they are still separated by spaces, and the last number should have a space as well

x=list(map(int,input().split()))
y= sorted(x)
for i in range(len(y)):
    print(y[i], end=' ')

Using the sort built-in function, the second paragraph can also be written as (), the value of the x list changes directly

The number of output lists is also possible:

for i in x: #Note the semicolon
 
    print(i,end=' ')#endThere's a space in there.

Python inputs a number of numbers, both floating point and integer, and outputs them in ascending order.

a=list(map(eval,input().split())) #eval is a built-in function in Python that, among other things, returns the result of an expression passed into a string.
 
y=sorted(a) #sorted is a built-in function, and will be sorted in ascending order by default if no special requirements are made.
 
for i in range(len(y)-1) : #Note that for requires ":"
 
            print(y[i],end=' ') #print must be a few spaces, so that it is within the for; end to add space in the
 
print(y[-1])     #-1Representing the penultimate number

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.