How can Python compare multiple numbers?

In Python, you can use the max() and min() functions to compare multiple numbers.

To find the largest value among multiple numbers, you can use the max() function, with the syntax:

max(num1, num2, num3, ...)

In this case, num1, num2, num3, … represent multiple numbers, which can be integers or floating-point numbers. The max() function will return the largest number among them.

To compare multiple numbers and find the smallest value, you can use the min() function, which syntax is:

min(num1, num2, num3, ...)

Similarly, num1, num2, num3, … are multiple numbers to be compared, which can be integers or floating-point numbers. The min() function will return the smallest among them.

Here is an example code comparing multiple numbers using the max() and min() functions.

num1 = 10
num2 = 20
num3 = 30

max_num = max(num1, num2, num3)
min_num = min(num1, num2, num3)

print("最大值为:", max_num)
print("最小值为:", min_num)

The output results are:

最大值为: 30
最小值为: 10
bannerAds