Lecture 4

Using and writing functions

CS1302 Introduction to Computer Programming

7.1 Function Basics

7.2 Parameter Passing

def name(parameter list):

           block

7.3 Documenting Functions

7.4 Function Examples

7.4 Function Examples

Output

I am method 1.
I am method 2.
I am method 1.
I am method 1.
I am method 2.
I am method 3.
I am method 1.
I am method 1.
I am method 2.

7.5 Refactoring to Eliminate Code Duplication

7.6 Custom Functions vs. Standard Functions

Generally speaking, if you have the choice of using a standard library function or writing your own custom function that provides the same functionality, choose to use the standard library routine.

6.1 Introduction to Using Functions

6.2 Functions and Modules

Import keyword: from math import sqrt

Three important parts: Name, Parameters,Result type

6.3 The Built-in Functions

Automatically available to any Python program:

no import statement is required.

6.4 Standard Mathematical Functions

The math library contains so many useful functions. You can get information from dir() and help(). 

6.4 Standard Mathematical Functions

6.5 time Functions

Warning

time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead

The time library can be used as a counter or to block current execution for certain amount of time.

6.6 Random Numbers

All algorithmic random number generators actually produce pseudorandom numbers, not true random
numbers.

6.7 System-specifific Functions

 

The sys.exit function accepts a single integer argument, which it passed back to the operating system
when the program completes. The value zero indicates that the program completed successfully; a nonzero
value represents the program terminating due to an error of some kind.

6.8 The eval and exec Functions

eval: evaluate a string in the same way that the interactive shell would evaluate it.

6.9 Turtle Graphics

Turtle library helps you draw shapes you want!

6.10 Other Techniques for Importing Functions and Modules

There are at least three ways to import a function from a library.

Calendar library contains some useful object about date for you to operate on.

CS1302 Lecture 4 Chapter 6 (II)

By Chung Chan

CS1302 Lecture 4 Chapter 6 (II)

  • 116