Posts

Python map() Built in Function

The map() function in Python executes a specified function for each item in an iterable. The item is sent to the function as a parameter. The map() function returns a map object, which is an iterator that yields the results of applying the function to each item. The map object can be converted into a list, tuple, set, or other collection type. Here are some examples of using map() function in Python: Using map() with a single iterable # This example applies the len() function to each element of a list and returns a list of lengths words = ["apple", "banana", "cherry"] lengths = list(map(len, words)) print(lengths) # Output: [5, 6, 6] Using map() with multiple iterables # This example applies the pow() function to two lists of numbers and returns a list of powers base = [2, 3, 4] exponent = [1, 2, 3] powers = list(map(pow, base, exponent)) print(powers) # Output: [2, 9, 64] Using map() with lambda functions # This example uses a lambda function to squ...

Python max() Built in Function

The max() function in Python returns the item with the highest value, or the item with the highest value in an iterable. An iterable can be a list, tuple, set, dictionary, string, etc. The max() function can also take two or more arguments and return the largest one. Here are some examples of using max() function in Python: Using max() with a single iterable # This example returns the largest number in a list numbers = [18 ,52 ,23 ,41 ,32] largest = max(numbers) print(largest) # Output: 52 Using max() with multiple arguments # This example returns the largest number among four arguments result = max(5 ,10 ,25 ,99) print(result) # Output: 99 Using max() with strings # This example returns the alphabetically last string in a list words = ["apple" ,"banana" ,"cherry" ,"date"] last = max(words) print(last) # Output: date Using max() with a key function # This example returns the longest string in a list using len() as a key function words = [...

Python memoryview() Built in Function

The memoryview() function in Python returns a memory view object of a given argument. A memory view object allows you to access the internal data of an object that supports the buffer protocol without copying it. The buffer protocol provides a way to access the low-level memory of an object. Examples of objects that support the buffer protocol are bytes, bytearray, array.array, etc. Here are some examples of using memoryview() function in Python: Using memoryview() with a bytearray # This example creates a memory view object from a bytearray and prints its elements byte_array = bytearray('ABC', 'utf-8') mv = memoryview(byte_array) print(mv[0]) # Output: 65 print(bytes(mv[0:2])) # Output: b'AB' print(list(mv[0:3])) # Output: [65 ,66 ,67] Using memoryview() to modify internal data # This example modifies the first element of a bytearray using a memory view object byte_array = bytearray('ABC', 'utf-8') print('Before update:', byte_ar...

Python min() Built in Function

The min() function in Python returns the smallest item in an iterable or the smallest item among two or more arguments. An iterable can be a list, tuple, set, dictionary, string, etc. The min() function can also take a key function to customize the comparison criteria. Here are some examples of using min() function in Python: Using min() with a single iterable # This example returns the smallest number in a list numbers = [9 ,34 ,11 ,-4 ,27] smallest = min(numbers) print(smallest) # Output: -4 Using min() with multiple arguments # This example returns the smallest number among four arguments result = min(5 ,10 ,25 ,99) print(result) # Output: 5 Using min() with strings # This example returns the alphabetically first string in a list words = ["apple" ,"banana" ,"cherry" ,"date"] first = min(words) print(first) # Output: apple Using min() with a key function # This example returns the shortest string in a list using len() as a key function w...

Python next() Built in Function

The next() function in Python returns the next item from an iterator. An iterator is an object that can be iterated over, such as a list, tuple, string, etc. The next() function can also take a default value as a second argument, which is returned if the iterator has no more items. Here are some examples of using next() function in Python: Using next() with a list iterator # This example creates an iterator from a list and prints its items one by one mylist = iter(["apple" ,"banana" ,"cherry"]) x = next(mylist) print(x) # Output: apple x = next(mylist) print(x) # Output: banana x = next(mylist) print(x) # Output: cherry Using next() with a default value # This example returns a default value when the iterator has reached its end mylist = iter(["apple" ,"banana" ,"cherry"]) while True:     item = next(mylist ,"end")     if item == "end":         break     print(item) # Output: apple banana cherry Us...