Python Chapter 4

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

How should you call your main function to distinguish it from other main() functions from imported modules?

if __name__ == "__main__": main()

What is the syntax for defining a function?

functionName(arguments):

pg 124

pg 124

List the three functions of the random module

random() randomint(min, max) randomrange([start,] stop [,step])

When is it a good idea to use named arguments?

When a function has many arguments. This simplifies the readability of the code

What is the benefit of importing modules into the global namespace?

You do not have to use the namespace prefix when calling a function.

How do you view the docstring of a module?

You import the module and use the help function, help(), using the module name as an argument

How do you code a docstring?

You surround the text by three triple quotes: """ """

What is a standard module?

A built in module with functions to perform specific tasks

What is a default value for an argument?

A default value is a value assigned to an argument in a function definition that is applied if no value is passed to the argument

What is a named argument?

A named argument occurs when the passing argument name is the same as the argument used in the function definition.

What is a specified namespace?

A namespace with a coded name that is different than the name of the module

What does scope refer to in programming?

A scope tells you where in your program you are allowed to use the variables and functions that you've defined

What is a namespace?

An area of memory that holds a module

Why should you call your main() function using the __name__ variable?

Any module that you run may have its own main() function. To help distinguish between your actual main() function and an imported one, Python sets the __name__ variable of a module that hasn't been imported to __main__.

Where should you code arguments with default values? Before or after regular arguments?

Arguments with default values are coded after other arguments

To use a function in your code, you must first _____ it.

Call

How do you code a default value for an argument?

Code the argument name, the assignment operator, and the default value. argumentName = defaultValue

How do you call a function in the namespace of an imported module?

Code the name of the namespace a dot and the name of the function: nameSpace.functionName

How do you import a module into the global namespace?

Enter 'from', the module name, 'import', and the function name(s) from moduleName import functionName1 [, functionName2...]

True or False: When you code a calling statement, the arguments you pass are required to be the same as the names that are used in the function definition.

False. The arguments and names can be different.

A _____ is a unit of code that performs a task.

Function

When coding a program, it is good practice to put all of your code in _____

Functions

What does the tkinter module contain?

Functions for building GUI applications

What does the random module contain?

Functions for generating random numbers

What does the math module contain?

Functions for mathematical operations

What does the decimal module contain?

Functions for working with decimal numbers

What does the csv module contain?

Functions for working with files that contain comma-separated values

List the two ways to call a global variable

If you are going to change the variable, type the word global followed by the variable name global variableName If you are not going to change the variable, type the variable name

How do you pass a named argument?

In the calling statement, the argument name is followed by the assignment statement, =, and the value argumentName = value

General code that isn't contained in a specific function should be placed where?

In the main() function

Where does the import statement import a module by default?

Into a namespace that has the same name as the module

If using different names for passed arguments than the names used in a function definition, what should you make sure to do?

Make sure the arguments in the calling statement are in the same sequence as the arguments in the function definition

A _____ in Python is any file that contains reusable code, like a function

Module

Why should you avoid importing modules into the global namespace?

Modules might use the same function names resulting in possible name collisions

By default, a global constant is _____-_____ inside of a function. To change it, you would have to use the global keyword

Read-only

What does the random() function do?

Returns a random float value that's greater than or equal to 0.0 and less than 1.0

What does the randomint() function do?

Returns a random int value that's greater than or equal to the min argument and less than or equal to the max argument

What does the randomrange() function do?

Returns a random value greater than or equal to the start argument, less than the stop argument, and a multiple of the step argument

It is a common practice to arrange the functions in the _____ in which they're called in your code

Sequence

How do you create a module?

Store one or more function in a Python file

List the two places to store a module to make them accessible to other modules?

Store them in the same directory as other modules Store them in a directory that is in the search path

What is a call stack?

The order in which you call functions in a program/script

What does the pickle module contain?

Functions for persistent data storage

How does one program use functions in another module?

The program must import the other module

What is the purpose of the return statement?

The return statement sends the results of the function back to the calling statement

If you code a value for an argument and a default value, which is applied?

The value passed by an argument will override a default value.

What is a global variable?

This is a variable that is defined outside of all functions so that it can be called by any function. It is said to have a global scope

What is a local variable?

This is a variable that is defined within a function and, therefore, is only available within that function, or locally

What is a docstring?

This is the documentation of a module to explain the purpose of its functions. It acts like a help file for others using your code.

What doe shadowing a global variable mean?

This refers to a function using a local variable that has the same name as a global variable

What is a global constant?

This refers to a global variable with a value that does not change

What is a global scope?

This refers to a variable's ability to be used by any function without being passed to the function first. It is accessible to all parts of the program, therefore it is global

What is a local scope?

This refers to variables that can only be used in the functions that define them

How do you call a function?

Type the function name followed by parenthesis: functionName()

How do you code a global constant?

Type the name in all caps

How do you import another module?

Use the import command followed by the module name and an optional namespace import moduleName [as namespace]

How do you create a specified namespace?

Use the import command, the module name, followed by the word 'as' and the new namespace name import moduleName as namespaceName

How do you import all functions of a module into the global namespace?

Use the wildcard, *, instead of the function names from moduleName import *

List two helpful rules to follow when creating function names.

• Start each function name with a verb (an action being taken) • Make the name as descriptive of the purpose of the function as possible getUsername readUserInput


संबंधित स्टडी सेट्स

aPHR Study Guide - Functional Area 2: Recruitment and Selection

View Set

Pharmacology Unit 2: Repiratory System

View Set

course point musculoskeletal ch 39

View Set

Ch. 40 Fluid, Electrolyte, and Acid-Base Balance

View Set

Lower Extremity Presentations/Questions

View Set