Python - Variable
Python Identifier
- Identifier is a name given to a variable, function, or class.
- Identifier consists of one or more characters.
Following are rules for defining identifier
- Identifier can be the combination of upper case (A-z) letters or lower case letters or digits(0-9), underscore(_).
- It begins with the letter. For an instant, multy is the valid identifier and 23-bb is not a valid one.
- Keywords are not used as an identifier.
- Special symbols ($,%,@,!,#,) are not allowed except(_).
- An identifier can be of any length.
What is a Variable?
- Variable is a named memory location that refers to a value.
- If you want to define a variable means, you are going to reserve some space in memory for storing values.
- The python uses the '=' symbol to assign a value to the variable.
- This '=' symbol is known as the assignment operator.
- The Statement that assigns a value to the variable is known as the Assignment statement.
For example:
a=10
Here the variable a is bound with a value of 10.
Python Variable Syntax
< variable_name> = < value >
In Python, There is no need to declare the variable in advance. Variables are declared and initialized by themselves at the time of assigning a value to the variable.
What is Value?
- Value is one of the basic things on which the program works, just like a number or letter.
- Value belongs to different data types such as integer, string, and floating point.
- If we want to know what type the value has, for this the interpreter uses the type() function.
For Example:
>> type(9)
<class 'int'>
>> type('10')
<class 'str'>
>> type(6.9)
<class 'float'>
>> type(True)
<class 'bool'>
Rules for defining Variable
- Variable names should be meaningful.
- Python's keywords are not used to define variables.
- Variable names should begin with a letter.
- Only underscore(-) is allowed to define variable.
- Variable names are case sensitive
For Example:
M=10
m='welcome'
print(m)
print(M)
Output:
10
Welcome
Python Keywords
- Keywords are the list of reserved words.
- Keywords have their own meanings.
- We can not use keywords as the variable name, function name, or constant name.
- If you are attempting to use the keyword as an identifier name, you will get an error message on the screen.
Following are the python keywords.
and | as | not |
assert | finally | or |
break | for | pass |
class | from | nonlocal |
continue | global | raise |
def | if | return |
del | import | try |
elif | in | while |
else | is | with |
except | lambda | yield |
False | True | None |