Python Built in Functions

Built-in functions


  Machine Learning in Python

Contents

There are close to 70 odd built-in functions in python that work at the global level – meaning they are not linked to strings or lists or other elements. They work on their own and are always available. You would not have to import anything either. Here is the list of in-built Python functions. Not all of them are relevant at this point in our course. We will look at some of the relevant ones at this point. We will however revisit the rest of them as the time comes.

len ( ) function

len ( ) is a generic Python function that is used to calculate the length of a variable. The variable could be string, list, tuple, dictionary etc. It essentially returns the number of items in the iterable. For example, to get the length of a string,

name = "Ajay Tech"
len(name)

9

The word iterable is important. We will learn more about iterables when we finish the next section – data structures. However, until now, think of iterables as a list of elements, and not simple variables like integers of floats. For example, when you try to find the length of an integer, you get an error.

i = 5
len(i)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-d5286433e459> in <module>
      1 i = 5
----> 2 len(i)

TypeError: object of type 'int' has no len()

On the other hand, if you take a range of numbers as a range object, it is an iterable – a sequence of elements – 1 to 100. So, you can use the len ( ) function on it.

i = range(100)
len(i)

100

abs function

abs stands for absolute. This function returns the absolute value of a number – it essentially strips away the negative in case of integers and floats. In case of complex numbers it just returns the magnitude.

age = -28 # oops.. age is always positive. 
age = abs(age)  # convert it to its absolute value.

print ( age )

28
age = "-28"
print ( abs ( age) )

Question – What is the output of the code above
28
-28
syntax error

max and min functions

max ( ) returns the maximum value in an iterable. Once again, this would make more sense when we cover data structures in Python – like lists, dictionaries etc. For now, we can use this function on the iterables that we know so far – strings & range.

max("Ajay Tech")  # returns the last element in the string in alphabetical order.

'y'
max ( range(100))  # returns the highest element in the range.
99

Similarly, the min ( ) function returns the minimum value in the iterable.

min( "Ajay Tech" )
' '
r = range( 100)
print ( min(r) )

Question – The output of the code above is
0
1
99
100

int(), str(), float(), complex() functions

We have already used a couple of these built-in functions in the type conversion section. We have used int ( ) for example to convert a string to an integer.

age = "21"
type(age)

str
age = int(age)
type(age)

int

A good use case for this is user entered data. For example, when loading files from a csv or text file, the column age could be like any other text column – like name. We will have to explicitly convert it to integer. And that’s where we would use this in-built type casting function.

age_1 = "21"
age_2 = "22"

Question – How to add these ages together
age = age_1 + age_2
age = int(age_1 + age_2)
age = int(age_1) + int(age_2)

input function

We have already used this function to get user input. A word of caution here – According to the definition of this function, the input from the user is always got as a string. So, once again, if you are getting the age of a person as an input, you have to explicitly convert it to an integer using the int ( ) type conversion function.

age = input("Enter age")
type(age)
str
age = int(age)
type(age)

int
name = "Ajay Tech"
name_reversed = reversed(name)
print ( name_reversed)