ThepExcel Logo
  • บทความ
    • Excel
      • Excel ทั่วไป
      • Excel Pivot Table
      • Excel Power Pivot
      • Power Query
      • Excel Array Formula
      • Excel VBA
      • Excel for Business
      • Excel and Maths
      • ฟังก์ชัน Excel ทั้งหมด
    • Power BI
      • Power Query
      • Data Model
      • DAX Formula
      • Power BI Report
    • Coding
      • Excel VBA
      • Python
      • Power Query M Code
    • AI
      • ChatGPT
      • Stable Diffusion
      • MidJourney
    • Highlights : บทความแนะนำ
    • คลิปวีดีโอ
  • อบรม
    • อบรมลูกค้าองค์กร
    • คอร์สออนไลน์ SkillLane
    • แนะนำวิทยากร
    • Excel/Power BI Skill Map
    • Quiz
  • Shop
    • คอร์สออนไลน์
    • สินค้าทั้งหมด
    • หนังสือเล่ม
    • E-Book
    • Cart
  • Download
    • Download ไฟล์จากเทพเอ็กเซล
    • ThepExcel-Mfx : M Code สำเร็จรูป
    • Date Table สำเร็จรูป
    • กราฟ My Skill
    • github.com/ThepExcel
  • รวม Link
    • รวม Link สอน Excel & Power BI ทั้งไทยและเทศ
    • รวม Link สอน Python / Programming
    • หนังสือแนะนำ
    • Facebook ThepExcel
    • YouTube ThepExcel
    • DAX Formatter
  • Contact
    • แนะนำ เทพเอ็กเซล (Thep Excel)
    • แนะนำวิทยากร : อาจารย์ ศิระ เอกบุตร (ระ)
    • นโยบายการคุ้มครองข้อมูลส่วนบุคคล (Privacy Policy)
  • Facebook
  • YouTube

หัดเขียนโปรแกรม Python สำหรับคนเป็น Excel มาก่อน : ตอนที่ 1

python for excel user

Categories 📂

Highlights : บทความแนะนำ, Python

Tags 🏷️

basic, comment, python, variable

ถ้าให้เลือกการเรียนรู้เขียนโปรแกรมคอมพิวเตอร์ใหม่ซักอันนึง ผมคิดว่าภาษา Python เป็นอีกภาษาที่ได้เรียนรู้แล้วคุ้มค้า เรียนรู้ง่าย เป็นที่นิยม มีคนใช้งานเยอะ จึงมี Library ให้ใช้เยอะแยะไปหมด อีกอย่างมันเป็นภาษาที่ทำได้ทุกอย่าง จะทำ Web ทำ App เขียนเกม ทำ AI/Machine Learning หรือจะใช้ทำงานร่วมกับไฟล์ Excel ก็ย่อมได้

แต่บอกไว้ก่อนว่าแม้ผมจะมีความเชี่ยวชาญการใช้ Excel, Power Query, Power BI พอสมควร แต่ผมไม่ใช่ผู้เชี่ยวชาญภาษา Python หรือการเขียนโปรแกรมภาษาไหนๆ เลย

สิ่งที่ผมเชี่ยวชาญมันเป็น Concept ของ Functional Language ทั้งนั้นเลย ทั้งฟังก์ชัน Excel, M, DAX ส่วน VBA ใน Excel ที่เป็นการเขียนโปรแกรมผมก็ทำได้ในระดับนึง ไม่ได้เชี่ยวชาญอะไรมากนัก ก็เลยคิดว่า ถ้าจะหัดเขียนโปรแกรมแล้ว หัด Python ไปเลยดีกว่า ใช้ได้ยาวๆ ใช้ได้หลายเรื่อง 555

ดังนั้นบทความที่ผมกำลังเขียนนี้ มันเหมือนกับการชวนเพื่อนๆ ที่ใช้ Excel เป็นแล้ว มาหัดใช้ Python ไปด้วยกัน เหมือนเพื่อนสอนเพื่อน ซึ่งข้อดีคือ ผมจะรู้ว่าคนใช้ Excel เข้าใจอะไร มีมุมมองแบบไหน ซึ่งน่าจะช่วยให้คนที่ใช้ Excel เหมือนๆ ผม สามารถเข้าใจการเขียน Python ได้ง่ายมากขึ้น เพราะผมตั้งใจจะพูดด้วย “ภาษาของพวกเดียวกัน” นั่นเอง

