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

Variable Op DataTypes

The document outlines a lecture on programming for data analytics, focusing on data manipulation, data types, and storage operations. It covers key concepts such as variable declaration, the difference between source code and object code, and the role of translators like compilers and interpreters. Additionally, it discusses the importance of data types and operators in programming, providing examples and definitions for each.

Uploaded by

matthew thomas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Variable Op DataTypes

The document outlines a lecture on programming for data analytics, focusing on data manipulation, data types, and storage operations. It covers key concepts such as variable declaration, the difference between source code and object code, and the role of translators like compilers and interpreters. Additionally, it discusses the importance of data types and operators in programming, providing examples and definitions for each.

Uploaded by

matthew thomas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Programming for

Data Analytics
Lecture Topics
Data Manipulation, Data Types, Storage, Operations

Prepared by Mrs Arleen Whittaker


1
Objectives

• By the end of this lesson you should be able to:


• Explain how data is manipulated in memory
• Differentiate between assemblers, compilers and interpreters
• Explain the difference between source code and object code
• Differentiate variables, constants and literals
• Explain variable declaration
• Explain the various data types
• Name variables and constants
• Categorize the different types of operators
• Differentiate between operators, expressions and statements
• Evaluate expressions based on precedence and associativity

2
First, Do It By Hand!

Upon being handed a


problem, you may first want to
start coding – don’t!
First solve the problem 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

• Recall that data is represented in 0s and 1s (machine


language – only form directly understood by the
computer)

• Programmers use high-level languages to write


programs that are independent of the CPU

• To execute high-level language programs, they need to


translated into the target computer’s machine
language.

5
Translators

• So how does human written code get turned into


machine code?

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

Compilers translates statements written in a high level


language into a separate machine language
Programme. You can then execute the machine
language programme anytime you wish. After
translation there is no need to run the compiler again
unless you make changes to the high level
programme.
Images Source: http://www.openbookproject.net/thinkcs/python/english2e/ch01.html
8
Interpreters

Interpreters simultaneously translates and executes the


statements written in a high level language. As the
interpreter reads each individual statement in the high-level
language program it translates it into machine language
code and then executes it . This process is repeated for
every statement in the program.
Images Source: http://www.openbookproject.net/thinkcs/python/english2e/ch01.html 9
Assemblers

• 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.

• Different CPU families will require different assemblers.

10
Integrated Development
Environment (IDE)

• An Integrated Development Environment (IDE) is a


software package.
• An IDE, or Integrated Development Environment,
enables programmers to consolidate the different
aspects of writing a computer program.
• IDEs increase programmer productivity by
combining common activities of writing software into
a single application: editing/writing source code,
building executables, and debugging.

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

An integrated development environment, such as THONNY or


CODE BLOCKS is a package that normally combines the
editor, compiler, linker and loader, as well as debugging tools.
13
First, Do It By Hand!

Upon being handed a


problem, you may first want to
start coding – don’t!
First solve the problem by
hand.

14
Data Manipulation, Data Types, Storage,
Operations

Variables & Constants

15
Variables

• A named location (e.g. num1) in the computer’s main memory


(RAM) where a program can store a value and change it as the
programme executes.
• Programmers use variable as a reference name for a specific
value of the variable

16
Variables

• Computer use 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)

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.

• Constants (symbolic constants)


• A named identifier (named location in memory) that that is given a value
which once specified cannot be changed during program execution (e.g.
specifying a value for PI)

• Literals (literal constants)


• A specific numeric, alphabetical, alphanumeric value or special symbol
that does not change during program execution (e.g. 1, ‘B’)

18
More on Variables

• All variables must be declared before they are used


• Declaration is the process of reserving a portion in main
memory (RAM) for storing the contents of a variable. In
many high level languages, the programmer must write a
specific statement to reserve that portion in RAM before the
variable can be used. In many cases the variable type need to
be specified so that the computer will know how many spaces
to reserve.
• More on variable declarations later!

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

• Named or Symbolic constants


• Customary they contain all uppercase letters to distinguish them from
variables
• A specific memory location and label is provided
• When the program is executed, named constant is given a value and
then referred to by name
• Actual value specified only once
• Whenever the value is needed, the name is used in a similar way as
variables (value is replaced)

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 )

• Consists of all single-digit numbers, letters and special symbols


