Mostly used functions in Python…
print(): Prints the specified value or values. Example:
print("Hello, World!")Output:Hello, World!input(): Reads a line from the console and returns it as a string. Example:
name = input("What is your name? ")Output:What is your name? Johnlen(): Returns the length of an object (string, list, tuple, etc.). Example:
my_list = [1, 2, 3, 4, 5]andprint(len(my_list))Output:5max(): Returns the largest item in an iterable or the largest of two or more arguments. Example:
numbers = [10, 20, 30, 40, 50]andprint(max(numbers))Output:50min(): Returns the smallest item in an iterable or the smallest of two or more arguments. Example:
numbers = [10, 20, 30, 40, 50]andprint(min(numbers))Output:10abs(): Returns the absolute value of a number. Example:
x = -10andprint(abs(x))Output:10round(): Returns a floating-point number rounded to a specified number of decimal places. Example:
x = 3.14159andprint(round(x, 2))Output:3.14int(): Converts a number or string to an integer. Example:
x = "42"andprint(int(x))Output:42float(): Converts a number or string to a floating-point number. Example:
x = "3.14"andprint(float(x))Output:3.14str(): Converts an object to a string. Example:
x = 42andprint(str(x))Output:"42"bool(): Converts a value to a boolean (True or False). Example:
x = 0andprint(bool(x))Output:Falseord(): Returns an integer representing the Unicode character. Example:
print(ord('A'))Output:65chr(): Returns a string representing a Unicode character. Example:
print(chr(65))Output:'A'range(): Returns a sequence of numbers starting from a specified beginning and ending at a specified end. Example:
for i in range(5):andprint(i)Output:0 1 2 3 4sorted(): Returns a new sorted list from the elements of its argument. Example:
numbers = [3, 1, 4, 1, 5, 9]andprint(sorted(numbers))Output:[1, 1, 3, 4, 5, 9]reversed(): Returns a reverse iterator over the elements of an iterable. Example:
my_list = [1, 2, 3, 4, 5]andprint(list(reversed(my_list)))Output:[5, 4, 3, 2, 1]zip(): Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. Example:
names = ['Alice', 'Bob', 'Charlie']andages = [25, 30, 35]andprint(list(zip(names, ages)))Output:[('Alice', 25), ('Bob', 30), ('Charlie', 35)]map(): Returns an iterator that applies a function to each item of an iterable. Example:
numbers = [1, 2, 3, 4, 5]andprint(list(map(lambda x: x**2, numbers)))Output:[1, 4, 9, 16, 25]filter(): Returns an iterator that filters elements from an iterable based on a condition. Example:
numbers = [1, 2, 3, 4, 5]andprint(list(filter(lambda x: x % 2 == 0, numbers)))Output:[2, 4]reduce(): Applies a rolling function to the elements of an iterable. Example:
from functools import reduceandnumbers = [1, 2, 3, 4, 5]andprint(reduce(lambda x, y: x + y, numbers))Output:15all(): Returns True if all elements of an iterable are true. Example:
my_list = [True, True, True]andprint(all(my_list))Output:Trueany(): Returns True if at least one element of an iterable is true. Example:
my_list = [False, True, False]andprint(any(my_list))Output:Trueenumerate(): Returns an iterator that produces tuples containing the index and value for each item in an iterable. Example:
my_list = ['apple', 'banana', 'cherry']andprint(list(enumerate(my_list)))Output:[(0, 'apple'), (1, 'banana'), (2, 'cherry')]zip_longest(): Returns an iterator that aggregates elements from each of the iterables. Example:
names = ['Alice', 'Bob', 'Charlie']andages = [25, 30]andprint(list(zip_longest(names, ages, fillvalue=0)))Output:[('Alice', 25), ('Bob', 30), ('Charlie', 0)]itertools.count(): Returns a count object that produces a sequence of numbers starting from a specified number. Example:
import itertoolsandcounter = itertools.count(start=10, step=2)andprint(list(itertools.islice(counter, 5)))Output:[10, 12, 14, 16, 18]itertools.cycle(): Returns an iterator that produces elements from an iterable in a cycle. Example:
import itertoolsandcolors = ['red', 'green', 'blue']andfor color in itertools.cycle(colors):andprint(color)Output:red green blue red green blue ...(continues indefinitely)itertools.repeat(): Returns an iterator that produces a specified value repeatedly. Example:
import itertoolsandprint(list(itertools.repeat(42, 3)))Output:[42, 42, 42]itertools.chain(): Returns an iterator that produces elements from each of the iterables. Example:
import itertoolsandnumbers = [1, 2, 3]andletters = ['a', 'b', 'c']andprint(list(itertools.chain(numbers, letters)))Output:[1, 2, 3, 'a', 'b', 'c']itertools.compress(): Returns an iterator that filters elements from an iterable based on a condition. Example:
import itertoolsanddata = [1, 2, 3, 4, 5]andselectors = [True, False, True, False, True]andprint(list(itertools.compress(data, selectors)))Output:[1, 3, 5]itertools.dropwhile(): Returns an iterator that drops elements from the start of an iterable until a condition is met. Example:
import itertoolsandnumbers = [2, 4, 6, 8, 10, 1, 3, 5]andprint(list(itertools.dropwhile(lambda x: x % 2 == 0, numbers)))Output:[1, 3, 5]
These built-in functions cover a wide range of functionality, from basic data manipulation to advanced itertools operations. Mastering these functions can significantly improve your Python programming efficiency and productivity.
Citations:
[1] https://blog.hubspot.com/website/python-built-in-functions
[2] https://www.shiksha.com/online-courses/articles/different-types-of-python-built-in-functions/
[3] https://sunscrapers.com/blog/30-built-in-python-modules-you-should-be-using-now/
[4] https://realpython.com/python-built-in-functions/
[5] https://www.simplilearn.com/tutorials/python-tutorial/python-functions
[6] https://www.linkedin.com/pulse/introduction-python-data-types-built-in-functions-suraj-kumar-soni
[7] https://www.geeksforgeeks.org/python-functions/
[8] https://www.digitalocean.com/community/tutorials/built-in-python-3-functions-for-working-with-numbers