ทำความรู้จักกับภาษา python (21) : ขอบเขตของตัวแปรฟังก์ชัน

แชร์ความรู้ภาษา Python ไพทอน การเขียนโปรแกรมภาษาไพทอน

Moderator: mindphp, ผู้ดูแลกระดาน

anuwat somsakul
PHP Full Member
PHP Full Member
โพสต์: 44
ลงทะเบียนเมื่อ: 08/08/2018 9:49 am

Re: ทำความรู้จักกับภาษา python (21) : ขอบเขตของตัวแปรฟังก์ชัน

โพสต์ที่ยังไม่ได้อ่าน โดย anuwat somsakul »

ขอบเขตของตัวแปล ใน การสร้างฟังก์ชั่น Scope of Variables

โค้ด: เลือกทั้งหมด

total = 0
def mysum(arg1,arg2,arg3):
    total = arg1 + arg2 + arg3 #ตัวแปร local
    print("แสดงค่าของตัวแปรที่เป็นlocal",total)
    return  total

#ชื่อตัวแปรglobalซ้ำ
total = 30
mysum(10,20,30)
print("แสดงค่าของตัวแปรที่เป็นglobal",total)
ผลลัพธ์
Selection_013.png
Selection_013.png (12.58 KiB) Viewed 1207 times
ศึกษาจาก : https://www.youtube.com/watch?v=9KP4C9g ... lzdKrpxsMM
ธวัชชัย แสนหาญ
PHP Super Member
PHP Super Member
โพสต์: 499
ลงทะเบียนเมื่อ: 15/11/2018 10:02 am

Re: ทำความรู้จักกับภาษา python (21) : ขอบเขตของตัวแปรฟังก์ชัน

โพสต์ที่ยังไม่ได้อ่าน โดย ธวัชชัย แสนหาญ »

การใช้ตัวแปรชนิด tuple ใน python
Tuple เป็นตัวแปรชนิดหนึ่งที่สามารถเก็บค่าได้หลายค่าใน1 ตัวแปร
เหมือนกับ list แต่ไม่สามารถ เพิ่ม และ ลบค่าได้
แต่ละค่าหรือสามชิกแต่ละตัวจะต้องอยู่ใน (...)

รูปที่ run ในโปรแกรม
51.png
51.png (29.49 KiB) Viewed 1157 times
โค้ดที่จะนำมาrun

โค้ด: เลือกทั้งหมด

Tuple1 =('in','out',1,2)
Tuple2 =(10,20,30,40)

print (Tuple1)        #แสดงคาใน Tuple1 ทั้งหมด
print (Tuple1[1])     #แสดงคาใน Tuple1 ใน index ที่1
print (Tuple2)        #แสดงคาใน Tuple2 ทั้งหมด
print (Tuple2[-1])    #แสดงคาใน Tuple2 -1คือ นับจากข้างหลังลงมา 1 ค่า
ผลลัพธ์ที่ได้
51.png
51.png (29.49 KiB) Viewed 1157 times
แนบไฟล์
52.png
52.png (12.81 KiB) Viewed 1157 times
ภาพประจำตัวสมาชิก
chatee supasand
PHP VIP Members
PHP VIP Members
โพสต์: 1666
ลงทะเบียนเมื่อ: 04/06/2019 10:06 am

Re: ทำความรู้จักกับภาษา python (21) : ขอบเขตของตัวแปรฟังก์ชัน

โพสต์ที่ยังไม่ได้อ่าน โดย chatee supasand »

สอนดีเวรี่กู้ดมากครับ
รูปภาพ
ภาพประจำตัวสมาชิก
jirawoot
PHP VIP Members
PHP VIP Members
โพสต์: 3129
ลงทะเบียนเมื่อ: 17/06/2019 10:30 am

Re: ทำความรู้จักกับภาษา python (21) : ขอบเขตของตัวแปรฟังก์ชัน

โพสต์ที่ยังไม่ได้อ่าน โดย jirawoot »

โค้ด: เลือกทั้งหมด

total = 0
def mysum(arg1, arg2, arg3):
    total = arg1+arg2+arg3
    print("ตัวแปรที่เป็น local=", total)
    return total

mysum(10,20,30)
print("ตัวแปรที่เป็น global=", total)
ผลลัพธ์
Selection_025.png
Selection_025.png (7.91 KiB) Viewed 351 times

โค้ด: เลือกทั้งหมด

total = 0
def mysum(arg1, arg2, arg3):
    total = arg1+arg2+arg3
    print("ตัวแปรที่เป็น local=", total)
    return total

total=30
mysum(10,20,30)
print("ตัวแปรที่เป็น global=", total)
ผลลัพธ์
Selection_026.png
Selection_026.png (7.17 KiB) Viewed 351 times

โค้ด: เลือกทั้งหมด

def myfunc(a,b):
    a=10
    print('ตัวแปร a =',a)
    print('ตัวแปร b =', b)

a=20
b=30
print('global a=',a)
print('global b=',b)
print('----------------')
myfunc(a,b)
ผลลัพธ์
Selection_027.png
Selection_027.png (7.8 KiB) Viewed 351 times

โค้ด: เลือกทั้งหมด

def myfunc2(c, d):
    print('เรียกใช้ฟังก์ชั้นที่ 2')
    return (c+d)

a=20
b=30
print('global a =',a)
print('globla b =',b)
print('----------------')
total= myfunc2(a, b)
print('local a+b=', total)
ผลลัพธ์
Selection_028.png
Selection_028.png (10.18 KiB) Viewed 351 times

โค้ด: เลือกทั้งหมด

def myfunc3():
    a = 0
    b = 1
    print('เรียกฟังก์ชั่นที่ 3')
    print("local a=",a)
    print('local b=', b)

a= 20
b=30
print('global a=',a)
print('globle b=',b)
print('---------------')
myfunc3()
ผลลัพธ์
Selection_029.png
Selection_029.png (8.71 KiB) Viewed 351 times
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 45