Lecture 1, Part 2: Introduction To Computing - Problem Solving and Data Manipulation
Lecture 1, Part 2: Introduction To Computing - Problem Solving and Data Manipulation
Lecture 1, Part 2: Introduction To Computing - Problem Solving and Data Manipulation
▪ op float float
▪ float op float float
o Relational operations
▪ int op int boolean, such as 4 ≥ 5
▪ float op float boolean
o Logical operations (in the context of Boolean data)
▪ op boolean boolean, such as not P
▪ boolean op boolean boolean
• And operations that are:
o Unary, such as – 7 = -7, or not P
Intro to Programming Monsoon 2023
o Binary, such as 4 * -6 = -24, or P and Q
9
o Expressions, such as 6 + 5 * 3 = 21 or (a ≥ 5 * 3 and a < 5^2)
Operations on scalar data types
• Computing is about ‘data’, and its manipulation
op is short for
• Operations on scalar data ‘operator’ or operation
o Arithmetic operations (op)
▪ op int int, such as – 7 = -7
▪ int op int int (there is an exception, though)
▪ op float float
▪ float op float float
o Relational operations
▪ int op int boolean, such as 4 ≥ 5
▪ float op float boolean
o Logical operations (in the context of Boolean data)
▪ op boolean boolean, such as not P
▪ boolean op boolean boolean
• And operations that are:
o Unary, such as – 7 = -7, or not P
Intro to Programming Monsoon 2023
o Binary, such as 4 * -6 = -24, or P and Q
10
o Expressions, such as 6 + 5 * 3 = 21 or (a ≥ 5 * 3 and a < 5^2)
Operations on scalar data types
• Computing is about ‘data’, and its manipulation
op is short for
• Operations on scalar data ‘operator’ or operation
o Arithmetic operations (op)
▪ op int int, such as – 7 = -7
▪ int op int int (there is an exception, though)
▪ op float >>> float
▪ float op float >>> float
o Relational operations
▪ int op int >>> boolean, such as 4 ≥ 5
▪ float op float >>> boolean
o Logical operations (in the context of Boolean data)
▪ op boolean >>> boolean, such as not P
▪ boolean op boolean >>> boolean
• And operations that are:
o Unary, such as – 7 = 6-7,+or5not * 3P = 21 or (a ≥ 5 * 3 and a < 5^2)
o Binary, such as 4 * -6 = -24, or P and Q
Intro to Programming Monsoon 2023 11
o Expressions, such as
Operations on integers
• Operations on integers (these are called ‘int’ in Python)
o Arithmetic operations result in integer values:
▪ Unary operation:
✔ Negate operation,, such as - 9 = -9
▪ Binary operations:
✔ Add operation, such as 3 + 4 = 7
✔ Subtract operation, such as 7 – 9 = -2
✔ Multiply operation, such as 7 * 8 = 56
✔ Exponentiation, such as 8^2 = 64 (a**b in Python, such as 8**2)
✔ Divide operation such as 6 / 8 why do you need this – we will discuss this
later
✔ ‘mod’ operation, a mod b, or remainder of a when divided by b,
e.g. 70 mod 9 = 7 (a%b in Python, such as 70%9 = 7)
mod operation is useful in computing GCD(a, b)
Example: GCD(21, 15) = GCD(15, 21 mod 15) = GCD(15, 6), …, = 3
P Q not P P and Q P or Q
FALSE FALSE TRUE FALSE FALSE
FALSE TRUE TRUE FALSE TRUE
TRUE FALSE FALSE FALSE TRUE
TRUE TRUE FALSE TRUE TRUE
22
Precedence rules to evaluate expressions
• Evaluating expressions involving scalar data items, and related operations
• Why are “precedence rules” required?
FOUR examples:
1. Consider 6 + 5 * 3 :
– depends upon whether add ‘+’ or multiply ‘*’ is performed first:
6 + (5 * 3) = 21 (if * is performed first)
(6 + 5) * 3 = 33 (if + is performed first)
2. Similarly consider 6 / 2 / 3 = 1 :
(6 / 2) / 3 = 1 (if the first ‘/’ is performed first)
6 / (2 / 3) = 9 (if the second ‘/’ is performed first)
3. Or consider 6 ^ 2 / 2 :
(6^2) / 2 = 18 (if ‘^’ is performed first)
6^(2 / 2) = 6 (if ‘/’ is performed first)
4. Or consider (not True) and False :
(not True) and False = False (if not is performed first)
not (True and False) = True (if and is performed first)
23
Precedence rules to evaluate expressions
• Evaluating expressions involving scalar data items, and related operations
• Why are “precedence rules” required?
FOUR examples:
1. Consider 6 + 5 * 3 :
– depends upon whether add ‘+’ or multiply ‘*’ is performed first:
6 + (5 * 3) = 21 (if * is performed first)
(6 + 5) * 3 = 33 (if + is performed first)
2. Similarly consider 6 / 2 / 3 = 1 :
(6 / 2) / 3 = 1 (if the first ‘/’ is performed first)
6 / (2 / 3) = 9 (if the second ‘/’ is performed first)
3. Or consider 6 ^ 2 / 2 :
(6^2) / 2 = 18 (if ‘^’ is performed first)
6^(2 / 2) = 6 (if ‘/’ is performed first)
4. Or consider (not True) and False :
(not True) and False = False (if not is performed first)
not (True and False) = True (if and is performed first)
24
Precedence rules to evaluate expressions
• Evaluating expressions involving scalar data items, and related operations
• Why are “precedence rules” required?
FOUR examples:
1. Consider 6 + 5 * 3 :
– depends upon whether add ‘+’ or multiply ‘*’ is performed first:
6 + (5 * 3) = 21 (if * is performed first)
(6 + 5) * 3 = 33 (if + is performed first)
2. Similarly consider 6 / 2 / 3 = 1 :
(6 / 2) / 3 = 1 (if the first ‘/’ is performed first)
6 / (2 / 3) = 9 (if the second ‘/’ is performed first)
3. Or consider 6 ^ 2 / 2 :
(6^2) / 2 = 18 (if ‘^’ is performed first)
6^(2 / 2) = 6 (if ‘/’ is performed first)
4. Or consider (not True) and False :
(not True) and False = False (if not is performed first)
not (True and False) = True (if and is performed first)
25
Precedence rules to evaluate expressions
• Evaluating expressions involving scalar data items, and related operations
• Why are “precedence rules” required?
FOUR examples:
1. Consider 6 + 5 * 3 :
– depends upon whether add ‘+’ or multiply ‘*’ is performed first:
6 + (5 * 3) = 21 (if * is performed first)
(6 + 5) * 3 = 33 (if + is performed first)
2. Similarly consider 6 / 2 / 3 = 1 :
(6 / 2) / 3 = 1 (if the first ‘/’ is performed first)
6 / (2 / 3) = 9 (if the second ‘/’ is performed first)
3. Or consider 6 ^ 2 / 2 :
(6^2) / 2 = 18 (if ‘^’ is performed first)
6^(2 / 2) = 6 (if ‘/’ is performed first)
4. Or consider (not True) and False :
(not True) and False = False (if not is performed first)
not (True and False) = True (if and is performed first)
• Clearly, good practice, or if this confusing:
add parentheses to make the order of evaluation explicit 26
Precedence rules to evaluate expressions
• BODMAS Precedence rules
• Evaluating expressions
o A precedence involvingwhich
rule specifies scalaroperations
data items,have
and related operations
higher precedence.
o When two operations have equal precedence then whether evaluation is left to
right or otherwise
o Of course the above are overruled by use of parentheses ‘(‘ & ‘)’
27
Precedence rules to evaluate expressions
• Precedence rules in Python:
• Parentheses have the highest precedence
2 * (3-1) is 4
(1 + 1)**(5 - 2) is 8
• Exponentiation has the next highest precedence
2**1 + 1 is 3 (and not 4)
3 * 1**3 is 3 (and not 27)
• Multiplication & division have same precedence, and higher than addition and
subtraction (‘+’ and ‘-’ have same precedence)
2 * 3 - 1 is 5 (and not 4)
6 + 4 / 2 is 8 (and not 5)
• Operators with same precedence are evaluated left to right (except
exponentiation, evaluated right to left)
X / 2 * π is (X / 2) * π - and not X / (2 * π)
Decreasing precedence
parentheses(…)
Decreasing precedence
x**y
-x
x%y x/y x*y
x-y x+y
x==y x!=y x>=y x>y x<=y x<y
not x
x and y x or y
Output:
1869, 10, 2
https://profile.iiita.ac.in/bibhas.ghoshal/lecture_slides_coa/Data_Representation.html
+1 = 00000001 Add 1
+0 = 00000000 Add 1
-1 = 11111111 Add 1
https://en.wikipedia.org/wiki/Two%27s_complement
-2 = 11111110
…
-128 = 1 0 0 0 0 0 0 0 (pl. check) Intro to Programming Monsoon 2023 38
Representation of floating point numbers
• Representation of natural nos., integers, floating point nos., Boolean
• Floating point nos., such -4.5
o Will have two constraints:
▪ Range: Or how large can the no. be? meaning how small it can be . eg: 2^32 in case of
32 bit systems
▪ Accuracy: Or how accurately can the no. be represented?:
o 32-bit single precision vs. 64-bit double precision (how is range, accuracy
impacted?)
o Even a 64-bit double precision representation is an approximation
▪ Consider representing 1/3 or π
even 64 bit computers cannot be represent 22/7 as it is an irrational number and there has to ben an approximation
44
In-class Exercise 1.3
• For Section A, follow: https://tinyurl.com/ms2p2t4v