Master Python Built-in Functions and Custom Function Basics
This article introduces Python's built-in functions such as type, len, sum, round, and sorted, explains data type conversion utilities, demonstrates numeric conversions, and provides a step‑by‑step guide to defining and using custom functions with default arguments.
1 Built-in Python Functions
Python includes several built‑in functions such as type, len, and sum that display data types, count elements, and compute sums. The article also introduces other commonly used functions.
1.1 round
The round function rounds a floating‑point number to a given number of decimal places.
>> round(2.555,2) # round to two decimal places
2.561.2 Data type conversion functions
Functions list, tuple, dict, set, and str convert values to the respective types.
>> list('abcd')
['a', 'b', 'c', 'd']
>>> tuple([1,2,3,4])
(1, 2, 3, 4)
>>> dict([(1,2),(3,4)])
{1: 2, 3: 4}
>>> set('abcd')
{'a', 'b', 'c', 'd'}
>>> str([1,2,3])
'[1, 2, 3]'1.3 Numeric conversion functions
Functions float, int, bool, and complex convert values to floating‑point, integer, boolean, and complex numbers.
>> float(2)
2.0
>>> int(3.56) # truncate decimal part
3
>>> bool(2) # any non‑zero is True
True
>>> bool(0)
False
>>> float('200')
200.0
>>> complex(3)
(3+0j)1.4 sorted
The sorted function returns a new sorted list. It can sort in ascending order, descending order with reverse=True, or using a custom key such as abs.
>> sorted([1,4,3,2])
[1, 2, 3, 4]
>>> sorted([1,4,3,2], reverse=True)
[4, 3, 2, 1]
>>> sorted([2,-1,4,-3], key=abs)
[-1, 2, -3, 4]1.5 help
The help function displays the documentation of a function or object, e.g., help(range).
help(range)2 Custom Functions
Defining your own functions allows modular code reuse. The syntax is def function_name(parameters): followed by indented statements and an optional return value.
def func_name(param1, param2, ...):
statement1
statement2
return resultExample of a quadratic function:
def func1(x):
return x**2 + 2*x + 3Example of a function computing triangle area:
def area(base, height):
return 1/2 * base * heightCalling the functions:
>> func1(3)
18
>>> area(3,6)
9Functions can have default arguments, which are used when the caller omits that parameter.
def area2(base, height=2):
return 1/2 * base * heightCalling with and without the default:
>> area2(3,6) # overrides default height
9
>>> area2(3) # uses default height=2
3Note: default arguments must follow non‑default positional arguments.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Model Perspective
Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
