avatarJames Asher

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

6635

Abstract

n> <span class="hljs-number">12345</span></pre></div><h2 id="cb72">len()</h2><p id="37b5">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.</p><div id="b8e7"><pre>my_dict = <span class="hljs-built_in">dict</span>({<span class="hljs-string">"name"</span> : <span class="hljs-selector-attr">[<span class="hljs-string">"james"</span>, <span class="hljs-string">"Tom"</span>]</span>, <span class="hljs-string">"age"</span>: <span class="hljs-selector-attr">[27,35]</span>}) my_str = <span class="hljs-string">"james"</span> my_list = <span class="hljs-selector-attr">[1,2,3,4,5]</span> <span class="hljs-function"><span class="hljs-title">len</span><span class="hljs-params">(my_dict)</span></span><span class="hljs-selector-id">#returns</span> <span class="hljs-number">2</span> <span class="hljs-function"><span class="hljs-title">len</span><span class="hljs-params">(my_str)</span></span> <span class="hljs-selector-id">#returns</span> <span class="hljs-number">5</span> <span class="hljs-function"><span class="hljs-title">len</span><span class="hljs-params">(my_list)</span></span><span class="hljs-selector-id">#returns</span> <span class="hljs-number">5</span> </pre></div><h2 id="0360">list()</h2><p id="a471">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</p><div id="d325"><pre><span class="hljs-attr">my_list</span> = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]</pre></div><p id="854a">but, you can use list() to separate out items into a list such as</p><div id="becf"><pre>my_str = <span class="hljs-string">"james"</span> <span class="hljs-function"><span class="hljs-title">list</span><span class="hljs-params">(my_str)</span></span> <span class="hljs-selector-id">#returns</span> <span class="hljs-selector-attr">[<span class="hljs-string">'j'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'m'</span>, <span class="hljs-string">'e'</span>, <span class="hljs-string">'s'</span>]</span></pre></div><h2 id="f0fa">map()</h2><p id="d35b">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.</p><div id="94bc"><pre>my_list = <span class="hljs-selector-attr">[1.3,4.5, 5.9]</span> <span class="hljs-function"><span class="hljs-title">list</span><span class="hljs-params">(map(round, my_list)</span></span>) <span class="hljs-selector-id">#returns</span> <span class="hljs-selector-attr">[1,4,6]</span></pre></div><h2 id="ad42">max() and min()</h2><p id="fccb">max() and min() return the maximum and minimum value of an iterable respectively.</p><div id="42f1"><pre><span class="hljs-attribute">max</span>(my_list) # returns <span class="hljs-number">5</span>.<span class="hljs-number">9</span> <span class="hljs-attribute">min</span>(my_list) # returns <span class="hljs-number">1</span>.<span class="hljs-number">3</span></pre></div><h2 id="2c6f">range()</h2><p id="8998">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).</p><div id="ebaf"><pre><span class="hljs-attribute">for</span> i in range(<span class="hljs-number">5</span>,<span class="hljs-number">20</span>,<span class="hljs-number">5</span>): <span class="hljs-attribute">print</span>(i) <span class="hljs-comment">#returns 5 10, 15</span></pre></div><div id="9f94"><pre><span class="hljs-keyword">for</span> <span class="hljs-selector-tag">i</span> <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">5</span>): <span class="hljs-built_in">print</span>(i) <span class="hljs-selector-id">#returns</span> <span class="hljs-number">0</span> <span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span> <span class="hljs-number">4</span></pre></div><p id="ae83">This is quite commonly used in conjunction with length to iterate over a range of a length of a variable such as a list.</p><h2 id="1490">round()</h2><p id="c32f">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</p><div id="624d"><pre><span class="hljs-function"><span class="hljs-title">round</span><span class="hljs-params">(<span class="hljs-number">3.4</span>)</span></span> <span class="hljs-selector-id">#returns</span> <span class="hljs-number">3</span></pre></div><h2 id="0903">set()</h2><p id="2bd7">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.</p><div id="66ef"><pre><span class="hljs-attribute">my_set</span> = {<span class="hljs-number">1</span>,<span class="hljs-number">4</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>} <span class="hljs-attribute">my_set</span> #returns {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>}</pre></div><p id="3736">As sets only contain unique items, you can use set() on a list to remove duplicates.</p><div id="8c62"><pre><span class="hljs-attribute">my_list</span> =<span class="hljs-meta"> [1,4,2,3,4,5]</span> <span class="hljs-attribute">my_list</span> #returns<span class="hljs-meta"> [1, 4, 2, 3, 4, 5]</span> <span class="hljs-attribute">set</span>(my_list) #returns {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>}</pre></div><h2 id="b1e9">sorted()</h2><p id="0c50">Returns a new sorted list from the items in an iterable.</p><div id="e693"><pre>my_list = <span class="hljs-string">[5,4,3,2,1]</span> sorted(my_list)# returns <span

Options