ผลพลอยได้จากการศึกษา Python นอกจากจะทำให้คุณ Logic ดีขึ้น และมีเครื่องมือทุ่นแรงมากขึ้นแล้ว มันยังจะทำให้คุณเข้าใจ Excel มากขึ้นได้ด้วยอย่างน่าอัศจรรย์ ไม่เชื่อก็ลองอ่านดูได้เลยครับ ^^

เพื่อไม่ให้เสียเวลา เรามาเริ่มกันเลย!

สารบัญ

  • เรียกใช้งาน Python
    • ถ้าจะโหลดมาลงในเครื่อง
    • ถ้าจะใช้ Python แบบ Online
  • ลองใช้งาน Python
    • แสดงผลออกมาด้วย print และการอ้างอิงข้อความ
    • การใส่ค่าให้ตัวแปรและอ้างอิงตัวแปร
    • การใส่ Comment
    • ตัวอย่างการใช้ตัวแปร และ comment
  • จบตอนแรกแบบง่ายๆ
  • สารบัญ Series Python

เรียกใช้งาน Python

การจะใช้งาน Python นั้น ทำได้หลายวิธีมากๆ มีทั้งแบบโหลดไฟล์มา Install ในเครื่อง และแบบใช้ Online ใน Website ได้เลย แต่ไม่ว่าจะแบบไหนมันก็ฟรีทั้งคู่! นี่คือข้อดีของ Python อย่างแรกเลย แถมใช้ได้หลายระบบปฏิบัติการด้วย (แต่ในบทความนี้ผมแนะนำให้ใช้ Google Colab ทำแบบ Online เลย ง่ายดี)

ถ้าจะโหลดมาลงในเครื่อง

  • ก็ต้องโหลดอันนี้ก่อน https://www.python.org/downloads/ (แนะนำว่าให้โหลด version ล่าสุดไปเลย)
  • จากนั้นผมแนะนำให้โหลดโปรแกรมพวก Code Editor ที่เอาไว้ช่วยเขียน Code ซึ่งมีหลายอันมากๆ แต่ส่วนตัวผมใช้ https://code.visualstudio.com/ ซึ่งก็มี extension ที่เป็น Python ให้ใช้ (รวมถึงภาษาอื่นด้วย แม้แต่ M Code ของ Power Query ก็มี)

ถ้าจะใช้ Python แบบ Online

  • ใช้ผ่านเว็บ https://www.python.org/shell/
  • ใช้ผ่าน Google Colab : https://colab.research.google.com/ (ผมแนะนำอันนี้ เพราะใช้งานง่ายดี ไม่ต้องลงโปรแกรม แค่กด new notebook ก็เข้าไปใช้งานได้แล้ว)

ลองใช้งาน Python

แสดงผลออกมาด้วย print และการอ้างอิงข้อความ

  • ถ้าเราใส่คำสั่ง (โดยกด + Code) ว่า print(123) แล้วสั่ง Run (กดปุ่ม Play) มันก็จะได้ผลลัพธ์ออกมาเป็น 123
  • คำสั่ง print ใช้ตัวพิมพ์เล็กนะ…
  • แต่ถ้าเรา print(แมว) มันจะ run ไม่ได้ ซึ่งต้องใส่ print(“แมว”) ถึงจะได้
  • สาเหตุเป็นเพราะมันคิดว่า แมว คือชื่อของตัวแปร ถ้าเราจะอ้างอิงถึงแมวที่เป็นข้อความ ต้องใส่ในเครื่องหมายคำพูด เช่น “แมว” (double quote) หรือ ‘แมว’ (single quote)
print(123)
print(แมว)
print("แมว")
print('แมว')

ซึ่ง Concept นี้เหมือนกับการเขียนสูตรใน Excel เป๊ะเลย หากพิมพ์ในสูตร หลังเครื่องหมายเท่ากับ เราพิมพ์เลขได้ แต่พิมพ์ข้อความตรงๆ ไม่ได้ เพราะ Excel จะคิดว่าเป็นชื่อ เราต้องพิมพ์ข้อความในเครื่องหมายคำพูดต่างหาก (ต่างกันนิดหน่อยตรงที่ใน Excel ใช้ Single Quote ไม่ได้)

