Some Built-in Functions
Python comes with many built-in functions, for this section we will focus on the following functions:
len()- Finding Lengthmin()andmax()- Finding Minimum and Maximumsum()- Adding Numbersabs()- Absolute Valuepow()- Power Functionround()- Rounding Numberschr()andord()- Character and Unicode Conversionid()- 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))) # 8min() 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)) # Hprint(max(text)) # o
text = "python"print(min(text)) # hprint(max(text)) # y
letters = "ABCabc"print(min(letters)) # Aprint(max(letters)) # c
numbers = range(5, 15)print(min(numbers)) # 5print(max(numbers)) # 14sum() - 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))) # 30Iterables 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)) # 5print(abs(-5)) # 5
print(abs(3 + 4j)) # 5.0pow() - Power Function
Raises a number to a power. pow(x, y) returns x raised to the power of y.
print(pow(2, 3)) # 8print(pow(5, 2)) # 25It 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)) # 3print(pow(5, 2, 3)) # 1round() - Rounding Numbers
Rounds a number to the nearest integer or to a specified number of decimal places.
print(round(3.7)) # 4print(round(3.2)) # 3When a second argument is provided, round() rounds to that many decimal places:
print(round(3.14159, 2)) # 3.14print(round(2.71828, 3)) # 2.718chr() 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')) # 65print(ord('a')) # 97print(ord('0')) # 48print(ord(' ')) # 32
print(chr(65)) # Aprint(chr(97)) # aprint(chr(48)) # 0print(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 = 5y = 5print(id(x))print(id(y))print(id(x) == id(y)) # True
a = 123456789123456789123456789b = 123456789123456789123456789print(a == b) # Trueprint(id(a) == id(b)) # Falsea = bprint(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)) # FalseExercises
Exercise 1: Understanding len()
What will this code output?
text = "Python Programming"print(len(text))The output will be: 18
Exercise 2: len() with range
What will this code output?
print(len(range(0, 15, 3)))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))The output will be:
HrExercise 4: sum() with range
What will this code output?
numbers = range(1, 11)print(sum(numbers))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))The output will be:
10105.02.0Exercise 6: pow() function
What will this code output?
print(pow(3, 4))print(pow(2, 7, 5))The output will be:
813Exercise 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))The output will be:
543.143.142Exercise 8: ord() and chr()
What will this code output?
code = ord('Z')print(code)print(chr(code - 2))The output will be:
90Xord('Z') returns the Unicode code point for ‘Z’, which is 90. chr(90 - 2) converts that code point back to the character ‘X’.
