3 Wonderful Hacks To Use A Python Variable You Need To Know

The concept of Python variable is more or less the same in every programming language, except for a few minor changes. Today, in this video we will learn if the variables in Python Programming are any different from others.

What are Variables in Python?

We can define a Python variable as “A name that represents a value in Computer’s Memory”. We can use that name to refer to that value later in the program.

How to create a Python variable?

Similar to every other programming language, in Python Programming we use an assignment statement to create a variable. I will show you how.

An assignment statement is made up of three components. Among those three components, two are operands and one is an assignment operator.

The assignment operator is equal to sign (=). Both the operands go on either side of the assignment operator.  The operand which sits on the left-hand side of the assignment operator is the name of the variable. While the operand which goes on the right-hand side of the assignment operator (=) is the value which you want to store in your variable.

Python Variable

For example, let’s say I need to use the value of ‘pi’ in my program repeatedly, therefore I decided to create a variable with the name ‘pi’ and assigned the value 3.141 to it.

pi = 3.141

This statement will create a variable in Python. That’s all you have to do to create a variable in Python. No data type, no data width. Moreover, there is no need to put a semi colon at the end of the statement. Although, you can but there is no need. See, how easy it is.

Naming Rules for Variables in Python

  1. Variable names are case sensitive which means UPPERCASE and lowercase characters are distinct. For example, variable Salary is not the same as salary
  2. Be careful with the keywords. You cannot use keywords or reserve words of Python for naming your variables.
  3. No spaces, please. A variable name cannot contain spaces in the middle. For example, name. FirstName is legal and can be used while the variable name First Name will raise an error.
  4. It’s all about the starting. The first character of the variable’s name must always be a letter or an underscore ( _ ). Your variable’s name must always start with a letter. It could be a small or a capital alphabetic letter or with an underscore. For example, name salary or _salary are perfect. On the other hand names like 1stClass is unacceptable in Python.

[wd_hustle id=”keyword-pdf” type=”embedded”]

Where is the datatype of the variable?

In Python Programming we don’t need to specify the data type of the variable while creating them. Python interpreter assigns a proper data type to the variable during runtime.

[bctt tweet=”Since Python is a Dynamically Typed Language so we don’t need to write the data type of the variable – ” username=”@RebellionRider”]

Let me explain this in detail to you.

Python- A dynamically Typed Language

Programming Languages are either Dynamically Typed or Statically Typed. In a statically typed programming language, we need to tell the compiler everything about the variable beforehand. For example, to declare a variable in PL/SQL we first need to write the name of the variable followed by the data type and then the data width enclosed in the parenthesis.

Var_name Data_Type (Data_width);

On the other hand, in a dynamically typed language, the data type, and the data width will get determined at the run-time automatically by the interpreter. In dynamically typed languages, the programmer doesn’t need to specify any detail about the variables, except the name and the value of the variable.

Python is one of those dynamically typed languages. That’s why you don’t have to go through all the trouble of specifying the data type or the data width of the variable. Python interpreter will handle it for you.  

Assign Values to Python Variable

There are three ways of storing values in a Python variable. Let’s learn those

1.Direct assignment.

The most commonly used assignment operation is a direct assignment of value into the variable. In this way, you create a variable and store a value into it.

For Example –

salary = 10000
salary_raise = 12.7
post = ‘Manager’
print (salary)
print (post)
print (salary_raise)

In the first three lines of the above code, we created three variables with names salary, raise and post. The first variable ‘salary’ contains an integer value which is 10000. The second variable ‘salary_raise’ contains a floating value which is 12.7. The third variable ‘post’ is of string type as we have stored a string into it.

2.Chained assignment

The second way of storing a value into the variable is chain assignment. In chain assignment, you create multiple variables and initialize them by storing a value into them.

For example –

Var1=var2=89
print(var1)
print(var2)

In the first line of the above code, we created two variables with the name ‘Var1’ and ‘var2’. Also, we initialized them with an integer value which is 89. In this way, both variables will share the same value.

Here the python interpreter will store the value at a location in computer’s memory. The value in my case is 89 and both the variables will point to that location.

Python Variable

I will explain to you what happens in the background when you declare a variable in a little bit, so hang on!

3.Multiple assignments

Python language lets you assign multiple variables with respective values using a single assignment statement.

For example –

Salary, salary_raise, post = 10000, 12.7, ‘Manager’
print (salary)
print (post)
print (salary_raise)

The first line of this code is an assignment statement. Using this we declare and initialize three variables at once.

In this process, we simply have to write the name of the variables separated by commas on the left-hand side of the assignment operator. While on the right-hand side, write their values again separated by commas. Just make sure you get the order of the values correct. This is because the first value will get stored into the first variable and second value into the second variable and so on.

Variable Declaration: What happens backstage!

Let me explain what happens in the background when you declare variables in Python. This will help you in better understanding the dynamically typed nature of the Python.

When you declare a variable, the interpreter checks the statement for the error. If the statement passes the check then python interpreter assigns a unique ID to the name of the variable. After that, it looks for the value which is getting stored in it. Depending on the nature of the value the interpreter sets the data type of the variable and allocates the memory to it, to store the value. All this process happens dynamically in the background. That’s how the data type of the variables in Python gets assigned at the run time.

Can we store expressions in a Python variable?

Yes, in Python we can assign an arithmetic expression to a variable. For example –

Exp = 3+(4*5)/3
print (Exp)

Here in the first line of the code I created a variable with the name EXP. After that I stored an expression into it.

What will be the data type of the variable EXP?

The data type of the variable will depend on the result of the expression. If the result of the expression is an integer then the data type will be an integer. If the result of the expression is float then data type will be a float.

How interpreter will process this?

The interpreter does all the heavy lifting for you here. Firstly, it checks the statement for any error. Secondly, it assigns a unique ID to the name of the variable. Once it is done, it comes to the expression and evaluates it. After a successful evaluation, it stores the value into the memory and assigns it to the variable. Lastly, it sets the data type of the variable according to the result of the expression it derived after evaluating it.   

That’s how variables get declared and initialized in Python Programming. Hope you enjoyed reading and got all your confusions cleared. Please do comment and let me know what part of the article you enjoyed the most. You can also send me a message on my Facebook & Twitter.  

Please, don’t leave without sharing this blog on your social media. Thanks and have a great day.