Built-in Functions in Python
What are Built-in functions?
Features which are given as part of a high-level language that can be implemented through a basic connection with statement definition are known as Built-in Functions in Python. Python offers a broad range of built-in functions which makes it more versatile, easy to use, and fast to develop. Due to this broad range of built-in functions, you can do more with less code.
Some of popular Built-in Functions in Python:
Functions | Description | Example |
---|---|---|
int() | Return an integer object constructed from a number or string x, or return 0 if no arguments are given |
>>> int(’23’) 23 |
len() | Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set). | >>>len([1,2,3,4,5]) 5 |
reversed() | returns a reverse iterator | >>> for i in reversed([1,2,3,4,5]): print(i,end=”, “) 5, 4, 3, 2, 1, |
float() | Return a floating point number constructed from a number or string x. If no argument is given, 0.0 is returned. | >>>float(‘+12.34’) 12.34 >>> float(123) 123.0 |
round() | Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input. | >>> round(2.122345,3) 2.122 >>> round(12.32) 12 |
input() | Function reads a line from the input, converts it to a string, and returns it. | >>> input() asdf ‘asdf’ |
sorted() | Return a new sorted list from the items in iterable. | >>> sorted([1,4,6,3,2,8,4]) [1, 2, 3, 4, 4, 6, 8] |
str() | Return a str version of object. | >>> str(123) ‘123’ |
Use of Some Important Built-in Functions in Python:
abs()
Return the absolute value of a number( An absolute numbers is the magnitude of real number without regard to its sign).
Syntax:
>>>abs(X)
# Program to illustrate usage of Abs() function. def FindAbsolute(x): print("Absolute Value of Given Number is:",abs(x)) x = int(input("Enter the number:")) FindAbsolute(x)
Output: Enter the number:-90 Absolute Value of Given Number is: 90
bin()
Converts an integer number to a binary string counterpart prefixed with “0b “.
Syntax:
>>> bin(x)
# Program to find Binary value of a decimal. number = int(input("Enter the Number:")) print("Binary value of entered number is:",bin(number))
Output: Enter the Number:14 Binary value of entered number is: 0b1110
hex()
Converts an integer number to a hexadecimal lowercase string prefixed with “0x”.
Syntax:
>>>hex(x)
Note: oct() is used same as bin() and hex() to represent decimal in octal number system.
# Program to find hexadecimal value of a decimal. number = int(input("Enter the Number:")) print("Hexadecimal value of entered number is:",hex(number))
Output: Enter the Number:90 Hexadecimal value of entered number is: 0x5a
range()
returns an iterator from a starting point to a end point with a specific step. It is most commonly used in looping with a FOR LOOP.
Syntax:
range(start,stop[, step])
Note: Stop is a mandatory argument.
Start and Step are optional (Default value of start is 0 and step is 1).
# Program to illustrate Range function. for i in range(1,10,3): print(i)
Output: 1 4 7
min()
Return the smallest item in an iterable or the smallest of two or more arguments.
If one positional argument is provided, it should be an iterable. The smallest item in the iterable is returned. If two or more positional arguments are provided, the smallest of the positional arguments is returned.
Syntax:
>>>min(iterable)
>>>min(arg1, arg2 ,…..)
# Program to illustrate min() function. myList = [2,3,42,1] print(min(myList)) print(min(33,44,22,12))
Output: 1 12
max()
Return the largest item in an iterable or the largest of two or more arguments.
If one positional argument is provided, it should be an iterable. The largest item in the iterable is returned. If two or more positional arguments are provided, the largest of the positional arguments is returned.
Syntax:
>>>max(iterable)
>>>max(arg1, arg2 ,…..)
# Program to illustrate max() function. myList = [2,3,42,1] print(max(myList)) print(max(33,44,22,12))
Output: 42 44
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment