![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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/1.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/2.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/3.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/4.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/5.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/6.jpg)
![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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/7.jpg)
![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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/8.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/9.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/10.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/11.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/12.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/13.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/14.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/15.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/16.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/17.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/18.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/19.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/20.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/21.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/22.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/23.jpg)
• 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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/24.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/25.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/26.jpg)
![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](https://reader035.vdocument.in/reader035/viewer/2022062422/56649e805503460f94b847dc/html5/thumbnails/27.jpg)