sondmk header
การเขียนโปรแกรมด้วยภาษา Python เบื้้องต้น

Python : Connect Sql Server

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)

Python : If , If-Else , If-Else-If

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")

Python :: Array

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']

Python :: For Loop

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 : 
ว่างๆ ไม่มีอะไร

Python :: Function

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

-----------------------------------------------------------------

Python :: Numpy Analysis

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()

Python :: เว็บสอนเขียน Python

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

1

How to Install Python 3.6.1 in Ubuntu 16.04 LTS

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.
>>>	
			

Lern Python Programming Onlines

//E-Books
https://www.cp.eng.chula.ac.th/books/wp-content/uploads/sites/5/2018/08/python101_workbook_v1.0.2.pdf

//Websites
http://marcuscode.com/lang/python
http://www.tutorialspoint.com/python/
https://www.datacamp.com/courses


//Youtube
การเขียนโปรแกรมเบื้องต้นด้วยภาษา Python
Python สำหรับแฮกเกอร์


สอนเขียน Python ตั้งแต่เริ่มต้น https://goo.gl/tKNthQ
บทที่ 1 ทำความรู้จักกับภาษา Python
บทที่ 2 เครื่องมือสำหรับเขียน Python วิธีการติดตั้ง Python บน Windows
บทที่ 3 ไวยากรณ์พื้นฐานของ Python รูปแบบการเขียนไพทอน
บทที่ 4 ตัวดำเนินการใน Python (บทนี้อธิบายยาวหน่อยนะ)
บทที่ 5 การกำหนดตัวแปรใน Python
บทที่ 6 ทำความรู้จัก ตัวแปรชนิด List การใช้งานตัวแปลประเภทลิส
บทที่ 7 ทำความรู้จัก ตัวแปรชนิด Tuple ตัวแปลทูเปิล
บทที่ 8 ทำความรู้จักตัวแปรชนิด Dictionary การใช้งานตัวแปลดิชันนารี Hash Table Type
บทที่ 9 ทำความเข้าใจเรื่อง Function การแปลงชนิดข้อมูล ฟังก์ชั่นที่ไพทอนมีให้ใช้
บทที่ 10 ทำความเข้าใจการกำหนดเงือนไขการทำงาน IF Statements
บทที่ 11 ทำความเข้าใจการเขียนลูป For loop Statements
บทที่ 12 ทำความเข้าใจการทำงานซ้ำด้วยลูป While loop statements
บทที่ 13 ทำความเข้าใจการเขียนลูป Foreach loop statements
บทที่ 14 การจัดการข้อผิดพลาดใน Python ดักจับ Error
บทที่ 15 สอนการสร้างฟังก์ชั่น Function Python
บทที่ 16 การสร้างฟังก์ชั่นโดยมี default value ในไพทอน Defining function Default value
บทที่ 17 การสร้างฟังก์ชั่น ในไพทอน Python Defining function โดยมี Keyword Arguments
บทที่ 18 การสร้างฟังก์ชั่น ในไพทอน Python function Variable-length arguments
บทที่ 19 การสร้างฟังก์ชั่นไม่ระบุชื่อ ใน ไพทอน The Anonymous Functions:
บทที่ 20 การสร้างฟังก์ชั่น ส่งค่าออกจากฟังก์ชั่นไพทอน The return Statement:
บทที่ 21 ขอบเขตของตัวแปล ใน การสร้างฟังก์ชั่น Scope of Variables
บทที่ 22 Modules ใน Python สอนการสร้างและใช้งาน โมดูล ของไพทอน
บทที่ 23 Modules ใน Python - การใช้ from...import
บทที่ 24 Namespaces and Scoping ใน Python Local Global
บทที่ 25 ทำความรู้จัก Packages การสร้างและใช้งานแพคเก็ตใน Python
บทที่ 26 (35) การรับของข้อมูลจาก Keyboard ใน Python raw_input input
บทที่ 27(36) ฟังก์ชั่นเกี่ยวกับไฟล์ Python write file การเขียนไฟล์ เปิดไฟล์ รองรับภาษาไทย
บทที่ 28(39) ไพทอนติดต่อฐานข้อมูล Mysql เตรียมตัวเขียน Python Connect Mysql 5 PyMySQL
บทที่ 29(40) ไพทอนสร้างตารางในฐานข้อมูล Mysql Python Create Table
บทที่ 30 (41) ไพทอนการเพิ่มข้อมูลเข้าไปในตาราง Python Insert data table Mysql
บทที่ 31(42) ไพทอนรับข้อมูลจากภายนอกเก็บลงใน ฐานข้อมูล Mysql Dynamic Insert data
บทที่ 32 (43) ไพทอนอ่านข้อมูลจากตารางในฐานข้อมูล Python Read Data From Table
บทที่ 33 (-) การแก้ไขข้อมูลในตาราง MySQL
บทที่ 34 (-) การลบข้อมูลในตาราง MySQL
บทที่ 35 (44) ไพทอนติดต่อฐานข้อมูล PGSQL เขียน Pyhton connect PostgreSQL setup
บทที่ 36 (-) การสร้างตารางฐานข้อมูล PostgreSQL
บทที่ 37 (-) การเพิ่มข้อมูลลงตาราง PostgreSQL
บทที่ 38 (-) รับข้อมูลจากภายนอกเก็บลงตาราง ฐานข้อมูล PostgreSQL
บทที่ 39 (-) แสดงผลจากตารางในฐานข้อมูล PostgreSQL SELECT Table
บทที่ 40 (-) การแก้ไขข้อมูลในตาราง PostgreSQL ฐานข้อมูล เขียนโปรแกรมติดต่อฐานข้อมูล
บทที่ 41 (-) การลบข้อมูลในตาราง PostgreSQL ฐานข้อมูล
บทที่ 42(-) ภาษา Python แบบ OOP - Object-oriented
บทที่ 43(47) คุณสมบัติของการสร้าง Class Python OOP
บทที่ 44(48) การจัดการกับ Object ที่ใช้ไม่ได้ Destroying Python OOP
บทที่ 45 (50) การสืบทอด Class ( Class Inheritance )
บทที่ 46 (51) Overriding Method Python OOP
บทที่ 47(54) Regular Expressions การเปรียบเทียบข้อความ String ใน Python
บทที่ 48 Search Function การค้นหาข้อความด้วย Python
บทที่ 49 ค้นหาและแทนที่ จบแล้ว
บทที่ 50 python oop super Function ฟังก์ชั่นนี้ ใช้มากใน OpenERP / Odoo ระบบ ERP
บทที่ 51 สอนการใช้ฟังก์ชัน Built in List ในภาษา python
บทที่ 52 สอนการใช้ฟังก์ชัน Built in Dictionary ในภาษา python
บทที่ 53 สอนการใช้ฟังก์ชัน Built in Tuple ในภาษา python
บทที่ 54 วิธีติดตั้ง การติดตั้ง Selenium ใน Python บน windows Webdriver

