Monday 29 April 2019

4. Variables

4.   Variables in python
ü  What is variable?
ü  Properties of variable
ü  Creating variable
ü  Invalid cases for variables
ü  Multiple variables in single line
ü  Single value for multiple variables
ü  Variable re-initialization
ü  Programs
4. Variables

4. 1 Variable
ü  A variable is a,
o   name.
o   refers to a value.
o   holds the data
o   name of the memory location.

Program      printing variable
Name          demo1.py

emp_id=101
print(emp_id)

Output
                  101

4.2 Properties of variable
ü  Every variable has a,
o   Type
o   Value
o   Scope
o   Location
o   Life time

4.3 Creating variable
ü  In python, to create a variable we need to specify,
o   The name of the variable
o   Assign a value to name of the variable.


Syntax
                  name_of_the_variable = value


Program       creating a variable
Name          demo2.py

                  age=16
                  print(age)
        
output
                           16

Program       creating a variable and displaying with a meaningful text message
Name          demo3.py

                  age=16
                  print(“My age is sweet: ”, age)
        
output
                           My age is sweet: 16

Make a Note
ü  We can print meaningful text message along with variable for better understanding
o   Text message we should keep in within double quotes.
o   Text message and variable name should be separated by comma symbol.
Above example:

                  age    ----->          name of the variable
                  16      ----->          value or literal

4. 4 Invalid cases for variables
1.   While defining a variable,
o   Variable name should be written in the left-hand side.
o   The value should be written in the right-hand side
o   Otherwise, we will get a syntax error.

Program       Creating variable in the wrong direction
Name          demo4.py

                  10 = emp_id
                  print(emp_id)

Error
                  SyntaxError: can't assign to literal

2.  
variables names,
o   should not give as keywords names, otherwise, we will get error

Program      printing a variable name which having digits in the name
Name          demo5.py

if = 10
print(if)

Error
                  SyntaxError: invalid syntax

4.5 Multiple variables in single line
1.   We can assign multiple variables to multiple values in a single one line.
2.   Rule
a.   While defining there should be the same number on the left and right-hand sides. Otherwise, we will get an error.

Program       assign multiple variables in single line
Name          demo6.py

a, b, c = 1, 2, 3
print(a, b, c)
output
                  1 2 3

Program       Invalid case: assign multiple variables which are not matched names and values
Name          demo7.py

a, b, c = 1, 2
print(a, b, c)
Error
                  ValueError: not enough values to unpack (expected 3, got 2)

Program       Invalid case: assign multiple variables which are not matched names and values
Name          demo8.py

a, b = 1, 2, 3
print(a, b, c)
Error
                  ValueError: too many values to unpack (expected 2)

4.6 Single value for multiple variables
ü  We can assign a single value to several variables simultaneously.

Program       Assign single value to multiple variables
Name          demo9.py

a = b = c = 1
print(a, b, c)        

Output                
1 1 1

4.7 Variable re-initialization
ü  We can reinitialize variable values.
ü  Re-initialize
o   In variable re-initialization, old values will be replaced or overridden with new values.

Program       Re-initialising variable
Name          demo10.py

a =10
print(“first time value:”, a)

a = 20
print(“variable is re-initialized, now the value is:”, a)     

a = 30
print(“again the variable is re-initialized, now the value is:”, a)

Output
first time value : 10
variable is re-initialized, now the value is: 20
again the variable is re-initialized, now the value is: 30