Online Python IDE
สื่อการสอนการเขียนโปรแกรม Python แบบออนไลน์
กดปุ่ม “รันโค้ด” เพื่อดูผลลัพธ์…
ตัวอย่างโค้ดสำหรับฝึกหัด
เรียนรู้การคำนวณพื้นฐานและการใช้ตัวแปร
# การคำนวณพื้นฐาน
a = 15
b = 4
print(f”ผลบวก: {a + b}”)
print(f”ผลคูณ: {a * b}”)
การจัดการข้อความและฟังก์ชัน string
# การจัดการ String
text = “สวัสดี Python”
print(text.upper())
print(len(text))
เรียนรู้การใช้ for loop และ while loop
# For Loop
for i in range(5):
print(f”รอบที่ {i+1}”)
# List comprehension
numbers = [x*2 for x in range(5)]
print(numbers)
การใช้งาน list และ dictionary ใน Python
# List และ Dictionary
fruits = [“แอปเปิ้ล”, “กล้วย”, “ส้ม”]
prices = {“แอปเปิ้ล”: 50, “กล้วย”: 30}
print(fruits)
print(prices[“แอปเปิ้ล”])
เรียนรู้การสร้างและใช้งานฟังก์ชัน
# การสร้าง Function
def greet(name):
return f”สวัสดี {name}!”
def calculate_area(width, height):
return width * height
print(greet(“นักเรียน”))
print(f”พื้นที่: {calculate_area(5, 3)}”)
เกมง่ายๆ ที่ใช้ random และ condition
# เกมทายตัวเลข
import random
secret = random.randint(1, 10)
print(“ทายเลข 1-10!”)
# guess = int(input(“ใส่ตัวเลข: “))
guess = 5 # ตัวอย่าง
if guess == secret:
print(“ถูกต้อง!”)
else:
print(f”ผิด! คำตอบคือ {secret}”)