Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Modules

2 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.

3 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.

4 Module random

5 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.

6

7

8 Shuffle a list

9 The time module

10 The math module

11 Create modules

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

13 Namespace

14

15 Namespaces in functions

16 Scope and lookup rules The scope of an identifier is the region of program code in which the identifier can be accessed, or used.

17 Scope and lookup rules There are three important scopes in Python. Local scope Global scope Built-in scope

18 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.

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

20 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.

21 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.

22 What gets printed? Our own one, or the built-in one? Using the scope lookup rules determine this:

23 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

24 Scope : more complex example

25 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

26

27


Download ppt "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."

Similar presentations


Ads by Google