สวัสดียามค่ำกับ Python ครับ หลายๆคนอาจจะพอได้ยินข่าวเรื่องการโจมตีแพลตฟอร์มจาวากันมาบ้างแล้ว ไม่ว่าจะเป็นเฟสบุ๊ค, ทวิตเตอร์, แอปเปิ้ล ไม่เว้นแม้กระทั่งไมโครซอฟท์ก็โดนไปตามๆกัน เวลานี้จึงเป็นช่วงเวลาที่ภาษาอื่นๆจะได้โผล่ขึ้นมาอวดโลกจริงๆจังๆ อย่างเช่น Python ที่ผมเขียนมา 3-4 บทความนี่เอง สำหรับบทความนี้ผมก็จะมาแสดงตัวอย่างเกี่ยวกับคำสั่งควบคุมต่างๆของ Python ครับ
คำสั่งเงื่อนไข : if-else
i = 0
if i >= 0:
print("Number is positive")
else:
print("Number is negative")
#Number is positive
คำสั่งเงื่อนไข : elif
i = 0
if i > 0:
print("Number is positive")
elif i == 0:
print("Number is zero")
else:
print("Number is negative")
#Number is zero
แต่รู้สึกว่าในส่วน switch-case ในไพทอนเหมือนจะไม่มีนะครับคำสั่งทำซ้ำ : while loop
i = 0
while (i < 3):
print("The count is:",i)
i = i + 1
#The count is: 0
#The count is: 1
#The count is: 2
คำสั่งทำซ้ำ : for loop
for i in range(0,5):
print("The count is:",i)
i = i + 1
#The count is: 0
#The count is: 1
#The count is: 2
#The count is: 3
#The count is: 4
