Python - Functions

[Pages:25]Python - Functions

CS101

Computer Science, CMU CS101 Introduction to computer

Motivation

Computer Science, CMU CS101 Introduction to computer

What is a function

(a named sequence of statements) () Example

>>> a = len("Python 3") >>> print(a) >>> 8

We call a function len( ) which takes one argument and returns a result. The result is called the return value.

Computer Science, CMU CS101 Introduction to computer

Why use functions? (1/2)

Abstraction

? ? ? arguments ?

( )

Computer Science, CMU CS101 Introduction to computer

Why use functions? (2/2)

Reusability

Easy maintenance, Readability

Computer Science, CMU CS101 Introduction to computer

Functions in Python

Built-in

Python See a list of the functions next page.

User defined

built-in

Pay attention to!

? Function specification: argument argument

? Return value:

Computer Science, CMU CS101 Introduction to computer

Python built-in functions

Computer Science, CMU CS101 Introduction to computer

Two types of built-in function

General function

len() len() string, list function len(argument)

Object-type specific function

uppercase string int float dot "mystring".upper() MYSTRING

Computer Science, CMU CS101 Introduction to computer

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download