การใส่ค่าให้ตัวแปรและอ้างอิงตัวแปร

ใน Python เราสามารถใส่ค่าลงในที่บรรจุข้อมูลที่เรียกว่า “ตัวแปร” ได้ โดยที่ไม่ต้องประกาศหรือสร้างตัวแปรขึ้นมาก่อนเหมือนโปรแกรมอื่นๆ พอไม่ต้องประกาศก็ทำให้สามารถใส่ข้อมูลประเภทอะไรลงไปก็ได้ และเปลี่ยนได้เรื่อยๆ ตามต้องการ

ตัวแปร = ค่า สูตร หรือ คำสั่งที่ต้องการ

โดยที่มันจะ เอาค่าด้านขวาใส่ลงไปในตัวแปรด้านซ้าย ซึ่งอันนี้คล้ายกับการเขียนสูตรใน Excel เลย คือ สิ่งที่อยู่หลังเครื่องหมายเท่ากับ จะถูกคำนวณกลายเป็น Value ของ Cell ที่เราเขียนสูตรนั่นเอง

ข้อดีของตัวแปรคือ สามารถเรียกตัวแปรนั้นมาใช้งานต่อในบริบทอื่นได้อีก เช่นเดียวกับการอ้างอิง Cell / Name ในสูตรของ Excel เลย เช่น ถ้าใน Excel เราสามารถอ้างอิงค่าใน A1 มาใช้ต่อใน A2 ได้

เหมือนกับว่าเราใส่ค่า 10 ลงไปในตัวแปรชื่อว่า A1 (ซึ่งเอาจริงๆ แล้ว Cell Reference ก็คล้ายๆ เป็นชื่อตัวแปรแบบนึง ราถึงไม่สามารถตั้ง Defined Name ซ้ำกับ Cell Reference ได้ไง) ซึ่งเราสามารถเขียนเอาค่า A1*2 ใส่ลงไปในตัวแปร A2 แล้วแสดงค่ามันออกมาได้

ถ้าเป็นใน Python จะเขียนดังนี้ เห็นมะว่ามันทำได้เหมือนกันเลย!

  • ใส่ค่า 10 ลงไปในตัวแปรชื่อว่า A1
  • เอาค่า A1*2 ใส่ลงไปในตัวแปร A2 (ซึ่งจะได้ 10*2 = 20)
  • print ค่า A2 ออกมา เลยได้ 20 นั่นเอง
A1=10
A2=A1*2
print(A2)

การใส่ Comment

เราสามารถใส่ Comment ใน Code โดยใส่เครื่องหมาย # นำหน้าเพื่อที่จะเขียน note อะไรบางอย่างเกี่ยวกับ code ของเราไว้ได้ (คล้ายกับการคลิ๊กขวา Insert Comment/Note ใน Excel)

อะไรก็ตามที่อยู่หลังเครื่องหมาย # จะถูก Comment ไว้ทั้งหมด

# อันนี้คือ Comment (นำด้วย #) ซึ่งมันจะไม่สนใจ Code หลังเครื่องหมายนี้เลย
# จะใส่ comment ด้านบนของ code หรือ ด้านขวาก็ได้

test=123   #เอาค่า 123 ใส่ในตัวแปรชื่อ test
#test=456
print(test)

ในรูปผม print ค่าใน test ออกมาได้ 123 เพราะ codeบรรทัด test=456 ถูก comment ไว้ จึงไม่ถูก run นั่นเอง

ตัวอย่างการใช้ตัวแปร และ comment

เอาจริงๆ คือ ถ้าผมเขียนการอธิบายความหมายของ Code ลงไปใน Comment ของ Code นั้นๆ เลย ซึ่งนิยม Comment จะไว้ด้านบน หรือด้านขวาของ code ในบรรทัดนั้นๆ เลยก็ย่อมได้ ผลลัพธ์ก็น่าจะทำให้สามารถอ่าน code ง่ายขึ้นอีกเนอะ (แต่เราควรจะ Comment ในประเด็นสำคัญๆ ก็พอเนอะ ไม่งั้นลายตา)

