Python Essentials: 60 Frequently Used Built-in Functions and Techniques
This article presents a comprehensive collection of 60 frequently used Python built-in functions and techniques, covering numeric operations, string handling, data structures, function definitions, class utilities, and useful tools, each illustrated with clear code examples and explanations for practical programming.
This guide compiles 60 commonly used Python snippets that demonstrate essential built-in functions and techniques for everyday programming.
Numeric utilities include absolute value ( abs(-6)), base conversions ( bin(10), oct(9), hex(15)), integer-ASCII mapping ( chr(65), ord('A')), truth-value checks ( all([1,2,3]), any([0,1])), boolean casting ( bool([])), complex numbers ( complex(1,2)), division with remainder ( divmod(10,3)), type casting ( float(3), int('12',16)), exponentiation with modulus ( pow(3,2,4)), rounding ( round(10.0222222,3)), and chained comparisons ( 1 < i < 3).
String handling covers conversion to bytes ( bytes(s,encoding='utf-8')), generic string conversion ( str(i)), dynamic code execution ( compile(s,"<string>","exec"), exec(r)), evaluation ( eval('1 + 3 +5')), and formatting ( "i am {0},age{1}".format("tom",18)).
Function utilities demonstrate sorting ( sorted(a,reverse=True)), custom key sorting, summation with start value ( sum(a,10)), nonlocal variable updates, global declarations, swapping ( def swap(a,b): return b,a), function objects list indexing ( [f,g][1]()), reverse range generation ( list(range(10,-1,-1))), and parameter patterns ( def f(a,*b,c=10,**d): ...).
Data structures show dictionary creation ( dict(a='a',b='b'), dict(zip(['a','b'],[1,2]))), frozen sets ( frozenset([1,1,3,2,3])), mutable sets ( set(a)), slice objects ( slice(0,5,2)), tuples ( tuple(i_am_list)), and reusable slice objects for indexing.
Classes and objects illustrate callable checks ( callable(str)), custom __repr__, class methods, attribute access ( getattr(obj,'name')), dynamic attribute deletion ( delattr(obj,'id')), type inspection ( type(obj)), metaclass concepts, and property definitions using @property decorators.
Useful tools include enumeration ( enumerate(seq,1)), memory size measurement ( sys.getsizeof(dict)), filtering ( filter(lambda x: x>10, seq)), hashing ( hash(obj)), help documentation ( help(obj)), user input ( input()), file I/O ( open('a.txt','r')), range creation, reversed iteration ( reversed([1,2,3])), aggregation with zip, chain operations, and JSON serialization of custom objects ( json.dump([obj1,obj2], f, default=lambda o: o.__dict__)).
The article concludes with a QR code offering free Python learning resources.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
