Featured

Built-In Functions in Python Programming

Even without integrating an additional module in Python, you have access to a whole range of built-in functions.

 

This table contains an overview of just some of the built-in functions in Python.

 

Some Built-In Functions

 

The max(), min(), and sum() Functions

The built-in max() and min() functions return the largest or smallest value of an iterable. The sum() function returns the sum of the elements of an iterable and is called with only one parameter.

 

Let’s look at an example using all three functions.

 

print("Max. value of a tuple:", max(3, 2, -7))

print("Min. value of a set:", min(set([3, 2, 3])))

print("Sum of a tuple:", sum((3, 2, -7)))

 

The following output is generated:

 

Max. value of a tuple: 3

Min. value of a set: 2

Sum of a tuple: -2

 

In this case, the max() function is called with several parameters that form a tuple of values, the largest element of which is determined. The min() function is called for a set. The individual parameter of the sum() function is a tuple that must be created before the function call using parentheses.

 

The chr() and ord() Functions

The built-in chr() function returns the corresponding character for a Unicode number. Conversely, you can use the built-in ord() function to obtain the Unicode number for a character. Let’s consider an example.

 

for i in range(48,58):

   print(chr(i), end="")

print()

 

for i in range(65,91):

   print(chr(i), end="")

print()

 

for i in range(97,123):

   print(chr(i), end="")

print()

 

for z in "abcde":

   print(ord(z), end=" ")

print()

 

for z in "abcde":

   print(chr(ord(z)+1), end="")

 

The following output is generated:

 

0123456789

ABCDEFGHIJKLMNOPQRSTUVWXYZ

abcdefghijklmnopqrstuvwxyz

97 98 99 100 101

bcdef

 

Positions 48 to 57 in Unicode contain the digits 0 to 9. The uppercase and lowercase letters are in positions 65 to 90 and 97 to 122 in Unicode.

 

In the last part of this program, each character of a string is converted into the character following it in the code. This part could be an example of a simple encryption process.

 

The reversed() and sorted() Functions

The built-in reversed() function returns the elements of a sequence in reverse order. The built-in sorted() function is used to create and deliver a sorted list of the elements in a sequence.

 

In our next example, these two functions are applied to a tuple and the values view of a dictionary.

 

t = 4, 12, 6, -2

print("Tuple:", t)

print("Reversed: ", end="")

for i in reversed(t):

   print(i, end=" ")

print()

print("Sorted:", sorted(t))

print()

dc = {"Peter":31, "Judith":28, "William":35}

print("Dictionary:", dc)

va = dc.values()

print("Values view:", va)

print("Reversed: ", end="")

for i in reversed(va):

   print(i, end=" ")

print()

print("Sorted:", sorted(va))

 

The following output is generated:

 

Tuple: (4, 12, 6, -2)

Reversed: -2 6 12 4

Sorted: [-2, 4, 6, 12]

 

Dictionary: {'Peter': 31, 'Judith': 28, 'William': 35}

Values view: dict_values([31, 28, 35])

Reversed: 35 28 31

Sorted: [28, 31, 35]

 

The reversed() function returns a sequence that contains the elements in reverse order. They can be output using a for loop. In the case of the reversed tuple, it is an object of the reversed type; in the case of the dictionary’s values view, it is an object of the dict_reversevalueiterator type.

 

The sorted() function returns a list containing the elements of the sequence in sorted order. Numbers are sorted in ascending order by value; characters are sorted in ascending order by code number.

 

Editor’s note: This post has been adapted from a section of the book Getting Started with Python by Thomas Theis.

Recommendation

Getting Started with Python
Getting Started with Python

If you want to program with Python, you’ve come to the right place! Take your first steps with this Python crash course that teaches you to use core language elements, from variables to branches to loops. Follow expert guidance to work with data types, functions, and modules—and learn how to manage errors and exceptions along the way. Apply Python programming to develop databases, graphical user interfaces, widgets, and more. Practice your skills with example exercises, and start developing your own applications with Python today!

Learn More
Rheinwerk Computing
by Rheinwerk Computing

Rheinwerk Computing is an imprint of Rheinwerk Publishing and publishes books by leading experts in the fields of programming, administration, security, analytics, and more.

Comments