Lecture 1, Part 2: Introduction To Computing - Problem Solving and Data Manipulation

Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

Lecture 1, Part 2: Introduction to Computing

–Problem Solving and Data Manipulation

Bijendra Nath Jain <bnjain@iiitd.ac.in> (Section A)


Md. Shad Akhtar <shad.akhtar@iiitd.ac.in> (Section B)

Intro to Programming Monsoon 2023 1


Course outline
• Part 1 Introduction to Computing and Programming (first 2 weeks):
o Problem solving: Problem statement, algorithm design, programming, testing,
debugging
o Scalar data types: integers, floating point, Boolean, others (letters, colours)
o Arithmetic, relational, and logical operators, and expressions
o Data representation of integers, floating point, Boolean
o Composite data structures: string, tuple, list, dictionary, array
o Sample operations on string, tuple, list, dictionary, array
o Algorithms (written in pseudo code) vs. programs
o Variables and constants (literals): association of names with data objects
o A language to write pseudo code
o Programming languages: compiled vs. interpreted programming languages
o Python as a programming language
o Computer organization: processor, volatile and non-volatile memory, I/O

Intro to Programming Monsoon 2023 2


Course outline (may change a bit)
• Part 2 Algorithm design and Programming in Python (balance 11 weeks):
– Arithmetic/Logical/Boolean expressions and their evaluations in Python
– Input/output statements (pseudo code, and in Python)
– Assignment statement (pseudo code, and in Python)
– Conditional statements, with sample applications
– Iterative statements, with sample applications
– Function sub-programs, arguments and scope of variables
– Recursion
– Modules
– Specific data structures in Python (string, tuple, list, dictionary, array), with
sample applications
– Searching and sorting through arrays or lists
– Handling exceptions
– Classes, and object-oriented programming
– (Time permitting) numerical methods: Newton Raphson, integration,
vectors/matrices operations, continuous-time and discrete-event simulation

Intro to Programming Monsoon 2023 3


Scalar and structured data types
• Computing is about ‘data’, and its manipulation
• Data is of:
o Scalar data types
o Structured data types
• Scalar data types:
o {‘None’} only in Python
o Natural number: {0, 1, 2, …}
o (Signed) integer: {…, -2, -1, 0, 1, 2, …}
o (signed) real numbers
o (Signed) floating point number, includes all positive & negative numbers, but only
approximations to rational & irrational numbers:
o {…, -2.1, …, -1/3, …, 0.00, …, 4/3, …, 3.1415926535, π, …, 4.023, …, 2.1*10 89}
o Boolean: {‘False’, ‘True’}
o Characters: {…, A, B, …, Z, a, b, …, z, ‘.’, ‘@’, …}
• Structured data types (will be discussed as we go along):
• String
o Tuple
o List
o Dictionary Intro to Programming Monsoon 2023 4
o Array
Scalar and structured data types
• Computing is about ‘data’, and its manipulation
• Data is of:
o Scalar data types
o Structured data types
• Scalar data types:
o {‘None’} only in Python
o Natural number: {0, 1, 2, …}
o (Signed) integer: {…, -2, -1, 0, 1, 2, …}
o (signed) real numbers
o (Signed) floating point number, includes all positive & negative numbers, but only
approximations to rational & irrational numbers:
o {…, -2.1, …, -1/3, …, 0.00, …, 4/3, …, 3.1415926535, π, …, 4.023, …, 2.1*10 89}
o Boolean: {‘False’, ‘True’}
o Characters: {…, A, B, …, Z, a, b, …, z, ‘.’, ‘@’, …}
• Structured data types (will be discussed as we go along):
• String
o Tuple
o List
o Dictionary Intro to Programming Monsoon 2023 5
o Array
Scalar and structured data types
• Computing is about ‘data’, and its manipulation
• Data is of:
o Scalar data types
o Structured data types
• Scalar data types:
o {‘None’} only in Python
o Natural number: {0, 1, 2, …}
o (Signed) integer: {…, -2, -1, 0, 1, 2, …}
o (signed) real numbers
o (Signed) floating point number, includes all positive & negative numbers, but only
approximations to rational & irrational numbers:
o {…, -2.1, …, -1/3, …, 0.00, …, 4/3, …, 3.1415926535, π, …, 4.023, …, 2.1*10 89}
o Boolean: {‘False’, ‘True’} Truly, the best we
o Characters: {…, A, B, …, Z, a, b, …, z, ‘.’, ‘@’, …} can do to store
• Structured data types (will be discussed as we go along): fraction 0.1 using
• String 16 bits (e.g.) is to
o Tuple save it as
o List 0.099975586
o Dictionary Intro to Programming Monsoon 2023 6
o Array
Scalar and structured data types
• Scalar data types:
o {‘None’} only in Python
o Natural number: {0, 1, 2, …}
o (Signed) integer: {…, -2, -1, 0, 1, 2, …}
o (signed) real numbers
o (Signed) floating point number, includes all positive & negative numbers, but only
approximations to rational & irrational numbers:
o {…, -2.1, …, -1/3, …, 0.00, …, 4/3, …, 3.1415926535, π, …, 4.023, …, 2.1*10 89}
o Boolean: {‘False’, ‘True’}
o Characters: {…, A, B, …, Z, a, b, …, z, ‘.’, ‘@’, …}
• Structured data types (will be discussed as we go along):
• String
o Tuple
o List
o Dictionary
o Array
o Files