firstName="Sira" #ใส่ข้อความว่า Sira ลงไปในตัวแปร firstName
#แสดงค่าในตัวแปร firstName ออกมา ซึ่งก็คือ Sira
print(firstName) 

tempNum=10 #ใส่ตัวเลข 10 ลงไปในตัวแปร tempNum
tempNum=5 #ใส่ตัวเลข 5 ทับลงไปในตัวแปร tempNum เหมือนเดิม
print(tempNum)

tempNum2=10 #ใส่ตัวเลข 10 ลงไปในตัวแปร tempNum2 
#เอาค่า tempNum2 ที่เก็บค่า 10 ไว้ ไปคูณ 2 ก็จะได้ 20 แล้วเอาใส่ลงไปในตัวแปรเดิม
tempNum2=tempNum2*2 
print(tempNum2)

นี่แหละคือประโยชน์ของการใส่ comment ครับ

จบตอนแรกแบบง่ายๆ

เดี๋ยวตอนต่อไปเราจะมาเรียนรู้เรื่องของประเภทข้อมูลใน Python ซึ่งจะมีหลากหลายกว่าใน Excel เพราะมันมีประเภทข้อมูลที่ไม่ได้มีค่าเดียว แต่เก็บข้อมูลเป็นชุดเลยด้วยครับ ซึ่งเป็น Concept ที่ Excel ปกติไม่มี (แต่มีใน Power Query นะ)

จะเป็นไง รออ่านต่อตอนต่อไปครับ!

สารบัญ Series Python

  • หัดเขียนโปรแกรม Python สำหรับคนเป็น Excel มาก่อน : ตอนที่ 1
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 2 – ประเภทข้อมูล (Data Types)
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 3 – การวน Loop และ เงื่อนไข if
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 4 – การทำงานกับ String และ List
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 5 – การสร้างฟังก์ชันขึ้นมาใช้เอง (Function)
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 6 – การเรียกใช้ Module / Packages เจ๋งๆ ที่มีคนสร้างไว้แล้ว
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 7 – Web Scraping ด้วย Beautiful Soup
  • แนวทางการใช้ Python ใน Power BI
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 8 – การสร้างกราฟด้วย Matplotlib
  • รวม Link สอน Python / Programming / AI/ Machine Learning แบบฟรีๆ

แชร์ความรู้ให้เพื่อนๆ ของคุณ
1.8K    
1.8K    

ติดตามเทพเอ็กเซล

  • Facebook
  • YouTube

อบรมกับเทพเอ็กเซล

🔥 คอร์สใหม่ล่าสุด 🔥

การทำ Optimization ด้วย Excel Solver
สำหรับงานวางแผน
คอร์สออนไลน์ เทพเอ็กเซล
คอร์สออนไลน์ จากเทพเอ็กเซล ดูกี่รอบก็ได้
อบรม Excel / Power BI ให้องค์กรของคุณ

บทความล่าสุด

  • วิธีสั่ง Prompt และตั้งค่าใน Stable Diffusion ให้รูปสวยโดนใจ [Part3]
  • วิธีเรียกใช้งาน Model เจ๋งๆ ใน Stable Diffusion [ตอนที่2]
  • วิธีใช้งาน AI สร้างรูปสุดเจ๋งและฟรีด้วย Stable Diffusion ฉบับมือใหม่ [ตอนที่1]
  • 10 ไอเดีย เรียนรู้ Excel ผ่าน ChatGPT AI สุดเจ๋ง
  • การทำ Simulation ด้วย Excel
  • แกะเคล็ดวิชา Excel Wizard ในการแข่ง Speed Run Excel ระดับโลก
  • เจาะลึก CALCULATE ใน DAX แบบลึกสุดใจ : Part 2

บทความแนะนำ

