Understanding Variables


Through variables, you can store and manipulate information, a fundamental aspect of programming. Python lets you create variables to organize and access this information.

Introducing the Greeter Program

Check out Figure 2.6 to see the results of the Greeter program.

click to expand
Figure 2.6: A shout-out to all the Larry's of the world.

From just a screen shot, the program looks like something you could have already written. But within the code lurks the whole, new, powerful concept of variables. Take a look:

 # Greeter # Demonstrates the use of a variable # Michael Dawson 1/13/03 name = "Larry" print name print "Hi, " + name raw_input("\n\nPress the enter key to exit.") 

Creating Variables

A variable provides a way to label and access information. Instead of having to know exactly where in the computer's memory some information is stored, you use a variable to get at it. It's kind of like calling your friend on his cell phone. You don't have to know where in the city your friend is to reach him. You just press a button and you get him. But before you use a variable, you have to create it, as in the following line:

 name = "Larry" 

This line is called an assignment statement. It creates a variable called name and assigns it the value "Larry". In general, assignment statements assign a value to a variable. If the variable doesn't exist, like in the case of name, it's created, then assigned the value.

Using Variables

Once a variable has been created, it refers to some value. The convenience and power of variables is that they can be used just like their values. So the line

 print name 

prints the string "Larry" just like the statement print "Larry" does. And the line

 print "Hi, " + name 

concatenates the values "Hi," and "Larry" to create a new string, "Hi, Larry.", and prints it out. The results are the same as the results of print "Hi," + "Larry".

Naming Variables

Like the proud parent of your program, you pick the names of your variables. For this program, I chose to call my variable name, but I could just as easily have used person, guy, or alpha7345690876, and the program would have run exactly the same. There are only a few rules that you have to follow to create legal variable names. Create an illegal one and Python will let you know about it with an error. The following are the two most important rules:

  1. A variable name can contain only numbers, letters, and underscores.

  2. A variable name can't start with a number.

In addition to the rules for creating legal variable names, the following are some guidelines that more experienced programmers follow for creating good variable names—because, once you've programmed for a while, you know the chasm of difference that exists between a legal variable name and a good one. (I'll give you one guideline right now: Don't ever name a variable alpha7345690876.)

  • Choose desccriptiv names. Variable names should be clear enough so that another programmer could look at the name and have a good idea what it represents. So, for example, use score instead of s. (One exception to this rule involves variables used for a brief period. Often, programmers give those variables short names, like x. But that's fine, because by using x, the programmer clearly conveys the variable represents a quick holding place.)

  • Be consistent. There are different schools of thought about how to write multiword variable names. Is it high_score or highScore? I use the underscore style. But it's not important which method you use, as long as you're consistent.

  • Follow the traditions off the language. Some naming conventions are just traditions. For example, in most languages (Python included) variable names start with a lowercase letter. Another tradition is to avoid using an underscore as the first character of your variable names. Names that begin with an underscore have special meaning in Python.

  • Keep the length in check. This may seem to go against the first guideline: Choose descriptive names. Isn't checking_account_balance a great variable name? Maybe not. Long variable names can lead to problems. They can make statements hard to read. Plus, the longer the variable name, the greater the chance of a typo. As a guideline, try to keep your variable names under 15 characters.

TRICK

Self-documenting code is written in such a way that it's easy to understand what is happening in the program independent of any comments. Choosing good variable names is an excellent step toward this kind of code.




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net