Intro to Programming Monsoon 2023 7


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 oolean, 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
8
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 What would be the
▪ int op int int (there is an exception, though) outcome of int / int

▪ 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

Intro to Programming Monsoon 2023 12


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; do we 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

Intro to Programming Monsoon 2023 13


Operations on integers
o Relational or compare operations result in Boolean values
▪ Binary operations:
✔ ‘<‘ such as 5 < 7 is True
✔ ‘>’ such as 5 > 7 is False
✔ ‘≤’ such as 5 ≤ 7 is True ( ‘<=‘ in Python, such as 5 <= 7 is True)
✔ ‘≥’ such as 5 ≥ 7 is False ( ‘>=‘ in Python, such as 5 >= 7 is False)
✔ ‘=‘ such as 5 = 7 is False ( ‘==‘ in Python, such as 5 == 7 is False)
✔ ‘!=‘, or “not equal to”, such as 5 != 7 is True
o Interesting property: there is a “total order” on the set of integers
▪ That is, for any pair of distinct numbers, n1 and n2, either n1 < n2 or n1 > n2.
▪ Equivalently, given a subset of integers, one can always sort them in
non-decreasing or non-increasing order

Intro to Programming Monsoon 2023 14


Operations on integers
o Relational or compare operations result in Boolean values
▪ Binary operations:
✔ ‘<‘ such as 5 < 7 is True
✔ ‘>’ such as 5 > 7 is False
✔ ‘≤’ such as 5 ≤ 7 is True ( ‘<=‘ in Python, such as 5 <= 7 is True)
✔ ‘≥’ such as 5 ≥ 7 is False ( ‘>=‘ in Python, such as 5 >= 7 is False)
✔ ‘=‘ such as 5 = 7 is False ( ‘==‘ in Python, such as 5 == 7 is False)
✔ ‘!=‘, or “not equal to”, such as 5 != 7 is True
o Interesting property: there is a “total order” on the set of integers
▪ That is, for any pair of distinct numbers, n1 and n2, either n1 < n2 or n1 > n2.
▪ Equivalently, given a subset of integers, one can always sort them in
non-decreasing or non-increasing order

Intro to Programming Monsoon 2023 15


Operations on floating point numbers
• Operations on floating point numbers (these are called ‘float’ in Python)
o Arithmetic operations result in floating point values:
▪ Unary operations:
✔ Negate operation, such as – 9.5 = -9.5
▪ Binary operations:
✔ Add operation, such as 3.01 + 4.02 = 7.03
✔ Subtract operation, such as 7.00 – 9.03 = -2.03
✔ Multiply operation, such as -7.1 * 2.0 = -14.2
✔ Exponentiation, e.g. 8.5^2 = 72.25 (this op is a**b in Python,
e.g. 8.5**2)
✔ Division operation, such as -9.6 / 3.0 = -3.2
✔ Division of integers in Python, such as 3/4 = 0.75 (this is performed
after converting integers into floating point numbers)

Intro to Programming Monsoon 2023 16


