GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python: Sort three integers without using conditional statements and loops - w3resource

Python: Sort three integers without using conditional statements and loops

Last update on May 17 2025 05:43:28 (UTC/GMT +8 hours)


Sort Three Numbers

Write a Python program to sort three integers without using conditional statements and loops.

Pictorial Presentation:

Sort three integers without using conditional statements and loops


Sample Solution:

Python Code:

# Prompt the user to input three integers and convert them to variables x, y, and z.
x = int(input("Input first number: "))
y = int(input("Input second number: "))
z = int(input("Input third number: "))

# Find the minimum value among x, y, and z and store it in variable a1.
a1 = min(x, y, z)

# Find the maximum value among x, y, and z and store it in variable a3.
a3 = max(x, y, z)

# Calculate the middle value (not the minimum or maximum) by subtracting a1 and a3 from the sum of x, y, and z.
a2 = (x + y + z) - a1 - a3

# Print the numbers in sorted order (a1, a2, a3).
print("Numbers in sorted order: ", a1, a2, a3)

Sample Output:

Input first number: 2                                                                                         
Input second number: 4                                                                                        
Input third number: 5                                                                                         
Numbers in sorted order:  2 4 5 

Flowchart:

Flowchart: Sort three integers without using conditional statements and loops.


For more Practice: Solve these Related Problems:

  • Write a Python program to sort four numbers without using loops or conditional statements.
  • Write a Python program to swap the values of three variables without using a temporary variable.
  • Write a Python program to sort three floating-point numbers in ascending order without using built-in functions.
  • Write a Python program to sort three numbers using bitwise operations.

Go to:

  • Python Basic Exercises Home ↩
  • Python Exercises Home ↩

Previous: Write a Python program to calculate the sum of the digits in an integer.
Next: Write a Python program to sort files by date.

Python Code Editor:

 

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.