Python built-in functions: From Zero To Hero — Part 1: Basics

Milad
6 min readMay 6, 2021

--

Python built-in functions

Python has a lot of useful built-in functions that make tedious tasks easier for you. So, you can achieve better result with less code. As a python programmer it’s better to follow The Zen of Python. By using the built-in functions, you’re on the right track of following The Zen of Python in the areas below:

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Readability counts.

Python has 69 built-in functions. You can find the documentation in here. We’re going to explore each function one by one starting with simple ones.

(At the top of every section, you can find list of the functions we’re going to discuss in that section. So you can quickly look at them and skip the section if you’re familiar with them.)

Handy Functions

print(), input(), format(), id(), type(), help(), dir()

I named these functions as handy functions because either they are controlling I/O or they help you to understand some module, object, etc.

print() function usage is to write something in CLI. You can pass several arguments (known as objects) to it and they turn to str() before printing. The print() function separate the objects by sep argument which is a space by default and after printing all of objects, it’s going to print end.

Note that sep, end, file, and flush should specify by keywords otherwise they consider as objects.

You can use input() to get the data from the user in CLI . Remember that whatever user enters as input, it’s gonna treat as str() . So, you have to convert it to your desired data type (for more information, look at next section). The optional argument is a string that will be written to standard output and user should input something after that.

This function is very useful when you want to generate a string. What I mean by generating a string is to input some variable into a string. Let’s see some examples:

See!! It’s gonna help you do some cool stuff with it. Remember that if you don’t use names (or format_spec) for your variables, you have to put the values in order, or you won’t get your desired result.

You can use f strings to get the same result faster! For more information, read this.

id() function will return an integer that indicates the identity of an input argument. This number is guaranteed to be unique. It’s useful when you want to make sure that two objects with different names are actually the same.

We can see x and y are different at first. But after assigning x to y , they’re gonna share the same id number.

It will return the type of the input object argument. For more details about data types, read the next section.

help() function is one of the useful functions that ever lived!! It’s mean to used in interactive environment. It shows the input argument’s help page.

If you call it without any arguments, it’ll prompt help> in which you can use as an interactive help system. If you pass a string to it, it will search for the help of module, function, class, method, etc. with the same name.

If you do not pass any arguments to this function, it just return the list of names in the current local scope. But if you pass an argument to it, it tries to return the list of valid attributes of that argument.

Simple Data Types

bool(), int(), float(), complex(), str()

These functions are used to convert a data type to another.

It returns the boolean value of input argument which either True or False based on Truth Value Testing.

It will return the integer of input argument which is either a number or a string. If no argument pass to this function, it will return 0 .

Return the float number of input argument which is either a number or a string.

  • complex([real,[imag]])

Return a complex number in which the real part is equal to real and the imaginary part is equal to imag . Also, you can convert a string or a number to complex number with this function. Remember if you want to pass a string to convert it to complex, there is no whitespace allowed in that string.

Return the string of input argument object.

Simple Math Functions

abs(), pow(), round(), divmod()

Python provides some simple math functions. So, you can use them instead of hard coding them.

  • abs(x)

Reruns the absolute value of input argument x. x can be int, float, or complex. If you pass x as complex , it will return the magnitude of the x.

  • pow(base, exp[,mod])

If you do not pass the mod argument, it will behave same as base**exp but if you pass the mod argument, it will return the (base**exp)%mod and it’s much faster than hard coding the above expression. Remember that the arguments have to be numeric. Otherwise it will raise an error.

  • round(number[,ndigits])

Return the input number rounded to ndigits precision after the decimal point. If you do not pass ndigits it will round the number to nearest integer.

  • divmod(a, b)

This function is useful if you want to find the quotient and remainder of a division. We know that a = q * b + r in which q is quotient and r is remainder. Also, we know that q = a // b and r = a % b . So, this function help us to do two different tasks at once which so useful and fun!!

Simple Collections

list(), tuple(), set(), frozenset(), dict()

Collections are one of the most important part of python. These functions help you to turn an iterable to a collection.

It will return a list from the input iterable. Remember that List is a sequence type. For more information look at docs.

  • tuple([iterable])

This is the same as list() but the only difference is that tuple is immutable which means you can not add or remove any element from it. For more information look at docs.

  • set([iterable])

Set is a collection that do not contain duplicate elements. This function turn the iterable to a set. For more information look at docs.

  • frozenset([iterbale])

This is the same as set() but the only difference is that frozenset is immutable which means you can not add or remove any element from it. For more information look at docs.

  • dict(**kwarg)

This function return a new dictionary which is mapping. The returned object is an instance of dictionary class. For more information look at docs.

--

--

No responses yet