avatarShu Hasegawa

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

4125

Abstract

be strings.</li></ol><p id="607a">However, a separator must be specified which will be used to connect the values in the specified iterable. This string will go before the function and be connected to it through a dot. Here is the full syntax:</p><p id="a7a9"><code><i>separator</i>.join(<i>iterable</i>)</code></p><p id="88bb">This function serves as an alternative to the traditional method of joining strings, which employs the use of loops and an accumulator (adding strings together concatenates/joins them). <code>join()</code>is also faster performance-wise.</p><p id="f2f7">Example:</p><div id="a556"><pre><span class="hljs-attr">sequence</span> = [<span class="hljs-string">'Ta'</span>, <span class="hljs-string">'n'</span>, <span class="hljs-string">'dr'</span>, <span class="hljs-string">'ew'</span>]</pre></div><div id="5871"><pre># joining a <span class="hljs-keyword">sequence</span> <span class="hljs-keyword">without</span> connecting them <span class="hljs-keyword">with</span> <span class="hljs-keyword">any</span> <span class="hljs-type">character</span>

<span class="hljs-keyword">using</span> an empty string <span class="hljs-keyword">to</span> <span class="hljs-keyword">connect</span> the <span class="hljs-keyword">sequence</span>

separator = <span class="hljs-string">''</span></pre></div><div id="690a"><pre><span class="hljs-comment"># output the joined string</span> <span class="hljs-built_in">print</span>(separator.join(sequence)) <span class="hljs-comment"># Output: Tandrew</span></pre></div><h1 id="30ca">3. The sum() function</h1><p id="244b">As its name suggests, the <code>sum()</code> function returns the sum of the values in an iterable (list, tuple, etc). This function takes in two parameters:</p><ol><li>The iterable containing the sequence the function will find the sum of.</li><li>An optional parameter, an extra value added to the final sum/return value.</li></ol><p id="a7ab">The <code>sum()</code> function is an excellent alternative to the traditional method of finding the sum of the values of an iterable, which involves using a loop in unison with an accumulator (a variable responsible for accumulating the values in a sequence).</p><p id="9614">Example:</p><div id="ca3f"><pre><span class="hljs-attr">sequence</span> = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]</pre></div><div id="7700"><pre># output <span class="hljs-built_in">sum</span> of <span class="hljs-keyword">sequence</span> <span class="hljs-built_in">print</span>(<span class="hljs-built_in">sum</span>(<span class="hljs-keyword">sequence</span>))

Output: <span class="hljs-number">6</span></pre></div><div id="eb1f"><pre><span class="hljs-comment"># output sum of sequence added to 10</span>

<span class="hljs-built_in">print</span>(<span class="hljs-built_in">sum</span>(sequence, 10)) <span class="hljs-comment"># Output: 16</span></pre></div><h1 id="8b08">4. The sorted() function</h1><p id="0ada">The <code>sorted()</code> function returns a sorted version of the specified iterable. This function can take in 3 parameters:</p><ol><li>The iterable that the function will be responsible for sorting.</li><li>An optional parameter is a “key” function that allows the user to modify the sorting process with the function's return value. For instance, if the user wants to sort a 2-dimensional list (list of lists) by the lengths of each list, they would specify the <code>len()</code> function. This function would be applied to every list inside the 2-dimensional list to acquire their lengths, and the program would sort these lists by comparing their lengths instead.</li><li>“Reverse”, an optional parameter that takes in a boolean. This parameter determines if the specified iterable would be sorted in an ascending or descending order. False, the default argument, results in sorting in ascending order. True will result in descending order.</li></ol><p id="6e00"><code>sorted()</code> utilizes Tim Sort, one of the fastest sorting algorithms. This makes it a suitable alternative to most sorting algorithms, on top of being cu

Options

stomizable and easy to implement in your code.</p><p id="9b67">Example:</p><div id="e4e4"><pre>sequence = <span class="hljs-comment">[3, 5, 4, 2, 1]</span> sequence2 = <span class="hljs-comment">[<span class="hljs-comment">[1, 2]</span>, <span class="hljs-comment">[3, 2, 4]</span>, <span class="hljs-comment">[1]</span>, <span class="hljs-comment">[1, 1, 2, 3]</span>]</span></pre></div><div id="19b6"><pre><span class="hljs-comment"># output sorted sequence</span> <span class="hljs-built_in">print</span>(<span class="hljs-built_in">sorted</span>(sequence)) <span class="hljs-comment"># Output: [1, 2, 3, 4, 5]</span></pre></div><div id="911d"><pre># <span class="hljs-built_in">output</span> sequence2 sorted by the lengths of its lists <span class="hljs-built_in">print</span>(sorted(sequence2, key=<span class="hljs-built_in">len</span>))

