Find the maximum python (improved)

I got some advice about the article I wrote last time (Let's find the maximum value python (correction ver)), so I tried to refactor it further. This time, I modified the code with an emphasis on readability.

change point

-How to set variables during loop processing of for statement


The code is below.

num = int(input("How many pieces do you want to substitute?"))
value_list = [] #Prepare an array to store numbers

for i in range(num):
    value = int(input("Substitute a number"))
    value_list.append(value) #Add values to the prepared list


max_value = value_list[0] #Initialize with list elements

for value in value_list:
    if value > max_value:
        max_value = value

print(max_value) #Output maximum value

Changed code

Change before


for i in range(num):
    if value_list[i] > max_value:
        max_value = value_list[i]

After change


for value in value_list:
    if value > max_value:
        max_value = value

This code is simpler and easier to understand. The more articles you write, the more discoveries you will make and it will be a great learning experience.

Recommended Posts

Find the maximum python (improved)
Find the maximum Python
Find the maximum value python (fixed ver)
Find the difference in Python
[Python] Find the second smallest value.
Solve the maximum subarray problem in Python
Find the mood value with python (Rike Koi)
Find the divisor of the value entered in python
Find the solution of the nth-order equation in python
Find the geometric mean of n! Using Python
[Python] Find the transposed matrix in a comprehension
Find the shortest path with the Python Dijkstra's algorithm
python xlwings: Find the cell in the last row
the zen of Python
List find in Python
[Python] Split the date
Find the cumulative distribution function by sorting (Python version)
Find out the location of Python class definition files.
Find the part that is 575 from Wikipedia in Python
Find the Hermitian matrix and its eigenvalues in Python
[Python] Find out about pip
I tried to find the entropy of the image with python
python3 Measure the processing speed.
Find out the apparent width of a string in python
Towards the retirement of Python2
[Python] Heron's formula functionalization and calculation of the maximum area
Compare the Python array-like guys
Write a python program to find the editing distance [python] [Levenshtein distance]
About the ease of Python
About the enumerate function (python)
Python --Find out number of groups in the regex expression
[Python] Adjusting the color bar
Find the index of the maximum value (minimum value) of a multidimensional array
Find permutations / combinations in Python
Find the factorial prime factorization
[Python] Get the previous month
[Python 2/3] Parse the format string
About the features of Python
Find the optimal cooking order
Find the eigenvalues of a real symmetric matrix in Python
[Python] Check the installed libraries
I downloaded the python source
The Power of Pandas: Python
From a book that programmers can learn (Python): Find the mode
[ffmpeg] After ffmpeg using Python subprocess, The system cannot find the path specified.
Find the definition of the value of errno
ABC memorandum [ABC159 C --Maximum Volume] (Python)
Leave the troublesome processing to Python
[Python] Check the current directory, move the directory
The story of Python and the story of NaN
Getting the arXiv API in Python
Check the behavior when assigning Python
[Python] The stumbling block of import
Python in the browser: Brython's recommendation
Save the binary file in Python
AtCoder: Python: Daddy the sample test.
Hit the Sesami API in Python
Try the Python LINE Pay SDK
[Python] Hit the Google Translation API
Find memory-consuming lists / arrays on Python
Get the desktop path in Python