Python Functions and Arguments
40 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What does the factorial function return when the input is 0?

  • 1
  • Infinity
  • 0
  • Undefined (correct)
  • What operator is used to unpack an iterable in Python?

  • @
  • %
  • * (correct)
  • &
  • What is the result of the following code: 'a, b, *c = [1, 2, 3, 4]'?

  • a = 2, b = 3, c = [1, 4]
  • a = 3, b = 4, c = [1, 2]
  • a = 1, b = 2, c = [3, 4] (correct)
  • a = 1, b = 2, c = [4]
  • How does the factorial function defined in the content handle recursive calls?

    <p>It calls the factorial function with the argument decremented by 1.</p> Signup and view all the answers

    What will the variable 'd' contain after running the code 'a, b, c, *d = [1, 2, 3, 4, 5, 6]'?

    <p>[4, 5, 6]</p> Signup and view all the answers

    What will happen if the number of variables in parallel assignment exceeds the number of elements in the iterable?

    <p>It will raise an IndexError.</p> Signup and view all the answers

    When unpacking a dictionary, what is the primary outcome?

    <p>Printing its key-value pairs.</p> Signup and view all the answers

    Which of the following is NOT a method to unpack an iterable in Python?

    <p>Using indexing</p> Signup and view all the answers

    What will be the output of the following code: print(main(10, 5, 5))?

    <p>20</p> Signup and view all the answers

    What is the primary purpose of a default argument in a function?

    <p>To allow functions to be called with varying numbers of arguments</p> Signup and view all the answers

    Which of the following statements correctly describes the use of mutable optional parameters?

    <p>Changes to them carry over between function calls.</p> Signup and view all the answers

    What will happen if a non-default argument is placed after a default argument in a function definition?

    <p>An error will occur.</p> Signup and view all the answers

    In the example function list_appender, what will be the output of print(list_appender('hola')) after the previous calls?

    <p>['hello', 'salut', 'hallo', 'hola']</p> Signup and view all the answers

    What is likely the output of the function call: print(main(10, 5)) if num3 is defined as a default argument with the value 0?

    <p>15</p> Signup and view all the answers

    Which of the following best describes the behavior of default arguments in the context of function calls?

    <p>They execute regardless of the number of arguments provided.</p> Signup and view all the answers

    How should parameters be structured in a function if one of them is to have a default value?

    <p>All parameters to its right must also have default values.</p> Signup and view all the answers

    What will the following code print? my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}; print_person(**my_dict)

    <p>John is 30 years old and lives in New York</p> Signup and view all the answers

    Which of the following is a correct use of the unpack operator (**)?

    <p>fun(**{'k1': 10, 'k2': 20, 'k3': 30})</p> Signup and view all the answers

    How do you convert the iterator returned by map() to a list?

    <p>list(squared_numbers_iterator)</p> Signup and view all the answers

    What type of data can the map() function operate on?

    <p>Any iterable data types</p> Signup and view all the answers

    What does the filter() function return when provided with a function returning True for all elements?

    <p>A copy of the original iterable</p> Signup and view all the answers

    What will the following code print? temperatures = [0, 10, 20, 30, 40]; fahrenheit_temperatures = list(map(lambda x: (9/5) * x + 32, temperatures)); print(fahrenheit_temperatures)

    <p>[32, 50, 68, 86, 104]</p> Signup and view all the answers

    Which of the following statements is true about the filter() function?

    <p>The first argument must always be a function that returns Boolean values.</p> Signup and view all the answers

    Using the unpack operator (**), what will be the result of combination = {**numbers, **letters} where numbers and letters are defined dictionaries?

    <p>A combination of both dictionaries without common keys.</p> Signup and view all the answers

    What will be the output when the first element of the array is modified to 0?

    <p>array('i', [0, 2, 3, 5, 7, 10])</p> Signup and view all the answers

    What does the filter() function do in Python?

    <p>Returns elements based on a condition from an iterable</p> Signup and view all the answers

    What is the effect of using the pop() method on an array?

    <p>It removes an element at the specified index.</p> Signup and view all the answers

    What will the following code output: print(list(filter(lambda x: x >= 0, [-1, 0, 1, 2])))?

    <p>[0, 1, 2]</p> Signup and view all the answers

    Which of the following statements about Python arrays is true?

    <p>All elements in an array must be of the same numeric type.</p> Signup and view all the answers

    What will the reduce() function return in this code: ans = reduce(lambda x, y: x + y, [1, 2, 3])?

    <p>6</p> Signup and view all the answers

    What happens if you try to delete an array using the del statement after removing all elements from it?

    <p>An error will occur stating the array is not defined.</p> Signup and view all the answers

    What is a key characteristic of arrays in programming?

    <p>They store multiple elements of the same type</p> Signup and view all the answers

    What will be the output of the following code snippet: numbers = arr.array('i', [10, 11, 12, 12, 13]); numbers.remove(12); print(numbers)?

    <p>array('i', [10, 11, 13])</p> Signup and view all the answers

    Which statement is true about the implementation of arrays in Python?

    <p>You need to import a specific module to create arrays.</p> Signup and view all the answers

    Which method can be used to reverse the elements of an array?

    <p>reverse()</p> Signup and view all the answers

    What is the primary difference between Python lists and arrays?

    <p>Arrays enforce element type consistency.</p> Signup and view all the answers

    What will the output be when this code is executed: print(reduce(product, [2, 3, 4])) where product is defined as: def product(x,y): return x*y?

    <p>24</p> Signup and view all the answers

    In the context of an array, what does an index represent?

    <p>The position of an element within the array</p> Signup and view all the answers

    What will be printed after executing my_array.reverse() on my_array = array.array('i', [1, 2, 3, 4, 5])?

    <p>Reversed array: 5 4 3 2 1</p> Signup and view all the answers

    How does the reduce() function differ from the filter() function?

    <p>Reduce returns a single cumulative value while filter returns filtered elements.</p> Signup and view all the answers

    Study Notes

    Python Functions and Arguments

    • Functions can have default arguments, allowing some parameters to be optional.
    • Example of a function with default argument:
      • def main(num1, num2, num3=0): return num1 + num2 + num3
    • Calling main(10, 5) returns 15, while main(10, 5, 5) returns 20.
    • Default arguments must follow non-default ones in function definitions.

    Mutable Optional Parameters

    • Mutable types (e.g., lists, dictionaries) as optional parameters maintain state across function calls.
    • Example of a function appending to a list:
      • def list_appender(item, items=[]): items.append(item); return items
    • Consecutive calls modify the same list, not a new one.

    Recursion in Python

    • A recursive function can call itself, useful for problems like factorials.
    • Example:
      • def factorial(x): return 1 if x == 1 else x * factorial(x-1)
    • factorial(3) computes 6.

    Unpacking Iterables

    • The unpacking operator (*) extracts elements from an iterable into variables.
    • Use case involves dictionaries, allowing key-value pairs to be accessible in functions.
    • Example with unpacking a dictionary:
      • fun(**d) where d is a dictionary.

    The map() Function

    • map(function, iterable) applies a function to each item in an iterable.
    • Returns a map object, which can be converted to a list.
    • Example for squaring numbers:
      • list(map(square, numbers)) returns squared values.

    The filter() Function

    • filter(function, iterable) filters items based on a condition.
    • Returns an iterable with only elements that returned True from the function.
    • Example filters to retain only non-negative numbers.

    The reduce() Function

    • Part of functools, reduce(function, iterable) returns a single cumulative value.
    • Example sums a list of numbers:
      • reduce(lambda x, y: x + y, nums).

    Arrays in Python

    • Arrays store multiple values of the same type, enhancing code efficiency.
    • Arrays can be created using the array module.
    • Access elements by index, starting at 0.

    Array Operations

    • Changing and adding elements can be done through direct assignment or slicing.
    • Example:
      • Change first element numbers[0] = 0.
    • Removing elements uses del, remove(), or pop().

    Differences Between Lists and Arrays

    • Lists can contain mixed types; arrays require all elements to be of the same type.
    • Error in arrays when mixed data types are attempted.

    Reversing and Extending Arrays

    • Reverse an array in place using array.reverse().
    • Extend an array with additional values via the extend() method.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    unit1-part2.pdf

    Description

    This quiz covers essential concepts related to Python functions, including default arguments, mutable optional parameters, recursion, and unpacking iterables. Test your understanding of these key programming principles and enhance your Python skills.

    More Like This

    Python Functions Quiz
    60 questions

    Python Functions Quiz

    GenerousChrysoprase avatar
    GenerousChrysoprase
    Python Functions and String Manipulation
    8 questions
    Python Functions Overview
    29 questions

    Python Functions Overview

    EffortlessArtNouveau1257 avatar
    EffortlessArtNouveau1257
    Python Functions and Recursion Quiz
    28 questions
    Use Quizgecko on...
    Browser
    Browser