Python สำหรับแฮกเกอร์ https://goo.gl/YKDmL8
Python สำหรับแฮกเกอร์ PART 0 - แนะนำ
Python สำหรับแฮกเกอร์ PART 1 - การติดตั้งและตั้งค่าโปรแกรม
Python สำหรับแฮกเกอร์ PART 2 - สร้างตัวแปรและการทำงาน
Python สำหรับแฮกเกอร์ PART 3 - ฟังก์ชั่นและการป้อนค่าตัวแปร
Python สำหรับแฮกเกอร์ PART 3.1 - อธิบาย Script ของ ฟังก์ชั่นและการป้อนค่า
Python สำหรับแฮกเกอร์ PART 4 - การวนซ้ำ (Loop)
Python สำหรับแฮกเกอร์ PART 5 - การตรวจสอบเงื่อนไข (if else) + ระบบ login ง่ายๆ
Python สำหรับแฮกเกอร์ PART 6 - การอ่านไฟล์และเขียนไฟล์
Python สำหรับแฮกเกอร์ PART 7 - การตรวจสอบข้อผิดพลาด
Python สำหรับแฮกเกอร์ PART 8 - PythonList คลังเก็บข้อมูลคืออะไรไปดู !
Python สำหรับแฮกเกอร์ PART 9 - Dictionary
Python สำหรับแฮกเกอร์ PART 10 - Network Program | Socket
Python สำหรับแฮกเกอร์ PART 11 - Network Program | Port Scanner
Python สำหรับแฮกเกอร์ PART 12 - Network Program | หา IP เว็บไซต์

Framework

Library


เครื่องมือพัฒนาเว็บ



การออกแบบและพัฒนาเว็บไซต์


Download SourceCode



copyAllright © 2016 soundmk.com