ทำความรู้จักกับภาษา python (18) : การสร้างฟังก์ชันโดยมี Variable-length (วาริเอเบิล-เล็งธ)

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

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

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

Re: ทำความรู้จักกับภาษา python (18) : การสร้างฟังก์ชันโดยมี Variable-length (วาริเอเบิล-เล็งธ)

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

การสร้างฟังก์ชั่น ในไพทอน Python function Variable-length arguments

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

def myfunc_var(arg1, *vartuple):

    print("Argument is",arg1)
    for v in vartuple:
        print("vartuple is",v)

myfunc_var('Mindphp.com');
print("-------------------")
myfunc_var('Mindphp.com','Traning','Python');
ผลลัพธ์
Selection_010.png
Selection_010.png (8.58 KiB) Viewed 741 times
ภาพประจำตัวสมาชิก
jirawoot
PHP VIP Members
PHP VIP Members
โพสต์: 3129
ลงทะเบียนเมื่อ: 17/06/2019 10:30 am

Re: ทำความรู้จักกับภาษา python (18) : การสร้างฟังก์ชันโดยมี Variable-length (วาริเอเบิล-เล็งธ)

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

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

def myfun_var(arg1,*vertuple):
    "this function is show you how variable"
    print("argument is", arg1)
    for a in vertuple:
        print("vartuple is:", a)

myfun_var("mindphp.com")
print('----------------------')
myfun_var('mindphp.com', 'training', 'python')
ผลลัพธ์
Selection_019.png
Selection_019.png (9.71 KiB) Viewed 439 times

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

def summary(*a):
    total=0
    for n in range(len(a)):
        total += a[n]
        print('argument =',n ,":",a[n])
    print(total)

summary(1,2,3,4,5,6,7)
summary(2.02,7.36)
ผลลัพธ์
Selection_020.png
Selection_020.png (11.32 KiB) Viewed 439 times
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

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

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