Skip to content

Instantly share code, notes, and snippets.

@kamontat
Last active April 24, 2021 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamontat/fc8269613ed362963a4e7e365bb727b6 to your computer and use it in GitHub Desktop.
Save kamontat/fc8269613ed362963a4e7e365bb727b6 to your computer and use it in GitHub Desktop.
Intro to programming

Data type

  1. Number - Int / Float - ตัวเลข
  2. Boolean - ค่าความเป็นจริงหรือเท็จ
  3. Character - ตัวอักษร
  4. String / Text

Int (Integer) จำนวนเต็ม

0, 1, 2, 3, 10, 100

Float (Double) เศษส่วน ทศนิยม

0.25, 0.8, 3.5

Boolean

True, False จริง กับ เท็จ

Create boolean

symbol >, <, == (= =)

  1. < น้อยกว่า
  2. มากกว่า

  3. <= น้อยกว่า หรือ เท่ากับ
  4. = มากกว่า หรือ เท่ากับ

Equals

= => assign (ใส่ค่าเข้าไปในตัวแปร) == => check equal (เช็คว่าเท่ากันไหม)

Character (ตัวอักษร)

  • มี 1 ตัวอักษรเสมอ

a, ก, "1", '1'

String (Text)

  • คือ หลายๆ character รวมกัน

Character(s)

'abc', 'กขค'

Question

String | Boolean | Number | Character

  1. FALSE - Boolean
  2. 'TRUE' - String
  3. 5 - Number
  4. 0 - Number
  5. '7' - Character
  6. '' - String (*)

Null (ความว่างเปล่า)

null

จำนวนติดลบ

1 + 1 = 2 2 - 1 = 1 1 - 2 = -1

Int (จำนวนเต็ม)

-Infinity => Infinity

-100, -10, -3, -2, -1, 0, 1, 2, 3, 5, 10, 100

Variable (ตัวแปร)

ชื่อตัวแปร = ค่าของตัวแปร

name = "kamontat" <- ชื่ออะไร age = 200 working = false (boolean)

Plus sign (+)

String/Character | Number

  1. Number + Number = Number
  2. String + String = String
  3. Character + Character = String
  4. String + Number = String

Number

1 + 1 = 2 3 + 4 = 7

String/Character (Concat) ต่อเพิ่ม

'abc' + 'def' = 'abcdef' 'กก' + 'ขข' = 'กกขข'

'abc' + 12 = 'abc12'

Character + Character = String (เสมอ) 'a' + 'b' = 'ab' (String)

เวลาโค๊ดรัน จะรันจากบรรทัดที่ 1 เสมอ

Final quiz

https://repl.it/@kamontatc/Lecture-1

Data (ข้อมูล)

ค่าของสิ่งต่างๆ

Variable (ตัวแปร)

name = "net"

Logic (วิธีการคำนวณ)

+, -, *, /

if-else

Area = ความยาว * ความกว้าง Left money = เงินที่เรามี - เงินที่จ่ายไป

Code

# data section (ข้อมูล)
width = 5
height = 6

# logic section (วิธีการคำนวณ)
area = width * height

If-else condition

ถ้า ... แล้ว ... ที่เหลือคือ ...

if ..A..:
  ..B..
else:
  ..C..

A - จะต้องเป็น boolean เสมอ B - การกระทำใดๆ (จะทำงานเมื่อ A เป็น True) C - การกระทำใดๆ (จะทำงานเมื่อ A เป็น False)

width = 10
height = 20

if width < height:
  print("ok")
else:
  print("not ok")

If-else-if condition

if ...A...:
  ...B...
elif ...C...:
  ...D...
else:
  ...E...

A, C - boolean เสมอ B, D, E - การกระทำใดๆ

Quiz

score = 7 # เต็ม 10 คะแนน

# ได้ คะแนนมากกว่า 7 คะแนน น้องจะสอบผ่าน

if score > 7:
  print("สอบผ่าน")
else:
  print("สอบไม่ผ่าน")
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for number in numbers:
  if number == 2:
    print(str(number) + " is even number.")

Final quiz

https://repl.it/@kamontatc/Lecture-2

