Variable Op DataTypes
Variable Op DataTypes
Data Analytics
Lecture Topics
Data Manipulation, Data Types, Storage, Operations
2
First, Do It By Hand!
3
Data Manipulation in Memory
• Computer programs and its associated data must be loaded into
memory (RAM) for them to be used
• Data is represented in a computer in binary form (combinations of
0s and 1s)
• Computers manipulate data by accessing the location or memory
address where the data is located or stored
• Each piece of data is stored at a specific address
• An identifier (label/constant/variable) keeps track of memory
addresses
• Identifiers allow for symbolic naming of addresses
• Names can be used to refer to data
4
Data Manipulation in Memory
5
Translators
This is made
possible by the
use of translators!
6
What are Translators?
• Translators are programs that convert high level language commands:
• print, IF, For etc.
• …into a set of machine code commands:
• 1011, 11001, 11000011110 etc
• …so that the CPU can process the data!
• There are two ways in which translators work:
1. Take the whole code and convert it into machine code before running it
(known as compiling).
2. Take the code one instruction at a time, translate and run the instruction,
before translating the next instruction (known as interpreting).
7
Compilers
• This type of translator is used for Assembly Language (not High Level Languages).
• It converts mnemonic assembly language instructions into machine code.
• Every CPU has an instruction set made up of assembly language mnemonics.
• And every CPU comes with a program called an assembler that translates
mnemonics directly into machine code that the CPU can read.
10
Integrated Development
Environment (IDE)
11
Integrated Development
Environment (IDE)
• IDEs will be specific to a certain high level language (e.g.:
PyScripter is built for Python).
• They will normally consist of:
• Source Code Editor – allowing the writing and editing of code
• Automation Tools – automate tasks such as finishing off key words and indenting on
your behalf
• Error Diagnostics - Debugger – identifies logic and syntax errors and shows
where they are.
• Translators - Interpreter – allows source code to be translated into
machine code one line at a time for testing
• Translators - Compiler – converts entire source code into machine code so it can be run
as its own individual program file.
• Auto-Documentation – stores lists of variables, modules, functions calls etc which are
documented for other programmers.
12
Source Code versus Object Code
• The statements the programmer writes in a high level language
is called source code (or simply code). The programmer first
type the source code into a programme known as a code
editor and then uses a compiler to translate it into machine
language program or an interpreter to translate and execute it
at the same time.
• Object code is code in machine readable form
14
Data Manipulation, Data Types, Storage,
Operations
15
Variables
16
Variables
StudID StudID
1600345 1690567
First Execution Next Execution
17
Variables and Constants
• Variables
• User-defined identifiers used to name memory locations that will hold
data (e.g. num1). Its value may change during programme execution.
18
More on Variables
19
More on Variables
• Programmers use variable as a reference name for a specific
value of the variable
• Computer uses variable name as a reference to help it find the
value in its memory
Example: Consider your student ID
• Actual number may change during the program execution (or
multiple executions)
• The variable name should be consistent with what the value for
the variable represents e.g. StudID
StudID StudID
1600345 1690567
20
First Execution Next Execution
More on Variables
• As was stated before, each has a name, data type (e.g. real,
integer, string) and should have a value
• Names should:
• Be logical, descriptive, useful (not too long or too short)
• Not contain restricted characters or symbols
• Not begin with a digit or be a reserved keyword
• Not have spaces between the words
• Not be too similar (be consistent in case)
• Values can change within a program
num1 = 20
num1 = num1 * 2
21
More on Constants
22
More on Variables & Constants
• Literals
• Constants that are not named
• Data is typed directly in the code as the value itself
• Not a memory location but can be assigned to one if
required
23
Data Manipulation, Data Types, Storage,
Operations
Data Types
24
Data Types
• Computers need data (unorganized facts) to process solutions
• Data is input to the computer, which then processes it and returns
information or output to the user
• Important to select the appropriate data type each variable will use
• Data type determines:
• What kinds of data can be stored in the memory location
• Amount of memory required to store the data
• Range (set) of values data can have
• The storage data type and the data type of the data being assigned should
be consistent.
25
Basic Data Types
• Numeric
• Integers: (±) whole numbers (e.g. 960, -4)
• Used when there is no reason for partial numbers (e.g. for counting) or if
specified
• Real numbers: (±) floating point (e.g. 3.78, 0.005, -9.5)
• Used for scientific notation, currency and other such calculations where
partial numbers are required
• No commas should be used when entering numbers
• Fractions must be entered in decimal form
• Numbers such as an account number, zip code, TRN etc. which do not
have calculations performed on them are usually specified as text data.
26
Basic Data Types
• Character (or Alphanumeric )
• String
• One or a combination of characters enclosed within quotes (note that
some languages do not differentiate between characters and string
data)
• Data items that will not have mathematical computations performed
on them are normally designated as string data
• Examples:
• addresses, names, telephone numbers, TRN numbers
• student ID numbers, motor vehicle registration no
• numbers with leading zeroes (00005678))
• Similar operations to characters
28
Basic Data Types
• Logical
• Consists of two values – True and False (Could also be yes, T or Y for
TRUE and no, F or N for FALSE)
• Used in making yes/no decisions (e.g. checking to see if a student is
enrolled on a module)
• These are NOT string data!
29
Data Manipulation, Data Types, Storage,
Operations
Data Types in C
30
The data type specific to the C language will be discussed later
31
Data Manipulation, Data Types, Storage,
Operations
32
The data type specific to PYTHON language will be
discussed later
33
Data Manipulation, Data Types, Storage,
Operations
34
Operator What it does Example
Function
+ Add 3+2=5
- Subtract 5–4=1
* Multiply 3 * 4 = 12
/ Divide 9/3=3
^ Raise a number to the power of 3^2=9
** another 3**2 =9
MOD (%) Gives remainder when two numbers 9 MOD 3 = 0
are divided 9%3=0
SQRT Determine square root of a number SQRT(9) = 3
36
Types of Operators
37
Arithmetic Operators
Assume variable a holds 10 and variable b holds 20, then:
Operator Description Example
= Assigns values from right side operands to left side operand c = a + b assigns value of a + b
into c
+= Add AND It adds right operand to the left operand and assign the result to left
operand c += a is equivalent to c = c + a
-= Subtract It subtracts right operand from the left operand and assign the result
AND to left operand c -= a is equivalent to c = c - a
*= Multiply It multiplies right operand with the left operand and assign the result
AND to left operand c *= a is equivalent to c = c * a
/= Divide AND It divides left operand with the right operand and assign the result to
left operand c /= a is equivalent to c = c / a
%= Modulus It takes modulus using two operands and assign the result to left
AND operand c %= a is equivalent to c = c % a
//= Floor It performs floor division on operators and assign value to the left
Division operand c //= a is equivalent to c40= c // a
Logical Operators
41
Data Manipulation, Data Types, Storage, Operations
42
Expression versus Statements
• An expression in a programming language is anything that the
program computes and yields a value.
http://farside.ph.utexas.edu/teaching/329/lectures/node11.html
43
Expressions
• The expression may consist of a single entity, such as a
constant or variable, or it may consist of some
combination of such entities, interconnected by one or
more operators (+, =, * etc.). Expressions can also represent
logical conditions which are either true or false.
• Consider the following:
a+b
• The first expression, which employs the addition operator
(+), represents the sum of the content of the variable ‘a’
and the content of the variable ‘b’.
http://farside.ph.utexas.edu/teaching/329/lectures/node11.html
44
More on Expressions
x=y
• The above expression involves the assignment operator (=), and causes
the value represented by y to be assigned to x.
t=u+v
• In the third expression above, the value of the expression (u + v) is
assigned to t.
x <= y
• The fourth expression takes the value 1 (true) if the value of x is less
than or equal to the value of y. Otherwise, the expression takes the value
0 (false). Here, <= is a relational operator that compares the values of x
and y.
http://farside.ph.utexas.edu/teaching/329/lectures/node11.html
45
Operands & Operators
Source: http://icarus.cs.weber.edu/~dab/cs1410/textbook/2.Core/operators.html
46
More on Operands & Operators
Source: http://icarus.cs.weber.edu/~dab/cs1410/textbook/2.Core/operators.html
47
Operator Precedence
Source: http://icarus.cs.weber.edu/~dab/cs1410/textbook/2.Core/operators.html
48
Precedence
Source: http://icarus.cs.weber.edu/~dab/cs1410/textbook/2.Core/operators.html 49
More on Precedence
• If ‘a’ is a defined variable, then in the expression a = 4 + 2 * 3, is
evaluated in this order:
• 2*3
• 4+6
• a = 10
• Parentheses are used to alter the order of evaluation. If, in the
example above, the programmer wanted the addition operation
to take place first, then the expression would be rewritten as: a =
(4 + 2) * 3, which is evaluated in this order:
• 4+2
• 6*3
• a = 18
Source: http://icarus.cs.weber.edu/~dab/cs1410/textbook/2.Core/operators.html 50
Associativity
Source: http://icarus.cs.weber.edu/~dab/cs1410/textbook/2.Core/operators.html 51
More on Associativity
Source: http://icarus.cs.weber.edu/~dab/cs1410/textbook/2.Core/operators.html
52
First, Do It By Hand!
53
Any Questions
54
THANK YOU!!!!!
55