CIS-225-I01 Introduction in Object-Oriented Programming - zyBooks Module 1 Lesson 5 Chapter 2 Secs 2.6-2.8

Ace your homework & exams now with Quizwiz!

2) Given a non-negative number x, which expression has the range -10 to 10? x % -10 (x % 21) - 10 (x % 20) - 10

(x % 21) - 10 x % 21 yields 0 to 20. Then - 10 yields -10 to 10.

2.6.3: Modulo examples. 1) Given a non-negative number x, which expression has the range of 5 to 10? x % 5 x % 10 x % 11 (x % 6) + 5

(x % 6) + 5 x % 6 yields 0 to 5. Then + 5 yields 5 to 10.

3) Which expression gets the tens digit of x? Ex: If x = 693, which expression yields 9? x % 10 x % 100 (x / / 10) % 10

(x / / 10) % 10 x / / 10 shifts right one place, putting the tens digit in the ones place. Then % 10 gets the (new) ones digit. Ex: 693 / / 10 is 69, then 69 % 10 is 9.

2.8.7: Using a unique seed for each program run. 1) In the absence of a seed being supplied initially, the expression random.randint(x, y) yields a different integer sequence with each successive iteration. True False

True By default the expression random.randint(x, y) uses a number based on the current time as the seed. Thus, each successive execution of the expression will yield a random integer.

3) randint() generates a "pseudo-random" sequence of values because the sequence begins repeating itself after about 20 numbers. True False

False The term "pseudo" refers to the sequence being the same for each seed, with the next "random" number being derived from the preceding one. The sequence will not exhibit a repeating pattern.

2.6.2: Compute change. A cashier distributes change using the maximum number of five-dollar bills, followed by one-dollar bills. Write a single statement that assigns num_ones with the number of distributed one-dollar bills given amount_to_change. Hint: Use %. Sample output with input: 19 Change for $19 3 five dollar bill(s) and 4 one dollar bill(s)

amount_to_change = int(input()) num_fives = amount_to_change // 5 num_ones = amount_to_change % 5 print('Change for $', amount_to_change) print(num_fives, '$5 bill(s) and', num_ones, '$1 bill(s)')

sqrt() is known as a ______, which is a list of statements that can be executed simply by referring to the function's name.

function

The process of invoking a function is referred to as a _______ ____.

function call

Figure 2.7.1: Importing the math module and calling a math module function.

import math num = 49 num_sqrt = math.sqrt(num)

2.7.3: Using math functions to calculate the distance between two points. Assign point_dist with the distance between point (x1, y1) and point (x2, y2). The calculation is:Distance = SquareRootOf( (x2 - x1)2 + (y2 - y1)2 ). Sample output with inputs: 1.0 2.0 1.0 5.0 Points distance: 3.0

import math x1 = float(input()) y1 = float(input()) x2 = float(input()) y2 = float(input()) point_dist = math.sqrt((math.pow((x2 - x1), 2)) + (math.pow((y2 - y1), 2))) print(f'Points distance: {point_dist:.1f}')

2.8.1: Random number basics. 1) What import statement is used to generate random numbers? import random_number import random

import random The random module in Python provides methods that are used to generate random values.

2.8.6: Random integer example: Moving seats. Example: Randomly moving a student The following program randomly moves a student from one seat to another seat in a lecture hall, perhaps to randomly move students before an exam. The seats are in 20 rows numbered 1 to 20. Each row has 30 seats (columns) numbered 1 to 30. The student should be moved from the left side (columns 1 to 15) to the right side (columns 16 to 30). Consider the above example. 1) The expression random.randint(1, 20) is used to generate a random row integer. random.randint(1, 20) is _____. inclusive exclusive

inclusive The expression is inclusive and therefore can produce a value ranging from 1 to 20, not 1 to 19.

The ________ module, in the Python Standard Library, provides methods that return random values.

random

The _________ method returns a random floating-point value each time the function is called, in the range 0 (inclusive) to 1 (exclusive).

random()

5) Which expression best mimics the random outcome of flipping a coin? random.randrange(1) random.randrange(2) random.randrange(3)

random.randrange(2) random.randrange(2) returns 0 or 1. Those values can reflect a coin flip's outcome, heads or tails.

