0% found this document useful (0 votes)
22 views5 pages

Adobe Scan May 12, 2023

This document discusses variables, constants, and data types in computer programming, noting that programs need to store and process different types of information in variables and constants, and that variables represent data that can change while constants refer to fixed values, with the type of data determining how it is stored in memory.

Uploaded by

Aadya sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Adobe Scan May 12, 2023

This document discusses variables, constants, and data types in computer programming, noting that programs need to store and process different types of information in variables and constants, and that variables represent data that can change while constants refer to fixed values, with the type of data determining how it is stored in memory.

Uploaded by

Aadya sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

) CAMBRIDGEIGCSETH&O LEVEL COMPUTER SCIENCE: PROGRAMMING BOON

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

by many programming inthe


Real is the data type used the syllabuses
languages and is also referred to in
str
(for example,
single character or symbol Forex
Char
A,z.S, 6).
Python treats characters as smal
usedot
AChar variable that holds a digit: it cannot be eota
in calculations. 32 my_var g
>2> type (my_var)
cclassstr
> my_var 3
> type (my_var)
cClas5 int'>
characters). str'
More than one character (a string of
String
but also
Used to hold words, names or sentences e.g
pUnctuation, numbers as text, etc. >3> my_atringyellow
2 type (myvar)
<Class str>
>> mobile 0774 333 333
2 type (my var)
<class 'str>
Boolean One of two values, either TRUE or FALSE. bool

Used to indicate the result of a condition. e.g


For example, in a computer game, a Boolean >>>sfx Talue
variable might be used to store whether a player
has chosen to have the sound effects on >>> type(sfx)
cclassbool
Table 2.1: Data types
and arithmetic
operatart
2 Variables

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.

2.2 Types of data


How can we know what data type has been allocated by Python in our programs?
To find outthe data type of a variable or constant being used in a Python proeram.
use the built-in typet) function. Study this interactive session in the Python shell to
sec bow to use this function:
2 Variables and arithmetic

2.3 Pseudo numbers


numbers They are a collection
Telenhone mumbers and 1SBN numbers are not rcally contain spaces or start with
used to uniquely identity an temThese are known as pseudo
not intended to be uscu tn
azero They are String variable. If you store a mobile
numbers and it iS normal to storc
leading zeroes will be removed, while spaces and
phone number as an integer, any
svmbols are not permitted

2.4 Namingnamingconventions in Python


conventions in Python, Here area few of them.
There are avariety of

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

2.5 Arithmetic operators


performedon
numerical datain

operations that can be pseudocode are


There are a number of cquivalent in
your programs
used in Python 1 and their
mportantoperators
h in Table 2.2
shown Description
held in the
Operation Example of use Adds the valuesand num2
variables num1 result in the
Addition Python: stores the
result num1 num2 and
variable result.
Pseudocode:
num1num2 held
Eesult Subtracts the value in
num2 from the value
Subtraction Python: in the result in
# num1 num2 numl and stores
result the variable result.
Pseudocode:
Iesult num num2 held
Multiplies the values
num1 and
MultiplicationPython: in the variables result in
result num1 num2 num2 and stores the
the variable result.
Pseudocode:
esult num1 num2
Raises the value held in
Power of Python: num1 to the power of num2.
result num1 ** num2 e.g. result 3**2
is the Python version of
Pseudocode: 3 and is written 3 ^2 in
result numl^num2
pseudocode
Division Python: Divides the value held in the
variable num1 by the value
result numl/ num2 held in num2 and stores
Pseudocode: |the result in the variable
result.
result num1 / num2
|Integer Python: Finds the number of times
Division num2 can go into num1
result = numl // num2
Pseudocode:
completely, discards the
remainder, and stores
the result in the
result nun1 DIV num2
result.
variable

(continued)
2 Variables and arithmetic

Operation Example of use Description


Modulus Python: Finds the number of times
num2 can go into num1
result = num1 % num2 completely, discards this
Pseudocode: value, and stores the
remainder in the variable
result + num1 MOD num2 result.

Table 2.2: Operators used in Python 3 and pseudocode

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

good time to open up a Python shell and have an interactive session


Now is a entering the
get you started, try
to try out some of these operators yourselt. To
code shown below into the Python shell, pressing return after each line.
>>> a = 7
>>> b = 3
>>> = a/b
>>> type (c)
>>> print (c)

PRACTICE TASK 2.1

Data type
value is stored in c after completing the interactive session.
Find out what

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy