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.
<code>>> round(2.555,2) # round to two decimal places
2.56
</code>1.2 Data type conversion functions
Functions list , tuple , dict , set , and str convert values to the respective types.
<code>>> 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]'
</code>1.3 Numeric conversion functions
Functions float , int , bool , and complex convert values to floating‑point, integer, boolean, and complex numbers.
<code>>> 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)
</code>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 .
<code>>> 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]
</code>1.5 help
The help function displays the documentation of a function or object, e.g., help(range) .
<code>help(range)
</code>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.
<code>def func_name(param1, param2, ...):
statement1
statement2
return result
</code>Example of a quadratic function:
<code>def func1(x):
return x**2 + 2*x + 3
</code>Example of a function computing triangle area:
<code>def area(base, height):
return 1/2 * base * height
</code>Calling the functions:
<code>>> func1(3)
18
>>> area(3,6)
9
</code>Functions can have default arguments, which are used when the caller omits that parameter.
<code>def area2(base, height=2):
return 1/2 * base * height
</code>Calling with and without the default:
<code>>> area2(3,6) # overrides default height
9
>>> area2(3) # uses default height=2
3
</code>Note: default arguments must follow non‑default positional arguments.
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.