การสร้างเวิร์กโฟลว์

Python เบื้องต้นสำหรับนักพัฒนา

Jasmin Ludolf

Senior Data Science Content Developer

เวิร์กโฟลว์ที่ซับซ้อน

  • วนซ้ำผ่านโครงสร้างข้อมูล
    • for, while
  • ประเมินเงื่อนไขหลายข้อ
    • if, elif, else, >, >=, <, <=, ==, !=
  • อัปเดตตัวแปร
    • +=
  • แสดงผลลัพธ์
    • print()
Python เบื้องต้นสำหรับนักพัฒนา

คีย์เวิร์ด "in"

  • in = ตรวจสอบว่าค่าอยู่ ใน ตัวแปร/โครงสร้างข้อมูลหรือไม่
recipe = {"pasta": 500, "tomatoes": 400, 
          "garlic": 15, "basil": 20}

if "pasta" in recipe.keys(): print(True) else: print(False)
True
  • เร็วกว่าการวนซ้ำตรวจสอบทุก key
Python เบื้องต้นสำหรับนักพัฒนา

คีย์เวิร์ด "not"

  • not = ตรวจสอบว่าเงื่อนไข ไม่เป็น จริง
  • มีประโยชน์สำหรับตรวจสอบว่าสิ่งใดสิ่งหนึ่งขาดหายไป
pantry_items = ["flour", "sugar", "olive oil"]

# Check if "salt" is NOT in our pantry if "salt" not in pantry_items: print(True) else: print(False)
True
Python เบื้องต้นสำหรับนักพัฒนา

คีย์เวิร์ด "and"

  • and = ตรวจสอบว่า เงื่อนไขหลายข้อ เป็นจริงพร้อมกัน
  • ใช้เมื่อต้องการให้ทุกเงื่อนไขเป็นจริง
pasta_quantity = 600
olive_oil_quantity = 30

# Check if we have enough of BOTH ingredients if pasta_quantity >= 500 and olive_oil_quantity >= 30: print(True) else: print(False)
True
Python เบื้องต้นสำหรับนักพัฒนา

คีย์เวิร์ด "or"

  • or = ตรวจสอบว่าเงื่อนไขใดเงื่อนไขหนึ่ง (หรือมากกว่า) เป็นจริง
  • ใช้เมื่อมีตัวเลือกที่ยอมรับได้หลายทาง
pasta_quantity = 600
olive_oil_quantity = 30

# Check if we have enough of EITHER ingredient if pasta_quantity >= 500 or olive_oil_quantity >= 30: print(True) else: print(False)
True
Python เบื้องต้นสำหรับนักพัฒนา

การบวก/ลบค่าจากตัวแปร

  • รวมคีย์เวิร์ดกับเทคนิคอื่นเพื่อสร้างเวิร์กโฟลว์ที่ซับซ้อน
ingredients_checked = 0
for ingredient in recipe_list:
    # ingredients_checked = ingredients_checked + 1
    ingredients_checked += 1

items_to_buy = 10 for item in shopping_list: # items_to_buy = items_to_buy - 1 items_to_buy -= 1
  • += เพิ่มค่าให้ตัวแปร, -= ลดค่าจากตัวแปร
  • วิธีอื่นในการอัปเดตตัวแปร
Python เบื้องต้นสำหรับนักพัฒนา

การ append

  • เก็บข้อมูลที่ตรงตามเงื่อนไขที่กำหนดไว้ในลิสต์
# Create empty list to hold results
shopping_list = []

# Loop through recipe ingredients for ingredient, qty_needed in recipe.items():
# Check if we need to buy it if ingredient not in pantry:
# Add to shopping list shopping_list.append(ingredient)
Python เบื้องต้นสำหรับนักพัฒนา

การ append

print(shopping_list)
['tomatoes', 'salt']
Python เบื้องต้นสำหรับนักพัฒนา

มาฝึกกันเถอะ!

Python เบื้องต้นสำหรับนักพัฒนา

Preparing Video For Download...