Python Builtin Function Flashcards

 

card 1 of 30

 

min(100,10)

 

min(a,b) means minimum of arguments a and b

10

card 2 of 30

 

min(2**5,3**4)

 

evaluate expressions as arguments first

32

card 3 of 30

 

max(2**5,3**4)

 

max(a,b) means maximum of arguments a and b

81

card 4 of 30

 

abs(5-9)

 

abs(x) means absolute value of x

4

card 5 of 30

 

len("abcd")

 

len(x) returns the length of string x

4

card 6 of 30

 

len('')

 

len(x) returns the length of string x

0

card 7 of 30

 

len("computing")+len("is")+len("logical")

 

evaluate left-to-right

18

card 8 of 30

 

log(3)

 

log(x) means logarithm of x, but ...

NameError - log is not defined (we will see how to get logarithms later)

card 9 of 30

 

float(3)

 

the float(x) function is used for type conversion

3.0

card 10 of 30

 

float("3.0e0")

 

the float(x) function converts strings to float-type

3.0

card 11 of 30

 

float("three")

 

the float(x) function attempts conversion from strings to float-type

ValueError - invalid literal (some things Python cannot do)

card 12 of 30

 

int(3.0)

 

the int(x) function converts to integer type

3

card 13 of 30

 

int(3.75e1)

 

the int(x) function converts to integer type

37

card 14 of 30

 

str(21)

 

the str(x) function converts x to a string type

'21'

card 15 of 30

 

str(2.1e3)

 

the str(x) function converts x to a string type

'2100.0'

card 16 of 30

 

str('abc')

 

some conversions do nothing

'abc'

card 17 of 30

 

str(False or False or True and True)

 

first evaluate the argument, then do the conversion

'True'

card 18 of 30

 

float(not True or False)

 

first evaluate the argument

0.0

card 19 of 30

 

bool(1.0)

 

bool(x) converts x to the True/False domain of values

True

card 20 of 30

 

bool(str(len("zero")))

 

with some exceptions, values tend to behave like True

True

card 21 of 30

 

str(int(bool("False"))

 

evaluate parenthesized expressions first

... Python is confused, looking for another ')' to be input

card 22 of 30

 

type(0)

 

type(x) returns the Python-type of the argument x

<type 'int'> (Python 2) -- means the argument has the integer type
<class 'int'> (Python 3) -- means the argument has the integer type

card 23 of 30

 

type(1.1)

 

type(x) returns the Python-type of the argument x

<type 'float'> (Python 2) -- means the argument has the float type
<class 'float'> (Python 3) -- means the argument has the float type

card 24 of 30

 

type(1j)

 

type(x) returns the Python-type of the argument x

<type 'complex'> (Python 2) -- means the argument has the complex number type
<class 'complex'> (Python 3) -- means the argument has the complex number type

card 25 of 30

 

type('abc')

 

type(x) returns the Python-type of the argument x

<type 'str'> (Python 2) -- means the argument has the string type
<class 'str'> (Python 3) -- means the argument has the string type

card 26 of 30

 

type(True)

 

type(x) returns the Python-type of the argument x

<type 'bool'> (Python 2) -- means the argument has the Boolean type
<class 'bool'> (Pyton 3) -- means the argument has the Boolean type

card 27 of 30

 

type(len("following"))

 

evaluate the argument first

<type 'int'> (Python 2) -- because len(x) returns an integer length value
<class 'int'> (Python 3) -- because len(x) returns an integer length value

card 28 of 30

 

type(type(True))

 

this may be a surprise

<type 'type'> (Python 2) -- Python even has a type for types!
<class 'type'> (Python 3) -- Python even has a type for types!

card 29 of 30

 

type(bool)

 

here "bool" is supposed to mean the same as type(True)

<type 'type'> (Python 2) -- bool is a name for class(True) or class(False)
<class 'type'> (Python 3)

card 30 of 30

 

type("Iowa")==str

 

here "str" is supposed to mean the same as type("xyz"), for example

True

 

End of Problem Set

(use browser reload to restart this problem set)