Hello everyone,
I’m a beginner in Python, and while browsing the Wikipedia page for the language I found a code example that raised some questions for me regarding style and best practices. I would like to know whether my observations are correct according to PEP 8 or common Pythonic conventions.
Use of CamelCase for variables
According to PEP 8, variables should use snake_case, so I believe they should be renamed.
Variable name setOfNumbers
In the example it is actually a list, not a set. Would a name like numbers_list or list_of_numbers be more appropriate?
Using print() followed by input()
Python allows writing something like value = input("message"). Is this generally preferred over separating print() and input() into two lines?
Nested conditionals instead of elif
Is it correct that using elif is more readable than placing an if inside an else block?
Parentheses around conditions (if (...))
Parentheses are not required in Python. Are they usually omitted for style reasons, or is it just a matter of preference?
I don’t want to edit Wikipedia myself without first confirming whether these would indeed be considered valid style improvements.
Thank you very much for any clarification!
(English is not my first language, so I used a translator to express my question clearly.)
PEP 8 governs Python’s own standard library. It’s entirely reasonable for other people’s code, including what you find on Wikipedia, to follow other conventions.
Why is the text of your link “es” but the link destination is “uk”? That’s a bit confusing.
Your points are all valid, IMO. I’d also replace that first loop (that repeatedly calls append) with a list comprehension, and replace int((firstPos + lastPos)/2) with a “floor division” (//).
Completely agreeing with Rosuav concerning the scope of PEP 8.
Concerning the points:
I would not be too much concerned with CamelCase, that’s a matter of taste.
I would agree; but that is not a PEP 8 issue.
value = input("message") and print("message"); value = input() are actually not exactly the same since for the second option a newline is printed before the user can enter a number. In the Pyodide console, the query from input is shown in the dialog box.
I would agree.
Parentheses can be added; I would only do it if I need to split the condition to several lines.
There are three more points:
I would not add a space before the colon :. This is an aspect where the code does not follow any convention.
In would not add a space between the name of a callable (function or constructor) and the following argument list. The code follows that convention with one exception: range (max) in line 4.
I would compute midPos via (firstPos + lastPost)//2 without using floats.
Yup, that’s one of the most obvious fixes IMO - in fact, I suspect that’s not a question of stylistic choice, but simply a typo. If the OP wants to make a non-controversial improvement, that’d be the safest one.
It may also be beneficial to rename the max variable. Not only is max a builtin name (shadowing), but it also does not really explain the purpose of the variable. max in this case is the total length of the array, so calling it length might be better.