Functions Flashcards

1
Q

calculation functions:

A

abs()
has one numerical parameter (an integer or a float). If the value is positive, it will return the value. If the value is negative, it will return the value multiplied by -1.

max()
has two or more numerical parameters, and returns the largest.

min()
has two or more numerical parameters, and returns the smallest.

pow()
has two numerical parameters, and returns the first to the power of the second. Optionally, it has a third numerical parameter. If that third parameter is supplied, it will return the value modulo that third parameter.

round()
has a numerical parameter and rounds it, mathematically, to a whole number. It has an optional second parameter. The second parameter must be an integer, and if it is provided, the function will round the first parameter to the number of decimals specified by the second parameter.

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

len()

A

len()
is a basic function that gets one parameter, and it returns the length of that parameter. For now, the only data type which you will use len() for is the string. len() returns the length of the string, i.e., its number of characters.

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

in math module has mathmatical functions:

A

math.exp()
gets one numerical parameter and returns e to the power of that parameter. If you do not remember e from math class: e is a special value that has many interesting properties, which have applications in physics, maths, and statistics.

math.log()
gets one numerical parameter and returns the natural logarithm of that parameter. The natural logarithm is the value which, when e is raised to the power of that value, gives the requested parameter. Just like e, the natural logarithm has many applications in physics, maths, and statistics.

math.log10()
gets one numerical parameter and returns the base-10 logarithm of that parameter.

math.sqrt()
gets one numerical parameter and returns the square root of that parameter.

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

in random module:

A

random()
gets no parameters, and returns a random float in the range [0,1), i.e., a range that includes 0.0, but excludes 1.0.

randint()
gets two parameters, both integers, and the first should be smaller than or equal to the second. It returns a random integer in the range for which the two parameters are boundaries, e.g., randint(2,5) returns 2, 3, 4, or 5, with an equal chance for each of them.

seed()
initializes the random number generator of Python. If you want a sequence of random numbers that are always the same for your program, start by calling seed with a fixed value as parameter, for instance, 0. This can be useful for testing purposes. If you want to re-initialize the random number generator so that it starts behaving completely randomly again, call seed() without parameter.

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

Write a function called printx() that just prints the letter “x”. Then write a function called multiplex() which takes as argument an integer and prints as many times the letter “x” as the integer indicates by calling the function printx() that many times.

A
# Printing x's
def printx():
    return ('x')
def multiplex(integer):
    return printx() * integer

multiplex(3)

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

Write the function random_element to get a single random element from a specified string. The function receives a string ‘s’ and always at least one character.

A

from random import randint

def random_element(text):
    i = randint(0, len(text))
    return text[i]

random_element(‘house’)

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