🔥ฟังก์ชันทั้งหมดใน Excel 🔥

  • แกะเคล็ดวิชา Excel Wizard ในการแข่ง Speed Run Excel ระดับโลก
  • เจาะลึก CALCULATE ใน DAX แบบลึกสุดใจ : Part 1
  • Series สอนดึงข้อมูลจากเว็บ ด้วย Power Automate Desktop
  • สรุปการใช้ LAMBDA ฟังก์ชันที่ใช้สร้างฟังก์ชันใน Excel 365 และผองเพื่อน
  • วิธีใช้ Excel คำนวณระยะเวลาการทำงานรวม แถมระบุเวลาพักได้แบบยืดหยุ่น
  • วิธีจัดการข้อมูลแย่ๆ ด้วย Power Query ทั้งข้อมูลปนกัน ข้อมูลอยู่บนหัวตาราง
  • แยกข้อมูลที่อยู่สุดเน่า ด้วย Excel Power Query

Categories

Tags

ai collection concepts copy database Data Model data table data validation date dax dropdown error excel filter finance find format formula function game graph IF index intro irr len link logic lookup match m code merge mid overview paste pivot power query row sort speed split substitute table text textjoin time tips trim vba vlookup

Archives

  • March 2023 (2)
  • February 2023 (2)
  • January 2023 (1)
  • October 2022 (1)
  • September 2022 (3)
  • August 2022 (3)
  • July 2022 (1)
  • June 2022 (3)
  • May 2022 (1)
  • April 2022 (2)
  • February 2022 (1)
  • December 2021 (2)
  • November 2021 (10)
  • September 2021 (2)
  • August 2021 (6)
  • July 2021 (2)
  • June 2021 (2)
  • May 2021 (10)
  • April 2021 (3)
  • March 2021 (3)
  • February 2021 (4)
  • January 2021 (8)
  • December 2020 (5)
  • November 2020 (13)
  • October 2020 (5)
  • September 2020 (11)
  • August 2020 (4)
  • July 2020 (13)
  • June 2020 (17)
  • May 2020 (16)
  • April 2020 (16)
  • March 2020 (10)
  • February 2020 (15)
  • January 2020 (16)
  • December 2019 (4)
  • November 2019 (3)
  • October 2019 (9)
  • September 2019 (1)
  • August 2019 (7)
  • June 2019 (3)
  • May 2019 (9)
  • April 2019 (9)
  • March 2019 (2)
  • February 2018 (1)
  • January 2018 (3)
  • November 2017 (3)
  • August 2017 (1)
  • July 2017 (1)
  • June 2017 (1)
  • May 2017 (6)
  • April 2017 (6)
  • March 2017 (7)
  • February 2017 (1)
  • January 2017 (2)
  • December 2016 (1)
  • October 2016 (2)
  • September 2016 (3)
  • August 2016 (2)
  • July 2016 (2)
  • June 2016 (1)
  • May 2016 (1)
  • April 2016 (1)
  • March 2016 (2)
  • February 2016 (1)
  • January 2016 (2)
  • December 2015 (2)
  • November 2015 (5)
  • October 2015 (3)
  • June 2015 (2)
  • May 2015 (1)
  • April 2015 (26)
  • January 2015 (1)
  • December 2014 (1)
  • November 2014 (2)
  • October 2014 (1)
  • September 2014 (2)
  • August 2014 (1)
  • June 2014 (1)
  • May 2014 (1)
  • April 2014 (3)
  • March 2014 (3)
  • February 2014 (12)
  • January 2014 (7)
  • December 2013 (2)
  • November 2013 (8)
  • October 2013 (2)

เทพเอ็กเซล : Thep Excel

copyright © 2022

  • Facebook
  • YouTube
เว็บไซต์นี้ใช้คุกกี้ (Cookies)
บริษัท เทพเอ็กเซล จำกัด ให้ความสำคัญต่อข้อมูลส่วนบุคคลของท่าน เพื่อการพัฒนาและปรับปรุงเว็บไซต์รวมถึงสินค้าและบริการต่างๆ หากท่านใช้บริการเว็บไซต์นี้ โดยไม่มีการปรับตั้งค่าใดๆ แสดงว่าท่านยินยอมที่จะรับคุกกี้บนเว็บไซต์ และนโยบายสิทธิส่วนบุคคลของเรา
ตั้งค่าคุกกี้ยอมรับทั้งหมดอ่านเพิ่มเติม
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT