0% found this document useful (0 votes)
49 views

Variables and Data Types

This document discusses variables, constants, and data types in programming. It defines variables as labels that represent values in memory that can change, while constants represent fixed values. The main data types are integers, floats, Booleans, characters, and strings. Programming languages require variables and constants to be declared with a specific data type to allocate the proper memory size. Input and output statements are used to get data into programs and display results.

Uploaded by

rajvir.ramdas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Variables and Data Types

This document discusses variables, constants, and data types in programming. It defines variables as labels that represent values in memory that can change, while constants represent fixed values. The main data types are integers, floats, Booleans, characters, and strings. Programming languages require variables and constants to be declared with a specific data type to allocate the proper memory size. Input and output statements are used to get data into programs and display results.

Uploaded by

rajvir.ramdas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Variables and data

types
2

Lesson Objectives

Students will learn about:


 Significance of variables and constants in programming
 Different data types
 Estimating the size of a data file
 Sequence of thinking process of a programmer
 Input and output statements in Python programming language
1.
Content
4

Why do we need variables?

 The representation of real-time components of a problem into


variables allows us to represent the problem in such a way that it can
be solved by a computer program.
 Here is a simple mathematical problem. A plumber earns $20 per
hour. In addition to that he earns a bonus of $50 for each job he
completes. In a week, if he works for 22 hours and earns $740
including a bonus, how many jobs does he complete in that week?
 Can you represent the this problem in terms of variables?
5

Why do we need variables?


A plumber earns $20 per hour. In Let number of project completed is n.
addition to that he earns a bonus Total hourly rate = $20 x 22 = $440
of $50 for each job he completes.
Total bonus = $50 x n = 50n
In a week, if he works for 22
hours and earns $740 including a Total income = $740 = 50n+440
bonus, how many jobs does he 50n=740-440
complete in that week?
50n=300
n=6
The plumber completes 6 jobs in a week.
6

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

Integer and Real data types


 A positive or negative whole number used
int_num = int(50) #declaring as integer
in mathematical operations.
float_num=float(50) #declaring as float
 A positive or negative number that allows
decimals and fractions in mathematical print("integer num= ",int_num)
operations. print("float num= ",float_num)
 Python program to understand the
difference between integer and real
number (float) is given.
 # symbol is used in Python programming
language to add comments.
12

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

 In Python, = symbol is used to assign values to variables and constants.


 Some programming languages use == symbol to assign values.
14

Declaring variables

 Programming languages require each and every variable and constant


to be declared at the start of the program.
 This is done to allocate the required amount of memory for that variable
or constant.
 It is also important to initialise a variable or constant to avoid usage of
previously stored values in a location. Not initialising a variable may
result in wrong answers.
15

Declaring variables

 In Python programming language, the variables are assigned when the


first data is entered to it.
 The variables are typecast by using commands such as float, int or str to
make them a floating-point, integer or string value. For example:
radius = float(input('Enter the radius of circle: '))
 In the above statement radius is typecast as floating-point variable.
16

Amount of memory required

Integer 2 or 4 bytes (can also be set to other values:1 to 8 bytes)

Real 4 or 8 bytes

Boolean 1 bit

Character 1 byte

String No limits (User may choose according to requirements)


17

Estimating size of a data file


 To estimate the size of a data file, the data size for each field is added and
multiplied by the number of records in the file.
 A file contains details of 1000 students:
 student’s identity number of 7 digits
 student’s name (up to 20 characters)
 mother’s name (up to 20 characters)
 father’s name (up to 20 characters)
 address (up to 30 characters)
 telephone number (up to 11 digits)
 email address (up to 30 characters)
 total percentage in final exams
 a Boolean data type representing whether the student is promoted or not.
 Estimate the size of this file.
18

Field Type Size Field Type Size

student’s identity Percentage in final exams Real 4B


string 7B
number

Promoted to next level Boolean 1 bit =1/8 B


student’s name string 20 B

mother’s name string 20 B Maximum space required for


142.125 B
each student

father’s name string 20 B


Multiplied by number of 142125 B =
Address string 30 B students 142.125 KB

telephone number string 11 B


Adding 10% for overheads
156.3375 KB
(14.2125 KB)
email address string 30 B
19

Input and Output

 In lesson 1, we learnt the input-process-output model a computer.


This is how a program works.
 It takes input from the user, processes it and then displays the
output.

Input Process Output


20

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.

Output Input Process


21

Input and Output


statements

 In Python programming language, num1=int(input('Enter the first number '))


the variables are typecast during num2=int(input('Enter the second number '))
the input statements.
print ('The sum is ', num1+num2)
 The output statement is used to
display output.
 A simple Python program to
calculate the sum of two numbers
is given.
22

Let’s review some concepts


Variables Constants Data types
Integer
Variables are labels that are used Constants are labels that are
Real
to represent values stored in used to represent values stored
Boolean
computer memory. in computer memory that remain
Character
unchanged during the execution
String
of a program.

Initialising a variable Estimating size of a data file Typecasting in Python


It is important to initialise a To estimate the size of a data file, The variables are typecast by
variable or constant to avoid the data size for each field is using commands such as float,
usage of previously stored values added and multiplied by the int or str to make them a
in a location. Not initialising a number of records in the file. floating-point, integer or string
variable may result in wrong value.
answers.
2.
Activity
24

Activity-1
Duration: 15 minutes

1. Estimate the size of a data file, that contains details of 3000


employees: employee identity number of 5 digits, employee’s name
(up to 20 characters), address (up to 30 characters), telephone
number (up to 11 digits), email address (up to 30 characters),
department (up to 15 characters) and salary.
3.
End of topic questions
26

End of topic questions

1. How are constants different from variables?


2. Why is it important to declare variables?
3. What are the different data types available in Python
programming language for storing numbers? How much memory
does each of these data types occupy?
4. What data type is used to store a sentence?
5. What is typecasting?
27

End of topic questions

6. In what sequence, does a programmer think to create a


program?
7. How are comments added in Python programming language?
Why are comments used in a program?

You might also like