-
[Python tutorial] 19. Built-in Function코딩/Python 2022. 12. 21. 17:18728x90
abs(n)
Returns the absolute value of a number
x = abs(-7.25)
all(iterable)
Returns True if all items in an iterable object are true
mylist = [True, True, True] x = all(mylist)
any(iterable)
Returns True if any item in an iterable object is true
mylist = [False, True, False] x = any(mylist)
ascii(object)
Returns a readable version of an object. Replaces none-ascii characters with escape character
x = ascii("My name is Ståle")
bin(n)
Returns the binary version of a number
x = bin(36)
bool(object)
Returns the boolean value of the specified object
x = bool(1)
bytearray(x, encoding, error)
Returns an array of bytes
x = bytearray(4)
bytes(x, encoding, error)
Returns a bytes object
x = bytes(4)
callable(object)
Returns True if the specified object is callable, otherwise False
def x(): a = 5 print(callable(x))
chr(number)
Returns a character from the specified Unicode code.
x = chr(97)
classmethod()
Converts a method into a class method
compile(source, filename, mode, flag, dont_inherit, optimize)
Returns the specified source as an object, ready to be executed
x = compile('print(55)', 'test', 'eval') exec(x)
complex(real, imaginary)
Returns a complex number
x = complex(3, 5)
delattr(object, attribute)
Deletes the specified attribute (property or method) from the specified object
class Person: name = "John" age = 36 country = "Norway" delattr(Person, 'age')
dict(keyword arguments)
Returns a dictionary (Array)
x = dict(name = "John", age = 36, country = "Norway")
dir(object)
Returns a list of the specified object's properties and methods
class Person: name = "John" age = 36 country = "Norway" print(dir(Person))
divmod(dividend, divisor)
Returns the quotient and the remainder when argument1 is divided by argument2
x = divmod(5, 2)
enumerate(iterable, start)
Takes a collection (e.g. a tuple) and returns it as an enumerate object
x = ('apple', 'banana', 'cherry') y = enumerate(x)
eval(expression, globals, locals)
Evaluates and executes an expression
x = 'print(55)' eval(x)
exec(object, globals, locals)
Executes the specified code (or object)
x = 'name = "John"\nprint(name)' exec(x)
filter(function, iterable)
Use a filter function to exclude items in an iterable object
ages = [5, 12, 17, 18, 24, 32] def myFunc(x): if x < 18: return False else: return True adults = filter(myFunc, ages) for x in adults: print(x)
float(value)
Returns a floating point number
x = float(3)
format(value, format)
Formats a specified value
x = format(0.5, '%')
frozenset(iterable)
Returns a frozenset object
mylist = ['apple', 'banana', 'cherry'] x = frozenset(mylist)
getattr(object, attribute, default)
Returns the value of the specified attribute (property or method)
class Person: name = "John" age = 36 country = "Norway" x = getattr(Person, 'age')
globals()
Returns the current global symbol table as a dictionary
x = globals() print(x)
hasattr(object, attribute)
Returns True if the specified object has the specified attribute (property/method)
class Person: name = "John" age = 36 country = "Norway" x = hasattr(Person, 'age')
hash()
Returns the hash value of a specified object
help()
Executes the built-in help system
hex(number)
Converts a number into a hexadecimal value
x = hex(255)
id(object)
Returns the id of an object
x = ('apple', 'banana', 'cherry') y = id(x)
input(prompt)
Allowing user input
print('Enter your name:') x = input() print('Hello, ' + x)
int(value, base)
Returns an integer number
x = int(3.5)
isinstance(object, type)
Returns True if a specified object is an instance of a specified object
x = isinstance(5, int)
issubclass(object, subclass)
Returns True if a specified class is a subclass of a specified object
class myAge: age = 36 class myObj(myAge): name = "John" age = myAge x = issubclass(myObj, myAge)
iter(object, sentinel)
Returns an iterator object
x = iter(["apple", "banana", "cherry"]) print(next(x)) print(next(x)) print(next(x))
len(object)
Returns the length of an object
mylist = ["apple", "banana", "cherry"] x = len(mylist)
list(iterable)
Returns a list
x = list(('apple', 'banana', 'cherry'))
locals()
Returns an updated dictionary of the current local symbol table
x = locals() print(x)
map(function, iterables)
Returns the specified iterator with the specified function applied to each item
def myfunc(n): return len(n) x = map(myfunc, ('apple', 'banana', 'cherry'))
max(n1, n2, n3, ...)
Returns the largest item in an iterable
x = max(5, 10)
memoryview(obj)
Returns a memory view object
x = memoryview(b"Hello") print(x) #return the Unicode of the first character print(x[0]) #return the Unicode of the second character print(x[1])
min(n1, n2, n3, ...)
Returns the smallest item in an iterable
x = min(5, 10)
next(iterable, default)
Returns the next item in an iterable
mylist = iter(["apple", "banana", "cherry"]) x = next(mylist) print(x) x = next(mylist) print(x) x = next(mylist) print(x)
object()
Returns a new object
x = object()
oct(int)
Converts a number into an octal
x = oct(12)
open(file, mode)
Opens a file and returns a file object
f = open("demofile.txt", "r") print(f.read())
ord(character)
Convert an integer representing the Unicode of the specified character
x = ord("h")
pow(x, y, z)
Returns the value of x to the power of y
x = pow(4, 3)
print(object(s), sep=separator, end=end, file=file, flush=flush)
Prints to the standard output device
print("Hello World")
property()
Gets, sets, deletes a property
range(start, stop, step)
Returns a sequence of numbers, starting from 0 and increments by 1 (by default)
x = range(6) for n in x: print(n)
repr()
Returns a readable version of an object
reversed(sequence)
Returns a reversed iterator
alph = ["a", "b", "c", "d"] ralph = reversed(alph) for x in ralph: print(x)
round(number, digits)
Rounds a numbers
x = round(5.76543, 2) print(x)
set(iterable)
Returns a new set object
x = set(('apple', 'banana', 'cherry'))
setattr(object, attribute, value)
Sets an attribute (property/method) of an object
class Person: name = "John" age = 36 country = "Norway" setattr(Person, 'age', 40)
slice(start, end, step)
Returns a slice object
a = ("a", "b", "c", "d", "e", "f", "g", "h") x = slice(2) print(a[x])
sorted(iterable, key=key, reverse=reverse)
Returns a sorted list
a = ("b", "g", "a", "d", "f", "c", "h", "e") x = sorted(a) print(x)
staticmethod()
Converts a method into a static method
str(object, encoding=encoding, errors=errors)
Returns a string object
x = str(3.5)
sum(iterable, start)
Sums the items of an iterator
a = (1, 2, 3, 4, 5) x = sum(a)
super()
Returns an object that represents the parent class
class Parent: def __init__(self, txt): self.message = txt def printmessage(self): print(self.message) class Child(Parent): def __init__(self, txt): super().__init__(txt) x = Child("Hello, and welcome!") x.printmessage()
tuple(iterable)
Returns a tuple
x = tuple(('apple', 'banana', 'cherry'))
type(object, bases, dict)
Returns the type of an object
a = ('apple', 'banana', 'cherry') b = "Hello World" c = 33 x = type(a) y = type(b) z = type(c)
vars(object)
Returns the
__dict__
property of an objectclass Person: name = "John" age = 36 country = "norway" x = vars(Person)
zip(iterator1, iterator2, iterator3 ...)
Returns an iterator, from two or more iterators
a = ("John", "Charles", "Mike") b = ("Jenny", "Christy", "Monica") x = zip(a, b)
728x90'코딩 > Python' 카테고리의 다른 글
[Python tutorial] 21. List/Array methods (0) 2022.12.21 [Python tutorial] 20. String methods (1) 2022.12.21 [Python tutorial] 18. File handling (0) 2022.12.21 [Python tutorial] 17. Try...Except (0) 2022.12.21 [Python tutorial] 16. Classes, Objects and Inheritance (1) 2022.12.21