Fundamentals 9 min read

Master Python’s Built‑In Functions: From range to zip

This article introduces Python’s most useful built‑in functions—including range, mathematical utilities, base‑conversion, string methods, and iterable helpers—explaining their parameters, showing practical examples, and demonstrating how they simplify common programming tasks.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python’s Built‑In Functions: From range to zip

Functions are organized, reusable code blocks that perform a single or related task. Python provides many built‑in functions, which this article introduces.

1. range() function

Creates an immutable sequence of numbers. It takes up to three integer arguments: start (default 0), stop, and step (default 1).

Example: range(4) yields [0, 1, 2, 3]; range(1, 10, 2) yields [1, 3, 5, 7, 9].

print(list(range(4)))  # [0, 1, 2, 3]
print(list(range(1, 10, 2)))  # [1, 3, 5, 7, 9]

range can be used with a for loop to repeat an operation.

for i in range(3):
    print("Hello!")

2. Common mathematical functions

abs(x)

: returns the absolute value. divmod(a, b): returns a tuple (quotient, remainder). round(x): rounds to the nearest integer. pow(a, b, mod=None): computes a**b, optionally modulo mod. sum(iterable): returns the sum of elements. min(...) and max(...): return the smallest or largest value.

print(abs(-5))          # 5
print(divmod(13, 2))    # (6, 1)
print(round(3.5))        # 4
print(pow(5, 2, 3))     # 1
print(sum([1,2,3,4,5,6,7,8,9,10]))  # 55
print(min(3,2,5,19,6,3)) # 2
print(max(3,5,12,8,5,11)) # 12

3. Base‑conversion functions

bin(x)

: returns binary string prefixed with “0b”. oct(x): returns octal string prefixed with “0o”. hex(x): returns hexadecimal string prefixed with “0x”.

print(bin(10))   # 0b1010
print(hex(10))   # 0xa
print(oct(10))   # 0o12

4. String‑related functions

lower()

: converts all characters to lowercase. upper(): converts all characters to uppercase. count(sub, start=0, end=None): counts occurrences of sub. startswith(prefix) / endswith(suffix): test string boundaries. find(sub, start=0, end=None): returns index or -1. index(sub): like find but raises ValueError if not found. replace(old, new, count=-1): replaces occurrences. strip(chars=None): removes leading/trailing whitespace or specified chars.

str1 = "aaBBccEEff"
print(str1.lower())   # aabbcceeff
print(str1.upper())   # AABBCCEEFF

s = "aabbaaccdddb"
print(s.count("a"))          # 4
print(s.count("a", 3))       # 2
print(s.count("a", 0, 3))    # 2

s2 = "abcd"
print(s2.startswith("a"))    # True
print(s2.endswith("d"))      # True

print(s2.find("b"))         # 1
print(s2.index("c"))        # 2

print(s2.replace("a","W"))  # WbcdWbcdWbcd
print("  abc  ".strip())     # abc
print("xxyyyzzzx".strip("x")) # yyyzzz

5. Iterable‑related functions

len(obj)

: returns number of items. sort() / sorted(iterable, reverse=False): sort items. zip(*iterables): aggregates elements from each iterable into tuples. filter(func, iterable): yields items where func(item) is true. map(func, iterable): applies func to each item.

lst = [5,7,6,12,1,13,9,18,5]
lst.sort()
print(lst)  # [1,5,5,6,7,9,12,13,18]

print(sorted(lst, reverse=True))  # [18,13,12,9,7,6,5,5,1]

a = ['a','b','c']
b = ['1','2','3']
c = ['one','two','three']
for item in zip(a, b, c):
    print(item)  # ('a','1','one') ...

def is_odd(i):
    return i % 2 == 1
print(list(filter(is_odd, range(1,10))))  # [1,3,5,7,9]

def identity(i):
    return i
print(list(map(identity, [1,2,3])))  # [1,2,3]
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythonprogrammingfundamentalsbuilt-in functions
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.