Multiple condition

  score = 70 # B
  score = 50 # B
  score = 40 # F

  พี่จะได้ A 'score > 80'
  พี่จะได้ F 'score < 50'

  พี่จะได้ B 'score > 50 และ score < 80'

if score > 80: print("A")

and (และ) | or (หรือ)

AND condition

'true' and 'true' => 'true'

พี่เป็นคนไทย => true พี่เป็นผู้ชาย => true

true

Condition

1. "text" == "text" # true
2. 10 == 15         # false

# ข้อ 3
if 50 == "50":
  print("จริง")
else:
  print("เท็จ")

# ข้อ 4
if true:
  print("จริง")
else:
  print("เท็จ")

# ข้อ 5

# มีนักเรียน 50 คน

exam1 = 5 # เต็ม 10
exam2 = 4 # เต็ม 10
exam3 = 3 # เต็ม 20
exam4 = 2 # เต็ม 15
exam5 = 3 # เต็ม 45

# คะแนนรวม 10 + 10 + 20 + 15 + 45 = 100 คะแนนพอดี

if exam1 + exam2 + exam3 + exam4 + exam5 > 80:
  print("ผ่าน")
else:
  print("สอบตก")

Python (เริ่มต้น)

ชื่อของภาษาโปรแกรม

# declare variable
name = "net"
score = 100
is_man = True

# Comment
# name = "net" ไม่ถูก run
# score = "net" ไม่ถูก run

if 10 > 10:
  print("A")
elif 20 > 20:
  print("B")
else:
  print("C")

# Character ????
# NOTE: ไม่มี Character 'a', 'b', 'c' <- string

Loop (การวนลูป)

กระทำอะไรซ้ำๆ จนถึงจุดๆหนื่ง

Variables

  • ชื่อตัวแปร
  • ค่าของตัวแปร

<ชื่อตัวแปร> = <ค่าของตัวแปร>

money = 10 <-- หมายความว่า เรามีเงิน 10 บาท name = "john" <-- หมายความว่า ชื่อ ชื่อว่า john age = 18 <-- หมายความว่า อายุ 18

Reassign

สร้างตัวแปรใหม่

money = 20 # <-- หมายความว่า เรามีเงิน 10 บาท candy = 8 # <-- รายจ่าย (สิ่งที่จ่ายไป) pencil = 2 # <-- รายจ่าย (สิ่งที่จ่ายไป)

left = money - candy # <-- เราเหลือเงิน

เปลื่ยนค่าตัวแปรเดิม

money = 20 # <-- หมายความว่า เรามีเงิน 10 บาท candy = 8 # <-- รายจ่าย (สิ่งที่จ่ายไป) pencil = 2 # <-- รายจ่าย (สิ่งที่จ่ายไป)

money = money - candy # <-- money เงินที่เหลือ (12) money = money - pencil # <-- money เงินที่เหลือ (10)

print(money) # <-- 10

If-else

elif: อ่านว่า else if

Condition sign

1 2 3 4 5 6 7
== 1 x
!= 5 x x x x x x
> 5 x x
>= 5 x x x
< 2 x
<= 2 x x

Syntax

language name: python

indent: เว้นวรรค

if True:
  print("hello world")

print("hello world")

while True:
  print("inside while loop")

print("outside while loop")

Multiple Condition

if 5 > 4:
  print("5 > 4")
2, 4, 6

number = 5

if number == 2:
  print("เลขคู่")
elif number == 4:
  print("เลขคู่")
elif number == 6:
  print("เลขคู่")

And / Or (และ / หรือ)

And: && Or: ||

Boolean: True and False

And

ต้องเป็น True ทั้งหมดเท่านั้น ถ้าจะเป็น True

True && True = True False && True = False True && True & True = True True && True & True & False = False

Or

ขอแค่มี 1 อันเท่านั้นที่เป็น True ถ้าจะเป็น True / ต้องเป็น False ทั้งหมดเท่านั้น ถ้าจะเป็น False

True || False = True False || False || False = False False || True || False = True

True || False && True = (True || False) && True = True && True = True False && True || False = False True || (False && True) || False = True || ???? || False = True

money = 55

pen = 20
paper = 15

# True && False
if money > pen and money < paper:
  print("hello")
else
  print("nothing")

Loop

number = 0

number = number + 1 # 0 + 1

number = number + 1 # 1 + 1

