What are variables?
A variable is something with a value which may change!
Its value may vary throughout the program!
People who are familiar with C and C++, will know the method to initialize a variable.
In C and C++, it is done as follows:
If i want to initialize a variable x with the value 3 which is of integer type,it is done as:
int x=3;
this stores the value 3 in the variable x which of on Integer type.
In Python, its a lot easier.
While initializing a variable with any value, we need not specify the data type of the variable.
Meaning, for the above example, the data type 'int' need not be specified.
In Python, we just have to type x=3!!
the Python interpreter itself will recognize the data type of the value entered and will initialize it to the variable!
Variables are used so that retrieving its value will be easier in the program.
Say we initialize the vaule of x as 3 by typing x=3
Now, This value of x can be made use of in any line of the program!
We can implement it this way..
To maybe print the contents of the variable x, we should type the following:
We can also perfom add,sub,mul and divide operations on the variable x as follows and also display the result:
Till now, we have only initialized variables with int value.
So type(x) would give us INT.
We can also store strings into variables, as given below:
name="XYZ"
This initializes the variable name with the value XYZ.
Strings must always be typed in quotes(" ").
So the value of the name variable can be displayed as follows:
This value of name can be accessed in any line of the program.
We'll talk more about variables in the coming Posts.
A variable is something with a value which may change!
Its value may vary throughout the program!
People who are familiar with C and C++, will know the method to initialize a variable.
In C and C++, it is done as follows:
If i want to initialize a variable x with the value 3 which is of integer type,it is done as:
int x=3;
this stores the value 3 in the variable x which of on Integer type.
In Python, its a lot easier.
While initializing a variable with any value, we need not specify the data type of the variable.
Meaning, for the above example, the data type 'int' need not be specified.
In Python, we just have to type x=3!!
the Python interpreter itself will recognize the data type of the value entered and will initialize it to the variable!
Variables are used so that retrieving its value will be easier in the program.
Say we initialize the vaule of x as 3 by typing x=3
Now, This value of x can be made use of in any line of the program!
We can implement it this way..
To maybe print the contents of the variable x, we should type the following:
We can also perfom add,sub,mul and divide operations on the variable x as follows and also display the result:
Till now, we have only initialized variables with int value.
So type(x) would give us INT.
We can also store strings into variables, as given below:
name="XYZ"
This initializes the variable name with the value XYZ.
Strings must always be typed in quotes(" ").
So the value of the name variable can be displayed as follows:
This value of name can be accessed in any line of the program.
We'll talk more about variables in the coming Posts.
No comments:
Post a Comment