WebProgrammingTalk Web Programming Python
Understanding Variable Hello There, Guest! (LoginRegister)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Understanding Variable
09-27-2010, 08:42 AM,
Understanding Variable
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:
[list type=decimal]
[*]A variable name can contain only numbers, letters, and underscores.

[*]A variable name can't start with a number.
[/list]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.
10-24-2010, 05:45 PM,
Re: Understanding Variable
Thanks for sharing, the main problem in understanding any programing or scripting knowledge is variables.we just need to know how to declare a variables, this a general basics and may lead to many errors if not declared properly in any program
fashion blog
12-04-2010, 12:03 AM,
Re: Understanding Variable
If i will learn Python someday i will use this Smile

Forum Jump:

Users browsing this thread: 1 Guest(s)
Contact Us | WebProgrammingTalk | Return to Top | | Lite (Archive) Mode | RSS Syndication