Python Variables Names and Binding


title: Python Variables Names and Binding

Having objects isn’t useful unless there is a way to use them. In order to use an object, there must be a way to reference them. In Python this is done by binding objects to names. A detailed overview of can be found here

One way this is done is by using an assignment statement. This is commonly called assigning a variable in the context of Python. If speaking about programming in the context of other languages, binding an object to a name may be more precise.

>>> some_number = 1 >>> print(some_number) 1

In the example above, the target of the assignment statement is a name (identifier), some_number. The object being assigned is the number 1. The statement binds the object to the name. The second statement, we use this binding print the object that some_number refers to.

The identifier is not preceeded by a type. That is because Python is dynamically-typed language. The identifier is bound to an object that does have a type, however, the identifier itself can be rebound to another object of a different type:

>>> some_variable = 1 >>> print(some_variable) 1 >>> some_variable = "Hello campers!" >>> print(some_variable) Hello campers!

When naming variables, you must follow these rules:

  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number or special characters ([email protected]#%^&*, etc.)
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (num, NUM and Num are three different variables)

This article needs improvement. You can help improve this article. You can also write similar articles and help the community.