'''Bad: Use lowercase letter at the beginning of a comment'''# get user inputn,*vals=map(int,input().split())'''Good: Always begin a comment with an uppercase letter'''# Get user inputn,*vals =map(int, input().split())
'''Bad: Ambiguous variable name with explanation'''s = num1 + num2 # Sum of the two numbers'''Good: Just use explanatory variable names without comments as explanation'''sum_of_nums = num1 + num2
'''Bad: Worthless comments'''# Add num1 with num2 and assign the result to sum_of_numssum_of_nums = num1 + num2'''Good: Just remove those useless comments'''sum_of_nums = num1 + num2
'''Worse: Misleading / Outdated comments'''defprocess_nums(nums):"""Return odd nums in the sequence."""returnfilter(lambdanum: num %2==0, nums)'''Good: Use correct / updated comments'''defprocess_nums(nums):"""Return even nums in the sequence."""returnfilter(lambdanum: num %2==0, nums)
'''Bad'''# Get the length of a stringdefget_text_length(s,log_output=False):ifnotisinstance(s, str):raiseTypeError("Argument `s` must be of type 'str'") result =len(s)if log_output:print(result)return result'''Good'''defget_text_length(/, string:str,*, log_output:bool=False) ->int:"""Get the length of a string. :param string: The string to be calculated. :param log_output: Log output to console or not (default False). :returns: The length of the string. :raises TypeError: Raised when argument `string` is not a string.>>> get_text_length('foobar') 6>>> get_text_length(42) Traceback (most recent call last): ... TypeError: Argument `string` must be of type 'str' """ifnotisinstance(s, str):raiseTypeError("Argument `string` must be of type 'str'") result =len(s)if log_output:print(result)return result