Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Study guide

Python Beginner Course Note-5

Rating
-
Sold
-
Pages
28
Uploaded on
15-09-2020
Written in
2019/2020

This is the continuation of the crash course material for Python. This material solely contains the topic of modules and file. Modules in Python are one of the features that make it distinct and handy from other programming languages. It's a great hands-on course.

Show more Read less
Institution
Course

Content preview

Day-5:
Module and
File
27/06/2018
Workshop on “Introduction to
Python




Department Of
Computer Science
Engineering
SKFGI, Mankundu

, 1.Modules
Sub Topics –
 Importing Module
 Math Module
 Random Module
 Namespace and DIR()
 Packages
 Composition


1.1.Why Modules?
If you quit from the Python interpreter and enter it again, the definitions
you have made (functions and variables) are lost. Therefore, if you want
to write a somewhat longer program, you are better off using a text editor
to prepare the input for the interpreter and running it with that file as
input instead. This is known as creating a script. As your program gets
longer, you may want to split it into several files for easier maintenance.
You may also want to use a handy function that you‟ve written in several
programs without copying its definition into each program.

To support this, Python has a way to put definitions in a file and use them
in a script or in an interactive instance of the interpreter. Such a file is
called a module; definitions from a module can be imported into other
modules or into the main module (the collection of variables that you
have access to in a script executed at the top level and in calculator
mode).

A module allows you to logically organize your Python code. Grouping
related code into a module makes the code easier to understand and use.
A module is a Python object with arbitrarily named attributes that you can
bind and reference.

, 1.2.Module Loading and Execution:
A module imported in a program must be located and loaded into memory
before it can be used. When you import a module, the Python interpreter
searches for the module in the following sequences −
 The current directory.
 If the module isn't found, Python then searches each directory in
the shell variable PYTHONPATH.
 If all else fails, Python checks the default installation specific path.
On UNIX, this default path is normally /usr/local/lib/python/.
 If the module is not located even there then an error ImportError
exception is generated .
A compiled version of the module with file extension .pyc is generated .
next time when the module is imported , this .pyc file is loaded rather
than .py file, to save the time of recompiling. A new compiled version of a
module is again produced whenever the compiled version is out of date.
Even programmer can force the python shell to reload and recompile the
.py file to generate a new .pyc file by using reload function.

1.3.The import Statement
We can use any Python source file as a module by executing an import
statement in some other Python source file. The import has the following
syntax −
import module1[, module2[,... moduleN]

import sys

print(sys.path)

Output:

['', 'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python36-
32\\Lib\\idlelib',
'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python36-
32\\python36.zip',
'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python36-
32\\DLLs',
'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python36-32\\lib',
'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python36-32',

Connected book

Written for

Institution
Course

Document information

Uploaded on
September 15, 2020
Number of pages
28
Written in
2019/2020
Type
Study guide

Subjects

$5.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
snehat1410

Get to know the seller

Seller avatar
snehat1410 MAKAUT
Follow You need to be logged in order to follow users or courses
Sold
1
Member since
5 year
Number of followers
1
Documents
14
Last sold
5 year ago

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions