built-in function គឺជា function ទាំងឡាយណាដែលត្រូវបានបង្កើតឡើងរួចជាស្រេចទុកនៅក្នុង built-in scope ។ ជាលទ្ធផល យើងអាចយក built-in function ទៅប្រើប្រា់ស់នៅក្នុង scope ណាក៏បានដែរ។ built-in funciton សំខាន់ៗមានដូចខាងក្រោមនេះ៖
abs() គឺជា built-in function ប្រើសំរាប់រកតំលៃដាច់ខាតនៃចំនួនណាមួយ។
1 2 3 | a = - 136 print ( 'The absolute value of a is' , abs (a)) |
1 | The absolute value of a is 136 |
divmod() គឺជា built-in function ប្រើប្រាស់សំរាប់គណនារកផលចែកនិងសំណល់រវាងចំនួនពីរ។
1 2 3 | a = 10.5 b = 3.14 print ( divmod (a,b)) |
1 | (3.0, 1.0799999999999996) |
len() គឺជា built-in function ប្រើសំរាប់រាប់ element ឬ key នៅក្នុងវត្ថុដែលជា container ណាមួយ។
1 2 3 4 5 | mixed_list = [ 100 , 1.5 , 'profit' , True ] mixed_dict = { 'name' : 'Kosal' , 'age' : 35 , 'gender' : 'male' } print ( len (mixed_list)) print ( len (mixed_dict)) |
1 2 | 4 3 |
max() គឺជា built-in function ប្រើសំរាប់រក element, key, ឬវត្ថុដែលធំជាងគេបំផុត។
1 2 3 4 5 6 | mixed_list = [ 100 , 1.5 , 3.14 , 1000 ] mixed_dict = { 'name' : 'Kosal' , 'age' : 35 , 'gender' : 'male' } print ( max (mixed_list)) print ( max (mixed_dict)) print ( max ( 1.05 , 15 )) |
1 2 3 | 1000 name 15 |
round() គឺជា built-in function ប្រើសំរាប់កែចំនួនពិតអោយទៅជាចំនួនគត់។
1 2 | sale = 1000.33 print ( round (sale)) |
1 | 1000 |
sorted() គឺជា built-in function ប្រើសំរាប់ចំឡងយកកំរងឬ key នៅក្នុងកំរង dictionary ណាមួយ មកបង្កើតជាកំរង list ថ្មីមួយទៀតដែលមាន element ត្រូវបានតំរៀបតាមលំដាប់ថ្នាក់ពីតូចទៅធំ។
1 2 3 4 5 | value_list = [ 100 , 0.5 , 1.34 , 200 ] mixed_dict = { 'C' : 100 , 'B' : 'Python' , 'A' : True } print ( sorted (value_list)) print ( sorted (mixed_dict)) |
1 2 | [0.5, 1.34, 100, 200] ['A', 'B', 'C'] |