Python: Fundamentals

Created by nowickihunter

What is a function?
A group of statements within a program that perform a specific task.

1/98

TermDefinition
What is a function?A group of statements within a program that perform a specific task.
What is a modularized program?A program where each task is in its own function.
What are the benefits of using functions?1. Simpler code 2. Code reuse 3. Netter testing and debugging 4. Faster development 5. Easier facilitation of teamwork.
What is a void function?A function that simply executes the statements it contains and then terminates.
What is a value-returning function?A function that executes the statements it contains and then returns a value back to the statement that called it.
What are some examples of value-returning functions?Input, int, and float functions.
What are the rules for naming a function?1. Cannot use keywords 2. Cannot contain spaces 3. First character must be a letter or underscore 4. All other characters must be a letter, number, or underscore 5. Case-sensitive; uppercase and lowercase characters are distinct
What is the naming convention for functions?Descriptive of the task carried out by the function, often includes a verb.
What does a function definition specify?The functions name and a parameter list, the block of statements excuted when the function is called.
What is a function header?The first line of a function, includes the keyword def, function name, parentheses, and colon.
What is a block?A set of statements that belong together as a group, such as the statements included in a function.
How do you call a function?By using its name followed by parentheses, including any arguments in parentheses that are required to be passed to the parameters of the function.
What happens when a function is called?The interpreter jumps to the function and executes the statements in the block, then jumps back to the part of the program that called the function.
What is the main function?Function called when the program starts
What does the main function do?Defines the mainline logic of the program
What is the purpose of indentation in Python?Each block must be indented, lines in block must begin with the same number of spaces or tabs. Do not mix spaces and tabs, use one or the other consistently throughout your source code.
What happens if you use both tabs and spaces for indentation in Python?It can confuse the Python interpreter
What does IDLE do with the lines in a block?Automatically indents them
How is a function call represented in a flowchart?As a rectangle with vertical bars at each side and the name of the function displayed within the vertical bars.
Where is the function name written in a flowchart?Inside the vertical bars of the rectangle symbol
Should you draw a separate flowchart for each function in a program?Yes
What does the end terminal symbol in a flowchart usually read?Return
What is another name for a hierarchy chart?Structure chart
What does a box in a hierarchy chart represent?A function in the program
What do the lines connecting boxes in a hierarchy chart illustrate?The relationship of functions on the hierarchy chart.
Does a hierarchy chart show the steps taken inside a function?No, it only shows the relationship between functions.
What function can be used to make a program wait for user input?input function
What is a local variable?A variable assigned a value inside a function and only available to that function.
Which function does a local variable belong to?The function in which it was created
Can statements inside another function access a local variable?No, an error will occur
What is the scope of a local variable?The function in which it was created
Can different functions have local variables with the same name?Yes, this allows multiple teams to collaborate and divide a project without having to coordinate variables used by the functions they author.
Can a function access another function's local variables?No, each function does not see the other function's local variables
What is an argument?A piece of data that is sent into a function
What is a parameter variable?Variable assigned the value of an argument in a function call.
What is the scope of a parameter?The function in which the parameter is used. Parameters are local variables defined by the function parameter list.
How are multiple arguments passed to a function?Separated by commas in the parameter list.
What happens when changes are made to a parameter value within a function?The argument remains unchanged.
What is a keyword argument?An argument that specifies which parameter to pass the value to. An example is the sep and end parameters of the print() function.
Can keyword and positional arguments be mixed when calling a function?Yes, but positional arguments must appear first.
What is a global variable?A variable created outside all functions and can be accessed by any statement in the program file.
What must be done to assign a value to a global variable within a function?The global variable must be redeclared within the function.
Why should the use of global variables be avoided?They make debugging difficult and can cause dependency issues in functions.
What are global constants?Global name that references a value that cannot be changed during the execution of the program.
Why do global variables make a program hard to understand?They make the function hard to transfer to another program. They make the logical flow of the program confusing. They create issues when modifying and maintaining your source code.
How can you simulate a global constant in Python?Create a global variable and do not re-declare it within functions.
What is a void function?A group of statements within a program for performing a specific task.
When should you call a function?When you need to perform a specific task.
What is a value-returning function?A function that returns a value to the part of the program that called it.
What are standard library functions?Pre-written functions that are included and installed with Python.
How are library functions viewed by programmers?As a 'black box' that performs common tasks. You can call the function, but you may not have access to the source code of the function. A programmer can see the input and output but not the internal processing of the function.
What are modules in Python?Files that store functions of the standard library and help organize them.
What is an import statement used for?To call a function stored in a module.
What is the format of an import statement?import module_name
Why are random numbers useful in programming?They are useful in a lot of programming tasks. You may need a randomly generated number when programming simulations of games.
What does the random module include?Library functions for working with random numbers. Includes functions like randint(), randrange(), and random().
What is dot notation used for?To call a function belonging to a module.
What is the format of dot notation?module_name.function_name()
What does the randint function do?Generates a random integer in the specified range. The arguments include the start and end values of the range. Unlike the range() function, the ending limit is included in the range.
Where is the random number returned to?To the part of the program that called the function.
What can the returned integer from randint be used for?Anywhere that an integer would be used.
How can you experiment with the randint function?You can learn about the randint() function by experimenting with it in interactive mode.
What is the randrange function?Returns randomly selected integer from a sequence, unlike randint() function, the randrange() function does not include the ending limit in the range.
What are the arguments for the randrange function?Same as the range function
What is the random function?Returns a random float between 0.0 and 1.0
Does the random function receive any arguments?No, it does not receive any arguments
What is the uniform function?Returns a random float within a specified range
What are random number seeds?Seed values that initialize the formula for generating random numbers
Why do we need to use different seeds?To get different series of random numbers
What is the default seed value used by random functions?System time
How can we specify a desired seed value?By using the random.seed() function
How do we write a value-returning function?Write a function and add one or more return statements
What is the format of a return statement?return expression
What is returned to the calling part of the program?The value of the expression in the return statement
Can the expression in the return statement be complex?Yes, it can be a complex expression
In what situations can value-returning functions be useful?Prompting user for input, simplifying mathematical expressions, complex calculations, using the returned value
What is an IPO chart?A chart that describes the input, processing, and output of a function
What is the purpose of an IPO chart?To design and document functions, displays the input, processing and output of a function.
How are IPO charts typically laid out?In columns
What information is included in an IPO chart?Brief descriptions of input, processing, and output
Can functions return strings?Yes, functions can return strings
What are boolean values?Values that are either true or false
What is a boolean function?Returns True or False
How are boolean functions used?To test conditions and simplify code
What are some common calculations that can be easily repeated using boolean functions?Checking if a number is even
What is the format for returning multiple values in Python?return expression1, expression2, etc.
What is the math module?Part of the standard library for mathematical calculations
What is the purpose of the import math statement?To use the math module in a program
What are the variables pi and e in the math module?Global Constants used as mathematical values for pi and e.
How can the constants pi and e be used in equations?To use the constants pi and e from the math module, use dot notation in the same way you call the functions in the math module. For example, circle area = radius**2 * math.pi
What is modularization?Grouping related functions in modules
What are the benefits of modularization?Easier understanding, testing, and maintenance of code
How can modularized code be reused?By importing the module into different programs
What are the rules for module names?Modules are files with filenames that end in .py, similar to source code files. Module names cannot be a Python keyword.
What is a menu-driven program?A program that displays a list of operations for the user to select
What is a menu?List of options to call functions in your program and displayed as a list on the screen.
Why do you use a decision structure in a menu-driven program?Used to determine the selected menu option and required operation.
What happens typically in a menu-driven program?Based on the option selected, a function is called and executed. The menu of options will be displayed until the user selects the option to quit the program.