4) Which expression generates a random integer in the range 0 to 30? random.randrange(29) random.randrange(30) random.randrange(31)

random.randrange(31) random.randrange(31) returns an integer with one of 31 possible values between 0 and 30.

Python's _________ method generates random integers within a specified range.

randrange()

Pseudo-random The numbers generated by the random module are known as pseudo-random. "Pseudo" means "not actually, but having the appearance of." Internally, the random module has an equation to compute the next "random" number from the previous one, (invisibly) keeping track of the previous one. For the first call to any random method, no previous random number exists, so the method uses a built-in integer based on the current time, called a ____, to help generate a random number. Since the time is different for each program run, each program will get a unique sequence.

seed

4) Given a 16-digit credit card number stored in x, which expression gets the last (right-most) four digits? (Assume the fourth digit from the right is non-zero). x / 10000 x % 10000

x % 10000 x % 10000 yields 0 - 9999, being the right-most four digits. To get other digits like the next four digits, divide first to shift the desired digits to the right-most digits, then use % to get just those digits.

The item passed to a function is referred to as an ________.

argument

2.6.2: Modulo. Determine the result. Type "Error" if appropriate. Only literals appear in these expressions to focus attention on the operators; most practical expressions include variables. 1) 50 % 2 |?|

0 50 / 2 is 25 with remainder 0.

2) What is the smallest possible value returned by random.randrange(10)? 0 1 9

0 The range of randrange(10) starts with 0, which is the smallest possible value.

2) The random number returned by random.random() will be in what range? 0 (inclusive) to 9 (exclusive) -100 (inclusive) to 100 (exclusive) 0 (inclusive) to 1 (exclusive)

0 (inclusive) to 1 (exclusive) Values between 0 (inclusive) and 1 (exclusive) are within the range of random().

2) 5 / 10 |?|

0.5 Division always yields a floating point.

2) 51 % 2 |?|

1 Note that any odd number % 2 is 1, while any even number % 2 is 0.

4) How many possible values can be produced by random.randint(-5, 5)? |?|

11 -5 to 5 has 11 possible values: -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5.Ranges involving negatives are treated the same as ranges with only positives.

2) The expression random.randint(1, _____ ) generates a random column within the left half. 15 20

15 The lecture hall contains 30 columns numbered 1 to 30. The left half has columns 1 to 15.

3) z = 4.5 z = math.pow(math.floor(z), 2.0) |?|

16.0 math.floor(z) returns 4. Then, math.pow(4, 2.0) returns 16.0.

2) x = 2.3 z = math.floor(x) |?|

2 math.floor(2.3) returns 2. (floor function = round down)

3) 5.0 // 2 |?|

2.0 Floor division rounds the result of a floating point division down to the closest whole number, represented in a floating point. So 2.5 is rounded down to 2.0.

2.8.3: randrange() method. 1) random.randrange(20) returns an integer with how many possible values? 21 20 19

20 randrange(20) returns a random integer between 0 and 19 (one of 20 possible values). The method randrange() excludes the upper bound of the argument (20).

The floor division operator // can be used to round down the result of a floating-point division to the closest smaller whole number value. The resulting value is an integer type if both operands are integers; if either operand is a float, then a float is returned:

20 // 10 is 2. 50 // 50 is 1. 5 // 10 is 0. (5/10 is 0 and the remainder 5 is thrown away). 5.0 // 2 is 2.0 For division, the second operand of / or // must never be 0, because division by 0 is mathematically undefined.

5) z = 4 z = math.factorial(z) |?|

24 4 * 3 * 2 * 1 is 24. (Just go with it :/)

2.7.1: Variable assignments with math functions. Determine the final value of z. 1) x = 2.3 z = math.ceil(x) |?|

3 math.ceil(2.3) returns 3. (ceil function = round up)

2.6.1: Division and floor division. Determine the result. Type "Error" if the program would terminate due to division by 0. If the answer is a floating-point number, answer in the form #.#, even if the answer is a whole number. 1) 12 / 4 |?|

3.0 Division always yields a floating point.

6) How many values are in the range -4 to 0 using the expression random.randrange(-4, 0) ? |?|

