What are all the functions in python?
Python has a set of built-in functions. Show
FunctionDescriptionabs() Returns the absolute value of a number all() Returns True if all items in an iterable object are true any() Returns True if any item in an iterable object is true ascii() Returns a readable version of an object. Replaces none-ascii characters with escape character bin() Returns the binary version of a number bool() Returns the boolean value of the specified object bytearray() Returns an array of bytes bytes() Returns a bytes object callable() Returns True if the specified object is callable, otherwise False chr() Returns a character from the specified Unicode code. classmethod() Converts a method into a class method compile() Returns the specified source as an object, ready to be executed complex() Returns a complex number delattr() Deletes the specified attribute (property or method) from the specified object dict() Returns a dictionary (Array) dir() Returns a list of the specified object's properties and methods divmod() Returns the quotient and the remainder when argument1 is divided by argument2 enumerate() Takes a collection (e.g. a tuple) and returns it as an enumerate object eval() Evaluates and executes an expression exec() Executes the specified code (or object) filter() Use a filter function to exclude items in an iterable object float() Returns a floating point number format() Formats a specified value frozenset() Returns a frozenset object getattr() Returns the value of the specified attribute (property or method) globals() Returns the current global symbol table as a dictionary hasattr() Returns True if the specified object has the specified attribute (property/method) hash() Returns the hash value of a specified object help() Executes the built-in help system hex() Converts a number into a hexadecimal value id() Returns the id of an object input() Allowing user input int() Returns an integer number isinstance() Returns True if a specified object is an instance of a specified object issubclass() Returns True if a specified class is a subclass of a specified object iter() Returns an iterator object len() Returns the length of an object list() Returns a list locals() Returns an updated dictionary of the current local symbol table map() Returns the specified iterator with the specified function applied to each item max() Returns the largest item in an iterable memoryview() Returns a memory view object min() Returns the smallest item in an iterable next() Returns the next item in an iterable object() Returns a new object oct() Converts a number into an octal open() Opens a file and returns a file object ord() Convert an integer representing the Unicode of the specified character pow() Returns the value of x to the power of y print() Prints to the standard output device property() Gets, sets, deletes a property range() Returns a sequence of numbers, starting from 0 and increments by 1 (by default) repr() Returns a readable version of an object reversed() Returns a reversed iterator round() Rounds a numbers set() Returns a new set object setattr() Sets an attribute (property/method) of an object slice() Returns a slice object sorted() Returns a sorted list staticmethod() Converts a method into a static method str() Returns a string object sum() Sums the items of an iterator super() Returns an object that represents the parent class tuple() Returns a tuple type() Returns the type of an object vars() Returns the __dict__ property of an object zip() Returns an iterator, from two or more iterators This article was published as a part of the Data Science Blogathon IntroductionIn Python, you’ll be able to use a list function that creates a group that will be manipulated for your analysis. This collection of data is named a list object. While all methods are functions in Python, not all functions are methods. There’s a key difference between functions and methods in Python. Functions take objects as inputs while Methods in contrast act on objects. Image Source: Google Images Python offers the subsequent list functions:
List RefresherIt is the primary, and certainly the foremost common container.
Image Source: Google Images Let’s see all the functions one by one with the help of an example,sort() methodThe sort() method is a built-in Python method that, by default, sorts the list in ascending order. However, you’ll modify the order from ascending to descending by specifying the sorting criteria. ExampleLet’s say you would like to sort the elements of the product’s prices in ascending order. You’d type prices followed by a . (period) followed by the method name, i.e., sort including the parentheses. Python Code: type() functionFor the type() function, it returns the class type of an object. ExampleIn this example, we will see the data type of the formed container. fam = ["abs", 1.57, "egfrma", 1.768, "mom", 1.71, "dad"] type(fam) Output: list append() methodThe append() method will add some elements you enter to the end of the elements you specified. ExampleIn this example, let’s increase the length of the string by adding the element “April” to the list. Therefore, the append() function will increase the length of the list by 1. months = ['January', 'February', 'March'] months.append('April') print(months) Output: ['January', 'February', 'March', 'April'] extend() methodThe extend() method increases the length of the list by the number of elements that are provided to the strategy, so if you’d prefer to add multiple elements to the list, you will be able to use this method. ExampleIn this example, we extend our initial list having three objects to a list having six objects. list = [1, 2, 3] list.extend([4, 5, 6]) list Output: [1, 2, 3, 4, 5, 6] index() methodThe index() method returns the primary appearance of the required value. ExampleIn the below example, let’s examine the index of February within the list of months. months = ['January', 'February', 'March', 'April', 'May'] months.index('March') Output: 2 max() functionThe max() function will return the highest value from the inputted values. ExampleIn this example, we’ll look to use the max() function to hunt out the foremost price within the list named price. prices = [589.36, 237.81, 230.87, 463.98, 453.42] price_max = max(prices) print(price_max) Output: 589.36 min() functionThe min() function will return the rock bottom value from the inputted values. ExampleIn this example, you will find the month with the tiniest consumer indicator (CPI). To identify the month with the tiniest consumer index, you initially apply the min() function on prices to identify the min_price. Next, you’ll use the index method to look out the index location of the min_price. Using this indexed location on months, you’ll identify the month with the smallest consumer indicator. months = ['January', 'February', 'March'] prices = [238.11, 237.81, 238.91] # Identify min price min_price = min(prices) # Identify min price index min_index = prices.index(min_price) # Identify the month with min price min_month = months[min_index] print[min_month] Output: February len() functionThe len() function returns the number of elements in a specified list. ExampleIn the below example, we are going to take a look at the length of the 2 lists using this function. list_1 = [50.29] list_2 = [76.14, 89.64, 167.28] print('list_1 length is ', len(list_1)) print('list_2 length is ', len(list_2)) Output: list_1 length is 1 list_2 length is 3 clear() functionThe clear() method removes all the elements from a specified list and converts them to an empty list. ExampleIn this example, we’ll remove all the elements from the month’s list and make the list empty. months = ['January', 'February', 'March', 'April', 'May'] months.clear() Output: [ ] insert() functionThe insert() method inserts the required value at the desired position. ExampleIn this example, we’ll Insert the fruit “pineapple” at the third position of the fruit list. fruits = ['apple', 'banana', 'cherry']
fruits.insert(2, "pineapple") Output: ['apple', 'banana', 'pineapple', 'cherry'] count() functionThe count() method returns the number of elements with the desired value. ExampleIn this example, we are going to return the number of times the fruit “cherry” appears within the list of fruits. fruits = ['cherry', 'apple', 'cherry', 'banana', 'cherry'] x = fruits.count("cherry") Output: 3 pop() functionThe pop() method removes the element at the required position. ExampleIn this example, we are going to remove the element that’s on the third location of the fruit list. fruits = ['apple', 'banana', 'cherry', 'orange', 'pineapple'] fruits.pop(2) Output: ['apple', 'banana', 'orange', 'pineapple'] remove() functionThe remove() method removes the first occurrence of the element with the specified value. ExampleIn this example, we will Remove the “banana” element from the list of fruits. fruits = ['apple', 'banana', 'cherry', 'orange', 'pineapple'] fruits.remove("banana") Output: ['apple', 'cherry', 'orange', 'pineapple'] reverse() functionThe reverse() method reverses the order of the elements. ExampleIn this example, we will be reverse the order of the fruit list, so that the first element in the initial list becomes last and vice-versa in the new list. fruits = ['apple', 'banana', 'cherry', 'orange', 'pineapple'] fruits.reverse() Output: ['pineapple', 'orange', 'cherry', 'banana', 'apple'] copy() functionThe copy() method returns a copy of the specified list and makes the new list. ExampleIn this example, we want to create a list having the same elements as the list of fruits. fruits = ['apple', 'banana', 'cherry', 'orange'] x = fruits.copy() Output: ['apple', 'banana', 'cherry', 'orange'] This ends our discussion! End NotesI hope you enjoyed the article. If you want to connect with me, please feel free to contact meonEmail Your suggestions and doubts are welcomed here in the comment section. Thank you for reading my article! The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion. How many functions are there in Python?The built-in Python functions are pre-defined by the python interpreter. There are 68 built-in python functions. These functions perform a specific task and can be used in any program, depending on the requirement of the user.
What are the 4 types of functions in Python?The following are the different types of Python Functions:. Python Built-in Functions.. Python Recursion Functions.. Python Lambda Functions.. Python User-defined Functions.. Is there an all function in Python?Python all() Function
The all() function returns True if all items in an iterable are true, otherwise it returns False. If the iterable object is empty, the all() function also returns True.
|