Built In Functions

Built in functions are pieces of code that are included when you install Python and can be used whenever you need them. There are many functions built into Python. In this section we describe and give examples of some of the more common functions.

abs(x)
Use the abs function to get the absolute value of a number. #Absolute values using abs x = -3.3 y = abs(x) print y    #absolute value of -3.3 is 3.3 which is what y equals now

bool(x)
Use the bool(x) function to get the boolean value of an integer where 0 is False and anything other than 0 is True. bool(0)     # False bool(1)     # True bool(1222)  # True bool(-5)    # True

chr(x)
Use the chr(x) function to get the ASCII character of an integer. y = chr(35)  # y now equals the string "#" x = chr(36)  # x now equals the string "$"

len(x)
Use the len(x) function to get the number of items in a list, tuple, or string. x = "Hello" y = len(x)  # y now equals 5 because there are 5 characters in "Hello" # len with lists a = [1,2,3,4,5,6,7,8,9,10] b = len(a)  #Since there are 10 items in a, b = 10 # len with tuples x = ("a","b","c") y = len(x)  #There are 3 items in x so y = 3

max(x)
Where x can be a list, tuple, or list of arguments. x = max(1,2,3,4)  # x equals 4 # max with a list as a parameter y = ["HI","BYE","WHATSUP"] z = max(y)        # z equals "WHATSUP" because its the largest string

min(x)
Where x can be a list, tuple, or list of arguments. a = min(14,99,2) # a equals 2 # min with a list as a parameter c = [3.3,1003.1, 499.3] b = min(c)   # b equals 3.3

round
With round you can supply a single number to round or you can supply a number to round as well as a number specifying how many decimal positions to round to. #Using round with 1 argument x = round(3.3)  # x now equals 3.0 #Using round with 2 arguments y = round(3.34564, 1) # This rounds 3.3 to 1 decimal position so y = 3.3 z = round(5.5667, 3) # round to 3 decimal places, so z = 5.567

sum
Use the sum(x) function to get value from adding all the values in a list together. #Using sum with a list of integers x = [2,3,7,9,4,2,1] y = sum(x)  #y now equals the summation of x's values which is 28

This is just a small subset of the many built-in functions available in Python. Here is some more information about Python's built-in functions.