Python Built-in Functions Quiz
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 are methods in Python primarily used for?

  • To define global variables
  • To compile code into executable format
  • To create new data types
  • To apply specific routines to objects (correct)
  • Which of the following built-in functions is used to convert a number into a binary string?

  • bin() (correct)
  • format()
  • hex()
  • oct()
  • Which built-in function would you use to check if a specific attribute exists in an object?

  • dir()
  • type()
  • hasattr() (correct)
  • getattr()
  • What does the exec() function do in Python?

    <p>Executes a string of Python code</p> Signup and view all the answers

    Which of the following functions is used to create a memory view object in Python?

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

    What is the purpose of the dir() function?

    <p>To display available methods and properties of an object</p> Signup and view all the answers

    Which built-in method is utilized to return the maximum item in an iterable?

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

    What is the primary function of the round() method?

    <p>To round a number to a specified number of decimal places</p> Signup and view all the answers

    What is a tuple primarily defined as?

    <p>A collection of values written as comma-separated items between a pair of round brackets</p> Signup and view all the answers

    What happens when you attempt to change an element within a tuple?

    <p>An error will occur, as tuples are immutable</p> Signup and view all the answers

    How do you access the second item in a tuple named 'vehicles'?

    <p>vehicles[1]</p> Signup and view all the answers

    Given the tuple fruits = ('Apple', 'Orange', 'Banana'), what would be the result of fruits[1:3]?

    <p>('Orange', 'Banana')</p> Signup and view all the answers

    What does the command fruits[-1] return when using the tuple fruits = ('Apple', 'Orange', 'Banana')?

    <p>'Banana'</p> Signup and view all the answers

    What would fruits[0:-1] return if fruits = ('Apple', 'Orange', 'Banana')?

    <p>('Apple', 'Orange')</p> Signup and view all the answers

    Which of the following correctly describes how to create a new tuple from an existing one?

    <p>Using concatenation of two tuples</p> Signup and view all the answers

    In Python, which of the following statements about tuples is true?

    <p>Tuples are immutable, meaning their contents can't be changed</p> Signup and view all the answers

    What method is used to retrieve all keys from a dictionary?

    <p>dictionary_name.keys()</p> Signup and view all the answers

    Which of the following statements is true regarding dictionary keys in Python?

    <p>Keys are immutable.</p> Signup and view all the answers

    What does the method dictionary_name.items() return?

    <p>A dict_items object containing key-value pairs.</p> Signup and view all the answers

    How can you change the value associated with a specific key in a dictionary?

    <p>dictionary_name[key] = new_value</p> Signup and view all the answers

    What will happen if you attempt to change a key in a dictionary directly?

    <p>An error will be thrown since keys are mutable.</p> Signup and view all the answers

    If you want to delete a key from a dictionary, which statement would you use?

    <p>del dictionary_name.key</p> Signup and view all the answers

    In a for-loop iterating over a dictionary's items, how are the key and value accessed?

    <p>for key, value in dictionary_name.items()</p> Signup and view all the answers

    What structure does a dict_items() object hold?

    <p>Tuples containing keys and values.</p> Signup and view all the answers

    Why is using an alias for a package or module beneficial?

    <p>It simplifies code when the package name is long.</p> Signup and view all the answers

    What command would you use to upgrade a package called 'example_package'?

    <blockquote> <p>pip install example_package --upgrade</p> </blockquote> Signup and view all the answers

    Which of the following packages is primarily used for data visualization?

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

    What is the correct command to uninstall a package named 'test_package'?

    <blockquote> <p>pip3 uninstall test_package</p> </blockquote> Signup and view all the answers

    Which command is used to install a package while specifying pip3?

    <blockquote> <p>pip3 install package_name</p> </blockquote> Signup and view all the answers

    Which package would you use to manage multi-dimensional arrays in Python?

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

    To send HTTP requests using Python, which package would you utilize?

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

    What command is used to list installed packages with pip?

    <blockquote> <p>pip3 list</p> </blockquote> Signup and view all the answers

    What is the correct way to merge two lists in Python?

    <p>combined = [fruits, vegetables]</p> Signup and view all the answers

    How can you access a value in a dictionary?

    <p>Both B and C</p> Signup and view all the answers

    What is the correct format for defining a dictionary in Python?

    <p>dictionary_name = {key1:value1, key2:value2}</p> Signup and view all the answers

    Which of the following methods can be used to print items in a list?

    <p>for item in list_name: print(item)</p> Signup and view all the answers

    What kind of structure does a dictionary maintain?

    <p>Unordered set of key-value pairs</p> Signup and view all the answers

    What should be used to allow users to input data into a list without a fixed number of entries?

    <p>input() function within a while loop</p> Signup and view all the answers

    How do you modify the middle item of a list?

    <p>list_name[middle_index] = new_value</p> Signup and view all the answers

    How is the price of 'Apple' accessed from the fruits_price dictionary?

    <p>Both A and B</p> Signup and view all the answers

    Study Notes

    Built-In Functions in Python

    • Functions such as abs(), all(), any(), and ascii() are predefined in Python for various operations.
    • Methods are routines that apply to specific object types, meaning certain methods work only with certain data types.
    • Common methods for specific data types can be found in references for tuples, lists, dictionaries, and strings.

    Tuples

    • A tuple is defined as an immutable collection of values within parentheses, e.g., tuple_name = (element1, element2, ...).
    • Typically used for data sharing without allowing modification, e.g., employee_A1 = ("Jimmy Chow", "Engineer", "Grade 2").
    • Indexing starts at 0; to access elements, use the index operator [] (e.g., fruits[0] refers to "Apple").
    • A tuple can be subsetted using a colon, e.g., tuple_subset = fruits[0:2], excluding the end index.
    • Negative indexing allows access to elements starting from the end, e.g., fruits[-1] refers to "Banana".

    Editing and Concatenating Tuples

    • Tuples are immutable and cannot be edited once created.
    • To merge tuples or lists, they can be combined into a new structure, e.g., combined = [fruits, vegetables].

    Dictionaries

    • A dictionary is an unordered set of unique key:value pairs, e.g., fruits_price = {"Apple": 1.20, "Orange": 0.70}.
    • Dictionary values are accessed via their unique keys, for example, apple_price = fruits_price["Apple"].
    • Use .keys(), .values(), and .items() to retrieve keys, values, and both keys and values, respectively.

    Editing Dictionaries

    • Dictionary values can be changed by assigning a new value to an existing key, e.g., fruits_price["Apple"] = 120.
    • Keys cannot be directly changed; a new key must be created, and the old key must be deleted using del.

    Packages and Installation

    • Numerous Python packages enhance its functionality, including:
      • matplotlib for data visualization
      • NumPy for multi-dimensional arrays
      • pandas for data table management
      • requests for sending HTTP requests
    • Use pip or pip3 to install packages: pip install package_name.
    • To upgrade or uninstall packages, commands such as pip install package_name --upgrade and pip uninstall package_name are used.

    General Programming Tips

    • Use loops to print list items. Example: for item in list_name: print(item).
    • User-defined data can be collected into lists using input() and a while-loop for continuous entries.
    • Create structured programs by defining clear tasks such as modifying list items and merging lists.

    Studying That Suits You

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

    Quiz Team

    Description

    Test your knowledge on Python's built-in functions with this engaging quiz. From abs() to zip(), see how well you understand their usage and applications. Perfect for beginners and experienced programmers alike!

    More Like This

    Use Quizgecko on...
    Browser
    Browser