Operations on floating point numbers
• Operations on floating point numbers (these are called ‘float’ in Python)
o Arithmetic operations result in floating point values:
▪ Unary operations:
✔ Negate operation, such as – 9.5 = -9.5
▪ Binary operations:
✔ Add operation, such as 3.01 + 4.02 = 7.03
✔ Subtract operation, such as 7.00 – 9.03 = -2.03
✔ Multiply operation, such as -7.1 * 2.0 = -14.2
✔ Exponentiation, e.g. 8.5^2 = 72.25 (this op is a**b in Python,
e.g. 8.5**2)
✔ Division operation, such as -9.6 / 3.0 = -3.2
✔ Division of integers in Python, such as 3/4 = 0.75 (this is performed
after converting integers into floating point numbers)

Intro to Programming Monsoon 2023 17


Operations on floating point numbers
• Relational, or compare operations result in Boolean values
▪ Binary operations:
✔ ‘<‘ such as -5.0 < 7.8 is True
✔ ‘>’ such as -5.0 > 7.8 is False
✔ ‘≤’ such as -5.0 ≤ 7.8 is True (this is ‘<=‘ in Python, such as -5.0 ≤ 7.8)
✔ ‘≥’ such as -5.0 ≥ 7.8 is False (this is ‘>=‘ in Python, such as -5.0 ≥ 7.8)
✔ ‘=‘ such as -5.0 = 7.8 is False (this is ‘==‘ in Python, such as -5.0 == 7.8)
✔ ‘!=‘, or “not equal to”, such as 5 != 7 = True
▪ Question: Is 4.0/3.0 == 1.3333333?

Intro to Programming Monsoon 2023 18


Operations on floating point numbers
• Relational, or compare operations result in Boolean values
▪ Binary operations:
✔ ‘<‘ such as -5.0 < 7.8 is True
✔ ‘>’ such as -5.0 > 7.8 is False
✔ ‘≤’ such as -5.0 ≤ 7.8 is True (this is ‘<=‘ in Python, such as -5.0 ≤ 7.8)
✔ ‘≥’ such as -5.0 ≥ 7.8 is False (this is ‘>=‘ in Python, such as -5.0 ≥ 7.8)
✔ ‘=‘ such as -5.0 = 7.8 is False (this is ‘==‘ in Python, such as -5.0 == 7.8)
✔ ‘!=‘, or “not equal to”, such as 5 != 7 = True
▪ Question: Is 4.0/3.0 == 1.3333333?

Intro to Programming Monsoon 2023 19


Operations on floating point numbers
• Relational, or compare operations result in Boolean values
▪ Binary operations:
✔ ‘<‘ such as -5.0 < 7.8 is True
✔ ‘>’ such as -5.0 > 7.8 is False
✔ ‘≤’ such as -5.0 ≤ 7.8 is True (this is ‘<=‘ in Python, such as -5.0 ≤ 7.8)
✔ ‘≥’ such as -5.0 ≥ 7.8 is False (this is ‘>=‘ in Python, such as -5.0 ≥ 7.8)
✔ ‘=‘ such as -5.0 = 7.8 is False (this is ‘==‘ in Python, such as -5.0 == 7.8)
✔ ‘!=‘, or “not equal to”, such as 5 != 7 = True
▪ Question: Is 4.0/3.0 == 1.3333333?
✔ You should be careful while comparing two floating numbers for equality

Intro to Programming Monsoon 2023 20


Operations on Boolean data
• Operations on Boolean data
o Logical operations that result in Boolean values:
▪ Unary:
✔ not , such as not P
▪ Binary:
✔ and, such as P and Q
✔ or, such as P or Q
o The “TRUTH TABLE” below describes the above operations

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

Intro to Programming Monsoon 2023 21


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)

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 ‘(‘ & ‘)’

• Good practice: add parentheses to make the order of evaluation explicit

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 * π)

Intro to Programming Monsoon 2023 28


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 * π)

Intro to Programming Monsoon 2023 29


Precedence rules to evaluate expressions
• Computing is about “data”, and its manipulation
• Evaluating expressions involving scalar data items, and related operations
• “Precedence rules” in Python (check these rules out using interactive mode in Python):
Advice: don’t try to remember these rules. If you can’t tell by looking at the
expression, use parentheses to make it obvious

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

Intro to Programming Monsoon 2023 30


In-class Exercise 1.2
• For Section A, follow: https://tinyurl.com/2p8ayp6y
• For Section B, follow: https://tinyurl.com/3bhthsxt

