Header Ads Widget

Responsive Advertisement

100 Most common python codes

 Below are 100 Python functions along with their uses and simple examples:

  1. print(): Displays text or variables. Example: print("Hello, World!")
  2. input(): Accepts user input. Example: name = input("Enter your name: ")
  3. len(): Returns the length of a string or list. Example: length = len("Python")
  4. type(): Returns the data type of a variable. Example: data_type = type(42)
  5. int(): Converts a value to an integer. Example: number = int("5")
  6. float(): Converts a value to a floating-point number. Example: decimal = float("3.14")
  7. str(): Converts a value to a string. Example: text = str(42)
  8. list(): Converts a sequence to a list. Example: my_list = list("Python")
  9. tuple(): Converts a sequence to a tuple. Example: my_tuple = tuple("Python")
  10. set(): Converts a sequence to a set. Example: my_set = set("Python")
  11. dict(): Creates a dictionary. Example: my_dict = dict(key1="value1", key2="value2")
  12. range(): Generates a sequence of numbers. Example: numbers = range(1, 6)
  13. zip(): Combines two or more sequences. Example: zipped = zip(["a", "b"], [1, 2])
  14. sum(): Adds up the elements of a sequence. Example: total = sum([1, 2, 3])
  15. min(): Returns the smallest element in a sequence. Example: minimum = min([4, 2, 7])
  16. max(): Returns the largest element in a sequence. Example: maximum = max([4, 2, 7])
  17. abs(): Returns the absolute value of a number. Example: absolute_value = abs(-5)
  18. round(): Rounds a number to a specified number of decimal places. Example: rounded = round(3.14159, 2)
  19. sorted(): Returns a sorted list from a sequence. Example: sorted_list = sorted([3, 1, 4])
  20. reversed(): Returns a reversed version of a sequence. Example: reversed_list = list(reversed([1, 2, 3]))
  21. any(): Returns True if at least one element in a sequence is true. Example: result = any([True, False, False])
  22. all(): Returns True if all elements in a sequence are true. Example: result = all([True, True, True])
  23. filter(): Filters elements in a sequence based on a function. Example: filtered = filter(lambda x: x > 0, [-1, 2, -3])
  24. map(): Applies a function to all elements in a sequence. Example: squared = map(lambda x: x**2, [1, 2, 3])
  25. enumerate(): Returns index and value pairs from a sequence. Example: pairs = list(enumerate(["a", "b", "c"]))
  26. bool(): Converts a value to a boolean. Example: is_true = bool("Hello")
  27. abs(): Returns the absolute value of a number. Example: absolute_value = abs(-5)
  28. pow(): Raises a number to a specified power. Example: result = pow(2, 3)
  29. slice(): Extracts a portion of a sequence. Example: portion = slice(1, 4)
  30. chr(): Returns a string representing a character with a given Unicode code. Example: character = chr(65)
  31. ord(): Returns an integer representing the Unicode character. Example: code = ord("A")
  32. format(): Formats a specified value. Example: formatted = format(3.14159, ".2f")
  33. capitalize(): Converts the first character of a string to uppercase. Example: text = "hello".capitalize()
  34. lower(): Converts a string to lowercase. Example: text = "HELLO".lower()
  35. upper(): Converts a string to uppercase. Example: text = "hello".upper()
  36. strip(): Removes leading and trailing whitespaces from a string. Example: text = " Hello ".strip()
  37. replace(): Replaces occurrences of a substring in a string. Example: new_text = "hello".replace("e", "a")
  38. count(): Returns the number of occurrences of a substring in a string. Example: count = "hello".count("l")
  39. find(): Returns the index of the first occurrence of a substring in a string. Example: index = "hello".find("l")
  40. startswith(): Checks if a string starts with a specified prefix. Example: result = "hello".startswith("he")
  41. endswith(): Checks if a string ends with a specified suffix. Example: result = "hello".endswith("lo")
  42. isdigit(): Checks if all characters in a string are digits. Example: result = "123".isdigit()
  43. isalpha(): Checks if all characters in a string are alphabetic. Example: result = "abc".isalpha()
  44. isalnum(): Checks if all characters in a string are alphanumeric. Example: result = "abc123".isalnum()
  45. isspace(): Checks if all characters in a string are whitespaces. Example: result = " ".isspace()
  46. isupper(): Checks if all characters in a string are uppercase. Example: result = "HELLO".isupper()
  47. islower(): Checks if all characters in a string are lowercase. Example: result = "hello".islower()
  48. join(): Joins elements of a sequence into a string. Example: `joined =
  1. split(): Splits a string into a list of substrings based on a delimiter. Example: words = "Hello,World".split(",")
  2. isnumeric(): Checks if all characters in a string are numeric. Example: result = "123".isnumeric()
  3. isdecimal(): Checks if all characters in a string are decimals. Example: result = "123.45".isdecimal()
  4. isidentifier(): Checks if a string is a valid identifier. Example: result = "variable_1".isidentifier()
  5. title(): Converts the first character of each word to uppercase in a string. Example: text = "hello world".title()
  6. isprintable(): Checks if all characters in a string are printable. Example: result = "Hello\nWorld".isprintable()
  7. capitalize(): Converts the first character of a string to uppercase. Example: text = "hello".capitalize()
  8. rjust(): Right-justifies a string in a field of a specified width. Example: justified = "hello".rjust(10)
  9. ljust(): Left-justifies a string in a field of a specified width. Example: justified = "hello".ljust(10)
  10. center(): Centers a string in a field of a specified width. Example: centered = "hello".center(10)
  11. isdigit(): Checks if all characters in a string are digits. Example: result = "123".isdigit()
  12. zfill(): Pads a numeric string with zeros on the left to a specified width. Example: padded = "42".zfill(5)
  13. swapcase(): Swaps the case of each character in a string. Example: swapped = "Hello World".swapcase()
  14. expandtabs(): Expands tab characters in a string to spaces. Example: expanded = "Tab\tExample".expandtabs(4)
  15. isascii(): Checks if all characters in a string are ASCII. Example: result = "Hello".isascii()
  16. hex(): Converts an integer to a hexadecimal string. Example: hex_string = hex(255)
  17. oct(): Converts an integer to an octal string. Example: octal_string = oct(8)
  18. bin(): Converts an integer to a binary string. Example: binary_string = bin(5)
  19. eval(): Evaluates a string as a Python expression. Example: result = eval("2 + 3")
  20. exec(): Executes a dynamically created Python program. Example: exec("print('Hello')")
  21. round(): Rounds a number to a specified number of decimal places. Example: rounded = round(3.14159, 2)
  22. dir(): Returns a list of names in the current scope or attributes of an object. Example: names = dir()
  23. globals(): Returns a dictionary representing the current global symbol table. Example: global_vars = globals()
  24. locals(): Returns a dictionary representing the current local symbol table. Example: local_vars = locals()
  25. repr(): Returns a string representation of an object. Example: representation = repr([1, 2, 3])
  26. hash(): Returns the hash value of an object. Example: hashed = hash("Hello")
  27. callable(): Checks if an object appears callable. Example: result = callable(print)
  28. id(): Returns the identity of an object. Example: identity = id([1, 2, 3])
  29. chr(): Returns a string representing a character with a given Unicode code. Example: character = chr(65)
  30. ord(): Returns an integer representing the Unicode character. Example: code = ord("A")
  31. compile(): Compiles a source into a code or AST object. Example: compiled = compile("print('Hello')", '<string>', 'exec')
  32. memoryview(): Returns a memory view object of an array. Example: view = memoryview(b'Hello')
  33. ascii(): Returns a string containing a printable representation of an object. Example: representation = ascii('Hello')
  34. bytearray(): Returns a mutable bytearray object. Example: arr = bytearray([65, 66, 67])
  35. bytes(): Returns an immutable bytes object. Example: b = bytes([65, 66, 67])
  36. filter(): Filters elements in a sequence based on a function. Example: filtered = filter(lambda x: x > 0, [-1, 2, -3])
  37. map(): Applies a function to all elements in a sequence. Example: squared = map(lambda x: x**2, [1, 2, 3])
  38. pow(): Raises a number to a specified power. Example: result = pow(2, 3)
  39. sum(): Adds up the elements of a sequence. Example: total = sum([1, 2, 3])
  40. enumerate(): Returns index and value pairs from a sequence. Example: pairs = list(enumerate(["a", "b", "c"]))
  41. reversed(): Returns a reversed version of a sequence. Example: reversed_list = list(reversed([1, 2, 3]))
  42. any(): Returns True if at least one element in a sequence is true. Example: result = any([True, False, False])
  43. all(): Returns True if all elements in a sequence are true. Example: result = all([True, True, True])
  44. hex(): Converts an integer to a hexadecimal string. Example: hex_string = hex(255)
  45. oct(): Converts an integer to an octal string. Example: octal_string = oct(8)
  46. bin(): Converts an integer to a binary string. Example: binary_string = bin(5)

Post a Comment

0 Comments