On my writing before, you have learned about tuple, list, and dictionary. You have to truly understand the topics before continuing to next step phases. Why ? because on my writing toda, we will be on advanced phases. Whereas on my writing before, we were on based introductions phase about Python Programming Language. I consider you have understood about all of based in Python. For you who is first time visiting this blog, you can explore first to my writings before so that making easy your learning. Well, as the title above we will learn about How to Find Maximum and Minimum Value on a sequence of numbers. For example, there is a sequence that contains a thousand numbers and then we want to make a program to find the largest number and the lowest number. How to make these program ? let see carefully below :
- Maximum Value
def maxNumber(List) : | --> Making function named maxNumber |
maxNumber = 0 | --> Making variable named maxNumber with value 0 |
for element in List : | --> Looping, checking the sequences of List one by one represented by var element |
if element > maximum : | --> If var element more than var maximum, so : |
maximum = element | --> Var maximum is equal with the var element above |
return maximum | --> Printing var maximum (it has chosen the largest number) |
Explanations :
- def function, to make a function
- var maximum = 0
The point is when we make a function to find maximum value, we have to make first variable with value less than all of elements in the sequence and otherwise.
- for function is looping on executing instuctions its below.
- if element > maximum
Comparation between element and maximum. If element is more than maximum, so value of maximum is equal with the element. And then the program will repeat continuouly until the last element of the sequence. The result is var maximum with the largest element.
I will give you an example below :
- Minimum Value
def minNumber(List) : | --> Making function named minNumber |
minNumber = 0 | --> Making variable named minimum with value 0 |
for element in List : | --> Looping, checking the sequences of List one by one represented by var element |
if element > miniimum : | --> If var element more than var minimum, so : |
minimum = element | --> Var minimum is equal with the var element above |
return minimum | --> Printing var minimum (it has chosen the largest number) |
Explanations :
- Running of the program between maxNumber and minNumber functions are same, only comparation and value that are compared are different.
We should see this example below :
I think this topic is not pretty difficult to understand, the most important is, you have to practice more to solve any problems. If you have not understood with the learning above, please ask me on comment column below, OK !