Latest web development tutorials

Python Tuple (tuple) tuple () metode

deskripsi

Python tupel tupel () fungsi mengkonversi daftar tupel.

tatabahasa

tuple () sintaks metode:

tuple( seq )

parameter

  • seq - untuk dikonversi menjadi urutan tuple.

Kembali Nilai

Mengembalikan elemen tupel minimum.

contoh

Contoh berikut menunjukkan tuple () berfungsi untuk menggunakan:

Contoh 1:

>>> tuple([1,2,3,4])

(1, 2, 3, 4)

>>> tuple({1:2,3:4})    #针对字典 会返回字典的key组成的tuple

(1, 3)

>>> tuple((1,2,3,4))    #元组会返回元组自身

(1, 2, 3, 4)

Contoh 2:

#!/usr/bin/python

aList = [123, 'xyz', 'zara', 'abc'];
aTuple = tuple(aList)

print "Tuple elements : ", aTuple

Contoh di atas output:

Tuple elements :  (123, 'xyz', 'zara', 'abc')