Table of contents
- Python Variables
Python variables are the named location that reserve memory to store references to the values. That is, when a variable is created, some space in memory is reserved to store a value assigned to that variable. The python interpreter allocates memory based on the data type of value assigned to a variable. The value of a variable can change throughout the program. The names given to the variables when creating them are called identifiers.
How to assign a value to a variable in Python?
Unlike many programming languages, Python doesn’t need an explicit variable declaration to reserve memory space. The memory space is allocated to a variable when a value is assigned to the variable itself. That is, the variable is declared when you assign a value to a variable. To assign a value to a variable, you can use the assignment operator = by putting a variable name at the left side of the equal sign (=) and putting the value at the right side of it.
For example: Assigning a value to a variable
var1 = 1 # variable with an integer value var2 = "yonep" # variable with a string value
Where var1 and var2 are variables, and 1 and “yonep” are values assigned to them respectively.
Another distinction between other popular languages (C, C++, Java) and python is that you do not need to declare the data type when declaring a variable. Python is a dynamically typed language, so python interpreter itself detects a data type of the variable based on the value assigned to it.
You can also change the value of a variable as your program needs. Python allows you to change the variable value to a different data type value and python interpreter automatically assigns a new data type to the variable.
For example: Changing the value of a variable
var1 = 999.99 var2 = 100
Here, we changed values of var1 from an integer data type in the previous example to a float type and var2 from string to an integer data type.
How to assign a value to multiple variables?
Python allows us to assign a single value to multiple numbers of variables in a single statement.
x = y = z = 10
In the above example, integer value 10 is assigned to all the variables x, y, and z simultaneously.
How to assign multiple values to multiple variables?
Python also allows assigning multiple values to multiple variables simultaneously.
x, y, z = 1, "Python", 999.99
The above example is the same as doing:
x = 1 y = "Python" z = 999.99
That is, it assigns 1 to x, “Python” to y and 999.99 to z.
Swap values in a single line in Python
In python, you can swap values of two variables in a single line. This is true for all the data types that we cover later in the tutorials.
a, b = 40, 100 # assigning 40 to a and 100 to b print(a) print(b) a, b = b, a # swapping values so that a = 100 and b = 40
Python Variable Naming Rules
You should be aware of certain rules when naming your variables. They are:
- The name of the variable can only begin with an underscore or a letter (a-z, A-Z) which can be followed by letters, numbers (0-9) and underscores. For example: user_name, count, __name, pw123, etc.
- All the python identifiers including variable names are case-sensitive. That is, userName and username are two different variables.
- Variable names can not contain punctuation characters like &, %, *, etc.
- Python keywords or reserved words can not be used as variable names. Here is a list of all the python keywords.
Python Variable Naming Conventions
Apart from the above-listed rules, there are some variable naming conventions that the python community follows.
- The variable name must be readable and descriptive. That is, when you create a variable, the name given to it must be easy to read and should explain to the reader what sort of values will be stored in it. For example, ‘count’ would be a better variable name than ‘idontknow’ to store the count of letters in a string.
- It is recommended to avoid using single letters like a, b, x, y, etc. as variable names for the same reason mentioned in the first point as they don’t really explain anything about the nature of variables.
- Even though you can create variables name with any length, it is recommended to maintain the length so that it is easy to read.