In this article, we’ll discuss the most commonly used Python functions with details. These built-in functions come very handy in day-to-day usage for various purposes from using it in the development of applications, data science projects, web scraping, and more.
Let us get started with the most basic. Allocating a value to a variable.
Variable
A python variable stores value in the physical memory location. It is used to allocate a value from integer, float, string, and so on.
x = “Welcome to my Article”
Here, the string, “Welcome to my Article” is stored in the variable ‘x’.
len()
len() is a function in python that enables to count of the length or number of elements in the given string.
Here,
len(x)
Ie. The total number of elements on the variable x is 21.
print()
print() function is responsible to print your desired data on the screen.
print(“Python is fun”)
print(“Python is fun”) outputs, Python is fun text on the screen.
lower()
The lower() function in python, returns the lowercase of the string.
x = “Welcome to my Article”
x.lower()
This output shows the lowercase of the string in variable x.
upper()
The upper() function in python returns the uppercase of the string input.
x = “Welcome to my Article”
x.upper()
This outputs, the uppercase of the string in variable x.
split()
The split functions as the name say splits the string into a list where the argument specifies the delimiter.
x = “Welcome to my Article”
x.split(‘ ’)
Here, space is the delimiter which makes the function check for space in the string and split the string based on the space.
join()
The join functions join the elements of a list into one complete string which can be used with the specific delimiter to join all the elements with.
x= ['Welcome', 'to', 'my', 'Article']
strip()
The strip function removes the white spaces if it is present in the beginning or at the end of the string.
y = “ here we go ”
y.split()
This removes the whitespaces in the front and at the end of the string but leaves the spaces in between the text as it is.
Python Functions for Numerical Data
abs()
The abs() function returns the absolute value for any inputted numerical value which could either be integer or float.
abs(-9/2)
This gives the absolute value of the fraction which results in a positive value.
max()
When inputted with a list or typed-in arguments of items, it outputs the largest value if numerical or alphabetical if a string.
max('cat', 'ball', 'apple', 'mango')
max(9,101,2)
The item from the argument, mango consisted m with the highest order in alphabetical order among the elements thus providing mango as the max item and 101 for the number values among, 9, 101, and 2.
min()
When inputted with a list or typed-in arguments of items, it outputs the smallest value if numerical or lowest degree alphabetical element if a string.
min('cat', 'ball', 'apple', 'mango')
min(9,101,2)
The output result ‘apple’ for the first element as a is the first element in alphabetical order among cat, ball, apple, and mango. Also, since 2 is the smallest number among, 9,101, 2 – the output was similar to what we expected for the lowest value.
sum()
The sum function adds up the elements in the list and prints it as output.
x = [9, 8, 3]
sum(x)
For the above list x, the function sum(x) as the elements 9 + 8 + 3 which is indeed 20.
round()
The round() function outputs the rounded value for the numerical value.
round(-5/4)
The real value of –5/4 is –1.25. Rounding off to the nearest real number, the output of the function displayed –1.
type()
The type() functions output the type of the variable we inputted.
x = 3
type(x)
x = “Welcome to C# Corner”
type(x)
For x = 3, the output displayed the ‘int’ meaning integer value, and for x = “Welcome to C# Corner”, the function outputted ‘str’ which stands for the string value.
Conversion of Numerical Value Type
int()
The int() function converts the other form of numerical values such as floats and complex values into integer values.
int(3.9)
This results in output 3. Contrary to the round() function we learned above which would output value 4, the int function only takes the value before the decimal point. Here, the truncation of the values occurs.
float()
The float() function converts the values into float values.
float(8)
This results in a value of 8.0 which is a floating-point number.
complex()
The complex function transforms any given value into a complex value.
complex(3)
Here, the complex function outputted 3 with its imaginary value ie. 0 with j.
Thus, in this article, we learned about various commonly used Python Functions which are built-in Python 3. We tested the functions in Python 3 Platform using Jupyter Notebook and learned the foundationally important functions which will enable us to work on more complex tasks.