Variables and Types, Operators, Math
Note
Input / Output และ f-string ต่อใน Module 3b 👉
โครงสร้างของโปรแกรม Python
# นำเข้าคำสั่งภายนอก (bring in external functions)
import datetime
print("----- Age Calculate Program -----")
# รับค่าอินพุตนำไปกำหนดค่าตัวแปร (get input, store in a variable)
yob = input("What's your year of birth? (e.g., 2004): ")
# ประมวลผล (process data using variables)
today = datetime.date.today() # ดึงค่า วัน-เวลา ปัจจุบัน
year = today.year # สนใจเฉพาะ ปี ปัจจุบัน
age = year - int(yob) # คำนวณอายุ
# แสดงผล (display output)
print("You are", age , "years old.")
print()
print("Congratulations on your first Python program.")ตัวแปร — การกำหนดชื่อให้พื้นที่ในหน่วยความจำ
a-z, A-Z และ _ (underscore)var_1 print_this_to_screen1variable ใช้ไม่ได้!, @, #, $, %Warning
Case-sensitive — var กับ Var เป็นคนละตัว
คำเหล่านี้ ห้ามใช้เป็นชื่อตัวแปร
ชนิดข้อมูลพื้นฐาน — str, int, float, bool
| ชนิด | คำอธิบาย | ตัวอย่าง |
|---|---|---|
str |
ข้อความ (string) | 'hello', "2345" |
int |
เลขจำนวนเต็ม | 50000, 1_000_000 |
float |
เลขทศนิยม | 3.14, 1.23e9 |
bool |
ค่าความจริง | True, False |
สร้างตัวแปรชื่อ var1 เพื่อเก็บค่า ผลคูณ ของ 100 กับ 2 แล้ว แสดงผล
Note
วิธีคิด
var1var1var1 ด้วยคำสั่ง printสร้างตัวแปรชื่อ var2 เพื่อเก็บค่า ผลบวก ของ 1,000,000 กับ 1 แล้ว แสดงผล
สร้างตัวแปรชื่อ var3 เพื่อเก็บค่าของ 10 หาร ด้วย 2 แล้ว แสดงผล
คิดก่อน: var3 จะเป็น int หรือ float?
Note
วิธีคิด
var3var3var3 ด้วยคำสั่ง printสร้างตัวแปรชื่อ s1 เพื่อเก็บข้อความ "this is a string" แล้ว แสดงผล
ข้อความ (String) ต้องอยู่ภายใต้เครื่องหมาย quote
| Quote | ใช้เมื่อ | ตัวอย่าง |
|---|---|---|
'...' |
ข้อความทั่วไป | 'hello' |
"..." |
มี apostrophe ในข้อความ | "isn't" |
'''...''' |
ข้อความหลายบรรทัด | '''line1\nline2''' |
แสดงผลข้อความ this is a simple string.
คิดก่อน: ใช้คำสั่งอะไรในการแสดงผล?
แสดงผลข้อความ this isn't a simple string.
คิดก่อน: ใช้ ' ' ได้ไหม?
แสดงผลข้อความ this is how to print "double quote" in sentence.
แสดงผลข้อความ 3 บรรทัด ดังนี้
this is another string
this is 2nd line
this is 3rd line
\ )" ", ' ' หรือ ''' '''\n, \t| Escape Code | Description |
|---|---|
\n |
newline (ขึ้นบรรทัดใหม่) |
\r |
carriage return |
\t |
tab (แท็บ) |
\v |
vertical tab |
\b |
backspace |
| Escape Code | Description |
|---|---|
\f |
form feed (page feed) |
\a |
alert (beep) |
\' |
single quote (') |
\" |
double quote (") |
\\ |
backslash (\) |
Python ไม่บังคับ ชนิดข้อมูลตายตัว (ต่างจาก C/C++, Java) ตัวแปรสามารถเปลี่ยนไปเก็บข้อมูลชนิดอื่นได้อัตโนมัติ (dynamic typing)
ตรวจสอบชนิดด้วย type()

ที่มา: xkcd #353 — “Python” (CC BY-NC 2.5)
1.234 และ 1.23e11 เป็นข้อมูลประเภทใด?
สร้างตัวแปร s2 เก็บผลบวกของ 2500 กับ 1000 แล้วแสดงค่า s2 และตรวจสอบ type
คิดก่อน: s2 จะเป็นชนิดใด?
ต่อจากข้อก่อน — หาค่า s2 หารด้วย 2 แล้วเก็บกลับใน s2
คิดก่อน: s2 จะเป็นชนิดใด?
สร้างตัวแปร s3 เก็บผลบวกของข้อความ 'hello' กับ 'there' (เว้นวรรคด้วย)
คิดก่อน: s3 จะเป็นชนิดใด?
ทดลองหาผลบวกของ s3 (string) กับ 1 (int)
คิดว่าบวกกันได้ไหม?
การเปลี่ยนชนิดข้อมูล — int(), float(), str()
| ฟังก์ชัน | แปลงเป็น |
|---|---|
int() |
จำนวนเต็ม |
float() |
ทศนิยม |
str() |
ข้อความ |
bool() |
True/False |
Note
Automatic conversion: int + float → Python แปลง int เป็น float ให้อัตโนมัติ
99 (int) + 1.23 (float) → ผลลัพธ์เป็น type อะไร?
Note
แล้ว 4 กับ 4.00 เป็น type เดียวกันหรือไม่🤔
หาผลบวกของ 42 กับ 3.5 แล้วเก็บเป็น integer ในตัวแปร i
แปลงตัวแปร i จาก integer ให้เป็น float
กำหนด s = '1234' (string) — แปลงเป็น integer แล้วแสดงผล
กำหนด str1 = '1234.50' — หาผลบวกของ str1 กับ 1000 เก็บในตัวแปร x
ลองคิด: จะแปลง str1 เป็น int หรือ float ดี?
แปลง string 'Hello' เป็น integer ได้หรือไม่?
ตัวดำเนินการ — Arithmetic, Comparison, Logical, Assignment
number + number → ได้ตามหลักคณิตศาสตร์string + string → นำข้อความมา เรียงต่อกัน (concatenation)string + number → ไม่ได้! จะเกิด TypeErrorกำหนด x = 16, y = 4
| Operator | ความหมาย | ผลลัพธ์ |
|---|---|---|
x + y |
บวก | 20 |
x - y |
ลบ | 12 |
x * y |
คูณ | 64 |
x / y |
หาร (ได้ float เสมอ) | 4.0 |
x // y |
หารปัดเศษทิ้ง (floor division) | 4 |
x % y |
เศษของการหาร (modulus) | 0 |
x ** y |
ยกกำลัง | 65536 |
แบ่งคนในห้อง 85 คน ออกเป็น 7 กลุ่ม — เหลือเศษกี่คน?
แบ่งคนในห้อง 85 คน เป็นกลุ่มละ 12 คน — ได้กี่กลุ่มที่มีสมาชิกครบ?
คำนวณ \(x^y\) เมื่อ \(x = 33\), \(y = 1.5\)
Tip
ทศนิยมเยอะไปไหม ถ้าต้องการทศนิยม 2 ตำแหน่งล่ะ🤔 ทำแบบนี้เลย…
อยากรู้เพิ่ม 👉 ดูที่หัวข้อ f-string
ให้ผลลัพธ์เป็น True หรือ False — กำหนด x = 10, y = 12
| Expression | ผลลัพธ์ |
|---|---|
x > y |
False |
x < y |
True |
x >= y |
False |
x <= y |
True |
| Expression | ผลลัพธ์ |
|---|---|
x == y |
False |
x != y |
True |
Warning
== เปรียบเทียบ = กำหนดค่า (assign) อย่าสับสน!
Note
True กับ False เป็นข้อมูล type ไหน🤔
กำหนด x=10, y=12 — ประโยค x > y และ x < y เป็นจริงหรือเท็จ? และเป็น type ไหน
จากข้อก่อน — จะตรวจสอบว่า
x เท่ากับ y หรือไม่?x ไม่เท่ากับ y หรือไม่?Note
ผลของ comparison operator ได้ตรรกะ True หรือ False แสดงว่าต้องมี operator ที่ใช้กระทำทางตรรกะแน่ ๆ เลย (👉 ดูหน้าต่อไป)
ใช้รวมเงื่อนไขหลายตัวเข้าด้วยกัน
and — ทั้งคู่ต้อง Trueor — แค่ตัวใดตัวหนึ่ง Truenot — กลับค่าNote
👉 ใน Module 3 เราเจอ Logical Operators แค่นิดเดียว — แต่จะได้ใช้งานจริงจังมากใน Module 5 (if) และ Module 6 (while loop) ตอนสร้าง เงื่อนไข control flow 🔀
กำหนด x = True, y = False — หาค่า x and y, x or y, not y
แบบพื้นฐาน — กำหนดค่า (=) 📌
ตัวย่อ — assign + คำนวณในตัวเดียว ⚡
| ตัวย่อ | เทียบเท่ากับ | ผลลัพธ์ (คิดจาก x = 10) |
|---|---|---|
x += 5 |
x = x + 5 |
x = 15 |
x -= 3 |
x = x - 3 |
x = 7 |
x *= 2 |
x = x * 2 |
x = 20 |
x /= 4 |
x = x / 4 |
x = 2.5 |
x **= 2 |
x = x ** 2 |
x = 100 |
x //= 4 |
x = x // 4 |
x = 2 |
x %= 3 |
x = x % 3 |
x = 1 |
ตัวย่อช่วยให้เขียนสั้นลง 👉 x += 5 มีความหมายเหมือน x = x + 5
อ่านว่า: “เอาค่า x เดิม มาบวก 5 แล้วเก็บกลับเข้า x”
ดังนั้นต้องมี x = 10 (กำหนดค่าเริ่มต้น) มาก่อนเสมอ ไม่งั้นจะไม่มีค่าเดิมให้บวกเพิ่ม
ลำดับจาก สูงสุด → ต่ำสุด
| Operators | Description | Associativity (การจับคู่) |
|---|---|---|
() |
Parentheses | — |
** |
Exponentiation (raise to the power) | ขวา → ซ้าย ⭐ |
+x, -x, ~x |
Unary plus, Unary minus, Bitwise NOT | ขวา → ซ้าย |
*, /, //, % |
Multiplication, Division, Floor division, Modulus | ซ้าย → ขวา |
+, - |
Addition, subtraction | ซ้าย → ขวา |
<<, >> |
Bitwise shift operators |
ซ้าย → ขวา |
& |
Bitwise AND |
ซ้าย → ขวา |
^, \| |
Bitwise XOR and OR |
ซ้าย → ขวา |
>, >=, <, <= |
Comparison operators | ซ้าย → ขวา |
==, != |
Equality operators | ซ้าย → ขวา |
=, +=, -=, *=, **=, /=, //= |
Assignment operators | ขวา → ซ้าย ⭐ |
is, is not |
Identity operators | ซ้าย → ขวา |
in, not in |
Membership operators | ซ้าย → ขวา |
not, or, and |
Logical operators | ซ้าย → ขวา |
กำหนด \(a=20, b=10, c=15, d=5\) — คำนวณในใจก่อน
a + b * c / d = ?(a + b) * c / d = ?a + b * (c / d) = ?a + (b * c) / d = ?กำหนด \(y = -3x - \dfrac{2}{10^2}\) — จงหา \(y\) เมื่อ \(x = 5\)
วิธีคิด
xyprint()จากวิธีคิดข้างบน ถ้าสลับขั้นที่ 1. กับ 2. จะมีผลกับการรันโค้ดยังไง?
ชุดคำสั่งคณิตศาสตร์ — import math
math module — ภาพรวมoperator พื้นฐานmathimport โมดูลเข้าสู่โปรแกรมเสียก่อนNote
ในการทำงานจริง เรา import module เข้ามาเพียงครั้งเดียวก็สามารถใช้งานต่อได้ ไม่จำเป็นต้อง import ทุกครั้ง
math — ฟังก์ชันและค่าคงที่ฟังก์ชัน
math.sqrt(x) — \(\sqrt{x}\)math.sin(x), cos(x), tan(x)math.log(x), log2(x), log10(x)math.factorial(n) — \(n!\)math.ceil(x) — ปัดขึ้นmath.floor(x) — ปัดลงค่าคงที่
math.pi — \(\pi = 3.14159...\)math.e — \(e = 2.71828...\)แปลงมุม
math.degrees(rad) — rad → degreemath.radians(deg) — degree → radคำนวณ \(\sqrt{25}\) — ทำได้กี่วิธี?
คำนวณพื้นที่วงกลมที่มีรัศมี 10 หน่วย
\[area = \pi r^2\]
แปลงมุม 2 radian เป็นหน่วย องศา (degree)
แปลงมุม 60 องศา เป็นหน่วย radian
ให้คำนวณค่าของ ① \(\sin\) ของมุมขนาด 30 เรเดียน, ② \(\cos\) ของมุมขนาด 60 องศา
Warning
sin, cos, tan รับค่าเป็น radian เสมอ!
Factorial — \(4! = 1 \times 2 \times 3 \times 4\)
ตัวแปรและชนิดข้อมูล
str, int, float, booltype()int(), float(), str()Operators
+ - * / // % **> < == !=and or not= += -=Math Module
import mathsqrt, sin, cos, pi, …ceil, floor, factorialNote
ต่อ Module 3b: Input / Output และ f-string 👉
🏠 259201 — Module 3a | ภาควิชาวิศวกรรมคอมพิวเตอร์ คณะวิศวกรรมศาสตร์ มหาวิทยาลัยเชียงใหม่