នៅក្នុងភាសា Python មានថ្នាក់មានស្រាប់មួយចំនួនត្រូវបានបង្កើតឡើងរួចជាស្រេចទុកនៅក្នុង built-in scope ។ បណ្តាថ្នាក់ទាំងនោះត្រូវហៅថា built-in class
កន្លងមក យើងបានបង្កើតវត្ថុផ្សេងៗ មានដូចជា ចំនួនគត់ ចំនួនពិត និង វត្ថុដែលជា container មួយចំនួនទៀត។ តាមពិត វត្ថុទាំងអស់នោះជា instance នៃថ្នាក់មានស្រាប់ទាំងនោះ តែការបង្កើត instance ទាំងនោះមានលក្ខណៈខុសប្លែកពីការបង្កើត instance នៃថ្នាក់ធម្មតា។
ដើម្បីអោយដឹងថា តើថ្នាក់របស់វត្ថុណាមួយជាអ្វីនោះ យើងត្រូវធ្វើដូចខាងក្រោមនេះ៖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | sale = 1000 buy = 800.5 on = True sentence = 'Python programming language' mixed_tuple = ( 100 , 1.33 , 'name' , True ) mixed_list = [ 2.5 , 300 , 'last name' , False ] mixed_dict = { 'sale' : 1000 , 'buy' : 900 } mixed_set = { 3.5 , True , 'promotion' } print ( type (sale)) print ( type (buy)) print ( type (on)) print ( type (sentence)) print ( type (mixed_tuple)) print ( type (mixed_list)) print ( type (mixed_dict)) print ( type (mixed_set)) |
1 2 3 4 5 6 7 8 | <class 'int' > <class 'float' > <class 'bool' > <class 'str' > <class 'tuple' > <class 'list' > <class 'dict' > <class 'set' > |
គ្រប់វត្ថុដែលជាកំរង list ទាំងអស់ជា instance នៃថ្នាក់ឈ្មោះ list ។ ដូចនេះ យើងអាចបង្កើតកំរង list ផ្សេងៗ ដោយយកថ្នាក់មានស្រាប់ឈ្មោះ list នេះមកប្រើ ដោយធ្វើដូចខាងក្រោមនេះ៖
1 2 3 4 5 6 7 8 | l1 = list ( "Python" ) l2 = list (( 100 , 1.5 , True )) l3 = list ({ 'A' : 100 , 'B' : 1.5 , 'C' : True }) l4 = list ({ 100 , False , 200 }) print (l1) print (l2) print (l3) print (l4) |
1 2 3 4 | ['P', 'y', 't', 'h', 'o', 'n'] [100, 1.5, True] ['A', 'B', 'C'] [False, 100, 200] |
យ៉ាងណាម៉ិញ នៅក្នុងថ្នាក់ស្រាប់ឈ្មោះ list មាន method មួយចំនួនដែលយើងអាចយកមកប្រើជាមួយនឹងកំរង list ទាំងឡាយ។ method ទាំងនោះមានដូចតទៅនេះ៖
append() ជា method ប្រើសំរាប់បន្ថែមធាតុណាមួយនៅខាងចុងកំរង list ណាមួយ។
1 2 3 | mixed_list = list ([ 100 , True , "profit" , 3.5 ]) mixed_list.append( 'sale' ) print (mixed_list) |
1 | [100, True, 'profit', 3.5, 'sale'] |
insert() គឺជា method ប្រើសំរាប់បញ្ចូលវត្ថុណាមួយចូលទៅក្នុងកំរង list ណាមួយ នៅខាងមុខ element មានលេខរៀងណាមួយ។
1 2 3 | mixed_list = list ([ 100 , True , "profit" , 3.5 ]) mixed_list.insert( 2 , 'sale' ) print (mixed_list) |
1 | [100, True, 'sale', 'profit', 3.5] |
pop() ជា method ប្រើសំរាប់កាត់យក element មានលេខរៀងណាមួយ នៅក្នុងកំរង list ណាមួយមកប្រើការ។
1 2 3 4 | mixed_list = list ([ 100 , True , "profit" , 3.5 ]) ele = mixed_list.pop( 2 ) print (ele) print (mixed_list) |
1 2 | profit [100, True, 3.5] |
remove() ជា method ប្រើសំរាប់លុប element ណាមួយចេញពីកំរង list ណាមួយ។
1 2 3 | mixed_list = list ([ 100 , True , "profit" , 3.5 ]) mixed_list.remove( 'profit' ) print (mixed_list) |
1 | [100, True, 3.5] |
reverse() ជា method ប្រើសំរាប់តំរៀប element នៅក្នុងកំរង list ណាមួយ អោយមានលំដាប់ថ្នាក់បញ្ច្រាសមកវិញ។
1 2 3 | mixed_list = list ([ 100 , True , "profit" , 3.5 ]) mixed_list.reverse() print (mixed_list) |
1 | [3.5, 'profit', True, 100] |
sort() ជា method ប្រើសំរាប់តំរៀប element នៅក្នុងកំរង list ណាមួយ អោយមានលំដាប់ថ្នាក់ពីតូចទៅធំ។
1 2 3 | mixed_list = list ([ 100 , 76 , 23 , 3.5 ]) mixed_list.sort() print (mixed_list) |
1 | [3.5, 23, 76, 100] |