CSCICMPE 4341 Topic Programming in Python Chapter 5

  • Slides: 11
Download presentation
CSCI/CMPE 4341 Topic: Programming in Python Chapter 5: Functions – Exercises Xiang Lian The

CSCI/CMPE 4341 Topic: Programming in Python Chapter 5: Functions – Exercises Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa. edu 1

Objectives • In this chapter, you will do some exercises about: – Functions in

Objectives • In this chapter, you will do some exercises about: – Functions in Python 2

Multiple Choices • Constructing a large program from smaller components is called ______. –

Multiple Choices • Constructing a large program from smaller components is called ______. – A. object-oriented programming B. algorithm design – C. divide and conquer D. writing pseudo-code • "Pre-packaged" functions or classes are available in Python ______. – A. functions B. classes C. modules D. packages • The ______ module functions allow programmers to perform common mathematical calculations. – A. random B. built-ins C. math D. iostream • The indented statements that follow a ______ statement form a function body. – A. void B. int C. def D. None of the above • The ______ module introduces the element of chance into Python programs. – A. rand B. math C. random D. cmath 3

Multiple Choices (cont'd) • A function ______ produces pseudorandom numbers. – A. Math. random()

Multiple Choices (cont'd) • A function ______ produces pseudorandom numbers. – A. Math. random() B. math. random. Num() – C. random. Randomrange() D. random. randrange() • During the code execution, three namespaces below can be accessed except for: – A. local namespace B. global namespace C. nested namespace – D. built-in namespace • A method that calls itself either directly or indirectly is a(n) _____method. – A. static B. dynamic C. public D. recursive • A recursive function converges on the _____. – A. initial case B. base case C. procedure that calls itself D. procedure that calls other functions 4

Multiple Choices (cont'd) • Which of the following statements generates a random number between

Multiple Choices (cont'd) • Which of the following statements generates a random number between 1 and 4 (inclusive)? – A. random. randrange(0, 4) – B. random. rand(1, 5) – C. random. randrange(1, 5) – D. random. randrange(0, 4)+1 5

True/False Statements • All variables declared in a function are global to the program

True/False Statements • All variables declared in a function are global to the program containing the function. • An import statement must be included for every module function used in a program. • Function fmod returns the floating-point remainder of its two arguments. • The keyword return displays the result of a function. • The keyword return is required in the Python function. 6

True/False Statements (cont'd) • Function call random. randrange(1, 7) produces a random integer in

True/False Statements (cont'd) • Function call random. randrange(1, 7) produces a random integer in the range 1 to 7, inclusive. • An identifier's scope is the portion of the program in which the identifier has meaning. • Every call to a recursive function is a recursive call. • Omitting the base case in a recursive function can lead to "infinite" recursion. • A recursive function may call itself indirectly. 7

What Does the Code Do? • math. floor(4. 5) • math. ceil(4. 0) •

What Does the Code Do? • math. floor(4. 5) • math. ceil(4. 0) • math. ceil(4. 1) 8

Debug Errors Def square(a) Return a*a ----------------------------void G(a) Return x*G(a-1) 9

Debug Errors Def square(a) Return a*a ----------------------------void G(a) Return x*G(a-1) 9

Write a Program • Write a method in Python to return the larger value

Write a Program • Write a method in Python to return the larger value of two decimal numbers. • Write another program in Python to return the largest value among 3 decimal numbers, by invoking the method above. 10

Write a Program (cont'd) • Write a recursive method in Python to compute the

Write a Program (cont'd) • Write a recursive method in Python to compute the Fibonacci number: – 1, 1, 2, 3, 5, 8, 13, 21, 34, . . – Given the first two numbers of the sequence (say, a 1 and a 2) – n-th number an, n >= 3, of this sequence is given by: an = an-1 + an-2 11