Some Built-in Functions

Learn about some built-in functions that are useful for loops and iteration.

Ali Berro

By Ali Berro

5 min read Section 7
From: Python Fundamentals: From Zero to Hero

Some Built-in Functions

Python comes with many built-in functions, for this section we will focus on the following functions:

  • len() - Finding Length
  • min() and max() - Finding Minimum and Maximum
  • sum() - Adding Numbers
  • abs() - Absolute Value
  • pow() - Power Function
  • round() - Rounding Numbers
  • chr() and ord() - Character and Unicode Conversion
  • id() - Object Identity

len() - Finding Length

Returns the number of items in a sequence.

text = "Hello"
print(len(text)) # 5
empty = ""
print(len(empty)) #0
print(len(range(5))) # 5
print(len(range(2, 10))) # 8

min() and max() - Finding Minimum and Maximum

The min() function returns the smallest item from a sequence. The max() function returns the largest item.

With Strings

When used with strings, min() and max() compare characters based on their Unicode (ASCII) values. Letters are ordered alphabetically, with uppercase letters having smaller values than lowercase letters.

text = "Hello"
print(min(text)) # H
print(max(text)) # o
text = "python"
print(min(text)) # h
print(max(text)) # y
letters = "ABCabc"
print(min(letters)) # A
print(max(letters)) # c
numbers = range(5, 15)
print(min(numbers)) # 5
print(max(numbers)) # 14

sum() - Adding Numbers

Adds all numbers in an iterable (like a range) and returns the total.

print(sum(range(1, 6)))
print(sum(range(0, 11, 2))) # 30

Iterables will be discussed in the next section. The sum() function only works with numbers. It cannot be used with strings directly.

abs() - Absolute Value

Returns the absolute value of a number. The absolute value is the distance from zero, so it’s always positive (or zero). For complex numbers, the absolute value is the magnitude of the complex number.

print(abs(5)) # 5
print(abs(-5)) # 5
print(abs(3 + 4j)) # 5.0

pow() - Power Function

Raises a number to a power. pow(x, y) returns x raised to the power of y.

print(pow(2, 3)) # 8
print(pow(5, 2)) # 25

It also takes an optional third argument for modular exponentiation pow(x, y, z). Which means that it returns x raised to the power of y modulo z. pow(x, y, z) is equivalent to (x ** y) % z, but it is more efficient.

print(pow(2, 3, 5)) # 3
print(pow(5, 2, 3)) # 1

round() - Rounding Numbers

Rounds a number to the nearest integer or to a specified number of decimal places.

print(round(3.7)) # 4
print(round(3.2)) # 3

When a second argument is provided, round() rounds to that many decimal places:

print(round(3.14159, 2)) # 3.14
print(round(2.71828, 3)) # 2.718

chr() and ord() - Character and Unicode Conversion

The ord() function returns the Unicode code point (an integer) of a single character. The chr() function does the reverse, it returns the character represented by a Unicode code point.

print(ord('A')) # 65
print(ord('a')) # 97
print(ord('0')) # 48
print(ord(' ')) # 32
print(chr(65)) # A
print(chr(97)) # a
print(chr(48)) # 0
print(chr(32)) # (space character)

id() - Object Identity

Returns the unique identifier (memory address) of an object. This is useful for understanding if two variables refer to the same object in memory.

For immutable types like int, float, str, tuple, bool, and None Python often interns or caches small or frequently used objects to save memory, especially for small integers. This means two “equal” immutable objects might actually share the same memory address (and thus the same id()).

x = 5
y = 5
print(id(x))
print(id(y))
print(id(x) == id(y)) # True
a = 123456789123456789123456789
b = 123456789123456789123456789
print(a == b) # True
print(id(a) == id(b)) # False
a = b
print(id(a) == id(b)) # True
text1 = "Hello"
text2 = "Hello"
print(id(text1) == id(text2)) # True
x = "Hello worlddddddddddddddddddddddddddddddddddddddddddddddddddd"
y = "Hello worlddddddddddddddddddddddddddddddddddddddddddddddddddd"
print(id(x) == id(y)) # False

Exercises

Exercise 1: Understanding len()

What will this code output?

text = "Python Programming"
print(len(text))
Answer:

The output will be: 18

Exercise 2: len() with range

What will this code output?

print(len(range(0, 15, 3)))
Answer:

The output will be: 5

Exercise 3: min() and max() with strings

What will this code output?

text = "Hello World"
print(min(text))
print(max(text))
Answer:

The output will be:

H
r

Exercise 4: sum() with range

What will this code output?

numbers = range(1, 11)
print(sum(numbers))
Answer:

The output will be: 55

Exercise 5: abs() function

What will this code output?

print(abs(-10))
print(abs(10))
print(abs(3 + 4j))
print(abs(-2j))
Answer:

The output will be:

10
10
5.0
2.0

Exercise 6: pow() function

What will this code output?

print(pow(3, 4))
print(pow(2, 7, 5))
Answer:

The output will be:

81
3

Exercise 7: round() function

What will this code output?

print(round(4.6))
print(round(4.4))
print(round(3.14159, 2))
print(round(3.14159, 3))
Answer:

The output will be:

5
4
3.14
3.142

Exercise 8: ord() and chr()

What will this code output?

code = ord('Z')
print(code)
print(chr(code - 2))
Answer:

The output will be:

90
X

ord('Z') returns the Unicode code point for ‘Z’, which is 90. chr(90 - 2) converts that code point back to the character ‘X’.

Course Progress

Section 14 of 17

Back to Course