Download presentation
Presentation is loading. Please wait.
1
Python Useful Functions and Methods
Peter Wad Sackett
2
Nothing more to learn ? There are quite a few built-in functions not mentioned yet, see So far has been mentioned 14 out of 68: dict(), float(), format(), input(), int(), len(), list(), open(), print(), range(), reversed(), set(), sorted() str() & tuple() Many of the unmentioned functions works with python’s object model or the inner workings of python. Useful for advanced python code.
3
Nice to know built-in functions
The following are nice to know for a beginner: abs(number) returns absolute value of a number dir(object) returns info on an object enumerate(iterable) returns an enumerated list of tuples help(string) returns help in the subject given id(object) returns the id of an object, guaranteed to be unique iter(iterable) returns an iterable – dicts as key/value tuples map(function, iterable) the function is applied on the iterable max(iterable) returns the maximum value min(iterable) returns the minimum value round(number) rounds a number sum(iterable) returns the sum of the iterable zip(iterables) returns a list of tubles of the input in ”pairs”
4
Built-in data types A useful sequence method:
sequencetype.count(item) returns the number of occurences New data types: bytearray and frozenset Bytearrays are like mutable strings and supports all string methods. They are more efficient when your strings change. name = bytearray(’Peter’) Frozensets are immutable sets and supports all set methods, that does not change the frozenset, obviously. Can be used as keys in dicts and sets. Frozensets are to sets, what tuples are to lists. fruits = frozenset(’apple’, ’banana’, ’cherry’, ’date’)
5
Standard libraries Python has many standard libraries, far too many to memorize. Each library contains several functions, all relevant and appropiate to the core functionality of the library. You won’t find string functions in the math library. Learn to find what you need from the list. In the course the following standard libraries have been used: string – string operations re – regular expressions math – mathmatical functions os – operating system interfaces sys – system specific functions Some new ones which are surprisingly often useful: random – pseudo random numbers copy – shallow and deep copy operations
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.