Python Variables


Variables are named locations that are used to store references to the object stored in memory. The names we choose for variables and functions are commonly known as Identifiers. You can imagine variable in Python as an empty container inside which you can put the data. Technically speaking in simple terms variable refers to the name given to the memory location which holds your data

Say for eg. you want to store 10 into a variable named as 'var', so when you will assign 10 to 'var' what will happen inside the memory is that, computer will search for a free memory location and on finding that it will store 10 into that location and name it as 'var'. Now after assigning whenever you will call the variable 'var' the computer will fetch contents from the corresponding  memory location.



How to declare and  assign values to variable

Unlike other programming languages, Python has no command for declaring a variable. They are created the moment you assign a value to it

Say for eg. you initialize a variable 'var_1'

var_1 = 5 

The moment you press enter after typing this command a variable a gets initialized and 5 is stored in it.  Another thing that should be kept in mind is that you need not to define any variable type while initializing the variable as in python it takes the datatype of the variable according to the data assigned on its own, However you can change the data type if required later.




What is a datatype

In simple words it can be said that it define what type of data is stored in a particular variable say 5 is a number and when we assign a = 5, then the datatype of a will be number
Every value in Python has a datatype. Different data types in Python are Numbers, List, Tuple, Strings, Dictionary, etc. 


General rules for initializing python variables
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables), However It is recommended to use lowercase letters for variable name
  • String variables can be declared either by using single or double quotes:

How to re declare a variable

Say you have declared  a variable name
name = 'nayan'

now if you want to re declare  the variable name you can just do it by
name = 'rahul'

Now when you call the variable name you will get the output as 'rahul' and not 'nayan'.




How to assign values to different variable at a time

If you need to assign multiple variables in a single line than you can easily do that in the following way.



How to assign same value to different variable

If there are multiple variables and that have to be assigned same value for each of them, then it can be done in this way



Up till now we have learned how to initialize the variables, but just merely initializing the variable without knowing how to use it is of no use for us so to  use these variable what we will do in this variable is that we will just try to output them


Output of the variable 

In python we use 'print' command to print or display an output.
The syntax is print()
If you want to print 'Hello World!' you can do that by just typing the below command.
print('Hello World!')
and for printing the variable we can just type print(var_name)




Deleting the variable

Once the variable has initialized then used and now if its work is finished then that variable can be deleted in python. You can delete the variable by the command 'del var_name'.
Once the variable is deleted than if you again try to call that then you will get an Error.



Python Constants

A constant is a type of variable whose value cannot be changed. For easily understanding you cab think of constants as containers that hold information which cannot be changed later.

In Python, constants are usually declared and assigned on a module. Here, the module means a new file containing variables, functions etc which is imported to main file. 
Inside the module, constants are written in all capital letters and underscores separating the words.

Eg. of Constants are

PI = 3.14
AUTHOR = 'J K ROWLING'

However the constants can be used in a similar way as variables.

Post a Comment

Previous Post Next Post