“Tuples vs Daftar” Kode Jawaban

Daftar vs Tuple

# compare the size
import sys
my_list = [0, 1, 2, "hello", True]
my_tuple = (0, 1, 2, "hello", True)
print(sys.getsizeof(my_list), "bytes")
print(sys.getsizeof(my_tuple), "bytes")

# compare the execution time of a list vs. tuple creation statement
import timeit
print(timeit.timeit(stmt="[0, 1, 2, 3, 4, 5]", number=1000000))
print(timeit.timeit(stmt="(0, 1, 2, 3, 4, 5)", number=1000000))
Tough Tarsier

Tuples vs Daftar

# A tuple might contain data about a person (heterogeneous type mixture)
person_a = (name, age, occupation, address)

# A list might contain a list of people (homogeneous type mixture - all tuples!)
people = [person_a, person_b, person_c]
Drab Dolphin

Jawaban yang mirip dengan “Tuples vs Daftar”

Pertanyaan yang mirip dengan “Tuples vs Daftar”

Lebih banyak jawaban terkait untuk “Tuples vs Daftar” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya