Hello friends, In this article I will help you to understand how variable work in python.
Python is a interpreted language just like JavaScript. This means that code is not compiled and converted to machine understandable code. This means that no binaries are produced. In fact, this conversion is done at run time. This makes python very efficient and high performance programming language.
That is the reason python is used in data analysis and complex computations like machine learning.
Creating a Python variables
It is most easy task to create a variable in the python programming language. We have to just create a variable and assign it a value.
The data type of the variable will be decided based on the data type of the value.
In the below screen shot I have written some variables for the understanding purpose. As you can see I haven’t used any keyword to initialize a variable.
I have assigned all type of values in the above code snippet for the demo purpose. As seen in the row 8 you can assign multiple variables in the same row.
And I have used the print function to print the value of the variable to the output console. In the row 17 on wards I have used the print function to get the type of the function.
As you can see in the above figure we can see the type of the variables are printed in the last three rows. And these string, float and integer.
Can a variable have different data type
Once we have assigned a value to a variable, the data type of the variable is defined based on that value. But while writing more code suppose we reassign a different data type value to the same variable.
What so you think will be the result? Will it be some error or will the code just work fine.
And the result of the above code sample is that we can safely assign a different data type value to an already used variable.
Below is the output of the above code.
One good thing in python language is that there is no use of brackets. In other programming languages we use brackets to define the scope for methods and variables.
I will discuss about the scope, function and indentation in my next article.
Leave a Reply