Adobe Scan May 12, 2023
Adobe Scan May 12, 2023
INTERACTIVE SESSION
2.3
Pseu
Yinteger
EYpety integerl
clane i n t is
used
ring helle
Ytring)
clase
31
are shown in Tabie
ho
nced to know
data types you
The mmost important 2 . 4N a
typeDescription and Use Python tYpevataabie)
Data positive or negative. Lnt There
areas
Whole numbers, either
intege such as the number of students
Used with quantities
cannot have halt a
student
at a school-you
Variab
Eloat
Positive or negative fractional values tse
a l llos
Rea
that require decimal Python does not use the term considere
Used with numerical values The equivalent data type in P
parts, such as currency
h en e
called floating point abe
Introduction
Prograns need to store
KEY WORDS
variable: a mernory
information This inform
The intormalion stored can be numbers fo red in varat ocation he
Howevet he noerience points dsgained by a claracter in a video yame. StoT the data can
person or item The va casily be some text, such as the name of a be changed during
program execution
s run or used to calculate neu d h may need to be updated as a program
constant: a named
operato. You will be familiar with most of the mathematical oneralors, such as
suhiraction. Iron your maths lessons This chapter explains how memory locatet
and mathematical operators when designing and writing Python used talue can
programs
be used but not
changed during
program execu
2.1 Variables and constants (However h
thor
Programs are normally designed to accept and input data. They also process
warables but indicate
ne ota to prouce the required output. There are different data types that the value of the
will process text data When t i o g mthat checks cmail Laddresses
a data should not be
changed by giving it
constants to refer to these data values. Avariable identifies data that can be a name in all capitals,
changed during the execution of a programA constant is used for data values that e.g Pl 314)
remain tixed, In many computer languages, the data type must be provided when
declaring or initialising variables. The data type is used by the computer to allocate data type:
suitable location in memory. These languages, such as Java, are said to be specification of the
strongly typed. kind of value that a
Python is an example of alooscly typed programming language The computer decides variable will store.
on avariable's data type from the context you provide Compare these two variable declaring variables:
declarations, first in Visual Basic then in Python setting up a variable
In Visual Basic: or constant. It is
important to declare
bim Score An tnteger
or initialise global
In Python: variables
score initialising variables:
giving a variable a
The same declaration, in pseudocode
start (initial) value
Score0 when it is first
Although loosely typed languages are casy for the programmer to write,it is still declared
important to be aware what data type your variables contain.
Variable names
words with underscores It is
Lse all lower case, starting with a letter and joining This
considered good practice to use descriptive names ands readability and reduco
reduces
programmer writes snotes
the need for so much commenting Commenting is where the vmbol. In KEY WORD
In Python these start with the symt
in the program that the computer ignores two slashes (//), You can see examples of
pseudocode, comments arepreceded with commenting: adding
commented code in Demo Task 2.1 in Section 2.6, later in this chapter. human readable
notes to a program.
For example: The comments are
score total 56 / intended to help
Total= 56 X explain how the code
56 X works. Comments
are ignored by the
Further Information: Computer when the
code is executed.
There are 31 reserved words that have a defined function in theown
Python
variable
programming language. These words cannot be used as your In pseudocode,
comments are
hames preceded with two
and as assert break class continue def del elif el se foward slashes //
except finally for from global if import in is lambda and in Python by a
with
honlocal not or pass print raise return try while
hash symbol #
yield.
Constants
Constants are values that do not vary. Constants keep the same value throughout our programns Use all upper case
characters to indicate constants
In Python:
PI = 3.1415
In pseudocode:
cONSTANT PI 3.1415
CUMI
CAMBRIDGE IGCSETM&O LEVEL
(continued)
2 Variables and arithmetic
TIP
In your maths lessons you may have been taught the acronym BIDMAS
(sometimes BODMAS or BOMDAS). The order of mathematical operations in
programming languages is the same as that taught in maths lessons.
e.g. 3x 4 +7 4= 13.75
However, this is very difficult to read and many errors can creep into programs
if we rely on doing this correctly. This is why programmers prefer to use plenty
of brackets, and you should too.
e.g.
>>> (3*4) + (7/4)
13.75
INTERACTIVE SESSION
Data type
value is stored in c after completing the interactive session.
Find out what