Intro to Programming Monsoon 2023 31


Structured data types: strings , tuples
https://chat.openai.com/share/eed10e96-0adf-48c0-85bb-862bb0c9bc9f
• Composite data structures: string, tuple, list, dictionary, array, etc.
o We will introduce these as we go along. For the present here are some examples:
o String, an ordered sequence of characters (letters, special characters, etc.), viz.
{…, A, B, …, Z, a, b, …, z, 0, 1, …, 9, ‘.’, ‘,’,’@’, …}
Example:
‘CSE 101 Introduction to Programming’
‘Mango’
Example operations:
fruit = ‘Mango’
fruit[0] is ‘M’, fruit[1] is ‘a’
len(fruit) is 5
len(‘fruit’) is 5
and more, such as ‘+’, ‘*’

Intro to Programming Monsoon 2023 32


Structured data types: strings , tuples
• Composite data structures: string, tuple, list, dictionary, array
o We will introduce these as we go along. For the present here are some examples:
o Tuple, an ordered sequence of scalar or structured data items (including strings)
Example of a tuple:
(‘Mahatma Gandhi’, ‘1869/10/2’)
Example operations:
emailTableEntry1 = (‘Bijendra Jain’, ‘bnjain@gmail.com’)
Bapu = (‘Mahatma Gandhi’, (1869, 10, 2) )
print(Bapu[1][:]) https://chat.openai.com/share/6342a55f-94a8-4a35-8719-89ea1a0829a0

Output:
1869, 10, 2

Intro to Programming Monsoon 2023 33


Structured data types: lists, dictionaries, arrays
• Composite data structures: string, tuple, list, dictionary, array
o We will introduce these as we go along. For the present here are some examples:
o List, an ordered sequence of scalar or structured data items
Example:
primes = [ 2, 3, 5, 7, 11, 13, 17, 18]
Example 0perations :
len(primes) is 8
primes[7] is 18
primes[7] = 19
and many more
o Dictionary, an ordered sequence of (key : value) pairs
Example:
English2Spanish = {'one': 'uno', 'three': 'tres', 'two':
'dos'}
Example operations:
>>> print(English2Spanish['one'])
>>> uno
o Array, like vectors, matrices Intro to Programming Monsoon 2023

Operations: search, sort, dot-product, matrix operations, etc. 34


Structured data types: lists, dictionaries, arrays
• Composite data structures: string, tuple, list, dictionary, array
o We will introduce these as we go along. For the present here are some examples:
o Dictionary, an ordered sequence of (key : value) pairs
Example:
English2Spanish = {'one': 'uno', 'three': 'tres', 'two': 'dos'}
Example operations:
print(English2Spanish['one'])
>>> uno

Intro to Programming Monsoon 2023 35


Structured data types: lists, dictionaries, arrays
• Composite data structures: string, tuple, list, dictionary, array
o We will introduce these as we go along. For the present here are some examples:
o Array, like vectors, matrices
Operations: search, sort, dot-product, matrix operations, etc.

Intro to Programming Monsoon 2023 36


Representation of numbers
• Representation of natural nos., integers, floating point nos., Boolean
• Natural nos.: 0, 1, 2, …
o Can only work with natural nos. limited to {0, 1, …, 2^32 -1} or {0, 1, …, 2^64 – 1} ,
depending upon whether we have 32 bit or 64 bit memory
▪ Question: what is a ‘bit’?
o For example with 8 bit representation we can work only with 0, 1, …, 2^8 – 1 (or 255)
0 = 0 0 0 0 0 0 0 0 , since 0 = 0*27 +0*26 +0*25 +0*24 +0*23 +0*22 +0*21 +0*20
1 = 0 0 0 0 0 0 0 1 , since 1 = 0*27 +0*26 +0*25 +0*24 +0*23 +0*22 +0*21 +1*20
153 = 1 0 0 1 1 0 0 1, since 153 = 1*27 +0*26 +0*25 +1*24 +1*23 +0*22 +0*21 +1*20

Most significant bit Write an algorithm to


Least significant bit compute the 8 bit
representation of any
natural no.
n ε {0, 1, 2, …, 255)

https://profile.iiita.ac.in/bibhas.ghoshal/lecture_slides_coa/Data_Representation.html

Intro to Programming Monsoon 2023 37


