[Part 1] Python 101: Basic programming

[Part 1] Python 101: Basic programming

Photo by Philipp Katzenberger / Unsplash

Basic Process

The Input-Process-Output (IPO) Model | Download Scientific Diagram
กระบวนการพื้นฐานของโปรแกรม

Basic syntax

  1. Comment

ใช้ # คั่นข้างหน้าสุด ส่วนนั้นจะไม่ทำงาน ใช้อธิบายโค้ดหรือใช้โน้ตสำหรับ Developer

# Hello Python
  1. Output
# print hello world
print("Hello World")
  1. 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
   
Falsedefifraise
Nonedelimportreturn
Trueelifintry
andelseiswhile
asexceptlambdawith
assertfinallynonlocalyield
breakfornot
classfromor
continueglobalpass

Data Types

1 ตัวแปรสามารถเก็บข้อมูลได้ชนิดเดียวเท่านั้น ซึ่ง python จะเป็นคนระบุให้เราเองผ่านค่าที่เรากำหนดในตัวแปร

  • Text: str
  • Numeric: int, float
  • Boolean: bool
  • Binary: bytesbytearray

การตรวจสอบ 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:

  1. สร้างโปรแกรมรับค่า a, b และนำมาแสดงผล
💡
แสดงผล "program result of a + b is {a+b}"
  1. สร้างโปรแกรมรับค่า ชื่อ, นามสกุล, อายุ และแสดงผลตามที่ user input เข้าไป
  2. จากข้อ 2 ให้ทำการเปลี่ยน input อายุเป็น input ค.ศ. ที่เกิด และนำมาคำนวณอายุโดยลบจากปีปัจจุบัน
💡
สูตรคำนวณใช้ 2024 - ปีเกิดที่กรอกเข้ามา