4 -4 to 0 has 4 possible values: , -4, -3, -2, -1.When given a min and a max, ranges involving negatives using randrange() are treated the same as ranges with only positives.

5) The expression random.randint( _____ ) produces 9 possible values between 4 and 12. |?|

4, 12 The two arguments (4, 12) can be passed to the randint() method to return an integer between 4 and 12. The expression's possible values: 4, 5, 6, 7, 8, 9, 10, 11, 12.

4) z = 15.75 z = math.sqrt(math.ceil(z)) |?|

4.0 math.ceil(z) returns 16. Then, math.sqrt(16) returns 4.0.

3) What is the largest possible value returned by random.randrange(45)? 44 45 46

44 The method randrange(45) returns an integer value between 0 and 44.

2) The expression random.randrange( _____ ) produces a random integer between 0 and 4. |?|

5 or 0, 5 0 to 4 has 5 possible values: 0, 1, 2, 3, 4.A single argument (5) or two arguments (0, 5) can be passed to the randrange() method to return an integer between 0 (inclusive) and a specified value 5 (exclusive).

3) How many possible values can be produced by random.randint(10, 15)? |?|

6 10 to 15 has 6 possible values: 10, 11, 12, 13, 14, 15.15 − 10 + 1 = 6. A common mistake is to forget the + 1.

4) 596 % 10 |?|

6 Note that any number % 10 yields the digit in the right-most (1s) place, in this case 6.

2.8.5: Generating random integers in a specific range. 1) The expression random.randrange( _____ ) returns one of 6 possible integers between 0 and 5. |?|

6 or 0, 6 A single argument (6) or two arguments (0, 6) can be passed to the randrange() method to return an integer between 0 (inclusive) and a specified value 6 (exclusive).

3) 78 % 10

8 78 / 10 is 7 with remainder 8.

import random # Generates random integers with 3 possible values print(random.randrange(3)) print(random.randrange(3)) print(random.randrange(3)) print(random.randrange(3)) print(random.randrange(3))

Each random.randrange(3) statement returns a random integer with 3 possible values: 0, 1, and 2. Output: 2 1 0 0 2

5) 100 % (1 // 2) |?|

Error (1 // 2) evaluates to a 0. 100 % 0 is undefined (as 100 / 0 is undefined) and causes the program to terminate.

4) 100 / 0 |?|

Error 100 / 0 is undefined and causes the program to terminate.

2.6 Division and modulo Division: Integer rounding The division operator / performs division and returns a floating-point number.

Ex: 20 / 10 is 2.0. 50 / 50 is 1.0. 5 / 10 is 0.5.

Modulo (%) The basic arithmetic operators include not just +, -, *, /, but also %. The modulo operator (%) evaluates the remainder of the division of two integer operands. Ex: 23 % 10 is 3.

Examples: 24 % 10 is 4. Reason: 24 / 10 is 2 with remainder 4. 50 % 50 is 0. Reason: 50 / 50 is 1 with remainder 0. 1 % 2 is 1. Reason: 1 / 2 is 0 with remainder 1.

3) The random right column can be generated with random.randrange(16, 30). True False

False The expression random.randrange(16, 30) produces a random integer between 16 and 29, not 16 and 30. The expressions random.randrange(16, 31) or random.randint(16, 30) produce a random number within the correct range, 16 to 30.

2) Generating a random integer with the expression random.randint(0, 5) using random.seed(3) yields a different integer sequence for each program run. True False

False With a specific seed, the random number sequence will be the same for every program run. Ex: The sequence may be 1, 5, 3. Every program run will yield that same sequence.

2.7 Math module While basic math operations like + or * are sufficient for some computations, programmers sometimes wish to perform more advanced math operations such as computing a square root. Python comes with a standard math module to support such advanced math operations.

A module is Python code located in another file. The programmer can import the module for use in their own file, or in an interactive interpreter. The programmer first imports the module to the top of a file.


Related study sets

social media exam review part 1 !!

View Set

CIS 377 Midterm/Final Quiz Questions

View Set

Chap 20 Disorders of Hearing and Vestibular Function

View Set

First Aid Test Review (Principles of Health Science)

View Set