Friday, December 18, 2020

Variables in Python


Variables are like containers holding data from a memory location. 

In different terms, Variable is used to retrieve/store value from/to memory. 

In programming languages, Variables are defined with specific data type and are used to any kind of processing related to the data type. 

How are variables defined in Python? There is no explicit declaration for variables in Python like most other languages. Variable is automatically created the moment the data is assigned. 

How does python know what data type the variable should be created with? Python creates the variables in the run time based on the data assigned and variable is created based on the type of the data is assigned. It is easy to understand with some examples. 


Let's assign an integer to 'first_variable' and string to 'second_variable'. 

Variables in Python

How do we know the data type of a variable? 'type' function would return the data type of a variable. 

Let's try to print the data in these two variables along with data type of the variables. 

Variables in Python

  • Line - 6: We are passing first_variable, type(first_variable) to print function. This should print the data stored in the variable and corresponding data type. 
  • Line - 11: We are passing second_variable, type(second_variable) to print function. This should print the data stored in the variable and corresponding data type.
Variables in Python

Type function has returned the data type along with the text 'class'. So, Would it always be the same? How should we check the data type inside the program (instead of printing)?

We still need to use the type function, and compare it with data type we need to check for. 

Variables in Python

  • Line - 13: We are checking if the type of the variable is 'int' and it is not enclosed with in quotes. This would consider this as a data type. Data type shouldn't be enclosed with in quotes (if it is it would consider as string and would execute else part).

Variables in Python

This would be the same for any other data type. Just use 'str' for String, 'float' for Float, 'list' for List...

We now know that Python assigns data type automatically when a value is assigned. What would happen if the data (of different data types) is assigned multiple times?

Let's see,

Variables in Python

Here we have assigned integer (line - 3) and string (line - 8) to first_variable and running print statement after each assignment. 

Variables in Python

We can see that first print statement has printed the data type as 'int' and second print statement has printed the data type as 'str'. So, data type of a variable can be changed based on the value assigned. 

Are there any constraints on what name to be used for a variable? Yes, there are few constraints on how a variable should be named. 

  • Variable name should start with a letter or underscore and cannot start with a number.
  • No special characters are allowed in a variable name (except underscore). 
  • Variable names are case sensitive (E.g.: First and first would be considered as two different variables).

If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

No comments:

Post a Comment

Different Ways of Sorting Data in a List - Python

Sorting Data in a List List is a collection of data (of different data types), much like an array. Like any data structure or data set, dat...