Numerical Programming for Engineers

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Write example code showing the difference between a function call and a function definition.

#function definition def math(a, b): c = a + c return c; #function call d = math(2, 8)

What does the % operator do?

% - it is used to get the remainder of a division

Use five pairs of parentheses to show the order in which the following expression will be calculated: 2/4*5**3**2/4

((2/4)*(5**(3**(2/4))))

What is "TDD"?

(Test-Driven Development) Write the test first, then code.

Is ** a unary or binary operator, and what does it do?

** is a binary operator because it requires two operands, for example, two integers.

What does -8//3 calculate to?

-8/3 = -2.66667 -8//3 = -3

What does the // operator do?

// - it is used for floor division. It rounds the result of a division down to an integer.

What exactly do the statements x = 6; x /= 3; print (x); give as a result?

2

What is the convention for the number of spaces in a single indentation?

4 spaces as per Python Enhancement Proposal 8 (PEP8)

What does 8%-3 calculate to?

8/-3 = 2 remainder -1 8%-3 = -1

What do = and == operators do?

= - it is used for assigning a variable to a type == - it is used to check for equality

What is a "module", why can it be helpful?

A file that defines a collection of useful functions and variables that, have a common theme/purpose. It can be use to call multiple functions from different modules.

What is "pseudocode", why can it be helpful?

A notation resembling a simplified programming language, used in program design. It helps by getting all the ideas on paper to then build the actual code.

What is a "program"?

A set of instructions, typically written by a human, that are to be executed by a computer.

Briefly describe the difference between a "deep copy" and a "shallow copy".

A shallow copy assigns one array to another array. The arrays will share the same data. x = np.array([1.0, 2.0, 3.0]) u = x u[0] = 100.0 print(x[0]) Displays: 100.0 A deep copy uses the copy method to just copy the data of a certain array. x = np.array([1.0, 2.0, 3.0]) u= x.copy() u[0] = 100.0 print(x[0]) Displays: 1.0

What is a "software bug"?

An error (or flaw/failure) in software code that causes an incorrect or unintended result.

What is an "expression"?

An expression is built out of operands and operators.

What is an "IDE"?

An integrated development environment (IDE) is an application that helps facilitate the editing , building, and debugging of software.

What is "quality assurance"?

Checking that software does what its specifications indicate it is supposed to do.

Explain what self.assertAlmostEqual (a, b, places = 5) is used for.

Checks that a and b are "close" by computing their difference, rounding to places decimal places, and comparing to zero.

What is a "statement" in a program?

Commands in a program that a computer understands.

True or False: Relational operations take precedence over arithmetic operations.

False

True or False: q == Q

False

What is import used for? Give an example.

Import is used to import packages to the module. For example: import numpy The package numpy can be used in the module.

What is a "PEP"?

Is a Python Enhancement Proposal.

What is a "package"?

It is a set of classes and functions that extend the standard Python library of types and functions.

What is a list?

Lists re compound data types used to group other type values.

Explain what machine epsilon represents.

Machine epsilon is the absolute difference between 1 and the next number that can be represented in a particular system.

What is an "operand"?

Operands are usually types, which are a set of values and operations on those values. operands: -2, 3.14159, "hi!"

What is a "relational operator" (include an example)?

Relational operators compares types by creating boolean values. An example is ==. The == operator checks for equality and outputs a boolean value of True or False.

Suppose the value π is hard-coded in a program as pi = 3.14. Show how to calculate both the relative error and the absolute error of this value with respect to the more precise value of 3.14159.

Relative Error (3.14 - 3.14159) = -0.00159 (-0.000159/3.14159) x 100% = -0.050611 Absolute Error |3.14 - 3.14159| = 0.00159

Give an example of what the .append () method is useful for.

The append method adds one more element to a set list. For example: x = [1, 2, 3, 4, 5] x.append(6) print(x) Displays [1, 2, 3, 4, 5, 6]

Give an example of what an assert statement is useful for.

The assert statement is a convenient way to insert debugging assertions into a program. For example: assert condition 1) Here, condition should evaluate to a bool. 2) If condition = True, nothing happens. 3) If condition = False, the program terminates with an error.

