Uploaded by Frank D

Computer Science Python Computer Science Test Functions

advertisement
random()random float number from 0-1
randrange()returns an integer within a range
seed()sets random number seed
choice()selects an element at random from a collection of elements
The seed value determines the e<span class=cloze>[...]</span> the program generatesThe seed value determines the e<span class=cloze>xact sequence of numbers</span> the program generates<br> variables inside a function are called <span class=cloze>[...]</span>variables inside a function are called <span class=cloze>local variables</span><br> <span class=cloze>[...]</span> are called local variables<span class=cloze>variables inside a function</span> are called local variables<br> three types of functions in pythonbuilt in functions<br>user defined functions<br>anonymous functions
what are built in functionsfunctions such as print() (functions built into python)
what are user defined functionsfunctions that users create - used by other users<br>declared with def keyword
what does UDF stand foruser defined functions<br>
what are anonymous functionsaka lamda functions, not declared with def
a <span class=cloze>[...]</span> is a piece of code written to carry out a specified taska <span class=cloze>function</span> is a piece of code written to carry out a specified task<br> a function is a piece of code written to <span class=cloze>[...]</span> a specified taska function is a piece of code written to <span class=cloze>carry out</span> a specified task<br> a function is a <span class=cloze>[...]</span> written to carry out a specified taska function is a <span class=cloze>piece of code</span> written to carry out a specified task<br> a function is a piece of code written to carry out a <span class=cloze>[...]</span>a function is a piece of code written to carry out a <span class=cloze>specified task</span><br> the function might or might not need <span class=cloze>[...]</span>the function might or might not need <span class=cloze>multiple inputs</span><br> the function can return one value or return multiple values but must be assigned to <span class=cloze>[...]</span>the function can return one value or return multiple values but must be assigned to <span class=cloze>multiple variables</span><br> the function can return one value <span class=cloze>[...]</span> but must be assigned to multiple variablesthe function can return one value <span class=cloze>or return multiple values</span> but must be assigned to multiple variables<br> the function can <span class=cloze>[...]</span> or return multiple values but must be assigned to multiple variablesthe function can <span class=cloze>return one value</span> or return multiple values but must be assigned to multiple variables<br> <div>It is possible that a function <span class=cloze>[...]</span>.</div><div>It is possible that a function <span class=cloze>may not return any value</span>.</div><br> Parameters are the names used <span class=cloze>[...]</span>.Parameters are the names used <span class=cloze>when defining a function or a method</span>.<br> Parameters are the <span class=cloze>[...]</span> when defining a function or a method.Parameters are the <span class=cloze>names used</span> when defining a function or a method.<br> Arguments are mapped into <span class=cloze>[...]</span>Arguments are mapped into <span class=cloze>parameters</span><br> arguments are the <span class=cloze>[...]</span> (or SENT) to any functionarguments are the <span class=cloze>things which are supplied</span> (or SENT) to any function<br> arguments are the things which are supplied (or SENT) <span class=cloze>[...]</span>arguments are the things which are supplied (or SENT) <span class=cloze>to any function</span><br> use the keyword <span class=cloze>[...]</span> to declare the functionuse the keyword <span class=cloze>def</span> to declare the function<br> four steps to defining a function in python:<br>1. keyword def to declare the function<br>2. add <span class=cloze>[...]</span> to the function, end line with a colon<br>3. add statements for the function to execute<br>4. return statement <br>four steps to defining a function in python:<br>1. keyword def to declare the function<br>2. add <span class=cloze>parameters</span> to the function, end line with a colon<br>3. add statements for the function to execute<br>4. return statement <br><br> four steps to defining a function in python:<br>1. keyword def to declare the function<br>2. add parameters to the function, end line with a <span class=cloze>[...]</span><br>3. add statements for the function to execute<br>4. return statement <br>four steps to defining a function in python:<br>1. keyword def to declare the function<br>2. add parameters to the function, end line with a <span class=cloze>colon</span><br>3. add statements for the function to execute<br>4. return statement <br><br> four steps to defining a function in python:<br>1. keyword def to declare the function<br>2. add parameters to the function, end line with a colon<br>3. add <span class=cloze>[...]</span> for the function to execute<br>4. return statement <br>four steps to defining a function in python:<br>1. keyword def to declare the function<br>2. add parameters to the function, end line with a colon<br>3. add <span class=cloze>statements</span> for the function to execute<br>4. return statement <br><br> four steps to defining a function in python:<br>1. keyword def to declare the function<br>2. add parameters to the function, end line with a colon<br>3. add statements for the function to execute<br>4. <span class=cloze>[...]</span> <br>four steps to defining a function in python:<br>1. keyword def to declare the function<br>2. add parameters to the function, end line with a colon<br>3. add statements for the function to execute<br>4. <span class=cloze>return statement</span> <br><br> four steps to defining a function in python:<br>1. keyword <span class=cloze>[...]</span> to declare the function<br>2. add parameters to the function, end line with a colon<br>3. add statements for the function to execute<br>4. return statement <br>four steps to defining a function in python:<br>1. keyword <span class=cloze>def</span> to declare the function<br>2. add parameters to the function, end line with a colon<br>3. add statements for the function to execute<br>4. return statement <br><br> Remember also that you can define <span class=cloze>[...]</span> for your UDFRemember also that you can define <span class=cloze>one or more function parameters</span> for your UDF<br> Remember also that you can define one or more function parameters for your <span class=cloze>[...]</span>Remember also that you can define one or more function parameters for your <span class=cloze>UDF</span><br> <div>IF YOU DO NOT HAVE A RETURN THEN IT IS MORE EFFICIENT TO  <span class=cloze>[...]</span> THE RETURN IN A FUNCTION</div><div>IF YOU DO NOT HAVE A RETURN THEN IT IS MORE EFFICIENT TO  <span class=cloze>OMIT</span> THE RETURN IN A FUNCTION</div><br> functions <span class=cloze>[...]</span> when it comes across a <span class=cloze>[...]</span>functions <span class=cloze>immediately exit</span> when it comes across a <span class=cloze>return statement</span><br> "<div><span style=""color: rgb(76, 136, 107);"">#what does this output:<br># Define `plus()`</span></div> <div><span style=""color: blue;"">def</span> plus(a,b):</div> <div>  <span style=""color: rgb(192, 0, 0);"">sum</span> <span style=""color: rgb(49, 132, 149);"">=</span> a <span style=""color: rgb(49, 132, 149);"">+</span> b</div> <div>  <span style=""color: blue;"">return</span> (<span style=""color: rgb(192, 0, 0); background-color: yellow;"">sum</span><span style=""background-color: yellow;"">, a</span>)</div> <div><span style=""color: rgb(76, 136, 107);""># Call `plus()` and unpack variables </span></div> <div><span style=""color: rgb(192, 0, 0); background-color: yellow;"">sum</span><span style=""background-color: yellow;"">, a</span> <span style=""color: rgb(49, 132, 149);"">=</span> plus(<span style=""color: rgb(0, 0, 64);"">3</span>,<span style=""color: rgb(0, 0, 64);"">4</span>)</div> <div><span style=""color: rgb(76, 136, 107);""># Print `sum()`</span></div> <div><span style=""color: blue;"">print</span>(<span style=""color: rgb(192, 0, 0);"">sum</span>)</div>"7
"types of arguments in python UDF's"default<br>required<br>keyword<br>variable number
what are default argumentsarguments that can take a default value if no arguments are passed during the call<br>(for example: (bold is default argument)<br>def plus(a, <b>b=2):</b>
required arguments are arguments that need to be passed in the right order and <span class=cloze>[...]</span>required arguments are arguments that need to be passed in the right order and <span class=cloze>right amount</span><br> example:<br>def plus(a,b):
required arguments are arguments that need to be passed in the <span class=cloze>[...]</span> and right amountrequired arguments are arguments that need to be passed in the <span class=cloze>right order</span> and right amount<br> example:<br>def plus(a,b):
<span class=cloze>[...]</span> arguments are arguments that need to be passed in the right order and right amount<span class=cloze>required</span> arguments are arguments that need to be passed in the right order and right amount<br> example:<br>def plus(a,b):
keyword arguments are arguments that use the parameter name to make sure that <span class=cloze>[...]</span>keyword arguments are arguments that use the parameter name to make sure that <span class=cloze>the parameters are in the right order</span><br> example:<br><br>plus(a=1, b=2)
<span class=cloze>[...]</span> are arguments that use the parameter name to make sure that the parameters are in the right order<span class=cloze>keyword arguments</span> are arguments that use the parameter name to make sure that the parameters are in the right order<br> example:<br><br>plus(a=1, b=2)
keyword arguments are arguments that use the <span class=cloze>[...]</span> to make sure that the parameters are in the right orderkeyword arguments are arguments that use the <span class=cloze>parameter name</span> to make sure that the parameters are in the right order<br> example:<br><br>plus(a=1, b=2)
"variable number of arguments are for when you don't know the <span class=cloze>[...]</span>. ""variable number of arguments are for when you don't know the <span class=cloze>exact number of arguments</span>. <br> "
"<span class=cloze>[...]</span> are for when you don't know the exact number of arguments. ""<span class=cloze>variable number of arguments</span> are for when you don't know the exact number of arguments. <br> "
variable number of arguments are used with the <span class=cloze>[...]</span> syntaxvariable number of arguments are used with the <span class=cloze>*</span> syntax<br> The asterisk (*) is placed before the variable name that holds the values of <span class=cloze>[...]</span> variable arguments.The asterisk (*) is placed before the variable name that holds the values of <span class=cloze>all non keyword</span> variable arguments.<br> The <span class=cloze>[...]</span> is placed before the variable name that holds the values of all non keyword variable arguments.The <span class=cloze>asterisk (*)</span> is placed before the variable name that holds the values of all non keyword variable arguments.<br> As programs get <span class=cloze>[...]</span> it is important to subdivide them into subprograms.As programs get <span class=cloze>bigger</span> it is important to subdivide them into subprograms.<br> As programs get bigger it is important to <span class=cloze>[...]</span> them into <span class=cloze>[...]</span>.As programs get bigger it is important to <span class=cloze>subdivide</span> them into <span class=cloze>subprograms</span>.<br> 3 benefits of breaking a programming object into functions:<br>cleaner code<br>reusability<br>data protection
three parts of a function definitionname<br>parameter<br>body<br>
return types in pythonfunctions that return a value<br>functions that do an action but do not return a value
Variables defined within functions are <span class=cloze>[...]</span>Variables defined within functions are <span class=cloze>local variables</span><br> Variables defined <span class=cloze>[...]</span> are local variablesVariables defined <span class=cloze>within functions</span> are local variables<br> local variables propertiesonly use memory when the program is running<br>same variable name can be used in many functions
a global variable is <span class=cloze>[...]</span>a global variable is <span class=cloze>defined outside of all functions</span><br> a <span class=cloze>[...]</span> variable is defined outside of all functionsa <span class=cloze>global</span> variable is defined outside of all functions<br> Any <span class=cloze>[...]</span> can legally access and/or modify a global variableAny <span class=cloze>function</span> can legally access and/or modify a global variable<br> Any function can legally access and/or modify a <span class=cloze>[...]</span>Any function can legally access and/or modify a <span class=cloze>global variable</span><br> Any function can legally <span class=cloze>[...]</span> a global variableAny function can legally <span class=cloze>access and/or modify</span> a global variable<br> 
Download