Chapter 3 - Complex Data Type (Python)
Chapter 3 - Complex Data Type (Python)
SCIENTIFIC
SKEE1033
COMPLEX DATA
TYPE
( WEEK 3 )
SKEE 1033 2
OPERATION
STATEMENT FUNCTION
TYPE
Execute statements specified Boolean
number of times using
while COMMAND
BOOLEAN OPERATOR
or ITERATION PROTOCOL for
Direct instructions
often used in the
syntax command line
3. DECISION
Execute statements if if REPL: Read-Eval-Print Loop, which
condition is TRUE using if-else is an interactive programming
if-elif-else environment that takes single user
BOOLEAN OPERATOR
inputs (commands), evaluates them,
and returns the result to the user.
SKEE 1033
3
ARITHMETIC OPERATION
INTRODUCTION
• Python, particularly with libraries like NumPy, supports two main types of
arithmetic operations: element-wise operations and matrix operations.
• Matrix operations
Use @ operator or functions like [Link]() , [Link]() and
[Link] for linear algebra rules
• Element-wise (Array) Operations:
Execute computations on each corresponding element and support
multidimensional arrays.
• The differentiation between element-wise and matrix operations is
explicit.
• Element-wise operations use standard arithmetic operators (+, -, *, /)
when working with NumPy arrays.
• Both element-wise and matrix operations are the same for addition (+)
and subtraction (-).
SKEE 1033 5
ARITHMETIC OPERATOR
Matrix and element-wise operation in Python (NumPy)
Operation Algebraic Python Matrix Operation Python Array (Element-wise)
Operation
Addition 𝑎𝑎 + 𝑏𝑏 𝑎𝑎 + 𝑏𝑏 𝑎𝑎 + 𝑏𝑏
Subtraction 𝑎𝑎 − 𝑏𝑏 𝑎𝑎 − 𝑏𝑏 𝑎𝑎 − 𝑏𝑏
Division 𝑎𝑎/𝑏𝑏 N/A 𝑎𝑎/𝑏𝑏
Multiplication 𝑎𝑎 × 𝑏𝑏 𝑎𝑎 @ 𝑏𝑏 or [Link](𝑎𝑎, 𝑏𝑏) 𝑎𝑎 ∗ 𝑏𝑏
Right Division (A / B) 𝑋𝑋𝑋𝑋 = 𝑏𝑏 [Link](a. T, b. T). T N/A
Left Division (A \ B) 𝑎𝑎𝑎𝑎 = 𝑏𝑏 [Link](a, b) N/A
Power 𝑎𝑎𝑏𝑏 [Link].matrix_power(𝑎𝑎, 𝑏𝑏) 𝑎𝑎∗∗ 𝑏𝑏
Transpose 𝑎𝑎T 𝑎𝑎. T N/A
Precedence
Order Operation
1 Parentheses
2 Power
3 Transpose
4 Multiplication & Division, left to right
5 Addition & Subtraction
6 Colon : (Slice Operation)
SKEE 1033 6
SKEE 1033 7
KEY DIFFERENCES BETWEEN
[Link] AND [Link] IN NUMPY
Dimensionality
[Link]: Multi-dimensional (1D, 2D, 3D, etc.)
[Link]: Always 2D (rows and columns)
Multiplication Handling
[Link]: * is element-wise multiplication. Use @ or [Link]() for matrix
multiplication
[Link]: * is matrix multiplication
Type of Object
[Link]: Slicing retains the same array type.
[Link]: Always returns a 2D matrix when sliced.
Recommendation
[Link]: More flexible and preferred for most tasks.
[Link]: Legacy; less common in modern projects.
SKEE 1033 8
MATRIX OPERATOR
MATRIX OPERATION
Example 1: Python script that demonstrates only matrix operations,
focusing on addition, subtraction, multiplication, right division, left
division, power, and transpose using NumPy.
import numpy as np
print("Matrix A:")
print(A)
Matrix A:
[[1 2]
[3 4]]
print("\nMatrix B:")
print(B)
Matrix B:
[[5 6]
[7 8]]
SKEE 1033 10
MATRIX OPERATION
Example 1 (continued):
# 1. Matrix Addition Matrix A: Matrix B:
matrix_add_result = A + B [[1 2] [[5 6]
print("\nMatrix Addition (A + B):") [3 4]] [7 8]]
print(matrix_add_result)
Matrix Addition (A + B):
[[ 6 8]
[10 12]]
# 2. Matrix Subtraction
matrix_sub_result = A - B
print("\nMatrix Subtraction (A - B):")
print(matrix_sub_result)
SKEE 1033 11
MATRIX OPERATION
Example 1 (continued):
# 3. Matrix Multiplication Matrix A: Matrix B:
# Using @ operator [[1 2] [[5 6]
matrix_mul_result = A @ B [3 4]] [7 8]]
print("\nMatrix Multiplication (A @ B):")
print(matrix_mul_result)
SKEE 1033 12
MATRIX OPERATION
Example 1 (continued):
# 4. Matrix Right Division
# Solving XA = B using [Link] (interpreted as A / B)
matrix_right_div_result = [Link](A.T, B.T).T Matrix A: Matrix B:
print("\nMatrix Right Division ([Link](A.T, B.T).T):") [[1 2] [[5 6]
print(matrix_right_div_result) [3 4]] [7 8]]
Matrix Right Division ([Link](A.T, B.T).T):
[[-1. 2.]
[-2. 3.]]
SKEE 1033 13
MATRIX OPERATION
Example 1 (continued):
# 6. Matrix Power
# Using [Link].matrix_power() to raise matrix A to the power of 2
matrix_power_result = [Link].matrix_power(A, 2) Matrix A: Matrix B:
print("\nMatrix Power ([Link].matrix_power(A, 2)):") [[1 2] [[5 6]
print(matrix_power_result) [3 4]] [7 8]]
Matrix Power ([Link].matrix_power(A, 2)):
[[ 7 10]
[15 22]]
# 7. Matrix Transpose
transpose_result = A.T
print("\nMatrix Transpose (A.T):")
print(transpose_result)
SKEE 1033 14
MATRIX : PRECEDENCE
SKEE 1033 15
MATRIX : PRECEDENCE
Example 2: Python script that demonstrates the precedence of
operations like addition, multiplication, transpose, power, and division
when combined in a single expression. Understanding precedence helps
predict the order in which operations are evaluated.
import numpy as np
print("Matrix A:")
print(A)
Matrix A:
[[1 2]
[3 4]]
print("\nMatrix B:")
print(B)
Matrix B:
[[5 6]
[7 8]]
SKEE 1033 16
MATRIX : PRECEDENCE
Example 2 (continued):
Matrix A: Matrix B:
# 1. Demonstrate Precedence of Transpose vs Multiplication
[[1 2] [[5 6]
# Transpose happens before multiplication
[3 4]] [7 8]]
precedence_result_1 = A.T @ B
print("\nPrecedence 1 (Transpose before Multiplication - A.T @ B):")
print(precedence_result_1)
Precedence 1 (Transpose before Multiplication - A.T @ B):
[[26 30]
[38 44]]
# 2. Demonstrate Precedence of Parentheses
# Parentheses change the order of operations
precedence_result_2 = (A + B) @ B
print("\nPrecedence 2 (Parentheses first - (A + B) @ B):")
print(precedence_result_2)
SKEE 1033 17
MATRIX : PRECEDENCE
Example 2 (continued):
Matrix A: Matrix B:
# 3. Demonstrate Power Precedence
[[1 2] [[5 6]
# Power operation happens before multiplication
[3 4]] [7 8]]
precedence_result_3 = [Link].matrix_power(A, 2) @ B
print("\nPrecedence 3 (Power before Multiplication - [Link].matrix_power(A, 2) @
B):")
print(precedence_result_3)
Precedence 3 (Power before Multiplication - [Link].matrix_power(A, 2) @ B):
[[105 122]
[229 266]]
SKEE 1033 18
MATRIX : PRECEDENCE
Example 2 (continued):
Matrix A: Matrix B:
# 5. Demonstrate Precedence of Addition and Multiplication
[[1 2] [[5 6]
# Multiplication happens before addition
[3 4]] [7 8]]
precedence_result_5 = A @ B + B
print("\nPrecedence 5 (Multiplication before Addition - A @ B + B):")
print(precedence_result_5)
SKEE 1033 19
ARRAY OPERATOR
ARRAY OPERATION
Example 3: This script focuses solely on element-wise addition, subtraction,
multiplication, division, power
import numpy as np
print("Array A:")
print(A)
Matrix A:
[[1 2]
[3 4]]
print("\nArray B:")
print(B)
Matrix B:
[[5 6]
[7 8]]
SKEE 1033 21
ARRAY OPERATION
Example 3 (continued):
# 1. Element-wise Addition Matrix A: Matrix B:
element_wise_add = A + B [[1 2] [[5 6]
print("\nElement-wise Addition (A + B):") [3 4]] [7 8]]
print(element_wise_add)
Element-wise Addition (A + B):
[[ 6 8]
[10 12]]
# 2. Element-wise Subtraction
element_wise_sub = A - B
print("\nElement-wise Subtraction (A - B):")
print(element_wise_sub)
SKEE 1033 22
ARRAY OPERATION
Example 3 (continued):
# 3. Element-wise Multiplication Matrix A: Matrix B:
element_wise_mul = A * B [[1 2] [[5 6]
print("\nElement-wise Multiplication (A * B):") [3 4]] [7 8]]
print(element_wise_mul)
Element-wise Multiplication (A * B):
[[ 5 12]
[21 32]]
# 4. Element-wise Right Division
element_wise_right_div = A / B
print("\nElement-wise Right Division (A / B):")
print(element_wise_right_div)
SKEE 1033 23
ARRAY OPERATION
Example 3 (continued):
# 5. Element-wise Left Division Matrix A: Matrix B:
element_wise_left_div = B / A [[1 2] [[5 6]
print("\nElement-wise Left Division (B / A):") [3 4]] [7 8]]
print(element_wise_left_div)
Element-wise Left Division (B / A):
[[5. 3. ]
[2.33333333 2. ]]
# 6. Element-wise Power
element_wise_power = A ** 2
print("\nElement-wise Power (A ** 2):")
print(element_wise_power)
Element-wise Power (A ** 2):
[[ 1 4]
[ 9 16]]
SKEE 1033 24
ARRAY SIZE AND RELATED ATTRIBUTES
Example 4: This script retrieves the size and shape of arrays, including the
shape, total number of elements, number of dimensions, and size of each
dimension.
import numpy as np
SKEE 1033 25
ARRAY SIZE AND RELATED ATTRIBUTES
Example 4 (continued):
# Total number of elements in the array Matrix A:
array_size = [Link] [[1 2]
print("\nTotal number of elements in the array:") [3 4]]
print(array_size) # 4
Total number of elements in the array:
4
SKEE 1033 26
VECTORIZING : COMPOUND INTEREST
In Python, using NumPy arrays allows you to calculate values for multiple inputs at
once, a technique called vectorization. This means you can perform operations on
entire arrays without writing loops, making the code faster and simpler.
Example 5
Lets consider a formula of compound interest as below where 𝐴𝐴 = invested money, 𝑟𝑟 =
interest rate, 𝑛𝑛 = total year, and 𝐵𝐵 = final balance:
𝑛𝑛
𝐵𝐵 = 𝐴𝐴 1 + 𝑟𝑟
Single Value Calculation:
If 𝐴𝐴 = 100,then using 𝑟𝑟 = 0.09 and 𝑛𝑛 = 10m , then scalar will result 𝐵𝐵 = 236.7.
Vectorized Calculation:
To compute 𝐵𝐵 for several values of 𝐴𝐴 without repeating the calculation multiple times, we can
represent A as an array
SKEE 1033 27
VECTORIZING : COMPOUND INTEREST
Python Code:
import numpy as np
# Define interest rate and number of years
r = 0.09; n = 10
SKEE 1033 28
VECTORIZING : VERTICAL DISPLACEMENT
𝑠𝑠 = 𝑔𝑔𝑡𝑡 2 /2
where:
SKEE 1033 29
VECTORIZING : VERTICAL DISPLACEMENT
SKEE 1033 30
VECTORIZING : VOLUME OF CONES
SKEE 1033 31
COMPLEX NUMBER ARITHMETIC
SKEE 1033 32
COMPLEX NUMBER ARITHMETIC
SKEE 1033 33
ARITHMETIC OPERATOR DRILL
7 4
𝑎𝑎 = 2; 𝑏𝑏 = 2 4 ; 𝑐𝑐 = ;
1 3
SKEE 1033 34
BOOLEAN OPERATION
BOOLEAN OPERATOR
SKEE 1033 36
BOOLEAN OPERATOR
Relational Operator Precedence
Symbol Meaning
< Less than
<= Less than or equal
== Equal
!= Not equal
> Greater than
>= Greater than or equal
Logical Operator
Logical Element-wise Short-circuiting
AND & and
OR | or
NOT ~ not
Logical Value
Function Value
true 1
false 0
SKEE 1033 37
TRUTH TABLE FOR LOGICAL OPERATIONS
SKEE 1033 38
LOGICAL OPERATORS IN PYTHON:
& vs AND, | vs OR, ~ vs NOT
Logical operators (and, or, not) output True or False depending on the
evaluation of Boolean conditions.
Bitwise operators (&, |, ~) output numeric results based on bitwise
operations on the binary representations of numbers.
True and False # Output: False
True or False # Output: True
not True # Output: False
SKEE 1033 39
LOGICAL OPERATORS
# Element-wise operations
and_result = A & B
or_result = A | B
not_result = ~A
SKEE 1033 40
RELATIONAL OPERATOR
# Relational operations
less_than = a < b # Less than
less_equal = a <= b # Less than or equal
equal = a == b # Equal
not_equal = a != b # Not equal
greater_than = a > b # Greater than
greater_equal = a >= b # Greater than or equal
# Print results
print("a =", a) print("b =", b)
print("a < b:", less_than) # Output: True
print("a <= b:", less_equal) # Output: True
print("a == b:", equal) # Output: False
print("a != b:", not_equal) # Output: True
print("a > b:", greater_than) # Output: False
print("a >= b:", greater_equal) # Output: False
SKEE 1033 41
RELATIONAL OPERATOR
Example 10 (continued):
# Print results a:
print("a =", a) print("b =", b) [5]
print("a < b:", less_than) # Output: True
print("a <= b:", less_equal) # Output: True b:
print("a == b:", equal) # Output: False [10]
print("a != b:", not_equal) # Output: True
print("a > b:", greater_than) # Output: False
print("a >= b:", greater_equal) # Output: False
a = 5
b = 10
a < b: True
a <= b: True
a == b: False
a != b: True
a > b: False
a >= b: False
SKEE 1033 42
OPERATORS PRECEDENCE
# Print results
print("Expression 1 ((a + b) * c):", expr1) # Output: (5 + 2) * 3 = 21
print("Expression 2 (a ** b + c):", expr2) # Output: 5**2 + 3 = 28
print("Expression 3 (~a + b):", expr3) # Output: -6 + 2 = -4
print("Expression 4 (a * b / c):", expr4) # Output: 5 * 2 / 3 = 3.3333...
print("Expression 5 (a + b - c):", expr5) # Output: 5 + 2 - 3 = 4
print("Expression 6 (a < b and b > c):", expr6) # Output: False and True = False
print("Expression 7 (a | b and c):", expr7) # Output: 3
SKEE 1033 43
OPERATORS PRECEDENCE
Example 11 (continued):
# Print results a:
print("Expression 1 ((a + b) * c):", expr1) # Output: (5 + 2) * 3 = 21 [5]
print("Expression 2 (a ** b + c):", expr2) # Output: 5**2 + 3 = 28
print("Expression 3 (~a + b):", expr3) # Output: -6 + 2 = -4
print("Expression 4 (a * b / c):", expr4) # Output: 5 * 2 / 3 = 3.3333... b:
print("Expression 5 (a + b - c):", expr5) # Output: 5 + 2 - 3 = 4 [2]
print("Expression 6 (a < b and b > c):", expr6) # Output: False and True = False
print("Expression 7 (a | b and c):", expr7) # Output: 3 c:
[3]
Expression 1 ((a + b) * c): 21
Expression 2 (a ** b + c): 28
Expression 3 (~a + b): -4
Expression 4 (a * b / c): 3.3333333333333335
Expression 5 (a + b - c): 4
Expression 6 (a < b and b > c): False
Expression 7 (a | b and c): 3
SKEE 1033 44
MIX OPERATORS
When working with Python, you often need to combine different types
of operations, such as arithmetic, relational, and Boolean logic, in a
single expression. Understanding how these operations interact is
crucial for writing effective and correct code.
Key Concepts:
• Arithmetic Operations:
• Basic mathematical calculations: addition (+), subtraction (-), multiplication
(*), division (/), modulo (%), and exponentiation (**).
• Relational Operators:
• Used to compare values: less than (<), greater than (>), equal to (==), not equal
to (!=), less than or equal to (<=), and greater than or equal to (>=).
• Boolean Operators:
Used to combine logical expressions: AND (and), OR (or), and NOT
(not).Short-circuiting: Evaluations stop as soon as the result is determined
(e.g., and stops if the first operand is False).
SKEE 1033 45
MIX OPERATORS
# Combined operations
# This expression combines arithmetic, relational, and boolean logic
result = ((x + y) > z) and ((x - y) * 2 < z) or (z % y == 0)
# Explanation:
# (x + y) > z checks if the sum of x and y is greater than z
# (x - y) * 2 < z) checks if double the difference between x and y is less than z
# (z % y == 0) checks if z is divisible by y (no remainder)
SKEE 1033 46
MIX OPERATORS
Example 12 (continued):
# Additional mixed calculations
final_result = (x * y < z + 5) or ((x + 2) == (y + 5) and (z // y) > 1)
# Explanation:
# (x * y < z + 5) checks if the product of x and y is less than z plus 5
# ((x + 2) == (y + 5)) checks if x + 2 equals y + 5
# (z // y) > 1 checks if the integer division of z by y is greater than 1
SKEE 1033 47
CHAINED COMPARISONS
SKEE 1033 48
CHAINED COMPARISONS
SKEE 1033 49
CHAINED COMPARISONS
SKEE 1033 50
CHAINED COMPARISONS
SKEE 1033 51
CHAINED COMPARISONS
Example 13 (continued):
# 5. More than two conditions in a chained comparison
temperature = 22
humidity = 65
pressure = 1010
print(15 < temperature < 30 < humidity < 70 and 1000 < pressure < 1020) # True
True
SKEE 1033 52
CHAINED COMPARISONS
Example 13 (continued):
# 8. Mixing comparison operators
p = 12 q = 18
print(10 < p < 15 < q < 20) # True, p is between 10 and 15, and q is between 15
and 20
True
SKEE 1033 53
CHAINED COMPARISONS
Example 13 (continued):
# 11. Checking if values are within a range
gpa = 3.5
print(2.0 <= gpa <= 4.0) # True, gpa is between 2.0 and 4.0
True
Example 13 (continued):
# 15. Using negative values in chained comparisons
depth = -5
print(-10 < depth < 0) # True, depth is between -10 and 0
True
SKEE 1033 55
CHAINED COMPARISONS
Example 13 (continued):
# 18. Chained comparison for leap year check
year = 2024
print(1900 < year < 2100 and year % 4 == 0) # True, 2024 is between 1900 and
2100, and it's a leap year
True
SKEE 1033 56
BOOLEAN OPERATOR DRILL
SKEE 1033 57