Python Built In Functions Example
Today we will be talking about some built-in functions in Python. Why are they important to know? Well, if you do import a lot of heavy libraries for simple tasks, that will be very inefficient. A good example of that is importing NumPy
just to calculate the sum of elements in a row! So basically, built-in functions don’t need to be imported. Let’s get started, shall we?
1. Basic functions
The first function would be abs()
. It stands for absolute value, and it shows the distance between the given number from zero
. For example, let’s create the program that asks a user for two numbers and returns the answer for either those numbers are identical or not.
Python
num1 = int(input()) num2 = int(input()) if abs(num1) == abs(num2): print("You put numbers that are either identical or have the same absolute value") else: print("Numbers are different")
Simple enough, right? If we put 1
and -1
, our output will be “You put numbers that are either identical or have the same absolute value”.
The second function I want to cover is probably one of the most used function. That is help()
. You can find more about any function you want by typing help()
. However, a lot of people prefer just google it. For example, let’s find out more about the ufw
:
Python
>>> help() Welcome to Python 3.5's help utility! If this is your first time using Python, you should definitely check out the tutorial on the Internet at http://docs.python.org/3.5/tutorial/. Enter the name of any module, keyword, or topic to get help on writing Python programs and using Python modules. To quit this help utility and return to the interpreter, just type "quit". To get a list of available modules, keywords, symbols, or topics, type "modules", "keywords", "symbols", or "topics". Each module also comes with a one-line summary of what it does; to list the modules whose name or summary contain a given string such as "spam", type "modules spam". help>ufw Help on package ufw: NAME ufw PACKAGE CONTENTS applications backend backend_iptables common frontend parser util FILE /usr/lib/python3/dist-packages/ufw/__init__.py
Once again, for some it’s easier to google.
2. Working with numbers
The other thing you may face is working with lists and check which element is a maximum and which one is a minimum. For those cases you should use max()
and min()
:
Python
exList = [] numberOfElements = int(input("How many numbers in your list?")) for x in range (numberOfElements): x = int(input()) exList.append(x) print("Your list is:", exList) print("Maximum is:", max(exList)) print("Minimum is:", min(exList))
Simple as it seems. The program asks a user to create a list and output the maximum and minimum elements.
Another cool feature is rounding! So let’s use round()
!
Python
>>> round(4.8) 5 >>> round(5.212) 5 #let's see a trick! #What happens with round(5.5) and round(4.5)? >>>round(5.5) 6 >>> round(4.5) 4
So the cool thing I wanted to tell you is a round()
function rounds to the closest even number! Be aware of that.
Now, we do know that there is math
library that has floor()
and ceil()
functions that round up and down. However, in most cases you don’t really need to use them as much.
3. Data types
There is a common mistake that many novice programmers have is to not look for data types. Fortunately, Python has some great functions that can convert data into different types. Let’s create a string which we will convert into int
, bool
and float
.
Python
>>> var = "12" >>> print(float(var)) 12.0 >>> print(bool(var))
So it’s simple as well. The main rule: as long as something can be converted to something else, it will be converted. Otherwise, you can’t convert str
“hello” to int
.
4. Summary
As we can see, built-in functions are essential to know especially at the beginning. You may work with lists and arrays. If you do that, that doesn’t always mean that you should strive for using NumPy
or Pandas
. Maybe, you just need to do some basic math. However, while you are doing some math, don’t forget about data types too!
As for any built-in functions which were not covered, I strongly recommend going here or use help()
.
5. Homework
There is a small homework I want you to do. Create checkABS.py
file and write a function that checks absolute value of two numbers. Example of how it can be done can be seen below:
checkABS.py
def checkABS(num1, num2): if abs(num1) == abs(num2): print("You put numbers that are either identical or have the same absolute value") else: print("Numbers are different")
The second task is to create a function that will output the maximum or minimum element in the given list. Possible solution can be seen below:
checkMaxMin.py
def checkMaxMin(): exList = [] numberOfElements = int(input("How many numbers in your list?")) for x in range (numberOfElements): x = int(input()) exList.append(x) print(exList) question = input("Max or Min?\n") if question.lower() == "max": print(max(exList)) elif question.lower() == "min": print(min(exList)) else: print("Incorrect input") checkMaxMin()
6. Download the Source Code
All necessary code can be downloaded.
You can download the full source code of this example here: python-built-in-functions.zip