Professional Documents
Culture Documents
Concept
K-01
2 / 34
Computation
●
Fundamentally:
– performs calculations : a billion calculations per
second!
– remembers results : 100 GigaBytes
– remembers data
●
What kinds of calculations ?
– built-in (bawaan) to the language
– ones that you define as the programmer
●
Computers only know what you tell them
●
Computers only do what you instruct
3 / 34
Types of Knowledge
●
declarative knowledge : statements of fact
●
e.g. :
– Tanggal 20 September 2018 pukul 17.00 turun hujan di
kota Bandung
– Terjadi kemacetan di jalan Supratman, Bandung
– Pertumbuhan ekonomi Indonesia tahun 2018 sebesar
5,2%
– ...
4 / 34
Types of Knowledge
5 / 34
Types of Knowledge
●
imperative knowledge : a recipe or "how-to"
●
ex. : cara menginstall python pada komputer Ubuntu
– 1. Login via SSH and update all installed packages
●
First of all, login to your Ubuntu 16.04 VPS via SSH as user root
●
ssh root@IP_Address -p Port_number
●
and update all installed packages
– 2. Check the currently installed version of Python
●
To check the currently installed version of Python, execute the
following command # python -V
– 3. Install Python 3.6 on Ubuntu 16.04, from source
●
tar -xvf Python-3.6.3.tgz
●
...
6 / 34
Imperative Knowledge: numerical
example
●
square root of a number x is y such that y * y = x
●
recipe for calculating square root of a number x:
1) start with a guess t
2) if t*t is close enough to x, stop and say t is the answer
3) otherwise make a new guess: t = ( t + x/t ) / 2
4) using the new guess, repeat process (3) until close
enough
7 / 34
Imperative knowledge : numerical
example
●
ex.: what is the square root of 16 ?
●
3 9 16/3 4.17
8 / 34
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 = ALGORITMA
9 / 34
Computers are Machines
●
how to capture a recipe in a mechanical process
-> computer program
●
fixed program computer, e.g. : calculators
●
stored program computer : machine stores and
executes instructions, e.g.: Personal Computers
10 / 34
Stored Program
●
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
11 / 34
Creating Recipes
●
a programming language provides a set of primitive
operations
●
expressions are complex but legal combinations of
primitives in a programming language -> syntax
●
expressions and computations have values and
meanings in a programming language -> semantic
12 / 34
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
13 / 34
Objects
●
programs manipulate data objects
●
objects have a type that defines the kinds of things
programs can do to them
– Budi adalah seorang manusia, sehingga Budi bisa
berbicara, berjalan, dll.
– Putih adalah seekor kucing, sehingga Putih bisa
melompat, dll
●
objects are
– scalar (cannot be subdivided)
– non-scalar (have internal structure that can be
accessed)
14 / 34
Scalar Objects
●
int : integer (bilangan bulat), ex. : 3, 6, 19
●
float : real numbers (bilangan riil), ex.: 3.14 , 5.778
●
bool : 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
15 / 34
Type Conversions (Cast)
●
can convert object of one type to another
●
float(5) -> converts integer 5 to float 5.0
●
int(7.9) -> converts float 7.9 to integer 7
●
float(int(4.7)) -> ...?
●
conversion process is called type casting
16 / 34
Printing to Console
●
Consensus: use Python versi 3.x
●
check python version with command: python --version
●
to show output from code to a user console, use
command print()
print("Hello, apa kabar?")
print("5+9=", 5+9, "Betul?")
●
Note: command print() of python version 3 is different
from python version 2 !
17 / 34
Expressions
●
Expression = combination of objects and operators
●
an expression has a value, which has a type
●
syntax for a simple expression (aturan pembentukan
expression) :
<object> <operator> <object>
18 / 34
Operators on int and float
●
5+7 # the sum (penjumlahan)
●
9.0 - 4.5 # the difference (pengurangan)
●
5.0 * 4.5 # the product (perkalian)
●
5.0 / 2.5 # the division (pembagian)
– Q: what will the type of the result from an operation of
an int object with a float object ?
●
5%2 # the remainder when 5 is divided by 2 (sisa
pembagian)
●
2 ** 3 # 2 to the power of 3 (pangkat)
19 / 34
Order of Operation / Operator
Precedence
●
parentheses ( ) tells python to prioritize operation
enclosed in that symbols
●
order of operation without parentheses ( ) :
– **
– * , / , % (left to right)
– + dan - (left to right)
20 / 34
Order of Operation
●
For example: if x = 4.0 and y = 6.0
z = x + 4 * ( y + x ) / 2.0
# what is the value of z ?
21 / 34
Order of Operation
●
if x = 4.0 and y = 6.0
z = x + 4 * ( y + x ) / 2.0
22 / 34
Order of Operation
●
x = 4.0 and y = 6.0
z = x + 4 * ( y + x ) / 2.0 ;
●
Where we should put ( ) to make z = 40.0 ?
23 / 34
Variables and Values
●
Variable : part of memory that is given a name, which we
can manipulate later (for example we can use it to save
data)
●
On python, variables are not needed to be declared or be
specified before they can be used
●
why use variables?
– to make programs reusable (for example, use the same
program with different data)
– easier to modify / change program code
24 / 34
Variables and Values
●
equal sign "=" is an assignment of a value to a variable
name
– pi = 3.1415
– 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"
25 / 34
Variable Names
Rules of naming a variable:
●
must be preceeded with a letter (a-z, A-Z) or underscore
(_)
●
the next character can be letters, numbers, or _
●
case-sensitive !
●
can be any length, but better to keep it short
●
python's keywords can not be used as variable names
26 / 34
Variable Names
Suggestion for naming a variable :
●
choose a name that has meaning, ex. : "nama_siswa" is
better than "ns"
●
choose a short name, ex. :
"nama_siswa_jurusan_teknik_elektro" -> "nm_siswa_EL"
●
choose a consistent naming style, ex: "nama_siswa" or
"NamaSiswa" (camel case)
27 / 34
Program Statement vs Mathematic
Statement
●
in programming :
– expression on the right, evaluated to a value
– variable name on the left
– you do not "solve for x"
●
which of the following is programming statement :
– x+5=9
– x=y+(2+4)
– 2x = y + 10
– x=x+5
28 / 34
Changing Bindings
●
can re-bind variable names using new assignment
statements
●
previous value may still stored in memory but lost the
handle for it
●
ex. :
pi = 3.14
radius = 2.2
area = pi * ( radius**2 )
radius = radius + 1
area = ?
29 / 34
Changing Bindings
●
ex.:
angka = 3.14
jumlah = angka + 2
print( jumlah )
type( angka )
check the result, then continue with :
angka = 3
jumlah = angka + 2
print( jumlah )
type (angka )
Q: conclusion?
30 / 34
Comment
●
Comment is part of program which is NOT read by python
interpreter
●
use comment to give description or to clarify program
code
●
to make comment: use "#" as the first character
●
ex.:
# Ini adalah program untuk menghitung luas lingkaran
# Versi : 1.0
# Date : 10 Sep 2015
31 / 34
Program Code
●
Write python code using a text editor
●
Save program file with extention .py
●
Run the program with command: python [nama_file].py
32 / 34
Summary
●
We have discussed programming & python in general
●
Python program struture :
– Operator
– Expression
– Variables
– Comments
●
We have learned how to write and run a simple python
program
33 / 34
Assignment 1
●
Install python
●
Visit website: https://sites.google.com/view/basicprog
34 / 34