Post by Goborijung at 2020-12-23 01:00:26 | ID: 933
1. install python-version
2. set path : python and pip script
3. goto python-version/Script
4. Open PowerShell : pip install pyodbc
>> check Python Version
python --version
>> check pip version
pip --version
>> check odbc driver
control panel > Administative Tools > ODBC DataSorce (32bit)
>> Example Connect to SqlServer
import pyodbc
databaseName = 'GSS_Test'
username = 'sa'
password = 'gik8nv@tpf'
server = 'S410717NB0201\MSSQLSERVER2'
driver= '{SQL Server Native Client 11.0}'
CONNECTION_STRING = 'DRIVER='+driver+';SERVER='+server+';DATABASE='+databaseName+';UID='+username+';PWD='+ password
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute('SELECT * FROM Unit')
for row in cursor:
print(row)Post by Goborijung at 2020-12-22 11:55:56 | ID: 930
>> IF
a = 1
b = 1
if a==b:
print("Yes")
>> IF - Else
a = 1
b = 2
if a==b:
print("Yes")
else:
print("No")
>> IF - Else - IF
a = 1
b = 1
if a > b:
print("a > b")
elif a == b:
print("Yes")
>> หรือ
a = 1
b = 1
if a > b:
print("a > b")
elif a == b:
print("Yes")
else:
print("Other")
--------------------------------------------
>> ShortHand IF
a = 1
b = 1
if a == b: print("Yes")
>> ShortHand IF-Else
print("Yes") if a == b else print("No")Post by Goborijung at 2019-12-18 11:52:30 | ID: 337
>>> Python Array
cars = ["Ford", "Volvo", "BMW"]
print(cars[0])
Output :
Ford
หรือ
car1 = "Ford"
car2 = "Volvo"
car3 = "BMW"
print(car1)
Output :
Ford
------------------------------------------------------------------
-- การ SET ข้อมูลใน Array ให้เป็นค่าใหม่
cars = ["Ford", "Volvo", "BMW"]
cars[0] = "Toyota"
print(cars[0])
Output :
Toyota
------------------------------------------------------------------
-- Len นับจำนวนสมาชิก หรือ สตริงใน Array
cars = ["Ford", "Volvo", "BMW"]
len(cars)
Output :
3
หรือ
len("Hello") //5
------------------------------------------------------------------
-- Count นับชื่อสมาชิก ใน Array
cars = ["Ford", "Volvo", "Volvo" , "BMW"]
x = cars.count("Volvo")
Output :
2
หรือ
points = [1, 4, 2, 9, 7, 8, 9, 3, 1]
x = points.count(9)
print(x) //2
------------------------------------------------------------------
-- Looping Array (วนลูปดึงข้อมูลสมาชิกใน Array)
cars = ["Ford", "Volvo", "BMW"]
for x in cars:
print(x)
Output :
Ford
Volvo
BMW
------------------------------------------------------------------
-- Add Array (Appen เพิ่มข้อมูลสมาชิกใหม่เข้า Array) :: append()
cars = ["Ford", "Volvo", "BMW"]
cars.append("Toyota")
for x in cars:
print(x)
Output :
Ford
Volvo
BMW
Honda
------------------------------------------------------------------
-- การเพิ่ม(Add) สมาชิกเข้า Array โดยกำหนด Index ให้ด้วย :: insert()
fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, "orange")
print(fruits)
Output :
['apple', 'orange', 'banana', 'cherry']
------------------------------------------------------------------
-- การเพิ่ม Array เข้าไปใน Array อีกที
a = ["apple", "banana", "cherry"]
b = ["Ford", "BMW", "Volvo"]
a.append(b)
print(a)
Output :
['apple', 'banana', 'cherry', ['Ford', 'BMW', 'Volvo']]
------------------------------------------------------------------
-- การรวม Array 2 ชุดเข้าด้วยกันด้วย :: Extend
fruits = ['apple', 'banana', 'cherry']
cars = ['Ford', 'BMW', 'Volvo']
fruits.extend(cars)
print(fruits)
Output :
['apple', 'banana', 'cherry', 'Ford', 'BMW', 'Volvo']
------------------------------------------------------------------
-- Remove Array (ลบข้อมูลหรือดึงสมาชิกออกจาก Array) :: pop()
-- การใช้ pop() ต้องดึงด้วย Index
cars = ["Ford", "Volvo", "BMW"]
cars.pop(1)
for x in cars:
print(x)
Output :
Ford
BMW
------------------------------------------------------------------
-- Remove Array By Name (ลบข้อมูลสมาชิกออกจาก Array โดยชื่อ) :: remove()
-- การใช้ remove() ต้องดึงด้วยชื่อสมาชิก
cars = ["Ford", "Volvo", "BMW"]
cars.remove("Volvo")
for x in cars:
print(x)
Output :
Ford
BMW
------------------------------------------------------------------
-- การล้างข้อมูลใน Array :: Clear()
fruits = ['apple', 'banana', 'cherry', 'orange']
fruits.clear()
print(fruits)
Output :
[]
------------------------------------------------------------------
-- การ Copy Array :: copy()
fruits = ["apple", "banana", "cherry"]
x = fruits.copy()
print(x)
Output :
['apple', 'banana', 'cherry']
------------------------------------------------------------------
-- การหา Index ของ Array ที่ต้องการค้นหา
fruits = ['apple', 'banana', 'cherry']
x = fruits.index("cherry")
print(x)
Output :
2
------------------------------------------------------------------
-- การเรียงข้อมูลสมาชิก จากหลังไปหน้า :: reverse()
fruits = ['apple', 'banana', 'cherry']
fruits.reverse()
print(fruits)
Output :
['cherry', 'banana', 'apple']
------------------------------------------------------------------
- การจัดเรียงข้อมูลสมาชิกใน Array จากน้อยไปมาก :: sort()
cars = ['Ford', 'BMW', 'Volvo']
cars.sort()
print(cars)
Output :
['BMW', 'Ford', 'Volvo']
------------------------------------------------------------------
-- การจัดเรียงข้อมูลสมาชิกใน Array จากน้อยไปมาก :: sort() + reverse=True
cars = ['Ford', 'BMW', 'Volvo']
cars.sort(reverse=True)
print(cars)
/*
ทำในส่วนของ sort ก่อน จะได้ ['BMW', 'Ford', 'Volvo']
แล้วค่อยมา reverse=True จะได้ ['Volvo', 'Ford', 'BMW']
*/
Output :
['Volvo', 'Ford', 'BMW']
------------------------------------------------------------------
-- การจัดเรียงโดย นับอักษรของสมาชิก จากน้อยไปมาก :: sort() + function
-- เรียงโดยการ นับตัวอักษร จากน้อยสุด ไปหา มากสุด
def myFunc(e):
return len(e)
cars = ['Ford', 'Mitsubishi', 'BMW', 'VW']
cars.sort(key=myFunc)
print(cars)
Output :
['VW', 'BMW', 'Ford', 'Mitsubishi']
------------------------------------------------------------------
-- การจัดเรียงโดยใช้ key Event จากน้อยไปมาก :: sort() + function
-- เรียงโดยการ นับปี(Year) จากน้อย ไปมาก
def myFunc(e):
return e['year']
cars = [
{'car': 'Ford', 'year': 2005},
{'car': 'Mitsubishi', 'year': 2000},
{'car': 'BMW', 'year': 2019},
{'car': 'VW', 'year': 2011}
]
cars.sort(key=myFunc)
print(cars)
Output :
[{'car': 'Mitsubishi', 'year': 2000}, {'car': 'Ford', 'year': 2005}, {'car': 'VW', 'year': 2011}, {'car': 'BMW', 'year': 2019}]
------------------------------------------------------------------
-- การจัดเรียงโดยใช้ e :: sort() + reverse + function
-- เรียงโดยการ นับ String จากน้อย ไปมาก แล้วค่อย reverse
def myFunc(e):
return len(e)
cars = ['Ford', 'Mitsubishi', 'BMW', 'VW']
cars.sort(reverse=True, key=myFunc)
print(cars)
Output :
['Mitsubishi', 'Ford', 'BMW', 'VW']Post by Goborijung at 2019-12-18 09:19:29 | ID: 336
>>> Python For Loop
-- Loop Array
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
Output :
apple
banana
cherry
-------------------------------------------------------------------
-- Loop ผ่าน String
for x in "banana":
print(x)
Output :
b
a
n
a
n
a
-------------------------------------------------------------------
-- คำสั่ง Break Loop (print ก่อน แล้วค่อยเช็ค)
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
Output :
apple
banana
-------------------------------------------------------------------
-- คำสั่ง Break Loop (เช็คก่อน แล้วค่อย print)
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x)
Output :
apple
-------------------------------------------------------------------
-- คำสั่ง Continue in Loop (คำสั่งดำเนินการต่อ)
-- ตัวอย่าง ถ้า x = banana ให้ข้ามไป
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
Output :
apple
cherry
-------------------------------------------------------------------
-- คำสั่ง Range() Loop ช่วงของตัวเลข
-- Loop แบบไม่กำหนดค่าเริ่มต้น
for x in range(6):
print(x)
Output :
0
1
2
3
4
5
-------------------------------------------------------------------
-- คำสั่ง Range() Loop ช่วงของตัวเลข
-- Loop แบบกำหนดค่าเริ่มต้น
for x in range(2, 6):
print(x)
Output :
2
3
4
5
-------------------------------------------------------------------
-- คำสั่ง Range() Loop ช่วงของตัวเลข
-- Loop แบบกำหนดค่าเริ่มต้น และ กำหนด Step = 3
for x in range(2, 30, 3):
print(x)
Output :
2
5
8
11
14
17
20
23
26
29
-------------------------------------------------------------------
-- คำสั่ง Else in For Loop
-- เมื่อ Loop เสร็จแล้วให้ทำอะไร
for x in range(6):
print(x)
else:
print("Finally finished!")
Output :
0
1
2
3
4
5
Finally finished!
-------------------------------------------------------------------
-- Nested Loops (คำสั่ง Loop ซ้อน Loop)
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)
Output :
('red', 'apple')
('red', 'banana')
('red', 'cherry')
('big', 'apple')
('big', 'banana')
('big', 'cherry')
('tasty', 'apple')
('tasty', 'banana')
('tasty', 'cherry')
-------------------------------------------------------------------
-- Pass คำสั่ง Bypass IF
-- เช่น เราเช็ค if เสร็จ แต่ไม่รู้จะให้ทำอะไรต่อ ก็ใส่ pass ไปก่อน
x = 40
if x-10 > 0:
pass
Output :
ว่างๆ ไม่มีอะไรPost by Goborijung at 2019-12-18 14:10:29 | ID: 338
>>> Python Function
-- การสร้าง Function
def my_function():
print("Hello from a function")
my_function()
Output :
Hello from a function
-----------------------------------------------------------------
-- การสร้าง Function ที่มีการส่งค่า Parameter มาด้วย
def my_function(fname):
print("Hello, " + fname)
my_function("Goborijung")
Output :
Hello, Goborijung
----------------------------------------------------------------
-- การสร้าง Function Set Default Parameter
def my_function(country = "Thailand"):
print("I am from " + country)
my_function()
my_function("USA")
Output :
I am from Thailand
I am from USA
-------------------------------------------------------------------
-- การส่งค่า Parameter ที่เป็น Array ใน Function
def my_function(food):
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
Output :
apple
banana
cherry
----------------------------------------------------------------------
-- การใช้ Return ส่งค่ากลับออกมา จาก Function
def my_function(x):
return 5 * x
print(my_function(3))
Output :
15
-------------------------------------------------------------------
-- การส่งค่า Parameter ที่เป็น Key = Value
def my_function(child1, child2, child3):
print("The youngest child is : " + child2)
my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus")
Output :
The youngest child is : Tobias
-------------------------------------------------------------------
-- การใช้ * ก่อนชื่อ Parameter Name สำหรับ Arguments ที่ไม่รู้จัก
def my_function(*kids):
print("The youngest child is : " + kids[1])
my_function("Emil", "Tobias", "Linus")
Output :
The youngest child is : Tobias
--------------------------------------------------------------------
-- การสร้างฟังก์ชั่น ปล่าวๆ ด้วย pass
def myfunction():
pass
// การใช้ pass เพื่อบอกว่า ให้ข้ามฟังก์ชั่นนี้ไปก่อน
--------------------------------------------------------------------
-- Recursion ฟังก์ชั่นสำหรับเรียกซ้ำ ตัวเอง
-- ลักษณะการทำงานเหมือน Loop เริ่มจาก 1 Result = Result + รอบ
-- เช่น
รอบที่ 1, result = 1 ได้ (1)
รอบที่ 2, result = 1+2 ได้ (3)
รอบที่ 3, result = 3+3 ได้ (6)
รอบที่ 4, result = 6+4 ได้ (10) ไปเรื่อย
def tri_recursion(k):
if(k>0):
result = k+tri_recursion(k-1)
print(result)
else:
result = 0
return result
print("
Recursion Example Results")
tri_recursion(6)
Output :
1
3
6
10
15
21
21
-----------------------------------------------------------------Post by Goborijung at 2019-12-27 10:33:12 | ID: 359
ตัวอย่างคำสั่งที่ใช้ใน Python Analysis speed = [10,20,30,40,50,60] >> หาค่าเฉลี่ย import numpy numpy.mean(speed) >> หาค่ามัธยฐาน import numpy numpy.median(speed) >> หาค่า Mode (ค่าที่เกิดซ้ำมากที่สุด) from scipy import stats stats.mode(speed) >> หาค่าเบี่ยงเบนมาตรฐาน import numpy numpy.std(speed) >> หาค่าความแปรปรวน import numpy numpy.var(speed) >> หาค่าเปอเซนไทน์ import numpy numpy.percentile(speed,50) >> หาค่าฮีสโตแกรม import numpy import matplotlib.pyplot as plt x = numpy.random.uniform(0.0,5.0,250) plt.hist(x,5) plt.show()
Post by Goborijung at 2019-10-20 14:27:52 | ID: 158
https://www.w3schools.com/python/default.asp >> คอร์สออนไลน์ โปรแกรมไพทอนสำหรับวิทยาการข้อมูล (Python Programming for Data Science) https://www.classcentral.com/course/thaimooc------python-programming-for-data-science-15110
sudo add-apt-repository ppa:jonathonf/python-3.6 sudo apt-get update sudo apt-get install python3.6 sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1 sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2 sudo update-alternatives --config python3 python3 -V sudo rm /usr/bin/python sudo ln -s python3.5 /usr/bin/python3 python -V ตรวจสอบ ls /usr/bin/python* /usr/bin/python /usr/bin/python2.7 /usr/bin/python3.5 /usr/bin/python3.6 /usr/bin/python3m /usr/bin/python2 /usr/bin/python3 /usr/bin/python3.5m /usr/bin/python3.6m rm /usr/bin/python ln -s /usr/bin/python3.6 /usr/bin/python ln -s /usr/bin/python3.6m /usr/bin/pythonm python Python 3.6.2 (default, Jul 20 2017, 08:43:29) [GCC 5.4.1 20170519] on linux Type “help”, “copyright”, “credits” or “license” for more information. >>>