available to the computer (e.g. see http://www.asciitable.com) placed
within quotation marks
• Examples: ‘A’, ‘z’, ‘7’, ‘*’
• Can be compared and arranged in alphabetical order (computer gives
each character a number)
• Cannot be used for calculations even if they consist only of numbers
(e.g. “4” + “3” is not 7!)
• Characters can be joined together (concatenation) (e.g. “4” + “3” =
“43”)
Helpful in joining first name with last name
or joining pieces of data together to make
a code
27
Basic Data Types

• 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

Data Types PYTHON

32
The data type specific to PYTHON language will be
discussed later

33
Data Manipulation, Data Types, Storage,
Operations

Common Mathematical Operators


& Functions

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

ABS Determines the absolute value of a ABS(-9) = 9


number
INT Largest Integer less than or equal to INT(8.88) = 8
35
a number
Data Manipulation, Data Types, Storage, Operations

MATHEMATICAL OPERATORS & FUNCTIONS

36
Types of Operators

• Programming languages support the following types of operators.


• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
• We will look at the first four. Please read up on the other Bitwise,
Membership and Identity operators

37
Arithmetic Operators
Assume variable a holds 10 and variable b holds 20, then:
Operator Description Example

+ Addition Adds values on either side of the operator. a + b = 30


- Subtraction Subtracts right hand operand from left hand operand. a – b = -10

* Multiplication Multiplies values on either side of the operator a * b = 200


/ Division Divides left hand operand by right hand operand b/a=2
% Modulus Divides left hand operand by right hand operand and returns b%a=0
remainder
** Exponent Performs exponential (power) calculation on operators a**b =10 to the
power 20
// Floor Division https://www.youtube.com/watch? 9//2 = 4 and 9.0//2.0
v=6kWLMD4cGwk = 4.0, -11//3 = -4,
-11.0//3 = 4.0
- The division of operands where the result is the quotient in
which the digits after the decimal point are removed. But if
one of the operands is negative, the result is floored, i.e., 38
rounded away from zero (towards negative infinity) −
Comparison (Relational) Operator
To allow the program to make decisions, and to control repetitive instructions
Assume variable a holds 10 and variable b holds 20, then:
Operator Description Example
== If the values of two operands are equal, then the condition (a == b) is not true.
becomes true.
!= If values of two operands are not equal, then condition becomes (a != b) is true.
true.
<> If values of two operands are not equal, then condition becomes (a <> b) is true. This is
true. similar to != operator.
> If the value of left operand is greater than the value of right (a > b) is not true.
operand, then condition becomes true.
< If the value of left operand is less than the value of right (a < b) is true.
operand, then condition becomes true.
>= If the value of left operand is greater than or equal to the value (a >= b) is not true.
of right operand, then condition becomes true.
<= If the value of left operand is less than or equal to the value of (a <= b) is true.
right operand, then condition becomes true.
39
Assignment Operator
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

**= Exponent Performs exponential (power) calculation on operators and assign


AND value to the left 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

Expressions versus Statements


Evaluation of Expressions

42
Expression versus Statements
• An expression in a programming language is anything that the
program computes and yields a value.

• In computer programming, a statements is the action that the


program takes. They do not return any result:
print "Hello, world!"
print "Strange things are afoot...“
A computer program is made up of a series of statements and may
also consist of expressions. More on statements in future lessons!

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

• Operators are symbols that instruct the computer to perform a


single, simple task, and most consist of just one or two
characters ( =, +, *, <=, or == - note that spaces between two-
character operators is not allowed).

• Operands are expressions or values on which an operator operates


or works (often constants or variables but sub-expressions are
also permitted).
t=u+v
• In the above ‘t’, ‘u’ and ‘v’ are operands and ‘=‘ and ‘+’ are
operators.

Source: http://icarus.cs.weber.edu/~dab/cs1410/textbook/2.Core/operators.html
46
More on Operands & Operators

• Operands may be constants, variables or complex


expressions.

Source: http://icarus.cs.weber.edu/~dab/cs1410/textbook/2.Core/operators.html
47
Operator Precedence

• All operators have two characteristics that affect the


order in which they are evaluated: precedence and
associativity. The rules that establish these
characteristics are either based on similar properties
from algebra or follow the best practices that have
evolved over decades of programming language
development.

Source: http://icarus.cs.weber.edu/~dab/cs1410/textbook/2.Core/operators.html
48
Precedence

• Operators with higher precedence are evaluated before operators with


lower precedence. Don’t forget BOMDAS!
• Brackets first
• Orders (i.e. Powers and Square Roots, etc.)
• Multiplication and Division (left-to-right)
• Addition and Subtraction (left-to-right)
• Hence:
• *, /, and % all have the same precedence
• + and - both have the same precedence, which is lower than *, /, and %
• The precedence of = is lower than the precedence of any of the arithmetic
operators

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

• Associativity determines the direction (left or right) in


which operators with the same precedence are evaluated.
operators are either left associative (evaluated left to right)
or right associative (evaluated right to left).
• Sometimes associativity is also called grouping: operators
are grouped left to right or are grouped right to left.
• All of the arithmetic operators are left associative
(evaluated left to right), so in the expression a + b + c, a +
b is evaluated first and then c is added to the result.

Source: http://icarus.cs.weber.edu/~dab/cs1410/textbook/2.Core/operators.html 51
More on Associativity

• The assignment operator is right associative (evaluated right to


left). Most of the time the associativity of the assignment
operator is not significant because its precedence is lower than
the other operators appearing in the same statement.

• In the statement a = (4 + 2) * 3;, the expression on the right side


of = is evaluated and then the result is stored in variable a. The
assignment operator has one unusual characteristic: it produces a
value (the value just stored in the variable appearing on the left
of the = operation just executed).

Source: http://icarus.cs.weber.edu/~dab/cs1410/textbook/2.Core/operators.html
52
First, Do It By Hand!

Upon being handed a


problem, you may first want to
start coding – don’t!
First solve the problem by
hand.

53
Any Questions

54
THANK YOU!!!!!

55

You might also like