Lesson 1 - Variables and Data Types
Lesson 1 - Variables and Data Types
GCSE Python
teachcomputerscience.com
2
Lesson Objectives
teachcomputerscience.com
1.
Content
teachcomputerscience.com
4
Variables
▪ Variables are labels that are used to represent values stored in computer
memory.
▪ The declaration of variables in a program is essential in some
programming languages (C, C++, Java, etc.) as the correct amount of space
in memory is allocated by the compiler.
▪ Python is a dynamically typed language and variables are automatically
assigned data types when they are assigned their first value.
▪ The value of the variable can be changed during program execution.
teachcomputerscience.com
5
teachcomputerscience.com
6
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.
teachcomputerscience.com
7
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
teachcomputerscience.com
8
Assignment Value
teachcomputerscience.com
11
teachcomputerscience.com
13
PYTHON: Java:
name = “Miss Doherty” string name = “Miss Doherty”
Variable declaration
▪ Python is a dynamically typed programming language. The variables are
assigned data types when the first data is entered to it.
▪ In some programming languages, it is essential to declare variables
before assigning values to them.
▪ For example, in visual basic, a number is declared as integer and then
initialised as given,
Dim num As Integer = 0
teachcomputerscience.com
14
Variable declaration
▪ Whereas, in Python, we directly assign value to the variable and the
computer decides what data type is suitable for the variable. Therefore,
in Python, the statement is simply,
num = 0
teachcomputerscience.com
15
Converting values
teachcomputerscience.com
16
Using float()
▪ Consider the code below in which an integer is
converted to a floating-point number.
num1=-1
num2=float(num1)
print('num1 = ',num1 ,'num2 = ' ,num2)
print('Type of num1= ', type(num1))
print('Type of num2= ', type(num2))
teachcomputerscience.com
17
Using int()
▪ Consider the code given below that converts a
floating-point number to an integer.
num1=-31.7913
num2=int(num1)
print('num1 = ',num1 ,'num2 = ' ,num2)
print('Type of num1= ', type(num1))
print('Type of num2= ', type(num2))
teachcomputerscience.com
18
Using str()
▪ Consider a variable that stores a phone
number. Initially, it is assigned an integer
data type. A programmer converts its data
type to a string using the str() function.
var=9781299152
varNew = str (var)
Mathematical operations cannot
print('var = ',var ,'varNew = ' ,varNew)
be performed on variables of
print('Type of var= ', type(var)) string data types.
print('Type of varNew= ', type(varNew))
teachcomputerscience.com
19
Why phone numbers cannot
be stored in numerical data
types?
▪ An integer or float data type cannot be
used to store phone numbers. This is
because leading zeros in number data
types are not permitted.
▪ For example, if we try to assign var =
0123457891, this will raise a syntax error.
▪ Therefore, phone numbers are always
stored using a string data type.
teachcomputerscience.com
20
Naming identifiers
▪ While naming variables and constants, it is important to follow the given
guidelines:
• Follow the naming conventions of Python such as beginning variable
names with lowercase letters. For example, number, value, variable,
length, width and breadth.
• Use all uppercase letters for naming constants. For example, PI. This
way programmers can make sure that this value is not changed
throughout the program. NAME, TAX,
• Use meaningful names for variables. For example, use total and
average instead of x and y. teachcomputerscience.com
21
Naming identifiers
• Do not use long names. For example, tempC and tempF for
temperature in Celsius and temperature in Fahrenheit respectively.
• Use camel caps to separate words. For example, totalSales and
averageSales. Another way to separate words in variable names is to
use an underscore. For example, total_sales and average_sales.
• All variable names are case-sensitive. For example, tempCelsius is
different from tempcelsius.
teachcomputerscience.com
22
teachcomputerscience.com
23
teachcomputerscience.com
25