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

Slides:



Advertisements
Similar presentations
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look 1 Xiang Lian The University of Texas – Pan American Edinburg,
Advertisements

Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 3: Control Structures (Part 1) – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries – Exercises Xiang Lian The University of Texas – Pan American Edinburg,
The University of Texas – Pan American
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
The University of Texas – Pan American
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
CSCI/CMPE 4341 Topic: Programming in Python Review: Final Exam Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 5: Functions Xiang Lian The University of Texas – Pan American Edinburg, TX
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Chapter 6: Repetition Continued. 2 Validity Checks What’s weak about the following code ? do { s1 = JOptionPane.showInputDialog (“Enter a number: ”);
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
Creating and Using Class Methods. Definition Class Object.
Controlling Program Flow with Decision Structures.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
© 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  Conditional control structure, also called a decision structure  Executes.
Chapter 9: Value-Returning Functions
Chapter 4 The If…Then Statement
Functions and an Introduction to Recursion
Methods Chapter 6.
Chapter 6 Functions.
JavaScript: Functions
CMPT 201 Functions.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
The University of Texas – Pan American
Chapter 5 - Functions Outline 5.1 Introduction
CHAPTER FOUR Functions.
The University of Texas – Pan American
The University of Texas – Pan American
Chapter 5 Function Basics
Exam 2 Review 1.
Chapter 6 - Functions Outline 5.1 Introduction
MSIS 655 Advanced Business Applications Programming
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
CSCI 3328 Object Oriented Programming in C# Review: Exam I
The University of Texas – Pan American
The University of Texas – Pan American
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
The University of Texas – Pan American
The University of Texas – Pan American
CSCI 6307 Foundation of Systems – Exercise (1)
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
The University of Texas – Pan American
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods
Chapter 9: Value-Returning Functions
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
The University of Texas – Pan American
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
6 Functions.
CSCI 3328 Object Oriented Programming in C# Review: Exam II
Presentation transcript:

CSCI/CMPE 4341 Topic: Programming in Python Chapter 5: Functions – Exercises Xiang Lian The University of Texas – Pan American Edinburg, TX

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 ______. – 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() B. math.randomNum() – 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 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 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 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) math.ceil(4.1) 8

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 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 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 a n, n >= 3, of this sequence is given by: a n = a n-1 + a n-2 11