Python Variables
1. Variables
Variables are nothing but reserved memory locations to store values. This means that
when you create a variable you reserve some space in memory. Based on the data type
of a variable, the interpreter allocates memory and decides what can be stored in the
reserved memory. Therefore, by assigning different data types to variables, you can
store integers, decimals or characters in these variables.
Rules for Python variables:
● A variable name must start with a letter or the underscore character
● A variable name cannot start with a number
● A variable name can only contain alpha-numeric characters and underscores
(A-z, 0-9, and _ )
● Variable names are case-sensitive (age, Age and AGE are three different
variables)
1.1 Assigning Values to Variables
Python variables do not need explicit declaration to reserve memory space. The
declaration happens automatically when you assign a value to a variable. The equal
sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the variable and the operand to
the right of the = operator is the value stored in the variable.
1.2 Creating Variables
Python has no command for declaring a variable.A variable is created the moment you
first assign a value to it.
Example :
x=5
y = "John"
print(x)
print(y)
Output
5
JOHN
1. Variables
Variables are nothing but reserved memory locations to store values. This means that
when you create a variable you reserve some space in memory. Based on the data type
of a variable, the interpreter allocates memory and decides what can be stored in the
reserved memory. Therefore, by assigning different data types to variables, you can
store integers, decimals or characters in these variables.
Rules for Python variables:
● A variable name must start with a letter or the underscore character
● A variable name cannot start with a number
● A variable name can only contain alpha-numeric characters and underscores
(A-z, 0-9, and _ )
● Variable names are case-sensitive (age, Age and AGE are three different
variables)
1.1 Assigning Values to Variables
Python variables do not need explicit declaration to reserve memory space. The
declaration happens automatically when you assign a value to a variable. The equal
sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the variable and the operand to
the right of the = operator is the value stored in the variable.
1.2 Creating Variables
Python has no command for declaring a variable.A variable is created the moment you
first assign a value to it.
Example :
x=5
y = "John"
print(x)
print(y)
Output
5
JOHN