image
image
image

Conventions When Naming Variables in Python

image

As earlier indicated, conventions are not rules per se are the established traditions that add value and readability to the way we name variables in Python.

a)  Uphold readability. Your variables should give a hint of what they are handling because programs are meant to be read by other people other than the person writing them. number1 is easy to read compared to n1. Similarly, first_name is easy to read compared to firstname or firstName or fn. The implication of all these is that both are valid/acceptable variables in python, but the convention is forcing us to write them in an easy to read form.

b)  Use descriptive names when writing your variables. For instance, number1 as a variable name is descriptive compared to yale or mything. In other words, we can write yale to capture values for number1, but the name does not outrightly hint what we are doing. Remember when writing programs; assume another person will maintain them. The person should be able to quickly figure out what the program is all about before running it.

c)  Due to confusion, avoid using the uppercase ‘O,’ lowercase letter ‘l’ and the uppercase letter ‘I’ because they can be confused with numbers. In other terms, using these letters will not be a violation of writing variables, but their inclusion as variable names will breed confusion.

Practice Exercise 1

Re-write the following variable names to (1) be valid variable names and follow (2) conventions of writing variable names.

I)  23doctor

II) line1

III) Option3

IV) Mydesk

V) #cup3

Practice Exercise 2

Write/Suggest variable names that are (1) valid and (2) conventional.

I) You want to sum three numbers.

II) You want to store the names of four students.

III) You want to store the names of five doctors in a hospital.