Python Program to sort numbers from 1 to 50
Python Program to sort numbers from 1 to 50
In this section, you will learn how to write a Program to sort numbers from 1 to 50 in Python Programming.
In this program, the given numbers sorted in ascending and descending order, i.e. the elements can be arranged in particular order from smaller to larger and from larger to smaller.
Syntax:
list.sort()
Let's take a look at code for Python Program to sort numbers from 1 to 50.
Python code:
a = int(input("enter number of terms:"))
b = []
i = 0
while(i<a):
i = i + 1
b.append(i)
b.sort()
print("numbers in ascending order")
print(b)
b.sort(reverse=True)
print("numbers in descending order")
print(b)
Output:
enter number of terms:50
numbers in ascending order
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
numbers in descending order
[50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4,3,2,1]
Recommended Post:
Python Program to check vowel or Consonant
Program to check character is alphabet or not in Python
Python to check whether the year is leap or not
Python to check if number is Positive, Negative or 0
Python Program to solve Quadratic equation
Program to generate Random numbers in Python
Program to swap two numbers in Python
Program to Add two numbers in Python
Python to convert decimal to binary, octal and hexadecimal
Python Program to find large number among three numbers
How to find absolute value of a number in Python
Program to add two binary numbers using bin function