วันพฤหัสบดีที่ 20 สิงหาคม พ.ศ. 2558

ทำความเข้าใจ " Basic Python พื้นฐาน "

ทำความเข้าใจ " Basic Python พื้นฐาน "

   การเขียน Python พื้นฐานก่อนอื่นน่ะครับต้องเข้าใจก่อนว่า ภาษา Python เป็นภาษาที่มี Syntax ที่ง่ายเเละเป็นการทำงานเเบบที่ล่ะบรรทัด ไม่มี '{' เเละ '}' เเต่จะใช้การย่อหน้าเเทนว่าเริ่ม หรือจบตรงไหนเพราะอย่างนี้เเหละครับ ควรระวังเวลาเขียนให้ดีควรเขียนให้เป็นระเบียบเพื่อให้ง่ายในการตรวจสอบน่ะครับ เเละการประกาศตัวเเปรไม่จำเป็นต้องระบุว่าเป็นตัวเเปรชนิดอะไร เช่น x = 100 Python จะทราบเองว่าตัวเเปรชนิดนี้เป็นตัวเเปรที่เป็น int หรือ เลขจำนวนเต็ม เป็นต้น เเละสามารถที่จะหาข้อมูลในการเขียนได้ง่ายตาม google น่ะครับในที่นี้ผมจะย่อตัวอย่างเเบบคร่าวๆน่ะครับ

Python Loops

คำสั่ง while loop
 
   #Syntaxwhile
   while expression:
       statement(s)

   #Example 
   count = 0
   while (count < 9):
       print 'The count is:', count
       count = count + 1
   print "Good bye!"

   #Output
   The count is: 0
   The count is: 1
   The count is: 2
   The count is: 3
   The count is: 4
   The count is: 5
   The count is: 6
   The count is: 7
   The count is: 8
   Good bye!
    
คำสั่ง for loop
   
   #Syntaxfor
   for iterating_var in sequence:
       statements(s)

   #Example
   for letter in 'Python':     # First Example
       print 'Current Letter :', letter
   fruits = ['banana', 'apple',  'mango']
   for fruit in fruits:        # Second Example
       print 'Current fruit :', fruit
   print "Good bye!"

   #Output
   Current Letter : P
   Current Letter : y
   Current Letter : t
   Current Letter : h
   Current Letter : o
   Current Letter : n
   Current fruit : banana
   Current fruit : apple
   Current fruit : mango
   Good bye!
       

Python if เเละ if...else

คำสั่ง if
 
         #Syntaxif
   if expression:
      statement(s)

   #Example
   var1 = 100
   if var1:
       print "1 - Got a true expression value"
       print var1
   var2 = 0
   if var2:
       print "2 - Got a true expression value"
       print var2
   print "Good bye!"

   #Output
   1 - Got a true expression value
   100
   Good bye!
    
คำสั่ง if...else
   
   #Syntax
   if expression1:
       statement(s)
   elif expression2:
       statement(s)
   elif expression3:
       statement(s)
   else:
       statement(s)

   #Example
   var = 100
   if var == 200:
       print "1 - Got a true expression value"
       print var
   elif var == 150:
       print "2 - Got a true expression value"
        print var
   elif var == 100:
       print "3 - Got a true expression value"
       print var
   else:
       print "4 - Got a false expression value"
       print var
   print "Good bye!"

   #Output
   3 - Got a true expression value
   100
   Good bye!
       

Python Functions

สร้าง Functions
   
   #Syntax
   def functionname( parameters ):
       function_suite
       return [expression]

   #Example
    def printstr( str ):
       print str
       return
    printstr("Python");

   #Output
   Python
       

ศึกษาเพิ่มเติมได้ที่ 


อ้างอิงข้อมูลจากwww.tutorialspoint.com

ไม่มีความคิดเห็น:

แสดงความคิดเห็น