tutorialcup
Python Variables
What is a variable in Python?
The variable in Python is a name given to the memory location containing the value. We can also say that a variable is a kind of identifier. The identifier identifies an entity uniquely in a program during the time of execution of a program. Function names, class names are other kinds of identifiers. In this tutorial, let's see how to create Python variables.
Rules for naming an Identifier
As Python variables are a kind of identifiers, the rules for naming an identifier applies to variables as well. Let's see what are the things to remember when naming Python variables.
- The name of the Python variables should start with a letter or underscore. Examples: var,_var
- A digit cannot be the first letter of a variable name.
>>> 1var=10
File "", line 1
1var=10
^
SyntaxError: invalid syntax
- Except for the first letter of a variable name, the remaining letters can be lower case characters(a-z), uppercase characters(A-Z), underscore(_) or digit(0-9).
- The white space or special characters like (!,$,%,@,#,&,*) must not be part of the Python variable name.
>>> var name =10
File "", line 1
var name =10
^
SyntaxError: invalid syntax
- Python variables must not be the same as the reserved word. Reserved words or keywords are defined with predefined meaning and syntax in the language. Check the list of Python keywords here.
- Python variables are case sensitive.
Python Variables
What is a variable in Python?
The variable in Python is a name given to the memory location containing the value. We can also say that a variable is a kind of identifier. The identifier identifies an entity uniquely in a program during the time of execution of a program. Function names, class names are other kinds of identifiers. In this tutorial, let's see how to create Python variables.
Rules for naming an Identifier
As Python variables are a kind of identifiers, the rules for naming an identifier applies to variables as well. Let's see what are the things to remember when naming Python variables.
- The name of the Python variables should start with a letter or underscore. Examples: var,_var
- A digit cannot be the first letter of a variable name.
>>> 1var=10
File "", line 1
1var=10
^
SyntaxError: invalid syntax
- Except for the first letter of a variable name, the remaining letters can be lower case characters(a-z), uppercase characters(A-Z), underscore(_) or digit(0-9).
- The white space or special characters like (!,$,%,@,#,&,*) must not be part of the Python variable name.
>>> var name =10
File "", line 1
var name =10
^
SyntaxError: invalid syntax
- Python variables must not be the same as the reserved word. Reserved words or keywords are defined with predefined meaning and syntax in the language. Check the list of Python keywords here.
- Python variables are case sensitive.