Finding Minimum and Maximum
Part of the Logic & Flow section of Coddy's Python journey — lesson 47 of 78.
Python offers built-in functions to find the minimum and maximum values in a collection of data, such as a list or a tuple. These functions, min() and max(), are straightforward to use and can be applied to various data types, including numbers and strings.
Finding the Minimum Value:
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
smallest = min(numbers)
print(smallest)
# Output: 1In this example, the min() function finds the smallest number in the numbers list.
Finding the Maximum Value:
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
largest = max(numbers)
print(largest)
# Output: 9Here, the max() function finds the largest number in the numbers list.
Working with Strings:
The min() and max() functions can also be used with strings. In this case, they compare the strings based on their lexicographical order (i.e., the order they would appear in a dictionary).
words = ["apple", "banana", "cherry"]
smallest_word = min(words)
largest_word = max(words)
print(smallest_word)
# Output: apple
print(largest_word)
# Output: cherryIn this example, min() returns "apple" because it comes first alphabetically, and max() returns "cherry" because it comes last alphabetically.
Challenge
EasyYou are given a list of numbers and a list of words. Your task is to write a program that:
- Finds and prints the smallest and largest number in the list of numbers using
min()andmax(). - Finds and prints the smallest and largest word in the list of words based on their lexicographical order.
Check the test cases for output format
Cheat sheet
Use min() and max() to find minimum and maximum values in collections:
Finding minimum and maximum numbers:
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
smallest = min(numbers)
largest = max(numbers)
print(smallest) # Output: 1
print(largest) # Output: 9Finding minimum and maximum strings (lexicographical order):
words = ["apple", "banana", "cherry"]
smallest_word = min(words)
largest_word = max(words)
print(smallest_word) # Output: apple
print(largest_word) # Output: cherryTry it yourself
numbers = [42, 17, 23, 56, 9, 34]
words = ["kiwi", "apple", "banana", "cherry", "date"]
# Write code hereThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary Sorter