number = number + 1

number = number + 1

number = number + 1

print(number)

While loop

while :

  1. check
    1. condition is True
    2. condition is False, stop and left
  2. run
  3. loop to step #1
number = 0
while number < 10:
  print("hello")
  number = number + 1 # number = 1

print("end")

น้อยกว่าเท่ากับ

<= = a < b or a == b

number = 0
while number < 10: # [1]
  print("hello") # [2]
  number = number + 1 # [3]
print("end") # [4]
number = 1
while number <= 10:
  print("hello")
  number = number + 1
  1. number = 1

  2. number <= 10 = 1 น้อยกว่า/เท่ากับ 10 = True

  3. print(hello)

  4. number = number + 1 = number = 2

  5. number <= 10 = 2 <= 10 = True

  6. print(hello)

  7. number = number + 1 = number = 3

  8. number <= 10 = 3 <= 10 = True

  9. print(hello)

  10. number = number + 1 = number = 10

  11. number <= 10 = 10 <= 10 = 10 < 10 || 10 == 10 = True

  12. print(hello)

  13. number = number + 1 = number = 11

  14. number <= 10 = 11 <= 10 = False

  15. END

Quiz

Modulo sign (%)

เหมือนกับการหาร แต่เลือกเฉพาะเศษ

  1. 10 / 2 = 5 <==> 10 % 2 = 0
  2. 9 / 2 = 4 1/2 <==> 9 % 2 = 1
  3. 16 / 5 = 3 1/5 <==> 16 % 5 = 1

เลขคู่

เลขที่หาร 2 ลงตัว

  • 2 / 2 = 1
  • 4 / 2 = 2
  • 5 / 2 = 2 1/2

number % 2 == 0 แปลว่าเป็นเลขคู่ number % 2 != 0 แปลว่าเป็นเลขคี่

size = 10
while size > 0:
  if size % 2 == 0:
    print("hello")
  size = size - 1
  1. size = 10
  2. size > 0 = True
  3. size % 2 == 0 = True
  4. print(hello)
  5. size = size - 1 = size = 9
  6. size > 0 = True
  7. size % 2 == 0 = False
  8. size = size - 1

Tips

start = start + 1 start = start - 1

start += 1 ความหมายเดียวกัน start = start + 1 start -= 1 ความหมายเดียวกัน start = start - 1 start *= 1 ความหมายเดียวกัน start = start * 1 start /= 1 ความหมายเดียวกัน start = start / 1

start = 1 # start = 1
start += 1 # start = 2
start += 1 # start = 3

print(start) # 3 

Function / Method

  • print() <- การทำอะไรซักอย่างหนึ่ง เพื่อพิมพ์ผลลัพท์ออกมา
  • function_name(parameter)

Function name

ชื่อของ function เพื่อใช้ในการเรียก function นั้นๆ โดยที่เราจะตั้งชื่อมัน ให้สอดคลองการสิ่งที่ function ทำ

Parameter(s)

ค่าที่เราส่งเข้าไปใน function นั้นๆ ตัวอย่าง print("hello")

Declare Function

def function_name():
  print("hello")

function_name()
def function_name(var):
  print(var)

function_name("hello")

Return value

def function_return():
  return 5

name = function_return() # 5
def format_name(name):
  return "Master " + name

firstname = format_name("tigger")
print(firstname)
  1. firstname = format_name(tigger)
  2. format_name(tigger)
  3. In format_name function name = "tigger"
  4. In format_name function return "Master " + name = "Master " + "tigger" = Master tigger
  5. In format_name function return 'Master tigger'
  6. firstname = "Master tigger"
  7. print(firstname) = Master tigger
def toggle(n): # <-- n = ??? 
  return False

variable = toggle(True)
print(variable)

Lecture 8 (Summary)

Datatype

  1. String (>1) / Character (1)
  2. Number
  3. Boolean
  4. Null

Variable

name = "tigger" # String age = 11 # Number is_male = True # Boolean

Plus (+)

ถ้ามีอันใดอันหนึ่งเป็น String จะต่อกัน

10 + 10 => 20 "str" + "ing" => "string" 10 + "ing" => "10ing" "10" + 10 => "1010" 20 + "20" => "2020" 20 + 20 => 40

