Variables in Python | Data Types | Rules For declaring variable |Storing and Printing the value of variable | Operation in variable

What are variable

As the name suggest a variable is entity whose value can be changed any time you want.


In other programming language like C,C++,Java we have a special command to declare variable.In these language we also have to mention the type of value variable is storing whether its Integer,characters or decimal.

But don't worry its python you don't need any command to declare a variable.Its declared when you first equate the value to a variable.

X = 5              (X is treated as integer)
Y = "Hello"   (Y is treated as string due to " ")
Z = 5.6            (Z is treated as decimal)

Datatype


Data type refer to the type of value a variable is storing

There are basically three type of datatype

  1. Integer --- These contains integral Data 5,4,47,100.We can     apply mathematical operation to these
  2. String  ---- These datatype contains words characters any    entity surrounded by "  " will be consider as  string.                    
  3. Float or decimal--- These contains decimal value.5.6,2.4

Rules for declaring variable

  1. Variable name must start with letter or underscore(_) not from a number
  2. Variable can only contain only alphabets,number or underscore.
  3. Variable name are case sensitive X and x are two different variable.

Storing and printing value of variable

Storing Integer value in a variable
               X = 5

Storing String Value in a variable. Any content in " " will be consider as string.  

                   z= "hello world"

                   z="5"   (z will be of string datatype)


Storing float value in Variable.We should keep in mind that 5 is a integer wher as 5.0 is a float
                           

                     T = 5.6


Printing value of a variable in Python console.

print(X)

print(Y)

print(Z)

print(T)


Operation on variable 

Operation has basically two entity operators and operand.

  1. Operator is special character which performs special operation may it be addition(+) ,division(/) ,multiplication(*) ,subtraction(-) 
  2. Operand are entity in which operation is to be done they can be integer decimal or string
All the operator during a operation should be of same datatype

Operation on string type variables

X= "hello"
Y= "world"
print(X+Y)




Operation on Integer and Float variable

They go through all arithmetic operation (+,-,*,/.....) 

X=5

Y=6

Z=X+Y

print(Z)




We should keep in mind that a float and integer cannot be operated or integer and string can't be operated two operator of different datatype can't be added.



Questions

1.take a variable name phone and store apple in it.

2.Take a variable whose value will be python take another variable whose value will be programming add them up to print python programming.

3.Take two variable and store some integer value in it try applying all arithmetic operation on these. 

Comments