PHY10016
Engineering
Programming
McS. Nguyen, Vuong Thuy Ngan
TODAY
01 Course info
02 What is computation
03 Python basics
04 Mathematical operations
05 Python variables and types
Lecturer profile
Bachelor:
Honnor degree of Physics and Engineering Physics – VNU-HCMUS
Master of Science:
Radio Physics and Application – VNU-HCMUS
Computer Science – Texas Tech University, US
Research direction:
Medical image processing;
Visualization and big data analysis;
Nguyễn Vương Thùy Ngân
EEG classification;
Lecturer
EMG analysis;
Active Noise Control; Active Echo Control;
[email protected]
TA profile
Master of Science:
?, VNU-HCMUS
[email protected]
Võ Hoàng Thủy Tiên
Lecturer
COURSE INFO
Attendance
10%
Exercises
theoretical class / exercise sessions
10%
Practical exam
30% (computer lab)
Final exam
50%
COURSE POLICIES
Communication:
Check Microsoft Teams regularly for updates and resources.
For queries, email the instructor or TA with a clear subject line, including your
name and course code.
No cheating accepted during exam
Attendance
Taking attendance for each session
If you are unable to attend a session, inform the instructor/TA in advance with
a valid reason.
Respect and Etiquette
Treat classmates, the instructor, and TA with respect and professionalism.
Avoid disruptive behavior and inappropriate use of technology during class.
Use of Technology
Laptops and tablets are allowed for course-related purposes only.
Refrain from using phones or other devices for non-educational activities
during class.
FAST PACED COURSE
New to programming?
PRACTICE. PRACTICE? PRACTICE!
can’t passively absorb download code before
programming as a skill lecture and follow along
do Lab’s exercises don’t be afraid to try out
Python commands!
PROBLEM
SOLVING
PRACTICE
KNOWLEDGE PROGRAMMING
OF CONCEPTS SKILL
TOPICS
Representing Knowledge: Utilizing data structures to model and
organize information.
Iteration and Recursion: Exploring these computational
metaphors to solve problems.
Abstraction: Understanding procedures and data types to
simplify complex systems.
System Organization: Modularizing code with object classes
and methods.
Algorithm Classes: Studying various algorithms, including
searching and sorting techniques.
Algorithm Complexity: Analyzing the efficiency and scalability
of algorithms.
Advances Topics*
Useful Links
https://www.w3schools.com/python/
http://www.pythontutor.com/
http://www.codecademy.com/tracks/python
http://www.singpath.com
Online compiler
https://onecompiler.com/python
How to install Python
https://www.geeksforgeeks.org/how-to-
install-python-on-windows/
WHAT DOES A COMPUTER
DO
Can you turn on the light?
Can you turn on the light?
How about 1,000 time?
Can you turn on the light?
How about 1,000 time?
per second?
How many number can you
remember in 3 second?
3.14159265359
WHAT DOES A COMPUTER
DO
What kinds of calculations?
Fundamentally
Performs calculations Built-in
a billion calculations/sec! Ones that you define
as the programmer
Remembers results
Tetrabytes of storage!
Computers only know what
you tell them
TYPES OF KNOWLEDGE
Declarative knowledge is statements of fact.
If you don’t study, you will watse both your time and
my time
imperative knowledge is a recipeor “how-to”.
1. Buy a lottery
2. Wait for the annoument
3. Search your number on the result list
4. If your number on the list. WINNER
A NUMERICAL EXAMPLE
square root of a number x is y such that y*y = x
recipe for deducing square root of a number x (16)
1. Start with a guess, g
2. If g*g is close enough to x, stop and say g is the answer
3. Otherwise make a new guess by averaging g and x/g
4. Using the new guess, repeat process until close enough
g g*g x/g (g+x/g)/2
3 9 16/3 4.17
4.17 17.36 3.837 4.0035
4.0035 16.0277 3.997 4.000002
WHAT IS A RECIPE
1. sequence of simple steps
2. flow of control process that specifies
when each step is executed
3. a means of determining when to stop
1+2+3 = an algorithm!
COMPUTERS ARE MACHINES
how to capture a recipe in a mechanical process
fixed program computer
calculator
stored program computer
machine stores and executes instructions
BASIC MACHINE ARCHITECTURE
MEMORY
CONTROL ARITHMETIC
UNIT LOGIC UNIT
program counter do primitive ops
INPUT OUTPUT
MEMORY STACK
Address
x076550
x076551
x076552
x076553
MEMORY STACK
Address
x076550
x076551
x076552
x076553
MEMORY STACK
Address
x076550
x076551
x076552
x076553
STORED PROGRAM COMPUTER
sequence of instructions stored inside computer
built from predefined set of primitive instructions
1. arithmetic and logic
2. simple tests
3. moving data
special program (interpreter) executes each instruction in order
use tests to change flow of control through sequence
stop when done
ASPECTS OF LANGUAGES
primitive constructs
English: words
programming language: numbers, strings, simple operators
Word Cloud copyright Michael Twardos, All Right Reserved. This content is excluded from our Word Cloud copyright unknown, All Right Reserved.
Creative Commons license. For more information, see https://ocw.mit.edu/help/faq-fair-use/. This content is excluded from our Creative
Commons license. For more information, see
https://ocw.mit.edu/help/faq-fair-use/.
ASPECTS OF LANGUAGES
syntax
English
"cat dog boy" -> not syntactically valid
"cat hugs boy" -> syntactically valid
programming language
"hi"5 -> not syntactically valid
3.2*5 -> syntactically valid
ASPECTS OF LANGUAGES
static semantics is which syntactically valid
strings have meaning
English
"I are hungry" -> syntactically valid
but static semantic error
programming language
3.2*5 -> syntactically valid
3+"hi" -> static semantic error
ASPECTS OF LANGUAGES
semantics is the meaning associated with a syntactically correct
string of symbols with no static semantic errors
English
can have many meanings "Flying planes can be dangerous"
programming language
have only one meaning but may not be what programmer intended
WHERE THINGS 01 syntactic errors
common and easily caught
GO WRONG
02 static semantic errors
some languages check for these
before running program
can cause unpredictable behavior
03 no semantic errors but
different meaning than
what programmer intended
program crashes, stops running
program runs forever
program gives an answer but
different than expected
PYTHON
PROGRAMS
What you know about Python?
GO to MS team and submit your ideas
PYTHON PROGRAMS
a program is a sequence of definitions and commands
definitions evaluated
commands executed by Python interpreter in a shell
commands (statements) instruct interpreter to do something
can be typed directly in a shell or stored in a file that is read into
the shell and evaluated
Problem Set 0 will introduce you to these
WHY PYTHON?
Consider the alternatives:
Perl, Ruby, C, Java, Javascript
Pick the best language for the job at hand
When to say "no" to Python:
Not supported on Platform
Key Library not supported
Where Python Shines:
Easy to install (free as in beer and as in freedom)
Clear concise logic description:
fewer lines of code, while maintaining readability
There is a library for that!
Prototyping
WHY PYTHON?
OBJECTS
programs manipulate data objects
objects have a type that defines the kinds of things
programs can do to them
Ana is a human so she can walk, speak English, etc.
Chewbacca is a wookie so he can walk, “mwaaarhrhh”,
etc.
objects are
scalar (cannot be subdivided)
non-scalar (have internal structure that can be accessed)
SCALAR OBJECTS
int – represent integers, ex. 5
float – represent real numbers, ex. 3.27
bool – represent Boolean values True and False
NoneType – special and has one value, None
can use type() to see the type of an object
>>> type(5)
int
>>> type(3.0)
float
TYPE CONVERSIONS (CAST)
can convert object of one type to another
float(3) converts integer 3 to float 3.0
int(3.9) truncates float 3.9 to integer 3
PRINTING TO CONSOLE
to show output from code to a user, use print command
In [11]: 3+2
Out[11]: 5
In [12]: print(3+2)
5
EXPRESSIONS
combine objects and operators to form expressions
an expression has a value, which has a type syntax for a
simple expression
<object> <operator> <object>
OPERATORS ON ints and floats
i+j -> the sum if both are ints, result is int
i-j -> the difference if either or both are floats,
i*j -> the product result is float
i/j -> division
result is float
i%j -> the remainder when i is divided by j
i**j -> i to the power of j
SIMPLE OPERATIONS
parentheses used to tell Python to do these operations first
operator precedence without parentheses
**
*
/
+ and – executed left to right, as appear in expression
Try this now with steps:
2+3*5-6**2/2+9
BINDING VARIABLES AND VALUES
equal sign is an assignment of a value to a variable name
variable value
pi = 3.14159
pi_approx = 22/7
value stored in computer memory
an assignment binds name to value
retrieve value associated with name or variable by invoking the
name, by typing pi
ABSTRACTING EXPRESSIONS
why give names to values of expressions?
to reuse names instead of values
easier to change code later
pi = 3.14159
radius = 2.2
area = pi*(radius**2)
Variable Names
Must start with a letter or an underscore (_)
Valid: x, _name, user1
Invalid: 1user, -var
Can contain letters, numbers, and underscores
Valid: user_name, value123, x_2
Invalid: user-name, value@
Cannot be a reserved keyword
Python keywords like for, if, else, class, etc., cannot be
used as variable names.
Case-sensitive
myVar and myvar are treated as distinct variables.
Should not include spaces
Use underscores (_) for readability instead of spaces.
PROGRAMMING vs MATH
in programming, you do not “solve for x”
pi = 3.14159
radius = 2.2
# area of circle
area = pi*(radius**2)
radius = radius+1 <=> radius =+1
CHANGING BINDINGS
can re-bind variable names using new assignment statements
previous value may still stored in memory but lost the handle
for it
value for area does not change until you tell the computer to
do the calculation again
pi = 3.14 3.14
radius = 2.2 pi
2.2
area = pi*(radius**2) radius
3.2
radius = radius+1 area
15.1976
Feedback
End....
contact using MSTeam/Email
[email protected]