COMP 101: CH 5

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

standard library

A collection of modules that are part of the normal installation of Python.

In Python a module is: A. A file containing Python definitions and statements intended for use in other Python programs. B. A separate block of code within a program. C. One line of code in a program. D. A file that contains documentation about functions in Python.

A file containing Python definitions and statements intended for use in other Python programs.

module

A file containing Python definitions and statements intended for use in other Python programs. The contents of a module are made available to the other program by using the import statement.

random number generator

A function that will provide you with random numbers, usually between 0 and 1.

random number

A number that is generated in such a way as to exhibit statistical randomness.

pseudo-random number

A number that is not genuinely random but is instead created algorithmically.

documentation

A place where you can go to get detailed information about aspects of your programming language.

deterministic

A process that is repeatable and predictable. always resulting in a particular pattern

To find out information on the standard modules available with Python you should: A. Go to the Python Documentation site. B. Look at the import statements of the program you are working with or writing. C. Ask the professor D. Look in this textbook.

A. Go to the Python Documentation site.

Which statement allows you to use the math module in your program? A. import math B. include math C. use math D. You don't need a statement. You can always use the math module

A. import math The module must be imported before you can use anything declared inside the module.

The correct code to generate a random number between 1 and 100 (inclusive) is: A. prob = random.randrange(1, 101) B. prob = random.randrange(1, 100) C. prob = random.randrange(0, 101) D. prob = random.randrange(0, 100)

A. prob = random.randrange(1, 101)

Which module would you most likely use if you were writing a function to simulate rolling dice? A. the math module B. the random module C. the turtle module D. the game module

B. the random module

One reason that lotteries don't use computers to generate random numbers is:

Because computers don't really generate random numbers, they generate pseudo-random numbers.

Do computer generated random numbers truly random? Why or why not? Describe a common algorithm for random number generation.

Computer generated numbers are not truly random because they start with a seed value. Computer generated numbers are pseudo-random. Virtually flipping a coin would be considered a random number generator. It could choose value of 1 for heads and value 0 for tails. Using a random number generator would randomly select between the two values.

math.floor()

Does not round; gets rid of all decimals ex. 1.8456563 becomes just 1

True / False: All standard Python modules will work in activecode.

False nly turtle, math, and random have been ported to work in activecode at this time.

random.uniform(min,max)

Generates random floating point values from min to max, where min is included and max is excluded.

How should turtle.Turtle be read?

In the module turtle, access the Python element called Turtle

What do you have to do first in order to use modules

Must first import the module -shows you ned to use it. -Once the module is imported , you can use the objects, functions, and constants defined in the imported modules

what is math.pow(2,3) do?

Raises 2 to the third power.

What does the math module contain?

The math module contains the kinds of mathematical functions you would typically find on your calculator and some mathematical constants like pi and e.

random() function random.random()

The random() function returns a floating point number in the range [0.0, 1.0) 0.0 is possible, but all returned numbers will be strictly less than 1.0 *** if you multiply by 10 you will move decimal to the right 1

dot notation

The syntax for calling a function in another module by specifying the module name followed by a dot (period) and the function name. providing the module name and the specific item joined together with a "dot"

What are uses for random module

To play a game of chance where the computer needs to throw some dice, pick a number, or flip a coin, To shuffle a deck of playing cards randomly, For encrypting your banking session on the Internet.

3 ways to import a module

import x - ex. import math result = math.sqrt(16) from x import* - ex. from math import* result=sqrt(16) from x import a,b,c - imports the module x and create references in the current namespace to given objects of that module. -ex. from math import sqrt result=sqrt(16) import

Which of the following is the correct way to reference the value pi within the math module. Assume you have already imported the math module. A. math.pi B. math(pi) C. pi.math D. math->pi

math.pi To invoke or reference something contained in a module you use the dot (.) notation.

Where do you find the right modules for Python?

on-line Python documentation Global Python Module Index

How do you randomly select a value from a list?

random.choice(list) -generates a value chosen from a list - you have to have a list first --create a list with a variable. ex. stuList = ['Joe','Shmoe','Low', 'fofo'] random.choice(stuLlist) wil select a value from the list

randrange function random.randrange(low end, high end)

similar to range function, but returns randomly selected integer from the resulting sequence function generates an integer between its lower and upper argument— so the lower bound is included, but the upper bound is excluded. All the values have an equal probability of occurring

true/false: Mathematical functions do not need to be constructed

true They simply perform a task. They are all housed together in a module called math. Once we have imported the math module, anything defined there can be used in our program.

what doed import math do?

we create a reference to a module object that contains these elements. sqrt function sin function cos function pi e

math.ceil()

you get the higher # round up ex. 4.12654 becomes 5


Ensembles d'études connexes

Food and Human Environment Final

View Set

Environmental Science 1127 Test 4

View Set

Google Apps, Plug-ins and Extensions

View Set

Correct statement about Coleoptera larvae

View Set