Wednesday 8 January 2014

Variable names and keywords in python

Programmers generally choose names for their variables that are meaningful—they document what the variable is used for.

Variable names can be arbitrarily long. They can contain both letters and numbers, but they have to begin with a letter. It is legal to use uppercase letters, but it is a good idea to begin variable names with a lowercase letter.

The underscore character (_) can appear in a name. It is often used in names with multiple words, such as my_name or airspeed_of_unladen_swallow.

If you give a variable an illegal name, you get a syntax error:
>>> 76trombones = 'big parade'
SyntaxError: invalid syntax
>>> more@ = 1000000
SyntaxError: invalid syntax
>>> class = 'Advanced Theoretical Zymurgy'
SyntaxError: invalid syntax

76trombones is illegal because it does not begin with a letter. more@ is illegal because it contains an illegal character, @. But what’s wrong with class?

It turns out that class is one of Python’s keywords. The interpreter uses keywords to recognize the structure of the program, and they cannot be used as variable names.


You might want to keep this list handy. If the interpreter complains about one of your variable names and you don’t know why, see if it is on this list

1 comment:

  1. Casino Slots for Android - Dr.MCD
    Download 전라남도 출장샵 Casino Slots for Android - Best Games & Slots 광양 출장샵 apps on Dr.MCD! The 문경 출장샵 site has you covered with the latest casino games and 과천 출장마사지 slots 울산광역 출장마사지 for free.

    ReplyDelete