
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Max and Min in Python
Finding maximum and minimum values from a given list of values is a very common need in data processing programs. Python has these two functions which handle both numbers and strings. We will see both the scenarios in the below examples.
Numeric Values
We take a list of numeric values which has integers and floats. The functions work appropriately to give both the max and the min values.
Example
x=[10,15,25.5,3,2,9/5,40,70] print("Maximum number is :",max(x)) print("\nMinimum number is :",min(x))
Output
Running the above code gives us the following result:
Maximum number is : 70 Minimum number is : 1.8
String Values
The string values are compared lexicographically to get the correct result. Internally the ascii values of the characters are compared.
Example
x=['Mon','Tue','Wed','Thu','Fri','Sat','Sun'] print("Maximum number is :",max(x)) print("\nMinimum number is :",min(x))
Output
Running the above code gives us the following result:
Maximum number is : Wed Minimum number is : Fri
Advertisements