Variables
A variable is a container that temporarily holds data or values, which can change over time.
It can hold different data types and is associated with a unique identifier, making it case-sensitive.
When two variables have the same value, they share the same memory location in Python.
Rules:
-
Identifiers are created using a combination of letters (A-Z, a-z), digits (0-9), and underscores (
_
). - They cannot start with a digit
-
It doesn't allow special characters like
@
,#
, or% except (_) underscore
.
- Keywords (35 in Python 3.10) cannot be used as identifiers.
-
Python identifiers are case-sensitive, meaning
VarName
andvarname
are treated differently.
-
The
.isidentifier()
method checks if a string is a valid identifier, returningTrue
orFalse
.
# E
sal = 1.2 (valid)
1sal = 34 (invalid)
_sal = 3.4 (valid)
_123 = 56 (valid)
tot_sal = 45 (valid)
tot-sal = 45 (invalid)
tot$sal = 45 (invalid)
tot_sal#India = 56 (valid, but # is treated as a comment).
while = 45 (invalid)
if = 56 (invalid)
_while = 67 (valid)
int = 45.67(valid but not recommended, as int is a predefined class).
AGE = 99
age = 98
Age = 97