Built-in Functions
To create some easy to use functionality within a programming language, some of it has to be included in the foundations of the language so you can do simple things. The ones that come with the python installation as standard are known as built-ins. By now you will have already seen or used some of them without realising it, the print() statement for instance is one such built-in.
How do built-ins work
To call a function you have to name the function and then use the curved braces after in, just like when we use print(). Any variables passed to the function are put inside the braces, in the case of print it is the string you want to output to the console. This is no different than how user functions work, but there are some key built-ins that are good to remember:
function | usage |
---|---|
print() | prints out a string to the command line. print("PASSED STRING") |
isinstance() | checks to see if a variable is of a specific type, returns True or False. isinstance(DATA, TYPE) |
type() | returns the type object of the given variable. type(int_object) |
len() | returns an integer to represent the length of an iterable passed to it. len(ITERABLE) |
list() | returns the passed variable as a list. list( ITERABLE ) |
range() | returns a generator object containing a range of numbers. range(START, STOP, STEP) |
reversed() | returns a string that is the reverse of the input string. |
hex() | returns the hex of an inputted integer. hex(INT) |
hash() | returns the hash of the passed object if it has one. hash(OBJECT) |
any() | returns true if any element of the iterable is true. any(STRING) |
all() | returns true if all elements of passed iterable are true (or if iterable is empty). all(ITERABLE) |
exec() | takes in a string and attempts to run it as if it's python code. exec("print('This is an example!')") |
This list is by no means exhaustive, but it gives you an idea of some of the things that python can do as standard. It's worth looking into these and other built-ins to get a better understanding of Python in general, and the adventurous among you could even look for it in the standard libraries and read the code that makes them run too. You can find some more on built-in functions in the python docs here.