Basic Built-in Python Functions To Know As A Beginner
In Python, getting familiar with built-in functions helps a lot while programming. They help in breaking big tasks into smaller chunks and increase code clarity. Also, extensive programs are broken down into smaller units, thus making it easier to read, understand and debug if the need arises. In this tutorial, we’ll be taking a look at some built-in functions in python programming. Before going into that, let’s review what Python is about.
Python is a general-purpose, high-level, object-oriented programming language. According to Wikipedia, it is garbage collected and dynamically typed. Guido Van Russom designed it in 1991. Python programming language can be applied in many areas, such as game development, web development, and data science, to mention a few!
What Are Built-in Functions
Built-in functions are pre-defined functions in Python. They are the functions included with Python interpreters. The tasks they are to complete have already been created, and you need to call them out to complete those tasks by your program.
Key Built-in function in Python
Here are vital built-in functions in Python:
- Print(): The print () method in Python accepts any number of variables and prints them on a single line of text.
The contents could be a string or another object which will be converted to a string before being shown.
Use case:
Print(‘Hello World’)
- Zip (): In simple words, they are simultaneously used for iterating over tuples. It is a built-in function that joins tuples together
Use case:
Names= [‘John’, ‘Sean’, ‘Charles’]
Ages= [12, 19, 25]
for Name, Age in zip (Name, Age):
print(Name, Age)
- Type (): This built-in function helps you to identify the class a variable belongs to.
Use case:
Name= ‘Joan’
print(type(Name))
It gives us indicating that the class type is a string.
- Sum (): This function adds up all the elements in a given list. It works with integers (whole numbers) and floats (decimals).
Use case:
Ages= [2, 4, 6, 8, 10]
print(sum(Ages))
- Set (): It is used for converting an iterable to a ‘SET’
NOTE: A set is an ungraded group of unique elements. In a list containing repeated numbers such as [1,1,4,4,6,6], the set function creates a new list by taking the unique elements, thus ignoring the repetition
Use case:
Ages= (1,1,4,4,6,6)
print(set(Ages))
- List (): It simply converts an iterable into a list. It creates a list object (an ordered sequence of elements)
NOTE: A list is known for always having square brackets…. Using the same example used for the set, if we are to convert the tuple back to a list
print(list (set(Ages)))
We see the elements in a square bracket, indicating that it is a list. To confirm it is a list, we can run this code:
print(type(list(set (Ages))))
- Sorted (): As the name implies, they are used for sorting out list elements. It categorizes the elements of an iterable in a particular order (i.e. either in ascending or descending order) and then gives the output as a list.
Use case:
Ages = [5,7,3,2,10,9,8]
print(sorted(Ages))
- Map (): It is a built-in function that applies a function to each element in an iterable and returns to a new iterable. It allows you to process and remold all of the elements in an iterable without the need to use a direct loop. When you need to apply a modification function to each element of an iterable and change it to a new one, the map() function comes in handy.
Use case:
def cube(x):
return x*x
old_list = [2,4,6,8]
new_list =list(map(square, old_list))
print (new_list)
- Input (): It takes a string representation of the user’s Input and returns it. A user can insert a value into a program. The input function is used when the program wants a response from the user.
Use case:
question = input ("What's your name")
The above-listed functions are basically built-in functions you would come across while programming in Python, but there are also some functions that weren’t listed above but would come in handy definitely, so I’ll briefly explain them below.
- Len function: This function counts the number of characters in a string. Immediately after writing the particular program to be performed, you add the .len(), which counts the elements inside the string.
Use case:
Names= [‘John’, ‘Sean’, ‘Charles’]
print(len(Names))
- Upper function: This function converts a string to uppercase [.upper ()].
Use case:
input("Where are you from").upper()
What this function does in this program above is that it converts whatever the user inputs into uppercase letters.
- Lower function: It is the opposite of the upper function. It converts a string into lowercase letters.
Conclusion
Many more built-in functions are not listed here, but the listed ones are important to know as they make programming more manageable, especially for a beginner.