Week1 2
Week1 2
SIC1006
Computer Programming for the Sciences
wktang.um.edu.my
1
Outlines
➢ Boolean Values, Conditional Execution, Loops, Lists and List Processing, Logical and Bitwise
Operations
➢ The Object-Oriented Approach: Classes, Methods, Objects, and the Standard Objective Features;
Exception Handling, and Modules, Packages
• 40%
• Assignment (10% +10%), Quiz (10%) and MicroPython project (10%)
How to computation ??
Programming
Skill Organize, modularize,
Classes of algorithm (library)
Apply computation
Solve scientific Design your Python codes
problem that suit your needs
5
What is Computation?
• Data management
• Analysis
• Visualization
• Statistics
• web development
Calculation Output
You Computer Physical Results
6
Human & Computer
Computational
thinking
You Computer Calculation Output
Physical Results
Computational language
• Bridge between human and computer
• Compiler – c++, fortran, python
Weather forecast
Applied • Numerical weather prediction (mathematic model)
Mathematics
Computational Geoscience
• seismology and seismic hazards
• Underground activity
Human Limitation
Problem solved efficiently – Chemistry, Physics, Geoscience, Management, more!!
WHY ?!!?!?!
CPU
Processor
Input Control Unit Logic Unit Output
Arithmetic
Computer
Why ??
• Easy productivity & efficiency ▪ Social media networking
▪ Cloud service
• machine learning / AI customer favorite
Python in Science (Analysis & Visualization)
Computational
You thinking Computer Calculation
Output
Computation Fundamental
• Computational thinking (Behavior or thinking before programming)
➢ Logical thinking
➢ Algorithmic thinking
➢ Sequence of clearly defined steps to describe a process with start and end points
➢ Decomposition
➢ Characterize the problem
➢ Break the complex problem into simpler parts
Computation Fundamental
• Computational thinking (Behavior or thinking before programming)
➢ Pattern generalization
➢ Look for pattern (things repeat)
➢ Abstraction
➢ Simplify the code from large dataset
➢ Reduce complexity and increase efficiency
Problem to Problem
How to convert the chemical problem to computational problem??
Chemical / Geoscience
▪ quantum chemistry
Computational problem ▪ iSALE hydrocode
▪ multi-phase computational fluid dynamics
solution
Chemical / Geoscience
How to Computational ??
Mathematics into computational
√50 = ?? √50 = ?? √50 = ??
algorithm
7.071067811 7.071067811
19
Computational Thinking
√a = b
ancient Babylon √150 = ? Your problem solved !!
• Find any root of a number at
17th century BC !! guess b = 10 any guess from user
How to Computational ??
Algorithm
Declarative knowledge √a = b
• Statement of facts b2 = a
• Computational Geosciences
22
Computational Problem
Computational problem / interest
➢ Physics
➢ Chemistry
➢ Mathematics
➢ Management Computer
Computational ➢ Customer (selection)
thinking !! ➢ Game
Convert your problem into computer task and let the computer calculate for you !!
Python Programming
Object
Class
• Program manipulate class, object (element of a class)
Blueprint of object
• Class
• declare and create objects object
• group of similar objects (classify or a group)
• No memory allocation
• Example: car
• Object
• physical entity; attributes, method
• Allocated with memory
• Access data members and functions of the class
• Example: myvi, persona, BMW, Vios, honda city
• float, int
int integers 1, 3, 5
float floating point (decimal) 3.142
complex real and imaginary 2.3 + 5j
bool Booleans True or False
str Strings - character, name “hello” ‘python’
list Lists - ordered sequence of objects [33.3, ”abc”, 10, 10]
dict dictionaries – key:value pairs {“key1”:”value1”, “name”:”Alfred”}
tup tuples - ordered (immutable) sequence (33.3, ”abc”, 10, 10)
set set – unorder collection of unique object {“33.3, ”abc”, 10}
Mechanical Problem Clothes Problem Surgery Problem
Mechanic Tool – Data Type A Sewing Tool – Data Type B Surgery Tool – Data Type C
Different function in each Different function in each Different function in each
Attendance Math Calculation Electronic Circuit
>>>print(format(4/3, ".30f"))
1.333333333333333259318465024990
>>>id(30528)
1787924957680 specific of memory location
>>>30528+6
30534
>>>30528*2
91584
How many times you want to type 30528 ??!?!?!?!?
Variable Assignments
a = 30528
Pros
• Easy to work throughout coding
• Fast and flexible – replace/overwrite the same variable
Cos
• May cause bugs for unexpected data types
• Need to consistently check variable with type()
Data Type - Number
Check your number data type Number Conversion
print () – prints the user specified text (within the parenthesis) to the screen
???
Print variable
syntax
print({format_field}.format(value))
Operator - Arithmetic >>>10/5
2.0
>>>10//5
• Create expressions (combinations of objects and binary operators) 2
• Expression return a value
• Syntax
• <operand> <operator> <operand> >>>50%3
2
a + b → sum + addition
a − b → subtraction − subtraction >>>5**2
a * b → multiplication * multiplication 25
a / b → division / division (return floating point)
a // b → division // division (return integer)
a % b → remainder % remainder
>>>10 * 3.1
a ** b → exponentiation ** exponentiation
31.0
Both are int, return in int type >>>10*3
Either of both are floating point, return in float type 30
Operator and Mathematical Function
int (parameters)
default in Python
float (parameters)
a += 10
//=
a = a + 10
**=
1. Carry out the addition
%=
2. Replace the value in a
44
Simple Module math
• Convenient – functionality -> modules/packages
• import math – ask python to import the math module and functionality inside
math.exp(object)
math.degree(object)