Module 2: Write Effective Code Python

Q: Which of the following choices is a valid header in a function definition?

  • def remove_user(username): 
  • def remove_user(username)
  • def (remove_user(username))
  • remove_user(username):
Explanation: Define function_name(parameters): is the format that a function header in Python should follow. The name of the function, which is removed_user in this instance, as well as any arguments that the function takes, which are contained in parentheses (), are specified in this header. There is a colon at the end of the header, which serves to signal the beginning of the code block for the function. 

Q: Which of the following calls to the type() function uses correct syntax?

  • type[81, 55, 17]
  • type[(81, 17)]
  • type([55, 81, 17]) 
  • type([17, 81]):
Explanation: Whenever you wish to identify the type of an object or value in Python, the type() function is preceded by parentheses (), which contain the object or value in question. It is permissible to use square brackets [] to indicate a list that contains the items 55, 81, and 17 in the third option, which is type([55, 81, 17]). 

Q: What is a parameter?

  • A variable returned from a function
  • The name of a function that is being defined
  • An object that is included in a function definition for use in that function 
  • The data brought into a function when it is called
Explanation: To accept data (arguments) when the function is called, parameters are variables that are stated in the function specification. Parameters are used to receive data or arguments. Specifically, they define the kind of arguments that a function can take in. By permitting functions to act on distinct sets of data when called with different arguments, parameters make it possible for functions to be more versatile and adaptable in their reuse.

Q: When working in Python, what is a library?

  • A collection of stylistic guidelines for working with Python
  • A collection of modules that provide code users can access in their programs 
  • A Python file that contains additional functions, variables, classes, and any kind of runnable code
  • A module that allows you to work with a particular type of file
Explanation: Collections of pre-written code modules that offer functionality that can be used by other programs are referred to as libraries in the Python programming language. They often include functions, classes, constants, and other resources that may be imported into your Python scripts to increase the capabilities of such scripts without having to build everything from scratch. NumPy is for numerical calculations, pandas stands for data processing and analysis, and requests stands for sending HTTP requests. These are all examples of Python libraries.

Q: What does this line of code return?

print(max(1,3,7))

  • 7
  • 1
  • 3
  • 11
Explanation: Python's max() method can return the highest possible value from among its parameters. In this particular scenario, the function max(1, 3, 7) examines the numbers 1, 3, and 7 and concludes that 7 is the single most significant value.

Q: What is returned from the following user-defined function if you pass it the arguments 2 and 3?

def add(num1, num2):

    result = num1 + num2

    return result

add(2, 3)

  • 1
  • 3
  • 2
Explanation: It will send 2 as num1 and 3 as num2 when you run the add(2, 3) function. In the next step, the function computes the sum of 2 and 3, which equals 5. In conclusion, the output of the function is the value 5, which is returned by the return result statement.

Q: Which of the following choices is a resource that provides stylistic guidelines for programmers working in Python?

  • PEP 8 
  • re
  • Python Standard Library
  • glob
Explanation: It is the style guide for Python code, which is known as PEP 8 (Python Enhancement Proposal 8). It outlines best practices and standards to ensure that the code is both accessible and consistent. It discusses a variety of subjects, including naming conventions, indentation, spacing, and other stylistic features that help the authoring of Python code that is straightforward and easy to maintain.

Q: What should you do when writing comments? Select all that apply.

  • Make them clear. 
  • Place them before every line of code.
  • Only place them at the beginning of a program.
  • Keep them up-to-date.
Explanation: It is not appropriate to utilize comments on every line; rather, they should be used sparingly to explain difficult areas or to clarify the idea.It is possible to include comments at various points throughout the code to clarify logic or explain certain portions.

Q: What are built-in functions?

  • Functions that take parameters
  • Functions that exist with Python and can be called directly 
  • Functions that return information
  • Functions that a programmer builds for their specific needs
Explanation: These functions are a part of Python's standard library and provide characteristics that are often used. They are easily accessible for usage in your projects and do not need any explicit definitions to be made. The functions print(), len(), max(), min(), sum(), range(), and others are examples of built-in functions in the Python programming language.

Q: Fill in the blank: A Python file that contains additional functions, variables, classes, and any kind of runnable code is called a _____.

  • module 
  • library
  • parameter
  • built-in function
Explanation: In the programming language Python, a module is a file that includes statements and definitions of Python. The name of the file matches the name of the module, with the suffix.py attached to it. Using modules, you can logically structure your Python code into reusable components that can be imported and used in other Python applications. Modules may also be used internally.

Q: Fill in the blank: The re, csv, glob, and time modules are all _____.

  • built-in functions
  • part of the Python Standard Library 
  • keywords in a function header
  • part of PEP 8
Explanation: In addition to being a collection of modules and packages, the Python Standard Library is also a collection of them. These modules provide a broad variety of features, some of which include the handling of regular expressions (re), working with of CSV files (csv), the matching of file paths (glob), and the handling of time-related activities (time), amongst many others.

Q: What does this line of code return?