class="hljs-string">[1,2,3,4,5]</span></pre></div><h2 id="6bc2">sum()</h2><p id="b1d2">returns the sum of an iterable. You can also include a start value too, which will overwrite the set value of 0.</p><div id="2e80"><pre>my_list = <span class="hljs-selector-attr">[5,4,3,2,1]</span> <span class="hljs-function"><span class="hljs-title">sum</span><span class="hljs-params">(my_list)</span></span> <span class="hljs-function"><span class="hljs-title">sum</span><span class="hljs-params">(my_list, start = <span class="hljs-number">10</span>)</span></span></pre></div><h2 id="40eb">tuple()</h2><p id="ee1e">Tuples work in the exact same way as sets and lists and use regular brackets. They, in contrast to lists, are immutable.</p><div id="1d8a"><pre><span class="hljs-attribute">my_tuple</span> = (<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>) <span class="hljs-attribute">my_tuple</span> #returns (<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>) <span class="hljs-attribute">my_list</span> =<span class="hljs-meta"> [1,2,3,4,5]</span> <span class="hljs-attribute">tuple</span>(my_list) (<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>)</pre></div><h2 id="021a">type()</h2><p id="55f9">Simply returns the type of an object.</p><div id="c8c3"><pre>my_tuple = (<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>) my_list = <span class="hljs-selector-attr">[1,2,3,4,5]</span> <span class="hljs-function"><span class="hljs-title">type</span><span class="hljs-params">(my_tuple)</span></span><span class="hljs-selector-id">#returns</span> tuple <span class="hljs-function"><span class="hljs-title">type</span><span class="hljs-params">(my_list)</span></span><span class="hljs-selector-id">#returns</span> list</pre></div><p id="b962">You can also use the isinstance() function to check whether an object is of a certain type</p><div id="9739"><pre><span class="hljs-built_in">isinstance</span>(my_list, <span class="hljs-built_in">list</span>) <span class="hljs-comment"># returns True</span> <span class="hljs-built_in">isinstance</span>(my_list, <span class="hljs-built_in">tuple</span>) <span class="hljs-comment"># returns False</span></pre></div><h2 id="642f">zip()</h2><p id="bd3c">Zip() iterates over several items in paralell, producing tuples with items from each one.</p><div id="678c"><pre><span class="hljs-keyword">for</span> <span class="hljs-variable">item</span> <span class="hljs-keyword">in</span> <span class="hljs-title function_ invoke__">zip</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], [<span class="hljs-symbol">'JAmes</span>', <span class="hljs-symbol">'Tom</span>', <span class="hljs-symbol">'Jack</span>]): <span class="hljs-title function_ invoke__">print</span>(item) #returns</pre></div><div id="2b75"><pre>(<span class="hljs-name">1</span>, <span class="hljs-symbol">'James</span>') (<span class="hljs-name">2</span>, <span class="hljs-symbol">'Tom</span>') (<span class="hljs-name">3</span>, <span class="hljs-symbol">'Jack</span>')</pre></div><h2 id="fe68">Conclusion</h2><p id="d31d">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.</p><p id="0e9c">I hope this helps.</p><p id="17b7">Cheers,</p><p id="b0ca">James</p><p id="95dd">If I’ve inspired you to join medium I would be really grateful if you did it through this <a href="https://jamesasher4994.medium.com/membership">link</a> — 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 <a href="https://365datascience.pxf.io/c/3458822/791349/11148">365 data science</a> through my <a href="https://365datascience.pxf.io/c/3458822/791349/11148">affiliate link.</a></p><p id="9c4a">Here are some more articles I’ve written.</p><div id="89d8" class="link-block"> <a href="https://towardsdatascience.com/econometrics-is-the-original-data-science-6725d3f0d843"> <div> <div> <h2>Econometrics Is The Original Data Science</h2> <div><h3>This is why you should know more about it</h3></div> <div><p>towardsdatascience.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*u7cAMgSgiXb0jI6TWR2x1g.jpeg)"></div> </div> </div> </a> </div><div id="344c" class="link-block"> <a href="https://towardsdatascience.com/how-to-easily-show-your-matplotlib-plots-and-pandas-dataframes-dynamically-on-your-website-a9613eff7ae3"> <div> <div> <h2>How to easily show your Matplotlib plots and Pandas dataframes dynamically on your website.</h2> <div><h3>A surprisingly easy approach to showcasing your plots and dataframes online for the whole world to see — in less than…</h3></div> <div><p>towardsdatascience.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*C1If2cnsug8oIXhe)"></div> </div> </div> </a> </div><div id="585b" class="link-block"> <a href="https://towardsdatascience.com/how-to-easily-run-python-scripts-on-website-inputs-d5167bd4eb4b"> <div> <div> <h2>How to Easily Run Python Scripts on Website Inputs</h2> <div><h3>Here’s a walkthrough of a website I built that will analyze text sentiment on the fly</h3></div> <div><p>towardsdatascience.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*2XqUTGmxsXjqqp5RpVTo3Q.jpeg)"></div> </div> </div> </a> </div></article></body>

Here Are The Most Useful Built-in Python Functions For Data Science

All Functions are equal, but for data science, some are more equal than others

Photo by David Clode on Unsplash

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.242
z = 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, 15
for 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.

Artificial Intelligence
Machine Learning
Python
Data Science
Programming
Recommended from ReadMedium