Functions used in Python

  1. print(): Used to display output to the console. print("Hello, World!") # Hello, World!

  2. input(): Reads input from the user via the console. name = input("Enter your name: ")

  3. len(): Returns the length of an object (e.g., a string, list, or tuple). length = len([1, 2, 3, 4, 5]) # 5

  4. type(): Returns the data type of an object. data_type = type(42) # class int

  5. int(): Converts a value to an integer. num = int("42") # 42

  6. float(): Converts a value to a floating-point number. pi = float("3.14159") # 3.14159

  7. str(): Converts a value to a string. text = str(123) # 123

  8. list(): Converts an iterable to a list. my_list = list('12345') # [1,2,3,4,5]

  9. tuple(): Converts an iterable to a tuple. my_tuple = tuple([1, 2, 3]) # (1,2,3)

  10. dict(): Creates a dictionary. my_dict = dict(key1=1, key2=2) # {'key1': 1, 'key2': 2}

  11. range(): Generates a sequence of numbers. numbers = list(range(1, 6)) # [1,2,3,4,5]

  12. max(): Returns the largest item from an iterable or among multiple arguments. maximum = max([4, 7, 2, 9]) # 9

  13. min(): Returns the smallest item from an iterable or among multiple arguments. minimum = min(10, 5, 8) # 10

  14. sum(): Returns the sum of all items in an iterable. total = sum([1, 2, 3, 4, 5]) # 15

  15. round(): Rounds a number to a specified number of decimal places. round(3.14159,2) # 3.14

  16. sorted(): Returns a sorted iterable. x=sorted([12,34,8,56,23]) # [8,12,23,34,56]

  17. zip(): Combines multiple iterables element-wise. x=[1,2,3,4] y=['a','b','c','d'] for i in zip(x,y): print(i) # (1,'a') (2.'b') (3,'c') (4,'d')

  18. enumerate(): Unpacks an iterable. x=['a','b','c','d'] for i in enumerate(x): print(i) # (0,'a') (1,'b') (2,'c') (3,'d')

  19. map(): It applies a function to all the items in an iterable. l1=[1.0,2.1,3.2,4.3] l2=list(map(int,l1)) print(l2) # [1,2,3,4]

  20. filter(): It filters items in an iterable depending upon a function numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] even_numbers = filter(lambda x: x % 2 == 0, numbers) print(list(even_numbers)) #[2,4,6,8,10]

  21. format(): Formats a string by replacing placeholders with values. name = "Alice" age = 30 message = "My name is {} and I am {} years old.".format(name, age) print(message) # My name is Alice and I am 30 years old

  22. chr(),ord(): Converts a Unicode code point to a character or vice versa. char = chr(65) code = ord('A') print(char, code) # A 65

  23. eval(): Evaluates a python expression. exp = "3 + 4 * 2" result = eval(exp) print(result) # 11

  24. pow(): Calculate raised to the power. result = pow(2, 3) print(result) # 8

Last updated