วันเสาร์ที่ 7 พฤษภาคม พ.ศ. 2559
การวนรอบ looping
ตอนที่ 7 การวนรอบ loop while, do while , for
การสร้าง loop while
หลักการเขียน loop while ก็ไม่ยาก
while เงื่อนไข :
คำสั่ง
ดังตัวอย่าง
การสร้าง loop do..while
เนื่องจาก python ไม่มี do..while โดยตรง จึงต้องใช้ loop while โดยใช้เทคนิคเล็กน้อย
ดังตัวอย่างโค้ดนี้
การสร้าง loop for
การสร้าง loop for จริงๆแล้ว simple มาก
หลักการคือ
for ชื่อตัวแปรที่ต้องการให้วนรอบ in range(ค่าเริ่มต้น,ค่าสิ้นสุด) : ดังตัวอย่างโค้ดนี้
การสร้าง loop while
หลักการเขียน loop while ก็ไม่ยาก
while เงื่อนไข :
คำสั่ง
ดังตัวอย่าง
number=10
loop=1
while loop<9 :
print(number)
number=number-1
loop=loop+1
การสร้าง loop do..while
เนื่องจาก python ไม่มี do..while โดยตรง จึงต้องใช้ loop while โดยใช้เทคนิคเล็กน้อย
ดังตัวอย่างโค้ดนี้
loop=1
while True :
print(loop)
loop=loop+1
if(loop>1) :
break
การสร้าง loop for
การสร้าง loop for จริงๆแล้ว simple มาก
หลักการคือ
for ชื่อตัวแปรที่ต้องการให้วนรอบ in range(ค่าเริ่มต้น,ค่าสิ้นสุด) : ดังตัวอย่างโค้ดนี้
for i in range (1,10) :
print("we are running from ",i," . ")
จริงๆใน python ยังมีอีกหลาย loop มากแต่เอาเท่านี้ไปก่อนนะ
แหล่งที่มาแอบอ้าง http://stackoverflow.com/questions/743164/emulate-a-do-while-loop-in-python
การตัดสินใจ Decision
ตอนที่ 6 การตัดสินใจ Decision
เงื่อนไข if และ if elif
if เงื่อนไข :
คำสั่งต่างๆ
ถ้าใช้ร่วมกับ elif ก็จะใช้แบบนี้นะ
if เงื่อนไข :
คำสั่งต่างๆ
elif เงื่อนไข :
คำสั่งต่างๆ
ตัวอย่างโค้ด
การใช้ switch ในภาษา python
เนื่องจาก python ไม่มี switch case จึงใช้ dictionary ในการ switch ข้อมูลแทน ดังตัวอย่าง
เงื่อนไข if และ if elif
if เงื่อนไข :
คำสั่งต่างๆ
ถ้าใช้ร่วมกับ elif ก็จะใช้แบบนี้นะ
if เงื่อนไข :
คำสั่งต่างๆ
elif เงื่อนไข :
คำสั่งต่างๆ
ตัวอย่างโค้ด
number=input("Please Enter number 1-10 : ")
if int(number)>5 :
print("You Entered number greater than 5")
elif int(number)<=5 :
print("You Entered number less than 6")
ตัวอย่างโค้ด 2 การใช้ if elif และ else
number=input("Please enter number 1-12 : ")
if int(number)>6 and int(number)<13 :
print("Hi")
elif int(number)==6 :
print("Middle")
elif int(number)>0 and int(number)<6 :
print("Lo")
else :
print("You entered the fucking wrong number.I said 1-12 bitch!!")
การใช้ switch ในภาษา python
เนื่องจาก python ไม่มี switch case จึงใช้ dictionary ในการ switch ข้อมูลแทน ดังตัวอย่าง
def numbers_to_strings(argument):
switcher = {
0: "zero",
1: "one",
2: "two",
}
return switcher.get(argument, "nothing")
print(numbers_to_strings(1))
แหล่งที่มาแอบอ้าง http://www.tutorialspoint.com/python/python_dictionary.htmวันศุกร์ที่ 6 พฤษภาคม พ.ศ. 2559
การแปลงชนิดของข้อมูล Data type conversion
ตอนที่ 5 การแปลงชนิดของข้อมูลด้วยฟังก์ชัน
int(ข้อมูล) แปลงข้อมูลให้เป็น integer
float(ข้อมูล) แปลงข้อมูลให้เป็น float
str(ข้อมูล) แปลงข้อมูลให้เป็น string
list(ข้อมูล) แปลงข้อมูลให้เป็น list
tuple(ข้อมูล) แปลงข้อมูลให้เป็น tuple ยกตัวอย่างดังโค้ดนี้
int(ข้อมูล) แปลงข้อมูลให้เป็น integer
float(ข้อมูล) แปลงข้อมูลให้เป็น float
str(ข้อมูล) แปลงข้อมูลให้เป็น string
list(ข้อมูล) แปลงข้อมูลให้เป็น list
tuple(ข้อมูล) แปลงข้อมูลให้เป็น tuple ยกตัวอย่างดังโค้ดนี้
int_=100
float_=5.94
str_="NeoIsTheBest"
str_1=str(int_)
str_2=str(float_)
print(int(float_),type(int(float_)))
print(float(int_),type(float(int_)))
print(str_1+str_2,type(str_1))
print(list(str_2),type(list(str_2)))
print(tuple(str_),type(tuple(str_)))
จะได้ผลลัพธ์ดังนี้ (ขออนุญาตไม่อธิบายนะ ลองวิเคราะห์กันดู)
int_=100
float_=5.94
str_="NeoIsTheBest"
str_1=str(int_)
str_2=str(float_)
print(int(float_),type(int(float_)))
print(float(int_),type(float(int_)))
print(str_1+str_2,type(str_1))
print(list(str_2),type(list(str_2)))
print(tuple(str_),type(tuple(str_)))
แหล่งที่มาแอบอ้าง http://www.pitt.edu/~naraehan/python2/data_types_conversion.html
การรับค่าจากแป้นพิมพ์ Input from Keyboard
ตอนที่ 4 การรับข้อมูลจากคีย์บอร์ด
ในการรับข้อมูลจากคีย์บอร์ดก็ง่ายมากคือใช้ input("ข้อความที่จะพิมพ์ออกคีย์บอร์ด")
ถ้าจะนำตัวแปรมารับข้อมูลเพื่อนำไปใช้ก็ทำได้เช่น
variable_name = input("คำถาม")
ส่วนการแสดงข้อมูลข้อความออกทางหน้าจอ เราจะเชื่อมข้อมูลกับตัวแปรด้วย
เครื่องหมาย + เช่น
print("ข้อความ"+variable_name+"ข้อความ2")
มาดูโค้ดเต็มๆได้ที่
แหล่งที่มาแอบอ้าง http://www.python-course.eu/input.php
ในการรับข้อมูลจากคีย์บอร์ดก็ง่ายมากคือใช้ input("ข้อความที่จะพิมพ์ออกคีย์บอร์ด")
ถ้าจะนำตัวแปรมารับข้อมูลเพื่อนำไปใช้ก็ทำได้เช่น
variable_name = input("คำถาม")
ส่วนการแสดงข้อมูลข้อความออกทางหน้าจอ เราจะเชื่อมข้อมูลกับตัวแปรด้วย
เครื่องหมาย + เช่น
print("ข้อความ"+variable_name+"ข้อความ2")
มาดูโค้ดเต็มๆได้ที่
name=input("What is your name?")
print("Hello "+name+" Nice to meet you.")
age=input("How old are you?")
print("Oh you're "+age+" years old")
ภาษา python ง่ายนิดเดี๋ยวแหล่งที่มาแอบอ้าง http://www.python-course.eu/input.php
การดำเนินการทางคณิตศาสตร์ใน python
ตอนที่ 3 arithmetic operators in python
การดำเนินการทางคณิตศาสตร์ใน python สรุปจากโค้ดเลยได้ดังนี้
การดำเนินการทางคณิตศาสตร์ใน python สรุปจากโค้ดเลยได้ดังนี้
a=10
b=15
c=5.5
d=-125
print(a+b) #10+15
print(b-a) #15-10
print(a*c) #10x5.5
print(b/a) #15/10
print(b//a) #15//10 result only floor
print(b%a) #15%a modulo the remainder after division
print((-a)+b) #-10+15
print(abs(d)) #absolute a
print(a**b) #a^b or a exponent b
ลำดับการดำเนินการของตัวดำเนินการ order of operations
Parentheses หรือ ( )
Exponents หรือ **
Multiplication and Division หรือ * , / , // , %
Addition and Subtraction หรือ + , -
มีเทคนิคการจำง่ายๆคือ Please excuse my dear aunt Sally. แหล่งที่มาแอบอ้าง https://en.wikibooks.org/wiki/Python_Programming/Basic_Math
มีเทคนิคการจำง่ายๆคือ Please excuse my dear aunt Sally. แหล่งที่มาแอบอ้าง https://en.wikibooks.org/wiki/Python_Programming/Basic_Math
วันพฤหัสบดีที่ 5 พฤษภาคม พ.ศ. 2559
การประกาศตัวแปรในภาษา python
ตอนที่ 2 การประกาศตัวแปรในภาษา python
data types ในภาษา python มีคร่าวๆดังนี้
Types are a category for things within Python with which Python will work. Types are
- integers
- Whole numbers from negative infinity to infinity, such as 1, 0, -5, etc.
- float
- Short for "floating point number," any rational number, usually used with decimals such as 2.8 or 3.14159.
- strings
- A set of letters, numbers, or other characters.
- tuples
- A list with a fixed number of elements. ie x=(1,2,3) parentheses makes it a tuple.
- lists
- A list without a fixed number of elements. ie x=[1,2,3] note the square brackets, a list
- dictionaries
- A type with multiple elements i.e. x = {1: 'a','b': 2,3: 3} where you address the elements with, e.g., a text.
- เผิ่นว่ามี data types ชนิดต่างๆใน python ดังนี้
- intergers ก็คือตัวเลขต่างๆตั้งแต่ค่าลบ ไปจนถึงค่าบวกเช่น 1,0,-5, อื่นๆ
- float เป็นชื่อย่อของ floating point numer คือจำนวนจริงที่ใช้กับเลขฐาน 10 เช่น 2.8 หรือ 3.14159
- strings ก็คือเซ็ทของข้อความ, ตัวเลข, หรืออักขระอื่นๆ
- tuples คือ รายการของจำนวนตัวเลขที่มีการกำหนดค่าคงที่ เช่น x=(1,2,3) อยู่ในวงเล็บ
- lists คือ รายการของตัวเลขที่ไม่ได้ถูกกำหนดค่าคงที่ เช่น x=[1,2,3]
- dictionaries คือชนิดของข้อมูลหลายองค์ประกอบ เช่น x={1:'a','b':2,3:3} where you address the elements with, e.g., a text.
- ------
- การประกาศตัวแปร
- ก็พิมพ์ค่าของตัวแปรเข้าไปได้เลยโดยไม่จำเป็นต้องระบุชนิด เหมือนภาษา C/C++
- ซึ่งถือว่าค่อนข้างง่ายมากๆ
เช่น
a=10
b=20.3
c='a'
d=(1,2,3)
e=[4,5,6]
x={1: 'a','b': 2,3: 3}
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(x))
ลองรันดูด้วยคำสั่ง python3 ตามด้วยชื่อไฟล์ที่ท่านเขียน ก็จะได้
<class 'int'>
<class 'float'>
<class 'str'>
<class 'tuple'>
<class 'list'>
<class 'dict'>
ออกทางหน้าจอ
ลืมบอกไปเรื่องคำสงวน คือคำที่ไม่สามารถเอามาใช้เป็นชื่อตัวแปรได้ มีดังนี้
คำสงวน
คำสงวน คือ ชื่อหรือคำที่ภาษาไพธอนสงวนไว้เฉพาะเพื่อใช้เป็นคำสั่ง หรือมีไว้เพื่อเขียนเป็นโครงสร้างของตัวภาษาเอง ฉะนั้นผู้เขียนโปรแกรมจึงควรหลีกเลี่ยงคำสงวนเหล่านี้ในการตั้งชื่อโปรแกรม ตัวแปร หรือชื่อใด ๆ ก็ตามที่ตั้งขึ้นมาใหม่แล้วตรงกับคำสงวน คำสงวนมีด้วยกัน 31 คำดังต่อไปนี้
and
as
assert
break
class
continue
def
| del elif else except exec finally for | from global if import in is lambda | not or pass raise return try |
แหล่งที่มาแอบอ้าง
https://en.wikiversity.org/wiki/Python/Basic_data_types
https://sites.google.com/site/dotpython/installation/variable
https://en.wikiversity.org/wiki/Python/Basic_data_types
https://sites.google.com/site/dotpython/installation/variable
สมัครสมาชิก:
บทความ (Atom)