Python, a versatile and powerful programming language, comes with an extensive range of built-in functions. These functions facilitate developers by offering out-of-the-box solutions, eliminating the need to implement common routines from scratch. They cater to a variety of tasks, from basic type conversions to complex operations. Here, we will delve into some prominent built-in functions in Python.
1. Mathematical Functions:
Function | Purpose | Example |
---|---|---|
min() | Returns the smallest item in an iterable or the smallest of two or more arguments. | min(1, 2, 3, 4, 5) returns 1. |
max() | Returns the largest item in an iterable or the largest of two or more arguments. | max(1, 2, 3, 4, 5) returns 5. |
abs() | Returns the absolute value of a number. | abs(-5) returns 5. |
pow() | Raises a number to the power of another number. | pow(2, 3) returns 8. |
sqrt() | Returns the square root of a number. | import math; math.sqrt(16) returns 4.0. |
ceil() | Returns the ceiling of a number. | import math; math.ceil(4.2) returns 5. |
floor() | Returns the floor of a number. | import math; math.floor(4.9) returns 4. |
pi | Returns the mathematical constant π. | import math; math.pi returns approximately 3.14159. |
cos() | Returns the cosine of a number. | import math; math.cos(math.pi/3) returns 0.5. |
sin() | Returns the sine of a number. | import math; math.sin(math.pi/2) returns 1.0. |
tan() | Returns the tangent of a number. | import math; math.tan(math.pi/4) returns 1.0. |
factorial() | Returns the factorial of a number. | import math; math.factorial(5) returns 120. |
gcd() | Returns the greatest common divisor of two numbers. | import math; math.gcd(15, 10) returns 5. |
2. String Functions:
Function | Purpose | Example |
---|---|---|
upper() | Converts all characters in string to uppercase. | "abc".upper() returns "ABC". |
lower() | Converts all characters in string to lowercase. | "ABC".lower() returns "abc". |
count() | Counts occurrences of a substring in the string. | "hello world".count("l") returns 3. |
find() | Searches for a substring and returns the first position of its occurrence. Returns -1 if not found. | "hello".find("e") returns 1. |
index() | Similar to find, but raises an exception if substring is not found. | "hello".index("e") returns 1. |
isalnum() | Checks if all characters in the string are alphanumeric. | "hello123".isalnum() returns True. |
isalpha() | Checks if all characters in the string are alphabetic. | "hello".isalpha() returns True. |
isdecimal() | Checks if all characters in the string are decimals (0-9). | "123".isdecimal() returns True. |
isdigit() | Checks if all characters in the string are digits. | "123".isdigit() returns True. |
islower() | Checks if all alphabetic characters in the string are lowercase. | "hello".islower() returns True. |
isnumeric() | Checks if all characters in the string are numeric characters. | "12345".isnumeric() returns True. |
isspace() | Checks if all characters in the string are whitespace characters. | " ".isspace() returns True. |
isupper() | Checks if all alphabetic characters in the string are uppercase. | "HELLO".isupper() returns True. |
replace() | Replaces a specified phrase with another specified phrase. | "hello world".replace("world", "Python") returns "hello Python". |
split() | Splits the string at a specified separator and returns a list. | "apple, banana, cherry".split(", ") returns ['apple', 'banana', 'cherry']. |
NOTE: We have already discussed the built-in functions of list, tuple, set, and dictionary.