Create Variables in Python:
|We can use the PyCharm code editor for this example. If you do not know about it then follow this link- How to install PyCharm for Python and create a program in it.|
(1) A variable is basically a name that represents some value. For example, you might want the name a to represent 13. To make it so, simply execute the following.
In the above image, a is the variable and we can set its value to 13. We can use the print (a) function to print its value. We can pass a in the print function without using quotes because if we use quotes then a is converted into a string and returns the value a instead of the defined value for the variable. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.
(2) If we want to print two variables, then follow the below image.
(3) If we pass the same value to the two variables, then both variables have the same memory address or location because in python memory address is generated according to the value as shown below.
(6) If we use a quote for a then the print function will print a because it is converted into a string as shown below.
(7) If we change the value of b, then its memory address will change as shown below.
(8) You can also store character values in the variable as shown below.
(8) Important Points:
|(a) Variable names always begin with alphabets or an underscore and not with numbers. For example- abc1, _abc1, etc.|
(b) There is no space between the variable’s name. For example- if you want to write information technology, you must write like this information_technology.
(c) They cannot contain punctuation marks.