Variables in Python
What are the variables?
Variables is a named location in memory that stores a value.
They used to store data values in a program.
Variables can be created by assigning a value to a new or existing variable name using the assignment operator (=).
The value assigned in a variable can be changed during program execution.
Structure to create variable in Python
variable_name = value
For example:
x = 5
y = "Hello World"
From the upside example we can see there are two variables x and y which contain specific type of data.In the simple language variable is containers which stores values.
There are few rules to follow when naming variables in Python:
- Variable names can only contain letters, numbers and underscores (A-z, 0-9, and _).
- A variable name must start with a letter or the underscore character.
- Variable name cannot start with number.
- Variables names should not be the same as keywords in Python.
- Variables names should be descriptive and meaningful.
Examples of valid and invalid variables:
Valid variables:
var = 57
_name = "Hello World"
Invalid variables:
1var = 5
$name = "Hello World"
No comments:
Post a Comment