number + number => plus string + string => concat

10 * 2 => 20 15 / 3 => 5 20 - 5 => 15

Logic (if-else-if)

if :

if : else:

if : elif <condition_2>: # (optional) elif <condition_2>: # (optional) elif <condition_2>: # (optional) else: # (optional)

if 10 > 20:
  print("True")
else:
  print("False")
if 10 > 20:
  print("True")
else:
  print("False")
name = "Tigger"
if name == "tigger":
  print("I")
else:
  print("You")

Condition

> | < | <= | >= | == | != (!= ไม่เท่ากับ)

Multiple condition

AND | OR

|| <== OR && <== AND

AND

ต้องเป็น True ทั้งหมด ถึงจะเป็น True ที่เหลือเป็น False

OR

ต้องเป็น False ทั้งหมด ถึงจะเป็น False ที่เหลือเป็น True

True && True => True False && True => False False && False => False True || True => True True || False => True False || True => True False || False => False

False || False || True => True (False && True) || True => False || True => True

False || False || (True && (True && False)) => False || False || (True && (False )) => False || False || (False ) => False

Extra knowledge

& <== Bit wise 0-F <== heximal (เลขฐาน 16) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F 0-9 <== decimal (เลขฐาน 10) 0 1 <== binary (เลขฐาน 2)

011 & 1 = 110 101 & 1 = 011

Function

def ชื่อฟังก์ชั่น():
  statement

ชื่อฟังก์ชั่น()
  1. Name - name of the function
  2. Parameter(s) - input value
  3. Statement - operation
  4. Output - return value
# declared ประกาศฟังค์ชั่น
# sum แปลว่า ผลบวกตัวเลข
def sum(input1, input2):
  return input1 + input2

# เรียกใช้

result = sum(1, 2) # <-
result = 1 + 2     # <-

# result คืออะไร??
# 1. String
# 2. Number
# 3. Variable (name)
# 4. Function <- ต้องต่อด้วยวงเล็บ


def format(input):
  return "A " + input

# input = "name" # value of input is "name"
# "A " + input => "A name"

# format("name") # Datatype: String
# "A input"

# "A" + "B" => "AB"
# a = "1"
# b = "2"
# a + b => "12"


# 1. Name         - name of the function
# 2. Parameter(s) - input value
# 3. Statement    - operation
# 4. Output       - return value

def positive(input): # input = -5
  if input < 0: # True
    return 0 # 
  else # ถ้า input ไม่ติดลบ
    return input # ให้ output เป็น input

positive(10) # 10
positive(0)  # 0
positive(-5) # 0
positive(1)  # 1
positive(-2) # 0






# 1. Name         - name of the function
# 2. Parameter(s) - input value
# 3. Statement    - operation
# 4. Output       - return value

def write():
  print("data")

write() # "data"

Loop

การทำงานใดๆ แบบซ้ำๆ

  1. condition (terminal) <- สิ่งที่บอกว่าเราทำงานไปอีกไหม
  2. pointer <- เลขนับ

While

while condition: # if condition is True, will start statement. otherwise, False it will stop
  <statement>



while True:
  print("tigger")





while False:
  print("tigger")






pointer = 0
while pointer < 10:
  print("tigger")

# 1. pointer = 0
# 2. pointer < 10 = True
# 3. print("tigger")
# 4. pointer < 10 = True
pointer = 0
while pointer < 10:
  if pointer %

Primariy Datatype

String Number Boolean Character

Objective Datatype

  • Object
    • sub-datatype
  • Array = The hardest datatype

2 * 10

result = 0
pointer = 0
while pointer < 10:
  result = result + 2
  pointer = pointer + 1

# 1. Name         - name of the function
# 2. Parameter(s) - input value
# 3. Statement    - operation
# 4. Output       - return value

# ประกาศ
def multiply(base, num):
  result = 0
  pointer = 0
  while pointer < num: # เอาใว้ ?? เช็คว่าจะทำ while ต่อไหม?
    result = result + base
    pointer = pointer + 1

  return result

# เรียกใช้
multiply(5, 5) # 25

# multiply คืออะไร??
# 1. String
# 2. Number
# 3. Variable (name)
# 4. Function <- ต้องต่อด้วยวงเล็บ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment