What is Variable in Python?
A 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?
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)
- if the variable declared as string, then variable assigned need to be wither within single or double quotes.
Post a Comment
Post a Comment