Basic Process

Basic syntax
- Comment
ใช้ # คั่นข้างหน้าสุด ส่วนนั้นจะไม่ทำงาน ใช้อธิบายโค้ดหรือใช้โน้ตสำหรับ Developer
# Hello Python
- Output
# print hello world
print("Hello World")
- Input
# filling name text
print('Enter your name:')
# scan input from keyboard
x = input()
# show output
print("this is output:", x)
Variables
Definition
ตัวแปรคือสิ่งที่ไว้ใช้จัดเก็บข้อมูล ซึ่งในภาษาโปรแกรมมิ่ง มีการคำนวณต่างๆมากมาย เราจึงต้องมีการเก็บข้อมูลใส่ไว้ในตัวแปร เพื่อใช้แสดงผลหรือใช้ในการคำนวณต่อไป
Syntax
# Variable definition
variable_name = "value"
# Single assign variable
firstname = "Pathompat"
print("my firstname is ", firstname)
# Multiple assign variable
x, y, z = "Orange", "Banana", "Cherry"
print("my fruits is ", x, y, x)
# Recieve input value to variable
lastname = input()
print("my lastname is ", lastname)
Reserved words
Python Keywords | |||
---|---|---|---|
False | def | if | raise |
None | del | import | return |
True | elif | in | try |
and | else | is | while |
as | except | lambda | with |
assert | finally | nonlocal | yield |
break | for | not | |
class | from | or | |
continue | global | pass |
Data Types
1 ตัวแปรสามารถเก็บข้อมูลได้ชนิดเดียวเท่านั้น ซึ่ง python จะเป็นคนระบุให้เราเองผ่านค่าที่เรากำหนดในตัวแปร
- Text:
str
- Numeric:
int
,float
- Boolean:
bool
- Binary:
bytes
,bytearray
การตรวจสอบ Type
# check a is type str
a = "Pathompat"
print(type(a))
# check b is type int
b = 5
print(type(b))
Operators
การเขียนโปรแกรมต้องใช้การดำเนินการทางคณิตศาสตร์ เช่น บวก ลบ คูณ หาร หรือการดำเนินการทางตรรกศาสตร์ เช่น And, Or, Not เพื่อใช้ตรวจสอบความถูกต้องของโปรแกรม และใช้เป็นเงื่อนไขในการทำงานของโปรแกรมได้
Arithmetic Operators:
+
บวก-
ลบ*
คูณ/
หาร%
Modulation (หารเอาเศษ)**
ยกกำลัง
ตัวอย่าง
x = 6
y = 5
print("sum x, y = ", x + y)
print("result x * y - x = ", x - y * x)
Comparison Operators:
เป็นตัวดำเนินการเปรียบเทียบ 2 ค่าจะ return ค่า Boolean (true, false) เท่านั้น
==
เท่ากับ เช่นx == y
!=
ไม่เท่ากับ เช่นx != y
>
มากกว่า และ>=
มากกว่าหรือเท่ากับ เช่นx > y
<
น้อยกว่า และ<=
น้อยกว่าหรือเท่ากับ เช่นx <= y
ตัวอย่าง (ใช้ร่วมกับ Arithmetic Operators)
x = 10
y = 5
# check x equal y ?
print("x equal y :", x == y)
# check x greather than or equal 2 times of y ?
print("x equal y :", x == 2 * y)
Logical Operators:
เป็นตัวดำเนินการทางตรรกศาสตร์ ให้ผลเป็น Boolean (true, false) เท่านั้นเช่นกัน
-
and
หรือและ
เช่นx == 0 and x >= y
คือ x เท่ากับ 0 และ x ต้องมากกว่า y ด้วย -
or
หรือหรือ
เช่นx < y or x > 5
คือ x น้อยกว่า y หรือ x มากกว่า 5 อย่างใดอย่างหนึ่ง not
เช่นnot(x == y)
คือ x ต้องไม่เท่ากับ y
ตัวอย่าง
x = 5
y = 2
# check x equal y and y greater than 2 ?
print("result: ", x == y and y > 2)
# check x + y is greather than 10 or y power 2 is less than 5
print("result: ", x + y > 10 or y ** 2 < 5)
Exercise:
- สร้างโปรแกรมรับค่า a, b และนำมาแสดงผล
💡
แสดงผล "program result of a + b is {a+b}"
- สร้างโปรแกรมรับค่า ชื่อ, นามสกุล, อายุ และแสดงผลตามที่ user input เข้าไป
- จากข้อ 2 ให้ทำการเปลี่ยน input อายุเป็น input ค.ศ. ที่เกิด และนำมาคำนวณอายุโดยลบจากปีปัจจุบัน
💡
สูตรคำนวณใช้ 2024 - ปีเกิดที่กรอกเข้ามา