Working With Variables – Python Programming

Working With Variables - Python Programming

We are on our quest to learn python programming and this brings 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.

basic python program

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.

running python program

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.

  • Should begin with uppercase or lowercase alphabets (a – z, A – B) or and underscore ( _ ). 1_age is not allowed whereas age_1 is allowed.
  • Preceding characters can contain alphabets, underscores, and numbers as well.
  • Variables are strictly case-sensitive. Age is different from age.
  • Reserved keywords themselves cannot be used, however, you can use them within a full variable name.A variable named if is not allowed but a variable named x_if if absolutely fine.

Here is a list of keywords in python.

python reserved variable keywords list

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.

python data types

​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.

how to run python 3 program in linux

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 in 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.

get user input python 3

The result should be an output where the correct sum is displayed.

create python 3 script

Casting is also possible with other data types like float(), str(), etc.

Conclusion

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

linux system administration bootcamp