Output: <span class="hljs-string">[[1], [1, 2], [3, 2, 4], [1, 1, 2, 3]]</span></pre></div><div id="09e8"><pre><span class="hljs-comment"># output sequence sorted in descending/reversed order</span>

<span class="hljs-built_in">print</span>(sorted(sequence, <span class="hljs-attribute">reverse</span>=<span class="hljs-literal">True</span>)) <span class="hljs-comment"># Output: [5, 4, 3, 2, 1]</span></pre></div><h1 id="7e51">5. The map() function</h1><p id="ab47">The <code>map()</code> function allows the application of a function to every element of an iterable. This function takes in two parameters:</p><ol><li>A function that will be executed by passing the values obtained from the iterable into its parameters. The contents of the function are essentially identical to the code you would write under your standard for loop, which will process each data value in the referenced iterable.</li><li>The iterable that the map function will be looping through.</li></ol><p id="5d3f">The <code>map()</code> function is a great substitute for loops, being substantially faster and shorter.</p><p id="de33">Example:</p><div id="fda9"><pre>sequence = <span class="hljs-comment">[<span class="hljs-comment">[4, 2]</span>, <span class="hljs-comment">[3, 2, 4]</span>, <span class="hljs-comment">[1]</span>, <span class="hljs-comment">[1, 4, 2, 3]</span>]</span></pre></div><div id="112e"><pre><span class="hljs-comment"># sort the lists within the sequence in ascending order</span> <span class="hljs-comment"># map returns a map object and needs to be casted into a list</span> <span class="hljs-built_in">print</span>(<span class="hljs-built_in">list</span>(<span class="hljs-built_in">map</span>(<span class="hljs-built_in">sorted</span>, sequence))) <span class="hljs-comment"># Output: [[2, 4], [2, 3, 4], [1], [1, 2, 3, 4]]</span></pre></div><p id="8d5f">For more tips and tricks involving the <code>map() </code>function, visit this article:</p><div id="662c" class="link-block"> <a href="https://readmedium.com/3-easy-ways-to-instantly-make-your-python-program-faster-e599e920ea28"> <div> <div> <h2>3 Easy Ways to Instantly Make Your Python Program Faster</h2> <div><h3>3 Minor changes for major results</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*ikZcdX7L-OaSq5YP)"></div> </div> </div> </a> </div><h1 id="41e8">Conclusion</h1><p id="76ea">Python’s extensive library is a powerful tool that many programmers fail to fully exploit. Excessive blocks of code can often be substituted by one-liners, or modified to be shorter and more efficient. Applying the knowledge of built-in functions is necessary for efficient coding and is often what separates a proficient programmer from beginners and amateurs. Doing so will not only refine your code but also fundamentally impact your programming abilities.</p><p id="c8b4">Thank you for reading. Follow and subscribe to <a href="undefined">Shu Hasegawa</a> to get notified of new content that can help you get started on your programming career.</p></article></body>

Start Using These Python Functions Now

5 Python functions every programmer must know

Photo by James Harrison on Unsplash

Many beginners often find coding in Python to be complicated and time-consuming, when in reality, the language has an extremely simplified syntax compared to other programming languages. These beginners do not realize that the long segments of their code they wasted significant time on writing are usually replaceable with one-liners that perform faster and better. Even experienced and intermediate programmers miss out on many of Python’s built-in functions, costing them time and effort. To be successful in programming, an arsenal consisting of several of these functions is a necessity. Thus, here are 5 such functions every programmer should be using in their programs.

1. The eval() function

The eval() function is extremely useful due to its capability to evaluate any specified mathematical expression. This function can take in three parameters:

  1. The mathematical expression to be evaluated, in the form of a string.
  2. An optional parameter that takes in a dictionary that references global variables to eval(). For example, by referencing the character “x” to a global variable named “number”, you will be able to use “x” as a representation of the value assigned to “number” in the first parameter (the expression).
  3. An optional parameter that takes in a dictionary that allows the direct assignment to variables specified in the expression. For instance, if a variable “x” is used in the expression, you can reference “x” to a numerical value in a dictionary. In contrast to the second parameter which links to global variables outside of the function, this parameter is used to create new local variables inside the function.

The eval() function can solve mathematical expressions of any length in your code without using numerous if statements to check for varying operations in a complicated and time-consuming process.

Example:

