We are on our quest to learn python programming and this bring us to one of the most fundamental topics. Variables are everywhere, every programming language has variables at its core. We will learn about variables and also see some basic operation and applications of variables in python.
What are Variables?
Variables are elements that store program data. This program data has its scope limited to the life cycle of the program. For example, let us say we are writing a program that asks for a user’s age as input. We will have to create a variable named age and store some value in it. This variable age can be used in the program by various functions. But once the program is terminated, Boom - Your variable is lost, you will have to ask for that age again.
A Basic program
I will not be writing down codes here as i want you to write them yourself. Recreate the code from the image below in your python file.
In the code above we have created a variable called age. We asked the user for an input and the value from that input will be assigned to the age variable. On the Next line we have simply called a print statement that prints our entered age. You should get an output similar to the one below.
That was easy. We created a variable, supplied it an input and used that variable. To be honest, we’ve done pretty good so far. Let us look at another important topic called naming a variable.
How to name a variable
The last variable we used, we called it age. There were no problems with the program and it ran well. However, there are certain rules when naming variables. You cannot just name variables whatever you want. Different languages have different rules regarding the naming of variables, But we will strictly look at the rules from python.
What are Data Types?
We know some basics about variables, However not all variables stores similar data. Some variables might store a number, some variables might store strings, some might store arrays, etc. Various operations are often performed with variables of the same type. Let us quickly run the following code.
We have asked for two variables and we will be printing their total sum. This means we should get an output that the sum is 5. But we don’t. See the image below.
We get 23. This is really strange. Well, it’s not actually. In most programming languages like C++, C, Java, etc. You are required to define the data type of variable. Since python is a dynamic language, that is not required. But the behavior above has resulted into a concatenation of 2 strings instead of addition of 2 numbers. The solution is easy. It is called typecasting. Simply add a wrap the input calls into an int() function. This will convert the input into an integer instead of strings. The following code should make it clear.
The result should be an output where the correct sum is displayed.
Casting is also possible with other data types like float(), str(), etc.
We saw how variables are created and how to access them. Since python is a dynamic language we may often need to convert variables into a particular type. I am sure you have a clear understanding of how variables work. If you are still confused about something, drop it in the comment section and I’ll be there for you.
Most Read Articles
Translate Our Site
Notice : Please be careful, after translation commands will change.
This website uses marketing and tracking technologies. Opting out of this will opt you out of all cookies, except for those needed to run the website. Note that some products may not work as well without tracking cookies.Opt Out of Cookies