In this tutorial, we will learn about python variables, data types, and their use cases.
As we learn Keywords and identifiers in previous tutorials now, we will learn about how to use them using variables.
Variables are used to store values in reserved memory locations. Variables are created at the moment when they are first declared at runtime. It does not need any keywords or any particular data type to declare it. Variables are declared based on identifiers.
Assigning the Variable
Variables are assigned based on the data types. It will automatically assign a value to a variable when we use an equal sign (=) to the variable.
Declaring and assigning value to a variable
Example 1: Declaring a single variable.
As you can see, we have stored a string in a variable call software, and then we are printing the variable.
In the example, we are declaring string, Integer, and float to variables.
Assigning Multiple Variables
We can assign multiple variables in different methods, just like assigning multiple variables in statements and multiple variables with multiple values.
Method 1 # Multiple variables with a single value.
Here we are assigned an int value in more than one variable in one statement.
Method 2 # Multiple variables and multiple values.
Example 1 :
In this example, we are assigning multiple values in multiple variables.
Changing the value of a variable
We can change the value of the variables at the time of runtime when we reassign them.
As you change, see in the example first we are assigning the int value to the variable X, and then after that, we are reassigning variable X with a String, and this is how we can reuse the same variable multiple times.
Casting of Variables
If you want to specify the data type of a variable, this can be done with casting.
Right Way to assign variable in Python
To assign a variable in python or any other language, we need to follow specific rules to declared variables as per data type.
Rule 1: A variable always starts with a letter or an underscore (_).
Example _x1 = “Pycharm” , _1 = 11 is allowed but 1x = 2 is not allowed.
Rule 2: When we assign a string in a variable, it should be Single to Double quotes; they both are the same.
Rule 3: Python is a Case-Sensitive Programming language that means
X = 3 and x = “Python” is completely different.
del Function in Python
The Python del keyword is used to del objects like variables and functions m classes. We will see the del keyword in future tutorials as it is going to be used in all other python tutorials. In
Python everything is an object, so the del keyword can also be used to delete tuple, lists, or parts of a list, etc.
Traceback (most recent call last):
File “Del_Keyword.py”, line 4, in <module>
NameError: name ‘X’ is not defined</module>
As you can see in the example, we have implemented a string variable, and then using the del keyword, we are deleting the variables. Then we are printing the same variable. As we have deleted the variable we got, the error with “X is not defined” means the Python interpreter could not find the variable.
type() Function in Python
Python has a lot of built-in functions, and the type() is one of them.
The type() function is used to get the type of the data type of that particular object.
As we can see in the above example, we assign an integer value to a variable, and then we print that variable inside the type function. We are getting the output of that particular variable as data types.
In this example, we are assigned an integer, float, and a string, then we are printing their type, and as we can see, we got the output of their types.
We can also check the type of all the objects and functions on python using the type() function. As an example, we will take the print function.
Tips for Python Variables
- Variable names should have a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_).
- Create a name that makes sense. For example, computers make more sense than C.
- If you are creating a variable name having two words, use underscore to separate them.
- Use capital letters possible to declare a constant.
- Never use special symbols like !, @, #, $, %, etc.
- Do not start a variable name with a digit.
- Only use the del function if necessary.
- Try to practice type functions in all other python functions you learn in the future.