Representation of integers
• Representation of (signed) Integers: …, -2, -1, 0, 1, 2, …
o With 32 bit representation, signed integer is best written in “2’s complement”
notation
o Can represent integers in {-2^31, …, -2, -1, 0, 1, 2, … , 2^31 -1}
o For example with 8 bit representation, MSB bit 7 is sign bit 0 for ‘+’, 1 for ‘–’
o Bits 6 through 0 essentially give the magnitude
{-2^7, …, 2, 1, 0, 1, 2, … , 2^7 -1} or {-128, …, -2, -1, 0, 1, 2, … 127}
To be sure:
+127 = 0 1 1 1 1 1 1 1

https://chat.openai.com/share/0ef7dee7-2610-4047-8179-c342ebaefb19
+2 = 00000010 Add 1 HOW TO CONVERT -VE DECIMAL NUMBER TO BINARY

+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

May also read: https://www.geeksforgeeks.org/floating-point-representation-basics/

Intro to Programming Monsoon 2023 39


Representation of floating point numbers
• Representation of natural nos., integers, floating point nos., Boolean
o Question: how is -4.5 represented: = -(4*100 +5*10-1)
▪ ‘decimal’ notation : -4.5(10)
▪ ‘base 2’ notation: -100.1(2)

= -(1*22 +0*21 +0*20 +1*2-1)

May also read: https://www.geeksforgeeks.org/floating-point-representation-basics/

Intro to Programming Monsoon 2023 40


Representation of floating point numbers
• Representation of natural nos., integers, floating point nos., Boolean
o Question: how is -4.5 represented: = -(4*100 +5*10-1)
▪ ‘decimal’ notation : -4.5(10)
▪ ‘base 2’ notation: -100.1(2) = -1.001(2) x 2+2

= -(1*22 +0*21 +0*20 +1*2-1)

May also read: https://www.geeksforgeeks.org/floating-point-representation-basics/

Intro to Programming Monsoon 2023 41


Representation of floating point numbers
• Representation of natural nos., integers, floating point nos., Boolean
o Question: how is -4.5 represented: = -(4*100 +5*10-1)
▪ ‘decimal’ notation : -4.5(10)
▪ ‘base 2’ notation: -100.1(2) = -1.001(2) x 2+2

= -(1*20 +0*2-1 +0*2-2 +1*2-3) * 22

= -(1*22 +0*21 +0*20 +1*2-1)

May also read: https://www.geeksforgeeks.org/floating-point-representation-basics/

Intro to Programming Monsoon 2023 42


Representation of floating point numbers
• Representation of natural nos., integers, floating point nos., Boolean
o Question: how is -4.5 represented:
▪ ‘decimal’ notation : -4.5(10)
▪ ‘base 2’ notation: -100.1(2) = -1.001(2) x 2+2

1 1000 0001 001 000…000

o The exponent is in ‘excess 127’ notation, 127 + exponent


o +1.0 value is represented as:
+0 = 0 01111111 00000000000000000000000
o A zero value and has two special representations:
+0 = 0 00000000 00000000000000000000000, or -0 = 1 00000000
00000000000000000000000

May also read: https://www.geeksforgeeks.org/floating-point-representation-basics/


43
Representation of floating point numbers
• Representation of natural nos., integers, floating point nos., Boolean
o Question: how is -4.5 represented:
▪ ‘decimal’ notation : -4.5(10)
▪ ‘base 2’ notation: -100.1(2) = -1.001(2) x 2+2

1 1000 0001 001 000…000

o The exponent is in ‘excess 127’ notation, 127 + exponent


o +1.0 value is represented as:
+0 = 0 01111111 00000000000000000000000
o A zero value and has two special representations:
+0 = 0 00000000 00000000000000000000000, or -0 = 1 00000000 00000000000000000000000

May also read: https://www.geeksforgeeks.org/floating-point-representation-basics/

44
In-class Exercise 1.3
• For Section A, follow: https://tinyurl.com/ms2p2t4v

• For Section B, follow: https://tinyurl.com/4ewdb95h

Intro to Programming Monsoon 2023 45


Q&A
• On algorithms
• On Python programs
• On testing
• On debugging
• On documentation
• On scalar data items
• On structured data
• On representation of scalar data

Intro to Programming Monsoon 2023 46

You might also like