Built-In Methods Flashcards

1
Q

abs(x)

A

abs(x)
accepts an integer or a float
returns the absolute value of that number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

all()

A

accepts an iterable

returns true if ALL elements in the iterable are True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

any()

A

accepts an iterable

returns true if ANY element in the iterable is True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

ascii()

A

accepts any object (strings, Tuples, lists)

returns a readable version of object and replaces any non-ascii chars with escaped chars

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

bool()

A

accepts an object

returns a boolean value after standard truth-testing procedure

This always returns true unless…
object is empty like: [] {} ()
object is False
object is zero
object is None

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

callable()

A

accepts an Object

returns True if X is callable

for example, built-in functions and classes are both callable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

delattr()

A

delattr(object, str)

deletes a property from an object, does not return anything.

string must be one of the objects attributes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

dict()

A

Accepts keyword arguments
Returns a new python dictionary

example:
dict(name = “John”, age = 36, country = “Norway”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

enumerate()

A

enumerate(iterable, start=0)

start = the index value from which the counter is to be started

returns an enumerate object with index/element pairsfromat

example:
seasons = [‘spring’, ‘summer’, ‘fall’, ‘winter’]
list(enumerate(seasons))
// [(0, ‘spring’), (1, ‘summer’), (2, ‘fall’), (3, ‘winter’)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

filter()

A

filter(truth-test function, iterable)

returns the elements in the iterable that return true in the filter function

use list() to view results

example:
seasons = [‘spring’, ‘summer’, ‘fall’, ‘winter’]
def filter_func(item):
return len(item) == 4

list(filter(filter_func, seasons)
// [‘fall’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

eval()

A

eval(string)

evaluates the specified expression

eval(“3+1”) = 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

exec()

A

exec(object)
executes the specified Python code

NOTE: eval() can only accept a single expression
Whereas exec() can accept large blocks of code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

float()

A

accepts a number or a string

returns a floating-point number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

getattr()

A

getattr(object, attribute, default)

returns object[attribute] if attribute is available
or default value if provided
otherwise, returns AttributeError

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

hasattr()

A

hasattr(object, attribute)

checks if object has the given named attribute

returns True / False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

hash()

A

hash(object)

returns hashed value if possible

17
Q

int()

A

int(number or string)

returns specified value in integer format

it

18
Q

len()

A

len(object)

returns the number of items in an object
returns the number of chars in a string

data

19
Q

list()

A

list(iterable)

returns a Python list

list(“ABCDEF”)
[“A”, “B”, “C”, “D”, “E”, “F”]

list({‘A’: 1, ‘B’: 2, ‘C’: 3, ‘D’: 4, ‘E’: 5})
[“A”, “B”, “C”, “D”, “E”, “F”]

list()
[]

20
Q

map()

A

map(function, iterable)

returns a “map object” which is the result of applying function to each element in the input iterable

need to pass the object to list() or tuple()

def addition(n):
return n+n

list(map(addition, [0,1,2])
[0,2,4]

21
Q

max()

A

max(iterable)
max(a, b, c, d)

returns the largest item in the iterable or the largest of 2 or more arguments

22
Q

min()

A

min(iterable)
min(a, b, c, d)

returns the smallest item in the iterable or the smallest of 2 or more arguments

23
Q

next()

A

next(iterator)

returns the next item in an iterator

mylist = iter([“apple”, “banana”, “cherry”])
x = next(mylist)
//”apple”
x = next(mylist)
//”banana”
x = next(mylist)
//”cherry”

24
Q

open()

A

open(file_name, mode)

modes:
“r” = read (default)
“w” = write
“a” = append
“x” = create

opens a file, returns it as a file object

25
Q

pow()

A

pow(base, exponent)

returns base to the power of exponent

pow(2,3) = 2^3 = 8

26
Q

range()

A

range(start=0, stop, step=1)

returns a sequence of numbers starting at start, in increments of step, ending before stop

27
Q

repr()

A

repr(object)

returns a readable string without formatting or rounding

28
Q

reversed()

A

reversed(iterable)

returns an iterator that accesses the given sequence in reverse order

29
Q

round()

A

round(number, digits=0)

returns a floating-point number that is rounded to the specified number of decimals

round(8.5) = 8
round(8.5, 1) = 8.5

30
Q

set()

A

set(iterable, sequence-like list)

returns a non-repeating element iterable

set((3, 4, 1, 4, 5)) = {1, 3, 4, 5}
set(range(3)) = {0, 1, 2}

31
Q

setattr()

A

setattr(object, attribute, value)

can be used to assign a new value to an object attribute, or initialize a new object attribute

32
Q

sorted()

A

sorted(iterable, key=None, reverse=False)
iterable = list, tuple, dictionary, etc
key = a function to execute to decide the order
reverse = False (Ascending) and True (Descending)

sorted({0: ‘this’, 9: ‘that’, 7: ‘whatever’})
[0, 7, 9]

sorted([9,2,7,5,6])
[2, 5, 6, 7, 9]

33
Q

str()

A

str(object)

converts specified value to a string

34
Q

sum()

A

sum(iterable, start)

returns the sum of all items in an iterable

35
Q

tuple()

A

tuple(iterable)

returns a tuple object

36
Q

type()

A

type(object)
returns the type of an object