Variables and Data Types
Variables and Data Types
types
2
Lesson Objectives
Variables
Use of variable n has made this problem simpler and permits the use of a
computer program to solve this problem.
Variables are labels that are used to represent values stored in computer
memory.
Declaration of variables in a program is essential as the correct amount of
space in memory is allocated by the compiler.
The value of the variable can be changed during execution.
7
Constants
Constants are labels that are used to represent values stored in computer
memory that remain unchanged during the execution of a program.
For example: pi(=3.142) is a constant used to calculate the radius and
circumference of a circle.
8
Example
pi=3.142
Python program to calculate
the area and circumference radius = float(input('Enter the radius of circle: '))
of circle is given. Perimeter=2*pi*radius
Constant pi is assigned the Area = pi*radius*radius
value of 3.142. print("Perimeter of circle is: %.2f" %Perimeter)
The value of radius is stored print("Area of circle is: %.2f" %Area)
in the variable radius.
9
Example
pi=3.142
Output:
radius = float(input('Enter the radius of circle: '))
Perimeter=2*pi*radius
Area = pi*radius*radius
print("Perimeter of circle is: %.2f" %Perimeter)
print("Area of circle is: %.2f" %Area)
10
Data types
To allocate memory for a variable, a program must know how much
memory is to be allocated.
The main types of data used in computer programming are:
a) Integer
b) Real
c) Boolean
d) Character
e) String
11
Data types
Boolean: A Boolean data type accepts only two values: TRUE or FALSE.
Character: A variable or constant that allocates space for a character.
For example: S,d, $, etc
String: A variable or constant that allows several characters in length.
This is used to store names, telephone numbers, etc.
13
Assigning values
Declaring variables
Declaring variables
Real 4 or 8 bytes
Boolean 1 bit
Character 1 byte
Sequence of thinking
process of a programmer
As a programmer, we have to think about the outputs required.
Using this, we analyse the inputs necessary to generate the
required output.
Once inputs and outputs are decided, then we decide about the
process that can be used.
Activity-1
Duration: 15 minutes