# outputting the evaluation of the expression 1+3*5
print(eval('1+3*5'))
# Output: 16
# add the value of the global variable "number" to the expression in # the form of x (the second parameter)
number = 1
print(eval('1+3*5+x', {'x': number}))
# Output: 17
# creating a local variable named "x" and assigning it to 10
print(eval('1+3*5+x', {}, {'x': 10}))
# Output: 26

2. The join() function

The join() function returns a string created from joining the string elements of a given sequence. This function takes in one parameter only:

  1. The iterable specifying the strings to be joined by the function. All values of this iterable must be strings.

However, a separator must be specified which will be used to connect the values in the specified iterable. This string will go before the function and be connected to it through a dot. Here is the full syntax:

separator.join(iterable)

This function serves as an alternative to the traditional method of joining strings, which employs the use of loops and an accumulator (adding strings together concatenates/joins them). join()is also faster performance-wise.

Example:

sequence = ['Ta', 'n', 'dr', 'ew']
# joining a sequence without connecting them with any character
# using an empty string to connect the sequence
separator = ''
# output the joined string
print(separator.join(sequence))
# Output: Tandrew

3. The sum() function

As its name suggests, the sum() function returns the sum of the values in an iterable (list, tuple, etc). This function takes in two parameters:

  1. The iterable containing the sequence the function will find the sum of.
  2. An optional parameter, an extra value added to the final sum/return value.

The sum() function is an excellent alternative to the traditional method of finding the sum of the values of an iterable, which involves using a loop in unison with an accumulator (a variable responsible for accumulating the values in a sequence).

Example:

sequence = [1, 2, 3]
# output sum of sequence
print(sum(sequence))
# Output: 6
# output sum of sequence added to 10
print(sum(sequence, 10))
# Output: 16

4. The sorted() function

The sorted() function returns a sorted version of the specified iterable. This function can take in 3 parameters:

  1. The iterable that the function will be responsible for sorting.
  2. An optional parameter is a “key” function that allows the user to modify the sorting process with the function's return value. For instance, if the user wants to sort a 2-dimensional list (list of lists) by the lengths of each list, they would specify the len() function. This function would be applied to every list inside the 2-dimensional list to acquire their lengths, and the program would sort these lists by comparing their lengths instead.
  3. “Reverse”, an optional parameter that takes in a boolean. This parameter determines if the specified iterable would be sorted in an ascending or descending order. False, the default argument, results in sorting in ascending order. True will result in descending order.

sorted() utilizes Tim Sort, one of the fastest sorting algorithms. This makes it a suitable alternative to most sorting algorithms, on top of being customizable and easy to implement in your code.

Example:

sequence = [3, 5, 4, 2, 1]
sequence2 = [[1, 2], [3, 2, 4], [1], [1, 1, 2, 3]]
# output sorted sequence
print(sorted(sequence))
# Output: [1, 2, 3, 4, 5]
# output sequence2 sorted by the lengths of its lists
print(sorted(sequence2, key=len))
# Output: [[1], [1, 2], [3, 2, 4], [1, 1, 2, 3]]
# output sequence sorted in descending/reversed order
print(sorted(sequence, reverse=True))
# Output: [5, 4, 3, 2, 1]

5. The map() function

The map() function allows the application of a function to every element of an iterable. This function takes in two parameters:

  1. A function that will be executed by passing the values obtained from the iterable into its parameters. The contents of the function are essentially identical to the code you would write under your standard for loop, which will process each data value in the referenced iterable.
  2. The iterable that the map function will be looping through.

The map() function is a great substitute for loops, being substantially faster and shorter.

Example:

sequence = [[4, 2], [3, 2, 4], [1], [1, 4, 2, 3]]
# sort the lists within the sequence in ascending order
# map returns a map object and needs to be casted into a list
print(list(map(sorted, sequence)))
# Output: [[2, 4], [2, 3, 4], [1], [1, 2, 3, 4]]

For more tips and tricks involving the map() function, visit this article:

Conclusion

Python’s extensive library is a powerful tool that many programmers fail to fully exploit. Excessive blocks of code can often be substituted by one-liners, or modified to be shorter and more efficient. Applying the knowledge of built-in functions is necessary for efficient coding and is often what separates a proficient programmer from beginners and amateurs. Doing so will not only refine your code but also fundamentally impact your programming abilities.

Thank you for reading. Follow and subscribe to Shu Hasegawa to get notified of new content that can help you get started on your programming career.

Programming
Technology
Python
Self Improvement
Coding
Recommended from ReadMedium