[ad_1]
All Functions are equal, but for data science, some are more equal than others
With each version of Python, they introduce more built-in functions. A few you will rarely use, some you will never use, and some can be made up by combining others. Therefore, I have compiled here all of the functions that I feel you will need on a day-to-day basis.
This only includes Pythons built-in functions. It does not include functions that you may find in Pandas or Numpy.
I’ll cut to the chase and run through them in alphabetical order — and how to use them.
abs()
Returns the absolute value of a number. The input can be a float, an integer or a complex number. If the argument is a complex number then its magnitude is returned.
abs(-5.242) #returns 5.242z = complex(3,4)
abs(z) # returns 5 - due to the magnitude of z: |z| = √x2 + y2
all()
Returns true if all elements of the iterable are true (or empty). It can be thought of as:
def all(iterable):
for element in iterable:
if not element:
return False
return True
This is a good way to look for Truthy or Falsy values.
Consider two lists:
list1 = [1,2,3,4,5,6,7,8,9]
all(list1) # Returns True
list2 = [0,1,2,3,4,5,6,7,9]
all(list2) # Returns False
The first list contains all truthy values while the second list contains one falsy value (0). Therefore the all() function will return True for the first list and False for the second. But be careful, an empty list is a falsy value, but all() will return True for this.
dict()
Simply creates a dictionary.
my_dict = dict("name" : "james", "age": 27)
my_dict #returns 'name': 'james', 'age': 27
enumerate()
Returns an enumerate object, input must be iterable. An eumerate object returns a tuple containing a count and the value from the iterable.
names = ["James", "Tom", "Jack"]
list(enumerate(names))
#returns [(0, 'James'), (1, 'Tom'), (2, 'Jack')]
This is quite commonly used in a for loop to access both the index and the value at the same time.
for x,y in enumerate(names):
print(x, y)#returns
0 James
1 Tom
3 Jack
float()
Returns a floating point number — most commonly used with strings.
string = "234.3454334"
float(string)/2
#returns 117.1727167 (as type float)
int()
Returns an integer object from a string or number.
num = 123.45
num2 = "12345"
int(num) #returns 123
int(num2)#returns 12345
len()
Returns the number of items in an object — or the length of an object. The argument may be a sequence (string, list, range etc) or a collection (dictionary, set etc). The difference being the latter is not ordered.
my_dict = dict("name" : ["james", "Tom"], "age": [27,35])
my_str = "james"
my_list = [1,2,3,4,5]
len(my_dict)#returns 2
len(my_str) #returns 5
len(my_list)#returns 5
list()
List is actually a mutable sequence type — but it can be convenient to think of it as a function. It creates lists. It’s convention just to use square brackets to define a list, such as
my_list = [1,2,3,4,5]
but, you can use list() to separate out items into a list such as
my_str = "james"
list(my_str) #returns ['j', 'a', 'm', 'e', 's']
map()
Map applies a function to every item of an iterable — returning an iterable. This iterable is often converted back to a list. For example, using map below applies the function round() to all items in my_list returning a map object which is then converted back to a list. The first argument of map is the function you which to apply and the second is the iterable.
my_list = [1.3,4.5, 5.9]
list(map(round, my_list))
#returns [1,4,6]
max() and min()
max() and min() return the maximum and minimum value of an iterable respectively.
max(my_list) # returns 5.9
min(my_list) # returns 1.3
range()
Similar to list(), range() is also an immutable sequence type. It returns a range of numbers up to the argument value. You can input one number with range(n) or input a start, stop and step values, such as range(5,20,5).
for i in range(5,20,5):
print(i)
#returns 5 10, 15for i in range(5):
print(i)
#returns 0 1 2 3 4
This is quite commonly used in conjunction with length to iterate over a range of a length of a variable such as a list.
round()
Returns a number rounded to n digits. If no n is provided it returns to zero decimal places and thus returns an integer. Be c
round(3.4) #returns 3
set()
Returns a set object — an unordered collection of unique items. Sets can return in random order. Similar to list, you can create sets with — instead of [] for lists.
my_set = 1,4,2,3,4,5
my_set #returns 1, 2, 3, 4, 5
As sets only contain unique items, you can use set() on a list to remove duplicates.
my_list = [1,4,2,3,4,5]
my_list #returns [1, 4, 2, 3, 4, 5]
set(my_list) #returns 1, 2, 3, 4, 5
sorted()
Returns a new sorted list from the items in an iterable.
my_list = [5,4,3,2,1]
sorted(my_list)# returns [1,2,3,4,5]
sum()
returns the sum of an iterable. You can also include a start value too, which will overwrite the set value of 0.
my_list = [5,4,3,2,1]
sum(my_list)
sum(my_list, start = 10)
tuple()
Tuples work in the exact same way as sets and lists and use regular brackets. They, in contrast to lists, are immutable.
my_tuple = (1,2,3,4,5)
my_tuple #returns (1,2,3,4,5)
my_list = [1,2,3,4,5]
tuple(my_list) (1,2,3,4,5)
type()
Simply returns the type of an object.
my_tuple = (1,2,3,4,5)
my_list = [1,2,3,4,5]
type(my_tuple)#returns tuple
type(my_list)#returns list
You can also use the isinstance() function to check whether an object is of a certain type
isinstance(my_list, list) # returns True
isinstance(my_list, tuple) # returns False
zip()
Zip() iterates over several items in paralell, producing tuples with items from each one.
for item in zip([1, 2, 3], ['JAmes', 'Tom', 'Jack]):
print(item)
#returns(1, 'James')
(2, 'Tom')
(3, 'Jack')
Conclusion
These functions should be the base of your Python knowledge. There are many more useful data science-related functions that can be imported, but you’d be surprised how many problems you can solve just using the functions above.
I hope this helps.
Cheers,
James
If I’ve inspired you to join medium I would be really grateful if you did it through this link — it will help to support me to write better content in the future. If you want to learn more about data science, become a certified data scientist, or land a job in data science, then check out 365 data science through my affiliate link.
Here are some more articles I’ve written.
[ad_2]
Source link