Page 55 - Python Basics: A Practical Introduction to Python 3
P. 55

3.3. Create a Variable


            seconds is a better name than s because it provides more context. But
            it still doesn’t convey the full meaning of the code. Is 3600 the number
            of seconds it takes for a process to finish, or is it the length of a movie?
            There’s no way to tell.

            The following name leaves no doubt about what the code means:

            seconds_per_hour = 3600

            When you read the above code, there’s no question that 3600 is the
            number of seconds in an hour. seconds_per_hour takes longer to type
            than both the single letter s and the word seconds, but the payoff in
            clarity is massive.

            Although naming variables descriptively means using longer variable
            names, you should avoid using excessively long names. A good rule
            of thumb is to limit variable names to three or four words maximum.


            Python Variable Naming Conventions

            In many programming languages, it’s common to write variable
            names in mixedCase. In this system, you capitalize the first letter
            of every word except the first and leave all other letters in lowercase.
            For example, numStudents and listOfNames are written in mixedCase.

            In Python, however, it’s more common to write variable names in
            lower_case_with_underscores. In this system, you leave every
            letter in lowercase and separate each word with an underscore. For
            instance, both num_students and list_of_names are written using the
            lower_case_with_underscores system.


            There’s no rule mandating that you write your variable names in
            lower_case_with_underscores. The practice is codified, though, in a
            document called PEP 8, which is widely regarded as the official style
            guide for writing Python.








                                                                          54
   50   51   52   53   54   55   56   57   58   59   60