VARIABLES IN PYTHON | COMPONENTS OF VARIABLES
A variable is like a container that stores value that you can access or change. A variable as the name suggest, has been derived from the word "vary" which states that the variable may vary during the execution of a program i.e. , the value of a variable keep changing during the program execution.
In Python, every element is termed as an object. Hence, a variable is also an object in Python.
Note-: VARIABLES provider way to associate names with object.
Through variables we can store the value in computer's memory . Variable is a named unit of storage. A program manipulates its value. Every variable has a type and this type defines the format and behavior of the variable.
For example, if we write
x = 3
It will automatically assign a value 3 to the variable named x
Similarly, if you type
x=10
Then automatically it will assign 10 to the variable named x
The above behavior exhibit that variable x has been given a value 10, which is stored inside a memory location with the name x. This is statement showing store 10 in X but does not get displayed until and unless we type x (>>>x) .
Let us take the another example working with the variables.
After executing the above
lines of the code, x is 4, y is 5 and z is 8.
Alternatively we can type
this program in script mode also, but for displaying the values of variables x ,
y and z, we need to give a print() statement explitily. print() statement is
used to display the value assigned to a variable.
The print statement can print
multiple values on a single line. The, (,) separates the list of values and
variables that are printed.
Now, save the code with table filename with extension .py
then run it ( RUN MY F5).
OUTPUT-:
Let's
understand this code line by line :
- x start with value 3 and y start with value 4.
- In line 3, a variable z is created and equals x + y, which is 7.
- Then the value of z is changed to equal one more than it currently equals, changing it from 7 to 8.
- Next, x is changed to the current value of y, which is 4.
- Finally, y is changed to 5. Note that in this does not affect x.
- So in the end, x is 4, y is 5 and z is 8.
For any query comment below
0 Comments