Variables
Before getting into variables, let us see what is a program.
• Program is a set of instructions written in a programming language to
perform tasks.
• A program has two important elements.
1. Data : It is a information such as facts and numbers upon which
operations are performed.
2. Instructions: A set of steps to perform operations upon data.
• Every program needs data and instructions irrespective of the
programming language used.
• Instructions without data is useless.
Let us see what are Variables?.
• Data in a program is handled by variables.
• Variables are the names given to the data stored in a memory
location.
• Variable is the identi er, reference to the data.
Lets us understand a variable with an example.
age = 15
Variable Data
fi
Another example
name = ‘soap'
price = 15
weight= 10
Variable Data
• Python variable declaration:
- Unlike other programming languages, in python variable is declared
and initialised at the same time.
“=“ Assignment operator
a = 10
Declaration Initialisation
- Declaring a variable is giving the name to the data.
- Initialisation is storing the value in a variable.
• In python declaring a variable without initialising or say with out the
data is not allowed.
b= 12.5 ( declaration and initialisation)
c (only declaration , is not allowed)
• Unlike other programming languages like c, c++, java, in python
declaration of data type for a variable is not required.
• The value assigned to the variable determines the data type of the
variable.
• Declaration and initialisation of multiple variables:
- In python multiple variables can be declared and initialised in a
single statement.
Example:
a,b,c = 5,10,15
Here:
a=5,b=10,c=15 , values will be stored in the respective
variables in the order they are declared.
name, price,weight=‘soap’,10,15
Here:
name=‘soap’, price=10, weight=15
Example : Assigning same value to multiple variables:
x, y, z=1,1,1 or x=y=z=1