modules. a module is a file containing python definitions and statements intended for use in other...

27
Modules

Upload: valerie-long

Post on 29-Dec-2015

231 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

Modules

Page 2: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

Modules

• A module is a file containing Python definitions and statements intended for use in other Python programs.

• There are many modules as part of standard library.

• Example : the turtle module and the string modules.

Page 3: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

Random numbers

We often use random number in programs.• To play a game of chance to pick a number,

throw some dice, flip a coin.• To shuffle a deck of playing cards randomly• To simulate possible rainfall .• For encryting banking sessions on the

Internets.

Page 4: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

Module random

Page 5: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

randrange method

• The randrange method call generates an integer between its lower and upper argument. Using the same semantics as range – so the lower bound is included, but the upper bound is excluded.

• The results are uniformly distributed.• Like range, randrange can also take an

optional step argument.

Page 6: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of
Page 7: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of
Page 8: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

Shuffle a list

Page 9: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

The time module

Page 10: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

The

mat

h m

odul

e

Page 11: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

Create modules

Page 12: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

Namespaces

• A namespace is a collection of identifiers that belong to a module, or a function, or a class)

Page 13: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

Namespace

Page 14: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

Namespace

Page 15: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

Namespaces in functions

Page 16: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

Scope and lookup rules

• The scope of an identifier is the region of program code in which the identifier can be accessed, or used.

Page 17: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

Scope and lookup rules

There are three important scopes in Python.• Local scope• Global scope• Built-in scope

Page 18: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

Local scope

• Local scope refers to identifiers declared within a function.

• These identifiers are kept in the namespace that belongs to the function, and each function has its own namespace.

Page 19: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

Global scope

• Global scope refers to all the identifiers declared within the current module, or file.

Page 20: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

Built-in scope

• Built-in scope refers to all the identifiers built into Python- those like range and min that can be used without having to import anything, and are always available.

Page 21: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

Precedence rules

• The same name could occur in more than one of these scopes.

• But the innermost, or local scope, will always take precedence over the global scope.

• The global scope always get used in preference to the built-in scope.

Page 22: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

What gets printed?Our own one, or the built-in one?

Using the scope lookup rules determine this:

Page 23: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

• Our own range function, Not the built-in function, is called.

• Because our range function is in global namespace, which take precedence over the built-in names.

What gets printed?Our own one, or the built-in one?

• So although names like range and min are built-in, they can be “hidden” from your use if you choose to defines your own variables or functions that reuse those names

Page 24: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

Scope : more complex example

Page 25: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of

Attributes and the dot operator

• Variables defines inside a module are called attributed of the module.

• Attributes are accessed using the dot operator(.)

• The university and name attribute of moduleA and moduleB is accessed using moduleA.university

moduleB.name

Page 26: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of
Page 27: Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of