What is the order of precedence (in decreasing order) of the boolean operators and, not & or?

The boolean operators have the following precedence: 1. not has the highest precedence (i.e. applied first) 2. and has the next highest precedence 3. or has the lowest precedence

Explain what the *= compound operator does.

The compound operator *= multiplies a variable with an integer or float value and then assigns it to the same variable. For example x = x*2 could be writing x *= 2.

Explain what the extra "hidden bit" (aka "implicit bit") does in an IEEE-754 float.

The extra hidden bit gives 53 bits in the mantissa.

List four "conventions worth following" of a thorough docstring for a function.

The first line should be a short, concise summary of the function. The first line should be a phrase ending in a period. If there are more lines of documentation, there should be a blank line separating the first line from these additional lines. Indent lines consistently.

What is the difference between a parameter and an argument (e.g., in a function definition)?

The function's parameters are variables used in the code block. def function_name("parameters") The values assigned to the parameters are called arguments.

What does the help function do?

The help functions allows users to see what an specific function does. For example: import function_name help(function_name)

Give an example of what the len() function does.

The len() function return the number of elements in a list. For example: x = [1, 2, 3, 4, 5] print(len(x)) Displays 5.

What is the purpose of an indentation?

The purpose of an indentation is to indicate what should be executed if the condition is True.

Explain how x = x**2 is a valid expression for any value of x

The right hand side is executing the value of x^2 and its assigning it to a x. Therefore x is now being assign its previous value but to the second power.

True or False: All relational operations have the same precedence.

True

True or False: Assert statements are intended for use in code you sell to a customer to use.

True

True or False: When a Python interpreter first encounters a function definition statement it does not execute it, nor any of the lines indented below it.

True

What are "unit testing" and "integration testing", and how are they different?

Unit test is an isolated test of a small, individual component of software. Integration testing is where one or more external dependencies are involved.

What are the rules for a valid variable name?

Variable names can use letters, numbers, and underscores must begin with a letter or underscore no dashes or other punctuation Python is case sensitive: x is different from X

What does a = np.linspace(0, 1, 5); print(a); display?

[0. 0.25 0.5 0.75 1.]

Given a list x of the digits 1 through 5, what will print (x[2::2]) display?

[3, 5]

In double precision IEEE-754 format. a) How many binary bits are used to store a real number? b)What information does the sign bit give? c) What information does the exponent give? d) What information does the significand (aka mantissa) give? e) Approximately how many decimal digits of precision does an IEEE-754 float have? f) Approximately what is the machine epsilon for an IEEE-754 float?

a) 64 bits b) The sign of the mantissa c) The exponent field represents either positive or negative exponents. d) The significand represents the precision bits of the number. e) 15-17 decimal digits of precision. f) = 1.11 x 10^-16

Give an example of a binary operator and in a few words explain what it does.

and - is a binary operator because it combines two bools into one.

Give examples of three "types" and describe each of them in a few words.

int - is a type for integers float - is a type for numbers with decimals string - is a type for words or statements

How would you express the second to last element in a list?

list[-2] - second to last element of a list

How would you express the third element of a list?

list[2] - third element of a list

List and briefly describe three attributes of an arbitrary NumPy ndarray.

ndim - the number of axes; 1 for vectors, 2 for matrices shape - the dimensions of the array, that is, the number of elements along each axis size - the total number of elements in the array

Give an example of unary operator and in a few words explain what it does.

not - is a unary operator that only acts on one operand (bool types)

If a function does not need to return anything, what does the return expression look like?

return None;

Circle the valid variable name? x1, 1x, _x1, _1x, (x1), (1x), x_1, 1_x

x1 _x1 _1x x_1


Ensembles d'études connexes

PNE 105 Med-Surg. Chapter 32: Caring for Clients with Disorders of the Lymphatic System

View Set

Ch. 10 review questions- physical geography

View Set

CSC200 Chapter 3 Review Questions

View Set

Small Animal Identification and Communication

View Set