print(sorted(["h32rb17", "p52jb81", "k11ry83"]))

  •  [“p52jb81”, “k11ry83”, “h32rb17”]
  •  [“h32rb17”]
  •  [“p52jb81”]
  •  [“h32rb17”, “k11ry83”, “p52jb81”] 
Explanation: In Python, the sorted() function is a built-in function that takes the elements of any iterable and produces a new sorted list. In this particular instance, the iterable is a list of strings. The process of sorting strings in Python adheres to lexicographical order, which is similar to that of a dictionary. This means that numbers come before letters, and uppercase letters come before lowercase letters.

Q: What is returned from the following user-defined function if you pass it the argument 9?

def subtract(num):

    total = 100 - num

    return total

subtract(9)

  • 91 
  • 9
  • 9.0
  • 100
Explanation: If you use subtract(9), it will send the value 9 as the num argument. Following that, the function computes 100 minus 9, which results in 91. In conclusion, the result of the function is 91, which is produced by the return total statement.

Q: What does PEP 8 contain?

  • A collection of modules that users can access in their programs
  • Suggestions for making Python easier to learn
  • Stylistic guidelines for programmers working in Python
  • Files with additional functions users can use in their code
Explanation: To improve the readability and maintainability of Python code, the Python Extensions Project (PEP) 8 presents guidelines and best practices. For example, it addresses naming standards, code structure, indentation, comments, and a variety of other topics. To guarantee that Python code is consistent and simple to comprehend for other developers, it is helpful to follow the PEP 8 guidelines.

Q: What is an advantage of including this comment in the following code? Select all that apply.

# For loop iterates to print an alert message 5 times

for i in range(5):

    print("alert")

  • It can help you understand the code if you revisit it in the future. 
  • It can help other programmers understand the purpose of this loop.
  • It is displayed in the output when the code is run in Python.
  • It ensures the loop will function when the code is run in Python.
Explanation: Comments provide clarity on the goal or functionality of the code, which is beneficial not only to the original author but also to other programmers who may read or maintain the code in the future. Comments are not visible in the output when the code is performed, nor do they immediately guarantee that the loop will operate properly when the code is carried out. The documentation and explanation of the code for human readers is the major objective of these documents.

Q: Which of the following statements accurately describe functions? Select all that apply.

  • When functions are updated, the changes are applied everywhere they are used. 
  • Functions are useful for automation. 
  • Functions can be used no more than 10 times from within a single program.
  • Functions can be reused throughout a program. 
Explanation: The assertion that "Functions can be used no more than ten times from within a single program" in no way accurately describes the situation. The number of times that a function may be used inside a program is not restricted in any way by the program itself. The logic of the program dictates the maximum number of times that a function may be called, and functions are designed to be reusable wherever possible.

Q: Fill in the blank: A Python file that contains additional functions, variables, classes, and any kind of runnable code is called a _____.

  • parameter
  • library
  • module 
  • built-in function
Explanation: As a means of organizing code into reusable parts, Python makes use of modules. They make it possible for you to encapsulate functionality and logically group together code that is linked to one another. There is a common correlation between a module and a.py file. This file is responsible for defining functions, classes, and variables that may be imported and used in other Python scripts.

Q: Which of the following components are part of the header in a function definition? Select all that apply.

  • The parameters used in a function 
  • The name of the function 
  • The keyword return
  • The keyword def 
Explanation: To identify the function and to call it at a later point in the code, the name of the function is an important component. Python allows for the definition of functions via the use of the keyword def. Before the name of the function and any arguments, it comes first. As a result of the fact that they describe the inputs that the function anticipates, the parameters that are used in a function contribute to the function header.

Q: In the following code, what is the argument?

def welcome_user(name):

    print("Welcome," name)

username="elarson"

welcome_user(username)

  • welcome_user
  • username 
  • name
  • def
Explanation: In the process of invoking the function welcome_user(username), the username, which is "elarson," is considered an input to the function.In the context of the function, the term "name" refers to the argument that is assigned the value "elarson" whenever the function is used.

Q: Fill in the blank: A collection of modules that users can access in their programs is a _____.

  • style guide
  • library
  • user-defined function
  • built-in function
Explanation: When discussing programming, the term "library" refers to pre-written code modules that enable other programs to make use of certain functions. To extend the capabilities of a programming language, these modules may include functions, classes, constants, and other resources. This includes the standard library for Python as well as third-party libraries, both of which are examples of libraries that provide a variety of features that developers may include in their projects.

Q: Why are comments useful? Select three answers.

  • They explain the code to other programmers. (CORRECT)
  • They make the code run faster.
  • They provide insight on what the code does. 
  • They make debugging easier later on.
Explanation: Your future self, along with other programmers, will have an easier time comprehending the code if you include comments that give clarity about the goal, logic, or functioning of the code. Comments provide a description of the required behavior or technical details of how the code operates, so assisting readers in following the logic and comprehending more complicated portions.

Post a Comment

Previous Post Next Post