Commonly Used Built-In Functions in Python

BeneficentMoose avatar
BeneficentMoose
·
·
Download

Start Quiz

Study Flashcards

30 Questions

What does the 'min()' function do?

Returns the minimum value in an iterable object

Which function is used to convert data to a floating-point number?

float()

What is the purpose of the 'sum()' function?

Returns the sum of elements in an iterable object

Which function converts any data type to a string representation?

str()

When using type conversion functions, what can happen if the input data cannot be converted?

An error may occur and raise an exception

What does the 'bool()' function do?

Converts any data type to a Boolean value (True or False)

What is the purpose of the 'return' statement in a function?

To specify the value the function should return

What happens if a function does not contain a 'return' statement?

It is considered to return 'None'

How many arguments does the 'add' function take?

Two arguments

What does the 'max_of_two' function return?

The maximum of the two arguments

When does a function terminate?

After the 'return' statement is executed

Can a function contain multiple 'return' statements?

Yes, but only one will be executed

What does the 'math.fsum(iterable)' function in Python return?

Sum of a sequence of floating-point numbers

Which function is used to determine if a given number in Python is not a number?

math.isnan(x)

What does the 'random.randrange(start, stop, step)' function do in Python?

Returns random integer from the range specified by 'start', 'stop', and 'step'

What is the purpose of writing 'import' followed by the name of the module in Python?

To use the functions defined in that module in the main program

Which Python function returns the square root of a given number 'x'?

math.sqrt(x)

What is the purpose of the 'random.choice(sequence)' function in Python?

To return a random item from the given sequence

Which statement accurately describes importing specific functions from a module?

It imports just the functions specified and not the entire module

Which function in Python is used to raise 'x' to the power of 'y'?

math.pow(x, y)

What is recursion in programming?

A technique where a function calls itself as a subroutine

How does the provided example of recursion calculate the factorial of a number?

By multiplying all positive integers up to 'n'

When importing specific functions from a module, do you need to prefix those functions with the name of the module?

No, not required when importing specific functions

In Python, what does 'import' followed by the name of a module allow you to do?

Use functions defined in that module

What does a local variable in Python do?

Temporarily store data specific to a particular function call

Which type of variable allows sharing data between functions in Python?

Global variables

What remains unchanged when local variables change in Python?

Global variables

Why is it important to be mindful of variable scope when writing functions in Python?

To avoid conflicts between variables with the same name

Which type of variable stores data that needs to be shared across multiple function calls in Python?

Global variables

What purpose do local variables and global variables serve in Python?

Local variables store data temporarily, global variables share data between functions

Study Notes

Function Calls and Parameters

  • A function can be called with arguments, which are values passed to the function.
  • Arguments are assigned to parameters, which are variables defined in the function definition.

Return Values

  • A function can return a value, which can be used in other parts of the program.
  • The return statement is used to specify the value that the function should return.
  • If a function does not contain a return statement, it is considered to return None.
  • The return statement can also be used in conditional statements to return different values based on conditions.

Math Functions

  • math.fsum(iterable) returns the sum of a sequence of floating-point numbers, accurately computed.
  • math.isnan(x) returns True if the given number x is not a number (NaN) and False otherwise.
  • math.pow(x, y) returns the value of x raised to the power of y.
  • math.sqrt(x) returns the square root of the given number x.

Random Numbers

  • The random module in Python provides several functions and methods for generating random numbers and performing random operations on sequences.
  • random.random() returns a random float in the interval between 0.0 to 1.0.
  • random.randrange(start, stop, step) returns a random integer from the range specified by start, stop, and step.
  • random.randint(a, b) returns a random integer between a and b inclusive.
  • random.choice(sequence) returns a random item from the given sequence.
  • random.shuffle(sequence) shuffles the given sequence in place.

Modules

  • A module is a file containing definitions and statements.
  • Modules can be imported into a program to use their functions and variables.
  • Specific functions can be imported from a module using the from ... import ... statement.

Recursion

  • Recursion is a technique where a function calls itself as a subroutine.
  • A recursive function can be used to calculate the factorial of a number.

Global and Local Variables

  • Global variables can be used to store data that needs to be shared across multiple function calls.
  • Local variables can be used to temporarily store data that is specific to a particular function call.
  • The scope of variables should be considered when writing functions in Python.

Commonly Used Built-in Functions

  • min() returns the minimum value in an iterable object (such as a list).
  • max() returns the maximum value in an iterable object (such as a list).
  • sum() returns the sum of the elements in an iterable object (such as a list).
  • round() returns a floating-point number to the specified number of decimal places.

Type Conversion Functions

  • int() is used to convert a number or a string representation of a number to an integer.
  • float() is used to convert a number or a string representation of a number to a floating-point number.
  • str() is used to convert any data type to a string representation.
  • bool() is used to convert any data type to a Boolean value (True or False).

Test your knowledge of commonly used built-in functions in Python such as min(), max(), sum(), and round(). Evaluate your understanding of how these functions operate on iterable objects like lists and how to utilize them in your Python code.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Use Quizgecko on...
Browser
Browser