Capítulo 5

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/35

flashcard set

Earn XP

Description and Tags

Checkpoints

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

36 Terms

1
New cards

What is a function?

A group of statements that exist within a program for the purpose of performing a specific task.

2
New cards

What is meant by the phrase “divide and conquer”?

A large task is divided into several smaller tasks that are easily performed.

3
New cards

How do functions help you reuse code in a program?

If a specific operation is performed in several places in a program, a function can be written once to perform that operation and then be executed any time it is needed.

4
New cards

How can functions make the development of multiple programs faster?

Functions can be written for the common tasks that are needed by the different programs. Those functions can then be incorporated into each program that needs them.

5
New cards

How can functions make it easier for programs to be developed by teams of programmers?

When a program is developed as a set of functions in which each performs an individual task, then different programmers can be assigned the job of writing different functions.

6
New cards

A function definition has what two parts?

A Header and a block.

  • Header: indicates the starting point of the function.

  • Block: a list of statements that belong to the function.

7
New cards

What does the phrase “calling a function” mean?

To execute the function.

8
New cards

When a function is executing, what happens when the end of the function’s block is reached?

The computer returns back to the part of the program that called the function, and the program resumes execution at that point.

9
New cards

Why must you indent the statements in a block?

Because the Python interpreter uses the indentation to determine where a block begins and ends.

10
New cards

What is a local variable? How is access to a local variable restricted?

  • A variable that is declared inside a function

  • A variable belongs to the function in which it is declared, and only statements in the same function can access it.

11
New cards

What is a variable’s scope?

The part of a program in which a variable may be accessed.

12
New cards

Is it permissible for a local variable in one function to have the same name as a local variable in a different function?

Yes, it is permissible.

13
New cards

What are the pieces of data that are passed into a function called?

Arguments

14
New cards

What are the variables that receive pieces of data in a function called?

Parameters

15
New cards

What is a parameter variable’s scope?

The entire function in which the parameter is declared.

16
New cards

When a parameter is changed, does this affect the argument that was passed into the parameter?

No, it does not.

17
New cards

The following statements call a function named show_data. Which of the statements passes arguments by position, and which passes keyword arguments?

  • show_data(name= ‘Kathryn’, age=25)

  • show_data(‘Kathryn', 25)

  • passes by keyword argument

  • passes by position

18
New cards

What is the scope of a global variable?

The entire program.

19
New cards

Give one good reason that you should not use global variables in a program.

(*hay dos más si quieres repasarlos en la página 692)

Functions that use global variables are usually dependent on those variables. If you want to use such a function in a different program, you will most likely have to redesign it so it does not rely on the global variable.

20
New cards

What is a global constant? Is it permissible to use global constants in a program? Why?

  • A name that is available to every function in the program.

  • It is permissible to use them.

    • bc their value cannot be changed during the program’s execution, you do not have to worry about its value being altered.

21
New cards

How does a value-returning function differ from the void functions?

The difference is that a value returning function returns a value back to the statement that called it. A simple function does not return a value.

22
New cards

What is a library function?

A prewritten function that performs some commonly needed task.

23
New cards

Why are library functions like “black boxes”?

The term “black box” is used to describe any mechanism that accepts input, performs some operation (that cannot be seen) using the input, and produces output.

24
New cards

What does the following statement do?

x = random.randint(1, 100)

It assigns a random integer in the range of 1 through 100 to the variable x.

25
New cards

What does the following statement do?

print(random.randint (1, 20))

It prints a random integer in the range of 1 through 20.

26
New cards

What does the following statement do?

print(random.randrange ( 10, 20))

It prints a random integer in the range of 10 through 19.

27
New cards

What does the following statement do?

print(random.random())

It prints a random floating-point number in the range of 0.0 up to, but not including, 1.0.

28
New cards

What does the following statement do?

print(random.uniform(0 . 1, 0 . 5))

It prints a random floating-point number in the range of 0.1 through 0.5.

29
New cards

When the random module is imported, what does it use as a seed value for random number generation?

It uses the system time, retrieved from the computer’s internal clock.

30
New cards

What happens if the same seed value is always used for generating random numbers?

The random number functions would always generate the same series of pseudorandom numbers.

31
New cards

What is the purpose of the return statement in a function?

It returns a value back to the part of the program that called it.

32
New cards

Look at the following function definition:

def do_something(number) :

  • return number * 2

  1. What is the name of the function?

  2. What does the function do?

  3. Given the function definition, what will the following statement display?

    1. print(do_something(10))

  • do_something

  • It returns a value that is twice the argument passed to it.

  • 20

33
New cards

What is a Boolean function?

A function that returns either True or False

34
New cards

What import statement do you need to write in a program that uses the math module?

import math

35
New cards

Write a statement that uses a math module function to get the square root of 100 and assigns it to a variable.

square_root = math.sqrt (100)

36
New cards

Write a statement that uses a math module function to convert 45 degrees to radians and assigns the value to a variable.

angle = math.radians(45)