Variables in Python

 What is Variable in Python?

Python variable is a reserved memory location to store values.

You can also look into my video:

        For example, please see the below screenshot where I have multiplied 3 * 6 and the output is displayed as 18. But this is just an output and I can't use it anywhere until I have stored this information in any particular place. This process of storing the values is referred as variables.





Please see the below screenshot, where I have used a variable "a" to store the output of 3 * 6 and used a variable "b" to store output of 2 * 4. So whenever from next time, I don't need to execute 3 * 6 or 2 * 4, I can just simply use the variable "a" or "b".

  • Please see the below output, where I have used a variable "c" to store the output of "a" * "b". From the above screenshot you can see the value of the variable "a" and "b". and from the below screenshot, if we multiply both "a" and "b", the output what we get belongs to the variable "c".

One second, Is the variable can be only numbers?

Variable can be integers, float, Boolean, string, list, dictionaries, tuples.

Types of Variables:

  • - string ---> It can be word or sentence that doesn't have any special meaning
  • - integer --> whole numbers
  • - float --> Decimal numbers
  • - Boolean --> It can be either True or False. In other words, 0 for false and 1 for True.
  • - list --> normally represented for an item that belongs to a particular community. Eg: fruits = ["apple", "orange", "grapes"]. Here I have used a variable fruits to a list that has the 3 items "apples", "oranges", and "grapes" which belongs to the community fruits.  You can add, delete or replace an item in the list
  • - tuples --> Similar to list with slight changes. normally represented for an item that belongs to a particular community. Eg: fruits = (apple", "orange", "grapes"). Here I have used a variable fruits to a list that has the 3 items "apples", "oranges", and "grapes" which belongs to the community fruits.  You cannot modify, add, delete an item in the tuple.
  • - dictionary --> normally used to represent any key that needs to match a value. Eg: student with mark or country with capital . Representation of dictionary: age = {"Ram": 20, "Rohit": 22}

Is there any certain conditions for the variable?

Yes. 

- Variable name cannot start with an number but it can contain numbers (4num is not allowed. but num4 is allowed)

- there should not be any space between the variable, but it is recommended to use the "_" in order to difference between the variable name if it has more than one word ("student marks" --> not allowed, but student_marks --> allowed

- if the variable declared as string, then variable assigned need to be wither within single or double quotes.

- if the variable that is declared as integer/ float/ bool,   quotes should not be used

How to find the What Type of variable is declared?
- You can check what type of variable is declared by using the syntax, type(_var_). Below is the sample example where I have configure variable "a" as integer, "b" as float and "c" as string. In order to find the type, I have executed the command type(a) to find the variable type of "a", type(b) to find the variable type "b" and type(c) to find the variable type(c)

Thanks for reading my blog.

Post a Comment