2.2. Built-in functions#
2.2.1. Absolute Value Function#
abs()
takes the absolute value of a number.
abs(-9)
: -9
is the argument. Arguments appear between the parenthesis after the function name. Arguments are evaluated left to right.
day_temperature = 3
night_temperature = 10
abs(day_temperature - night_temperature)
7
Because function calls produce values, they can be used in expressions:
abs(-7) + abs(3.3)
10.3
2.2.2. Functions to Convert Data Type#
These functions convert the data type from one type to another if possible. This is called type casting.
2.2.2.1. int#
Note, for floating point numbers, int()
truncates towards zero.
int(34.6)
34
int('34')
34
This will fail!
int('34.6')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[5], line 1
----> 1 int('34.6')
ValueError: invalid literal for int() with base 10: '34.6'
int(-4.3)
-4
2.2.2.2. float#
float(21)
21.0
float('3.14')
3.14
float('300')
300.0
2.2.2.3. str#
str(21)
'21'
str(3.14)
'3.14'
2.2.2.4. Rounding#
The round()
function can be used to round floats. The optional second input argument specifies the number of digits after the decimal place.
round(3.8)
4
round(3.3)
3
round(3.5)
4
round(4.5)
4
Warning
Why do both 3.5
and 4.5
go to 4? Python uses IEEE 754 standard for rounding called the banker’s rounding. In this method when a number is between two numbers, the number is rounded to the nearest value with an even least significant digit. Still, the behavior of round()
can be surprising.
The round function can take an OPTIONAL second argument
round(3.141592653,2)
3.14
round(2.675, 2)
2.67
round(4.55, 1)
4.5
round(4.65, 1)
4.7
round(3.55, 1)
3.5
round(3.65, 1)
3.6
round(-3.3)
-3
round(-3.5)
-4
You can also ceil and floor values by first importing the math
module and using ceil and floor
import math
print('round:', round(3.141592653))
print('ceil:', math.ceil(3.141592653))
print('floor:', math.floor(3.141592653))
round: 3
ceil: 4
floor: 3
2.2.3. Why some numbers are not rounded as expected?#
Floats are only an approximation of the real number.
import decimal
print(decimal.Decimal(2.675))
print(decimal.Decimal(2.6757))
print(decimal.Decimal(4.65))
print(decimal.Decimal(3.65))
2.67499999999999982236431605997495353221893310546875
2.67569999999999996731503415503539144992828369140625
4.6500000000000003552713678800500929355621337890625
3.649999999999999911182158029987476766109466552734375
2.2.3.1. help#
The help(fxn)
function gives information about a function
help(round)
Help on built-in function round in module builtins:
round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None. Otherwise
the return value has the same type as the number. ndigits may be negative.
help(pow)
Help on built-in function pow in module builtins:
pow(base, exp, mod=None)
Equivalent to base**exp with 2 arguments or base**exp % mod with 3 arguments
Some types, such as ints, are able to use a more efficient algorithm when
invoked using the three argument form.
2.2.3.2. pow#
Using the pow
function
pow(2, 4)
16
pow(2, 4, 3)
1
2.2.3.3. min#
min(2, 3, 4)
2
2.2.3.4. max#
max(2, -3, 4, 7, -5)
7
max(2, -3, min(4, 7), -5)
4
2.2.3.5. id#
id(-9)
139912119997392
id(23.1)
139912121609776
print(id(8.5))
size1 = 8.5
print(size1)
print(id(size1))
size2 = 8.5
print(size2)
print(id(size2))
139912121599312
8.5
139912121598704
8.5
139912119997296
2.2.4. Function Memory Address#
Function objects have memory addresses just like variables.
id(abs)
139912318097968
id(round)
139912318100928
2.2.5. Using Function Calls as Arguments to Other Functions#
pow(abs(-2), round(4.3))
16
There are many other built-in functions. The Python documentation describes all of here