Python PDF 4
Python PDF 4
Python file object provides methods and attributes to access and manipulate files. Using
file objects, we can read or write any files.
Whenever we open a file to perform any operations on it, Python returns a file object. To
create a file object in Python use the built-in functions, such as open() and os.popen().
IOError exception is raised when a file object is misused, or file operation fails for an I/O-
related reason.
In Python, there are three different categories of a file object, which are listed below:
1. Text files
2. Binary files
3. Raw files
The text file type is most common. Usually, we use text files to store character data or storing
information in plain text with no special formatting beyond basic fonts and font styles.
We open a text file using the open() function. For example, open('test'.txt', 'r'). When we open
a text file, it returns a TextIOWrapper file object.
Example
Data is stored on a disk in the form of binary. For example, we use binary files to store data
like images or videos. Binary files are a computer-readable form of storing data.
A program is needed to interpret the data in a binary file and display it to the user. The binary
files are also called buffered files. This file type is used for reading and writing binary data.
Open the binary files using the open() function in binary mode. For example, open('abc.txt',
'rb'). It opens the file to read-only in binary mode. The file pointer exists at the beginning of
the file.
The open() function will return the BufferedReader when we open the binary file for reading
and the BufferedWriter file object when we open a binary file for writing.
Example
Raw Files
A raw file is a collection of unprocessed data. It means the raw file has not been altered or
manipulated in any way by the computer.
The raw files are also called unbuffered files, and this file type is generally used as a low-
level building block for binary and text streams. Mostly, The raw file is not used.
When we open these files, using the open() function will return a FileIO object.
Example
The raw files are also called unbuffered files, and this file type is generally used as a low-
level building block for binary and text streams. Mostly, The raw file is not used.
When we open these files, using the open() function will return a FileIO object.
Example
name: Return the name of the file. It is a read-only attribute and may not be
present on all file-like objects. If the file object was created using
the open() function, the file’s name is returned. Otherwise, some string
indicates the source of the file object is returned.
encoding: It returns the encoding this file uses, such as UTF-8. This attribute is
read-only. When Unicode strings are written to a file, they will be converted to
byte strings using this encoding. It may also be None. In that case, the file uses
the system default encoding for converting Unicode strings.
mode: Returns the file access mode used while opening a file.
closed: Returns True if a file is closed. It is a boolean value indicating the
current state of the file object.
newline: Files opened in universal newline read mode keep track of the
newlines encountered while reading the file. The values are ‘\r’, ‘\n’, ‘\r\n’,
None (no newlines read yet), or a tuple containing all the newline types seen.
For files not opened in universal newline read mode, the value of this attribute
will be None
Example:
File object has the following methods that we can use to accessing a file: A file can be
opened with a built-in function called open(). This function takes in the file’s path and the
access mode and returns a file object.
Method Description
seekable() Returns whether the file allows us to change the file position.
fileno() Returns a number that represents the stream, from the operating system’s perspective.
Note: As a default optional argument, it includes -h, along with its long version –help.
File System
In python, the file system contains the files and directories. To handle these files and directories
python supports “os” module. Python has the “os” module, which provides us with many useful
methods to work with directories (and files as well).
The os module provides us the methods that are involved in file processing operations and
directory processing like renaming, deleting, get current directory, changing directory etc.
The os module provides us the rename() method which is used to rename the specified file to
a new name.
Syntax:
Example:
import os;
#rename file2.txt to file3.txt
os.rename("file2.txt","file3.txt")
Removing the file – remove()
The os module provides us the remove() method which is used to remove the specified file.
Syntax:
os.remove(“file-name”)
Example:
import os;
#deleting the file named file3.txt
os.remove("file3.txt")
The mkdir() method is used to create the directories in the current working directory.
Syntax:
os.mkdir(“directory-name”)
Example:
import os;
#creating a new directory with the name new
os.mkdir("dirnew")
The chdir() method is used to change the current working directory to a specified directory.
Syntax:
os.chdir("new-directory")
Example:
import os;
#changing the current working directory to new
os.chdir("dir2")
os.getcwd()
Example:
import os;
#printing the current working directory
print(os.getcwd())
os.rmdir(“directory name”)
Example:
import os;
#removing the new directory
os.rmdir("dir2")
All files and sub directories inside a directory can be known using the listdir() method. This
method takes in a path and returns a list of sub directories and files in that path. If no path is
specified, it returns from the current working directory.
Syntax:
os.listdir([“path”])
Example:
import os;
#list of files and directories in current working directory
print(os.listdir())
#list of files and directories in specified path
print(os.listdir(“D:\\”))
Persistent Storage Modules
In many of the exercises in this text, user input is required. After many iterations, it may be
somewhat frustrating being required to enter the same data repeatedly. The same may occur if
you are entering a significant amount of data for use in the future. This is where it becomes
useful to have persistent storage, or a way to archive your data so that you may access them
at a later time instead of having to re-enter all of that information. When simple disk files are
no longer acceptable and full relational database management systems (RDBMSs) are
overkill, simple persistent storage fills the gap. The majority of the persistent storage modules
deals with storing strings of data, but there are ways to archive Python objects as well.
As we mentioned before, both marshal and pickle can flatten Python objects. These modules
do not provide "persistent storage" per se, since they do not provide a namespace for the
objects, nor can they provide concurrent write access to persistent objects. What they can do,
however, is to pickle Python objects to allow them to be stored or transmitted. Storage, of
course, is sequential in nature (you store or transmit objects one after another). The difference
between marshal and pickle is that marshal can handle only simple Python objects (numbers,
sequences, mapping, and code) while pickle can transform recursive objects, objects that are
multi-referenced from different places, and user-defined classes and instances.
The pickle module is also available in a turbo version called cPickle, which implements all
functionality in C.
The pickle module allows you to store Python objects directly to a file
without having to convert them to strings or to necessarily write them out
as binary files using low-level file access. Instead, the pickle module
creates a Python-only binary version that allows you to cleanly read and
write objects in their entirety without having to worry about all the file
details. All you need is a valid file handle, and you are ready to read or
write objects from or to disk.
The two main functions in the pickle module are dump() and load().
The dump() function takes a file handle and a data object and saves the
object in a format it understands to the given file. When a pickled object is
loaded from disk using load() , it knows exactly how to restore that object
to its original configuration before it was saved to disk. We recommend
you take a look at pickle and its "smarter" brother, shelve, which gives you
dictionary-like functionality so there is even less file overhead on your
part. cPickle is the faster C-compiled version of pickle
Exceptions in Python
Errors that occur at runtime (after passing the syntax test) are called exceptions or logical
errors.
For instance, they occur when we
If not handled properly, it prints a traceback to that error along with some details about why
that error occurred.
divide_numbers = 7 / 0
prit(divide_numbers)
Run Code
Output
Here, while trying to divide 7 / 0, the program throws a system exception ZeroDivisionError
Python Built-in Exceptions
Illegal operations can raise exceptions. There are plenty of built-in exceptions in Python that
are raised when corresponding errors occur.We can view all the built-in exceptions using the
built-in local() function as follows:
print(dir(locals()['__builtins__']))
Run Code
KeyboardInterrupt Raised when the user hits the interrupt key (Ctrl+C or Delete).
RuntimeError Raised when an error does not fall under any other category.
Errors represent conditions such as compilation error, syntax error, error in the logical part
of the code, library incompatibility, infinite recursion, etc.
Errors are usually beyond the control of the programmer and we should not try to handle
errors.
In the tutorial, we will learn about different approaches of exception handling in Python with
the help of examples.
The try...except block is used to handle exceptions in Python. Here's the syntax
of try...except block:
try:
# code that may cause exception
except:
# code to run when exception occurs
Here, we have placed the code that might generate an exception inside the try block.
Every try block is followed by an except block.
When an exception occurs, it is caught by the except block. The except block cannot be used
without the try block.
Example: Exception Handling Using try...except
try:
numerator = 10
denominator = 0
result = numerator/denominator
print(result)
except:
print("Error: Denominator cannot be 0.")
# Output: Error: Denominator cannot be 0.
Run Code
In the example, we are trying to divide a number by 0. Here, this code generates an
exception.
To handle the exception, we have put the code, result = numerator/denominator inside
the try block. Now when an exception occurs, the rest of the code inside the try block is
skipped.
The except block catches the exception and statements inside the except block are executed.
If none of the statements in the try block generates an exception, the except block is skipped.
For each try block, there can be zero or more except blocks. Multiple except blocks allow us
to handle each exception differently.
The argument type of each except block indicates the type of exception that can be handled
by it. For example,
try:
even_numbers = [2,4,6,8]
print(even_numbers[5])
except ZeroDivisionError:
print("Denominator cannot be 0.")
except IndexError:
print("Index Out of Bound.")
Here, we are trying to access a value to the index 5. Hence, IndexError exception occurs.
When the IndexError exception occurs in the try block,
The ZeroDivisionError exception is skipped.
The set of code inside the IndexError exception is executed.
In some situations, we might want to run a certain block of code if the code block
inside try runs without any errors.
For these cases, you can use the optional else keyword with the try statement.
Let's look at an example:
try:
num = int(input("Enter a number: "))
assert num % 2 == 0
except:
print("Not an even number!")
else:
reciprocal = 1/num
print(reciprocal)
Run Code
Output
If we pass an odd number:
Enter a number: 1
Not an even number!
Enter a number: 4
0.25
However, if we pass 0, we get ZeroDivisionError as the code block inside else is not handled
by preceding except.
Enter a number: 0
Traceback (most recent call last):
File "<string>", line 7, in <module>
reciprocal = 1/num
ZeroDivisionError: division by zero
Note: Exceptions in the else clause are not handled by the preceding except clauses.
Python try...finally
In Python, the finally block is always executed no matter whether there is an exception or
not.
The finally block is optional. And, for each try block, there can be only one finally block.
try:
numerator = 10
denominator = 0
result = numerator/denominator
print(result)
except:
print("Error: Denominator cannot be 0.")
finally:
print("This is finally block.")
Run Code
Output
Context Managers
Context managers allow you to allocate and release resources precisely when you want to.
The most widely used example of context managers is the with statement. Suppose you have
two related operations which you’d like to execute as a pair, with a block of code in between.
Context managers allow you to do specifically that. For example:
The above code opens the file, writes some data to it and then closes it. If an error occurs
while writing the data to the file, it tries to close it. The above code is equivalent to:
While comparing it to the first example we can see that a lot of boilerplate code is eliminated
just by using with . The main advantage of using a with statement is that it makes sure our
file is closed without paying attention to how the nested block exits.
A common use case of context managers is locking and unlocking resources and closing
opened files (as I have already shown you).
Let’s see how we can implement our own Context Manager. This should allow us to
understand exactly what’s going on behind the scenes.
At the very least a context manager has an __enter__ and __exit__ method defined. Let’s
make our own file-opening Context Manager and learn the basics.
class File(object):
def __init__(self, file_name, method):
self.file_obj = open(file_name, method)
def __enter__(self):
return self.file_obj
def __exit__(self, type, value, traceback):
self.file_obj.close()
Just by defining __enter__ and __exit__ methods we can use our new class in
a with statement. Let’s try:
1. The with statement stores the __exit__ method of the File class.
2. It calls the __enter__ method of the File class.
3. The __enter__ method opens the file and returns it.
4. The opened file handle is passed to opened_file .
5. We write to the file using .write() .
6. The with statement calls the stored __exit__ method.
7. The __exit__ method closes the file.
Exceptions
>>> 10 * (1/0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> 4 + spam*3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'spam' is not defined
>>> '2' + 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
The last line of the error message indicates what happened. Exceptions come in different
types, and the type is printed as part of the message: the types in the example
are ZeroDivisionError, NameError and TypeError. The string printed as the exception type is
the name of the built-in exception that occurred. This is true for all built-in exceptions, but
need not be true for user-defined exceptions (although it is a useful convention). Standard
exception names are built-in identifiers (not reserved keywords).
The rest of the line provides detail based on the type of exception and what caused it.
The preceding part of the error message shows the context where the exception occurred, in
the form of a stack traceback. In general it contains a stack traceback listing source lines;
however, it will not display lines read from standard input.
Raising Exceptions
The raise statement allows the programmer to force a specified exception to occur. For
example:
The sole argument to raise indicates the exception to be raised. This must be either an
exception instance or an exception class (a class that derives from BaseException, such
as Exception or one of its subclasses). If an exception class is passed, it will be implicitly
instantiated by calling its constructor with no arguments:
If you need to determine whether an exception was raised but don’t intend to handle it, a
simpler form of the raise statement allows you to re-raise the exception:
>>> try:
... raise NameError('HiThere')
... except NameError:
... print('An exception flew by!')
... raise
...
An exception flew by!
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
NameError: HiThere
What is Assertion?
Assertions are statements that assert or state a fact confidently in your program. For example,
while writing a division function, you're confident the divisor shouldn't be zero, you assert
divisor is not equal to zero.
Assertions are simply boolean expressions that check if the conditions return true or not. If it
is true, the program does nothing and moves to the next line of code. However, if it's false,
the program stops and throws an error.
It is also a debugging tool as it halts the program as soon as an error occurs and displays it.
assert <condition>
def avg(marks):
assert len(marks) != 0
return sum(marks)/len(marks)
mark1 = []
print("Average of mark1:",avg(mark1))
Run Code
AssertionError
We got an error as we passed an empty list mark1 to assert statement, the condition became
false and assert stops the program and give AssertionError.
Now let's pass another list which will satisfy the assert condition and see what will be our
output.
Example 2: Using assert with error message
def avg(marks):
assert len(marks) != 0,"List is empty."
return sum(marks)/len(marks)
mark2 = [55,88,78,90,79]
print("Average of mark2:",avg(mark2))
mark1 = []
print("Average of mark1:",avg(mark1))
Run Code
We passed a non-empty list mark2 and also an empty list mark1 to the avg() function and we
got output for mark2 list but after that we got an error AssertionError: List is empty.
The assert condition was satisfied by the mark2 list and program to continue to run.
However, mark1 doesn't satisfy the condition and gives an AssertionError
1
Exception
Base class for all exceptions
2
StopIteration
Raised when the next() method of an iterator does not point to any object.
3
SystemExit
Raised by the sys.exit() function.
4
StandardError
Base class for all built-in exceptions except StopIteration and SystemExit.
5
ArithmeticError
Base class for all errors that occur for numeric calculation.
6
OverflowError
Raised when a calculation exceeds maximum limit for a numeric type.
7
FloatingPointError
Raised when a floating point calculation fails.
8
ZeroDivisionError
Raised when division or modulo by zero takes place for all numeric types.
9
AssertionError
Raised in case of failure of the Assert statement.
10
AttributeError
Raised in case of failure of attribute reference or assignment.
11
EOFError
Raised when there is no input from either the raw_input() or input()
function and the end of file is reached.
12
ImportError
Raised when an import statement fails.
13
KeyboardInterrupt
Raised when the user interrupts program execution, usually by pressing
Ctrl+c.
14
LookupError
Base class for all lookup errors.
15
IndexError
Raised when an index is not found in a sequence.
16
KeyError
Raised when the specified key is not found in the dictionary.
17
NameError
Raised when an identifier is not found in the local or global namespace.
18
UnboundLocalError
Raised when trying to access a local variable in a function or method but
no value has been assigned to it.
19
EnvironmentError
Base class for all exceptions that occur outside the Python environment.
20
IOError
Raised when an input/ output operation fails, such as the print statement
or the open() function when trying to open a file that does not exist.
21
OSError
Raised for operating system-related errors.
22
SyntaxError
Raised when there is an error in Python syntax.
23
IndentationError
Raised when indentation is not specified properly.
24
SystemError
Raised when the interpreter finds an internal problem, but when this error
is encountered the Python interpreter does not exit.
25
SystemExit
Raised when Python interpreter is quit by using the sys.exit() function. If
not handled in the code, causes the interpreter to exit.
26
TypeError
Raised when an operation or function is attempted that is invalid for the
specified data type.
27
ValueError
Raised when the built-in function for a data type has the valid type of
arguments, but the arguments have invalid values specified.
28
RuntimeError
Raised when a generated error does not fall into any category.
29
NotImplementedError
Raised when an abstract method that needs to be implemented in an
inherited class is not actually implemented.
In Python, we can define custom exceptions by creating a new class that is derived from the
built-in Exception class.
Here's the syntax to define custom exceptions,
class CustomError(Exception):
...
pass
try:
...
except CustomError:
...
Here, CustomError is a user-defined error which inherits from the Exception class.
Note:
When we are developing a large Python program, it is a good practice to place all the user-
defined exceptions that our program raises in a separate file.
try:
input_num = int(input("Enter a number: "))
if input_num < number:
raise InvalidAgeException
else:
print("Eligible to Vote")
except InvalidAgeException:
print("Exception occurred: Invalid Age")
Run Code
Output
If the user input input_num is greater than 18,
Enter a number: 45
Eligible to Vote
If the user input input_num is smaller than 18,
Enter a number: 14
Exception occurred: Invalid Age
We can further customize this class to accept other arguments as per our needs.
To learn about customizing the Exception classes, you need to have the basic knowledge of
Object-Oriented programming.
class SalaryNotInRangeError(Exception):
"""Exception raised for errors in the input salary.
Attributes:
salary -- input salary which caused the error
message -- explanation of the error
"""
Here, we have overridden the constructor of the Exception class to accept our own custom
arguments salary and message.
Then, the constructor of the parent Exception class is called manually with
the self.message argument using super().
The custom self.salary attribute is defined to be used later.
The inherited __str__ method of the Exception class is then used to display the
corresponding message when SalaryNotInRangeError is raised.
Example:
import sys
print(sys.version)
Output:
3.6.9 (default, Oct 8 2020, 12:12:24)
[GCC 8.4.0]
In the above example, sys.version is used which returns a string containing the version of
Python Interpreter with some additional information. This shows how the sys module interacts
with the interpreter. Let us dive into the article to get more information about the sys module.
Input and Output using sys
The sys modules provide variables for better control over input or output. We can even redirect
the input and output to other devices. This can be done using three variables –
stdin
stdout
stderr
stdin: It can be used to get input from the command line directly. It is used for standard input.
It internally calls the input() method. It, also, automatically adds ‘\n’ after each sentence.
Example:
import sys
if 'q' == line.rstrip():
break
print(f'Input : {line}')
print("Exit")
stdout: A built-in file object that is analogous to the interpreter’s standard output stream in
Python. stdout is used to display output directly to the screen console. Output can be of any
form, it can be output from a print statement, an expression statement, and even a prompt direct
for input. By default, streams are in text mode. In fact, wherever a print function is called within
the code, it is first written to sys.stdout and then finally on to the screen.
Example:
import sys
sys.stdout.write('Geeks')
Output
Geeks
import sys
def print_to_stderr(*a):
print_to_stderr("Hello World")
Output:
Namespaces
Namespace in Python
In this tutorial, we will learn about the namespace in Python, the structure used to organize the
symbolic names assigned to objects in a Python program, why namespace is important, and
how we can use them in our Python program. Let's have a brief introduction to a namespace.
What is Namespace?
A namespace is a way of providing the unique name for each object in Python. Everything in
Python is an object, i.e., a variable or a method. In other words, it is a collection of the defined
symbolic names along with the information about the object that each name references. A
namespace can be understood as a dictionary where a name represents a key and objects are
values. Let's understand it with a real-life example - A namespace is like a surname. A "Peter"
name might be difficult to find in the class if there are multiple "Peter," but when we
particularly ask for "Peter Warner" or "Peter Cummins,". It might be rare to find the same name
and surname in a class for multiple students.
The namespace helps the Python interpreter to understand what exact method or variable is
trying to point out in the code. So its name gives more information - Name (which means name,
a unique identifier) + Space (related to scope).
In Python, there are four types of namespaces which are given below
o Built-in
o Global
o Enclosing
o Local
As these namespace various have lifetimes, Python interpreter creates namespaces as necessary
and deletes them when they are no longer needed.
As its name suggests, it contains pre-defined names of all of Python's built-in objects already
available in Python. Let's list these names with the following command.
Command -
1. dir(__builtins__)
Output:
The built-in namespace creates by the Python interpreter when its starts up. These are
terminated when Python interpreter terminates.
The global namespace consists of any names in Python at any level of the main program. It is
created when the main body executes and remains in existence until the interpreter terminates.
The Python interpreter creates a global namespace for any module that our Python loads with
the import statement. To get more information, visit our Python Module.
The function uses the local namespaces; the Python interpreter creates a new namespace when
the function is executed. The local namespaces remain in existence until the function
terminates. The function can also consist of another function. We can define one function inside
another as below.
Example -
1. def f():
2. print('Initiate f()')
3.
4. def g():
5. print('Initiate g()')
6. print('End g()')
7. return
8.
9. g()
10.
11. print('Initiate f()')
12. return
13. f()
In the above example, the function g() is defined within the body of f(). Inside the f() we called
the g() and called the main f() function. Let's understand the working of the above function -
The scope is term which defines the coding region from a particular Python object is accessible.
Every object/variable has its scope where we can access that particular variable in the program.
For example - A variable in a function can only access inside the function. Let's understand the
following example -
Example -
1. def scope_func():
2. print("Inside scope_func")
3. def scope_inner_func():
4. var = 20
5. print("Inside inner function, value of var:",var)
6. scope_inner_func()
7. print("Try printing var from outer function: ",var)
8. scope_func()
Output:
Inside scope_func
Inside inner function, value of var: 20
Traceback (most recent call last):
File "d:/Python Project/listproblems.py", line 343, in
scope_func()
File "d:/Python Project/listproblems.py", line 342, in scope_func
print("Try printing var from outer function: ",var)
NameError: name 'var' is not defined
Python implements global and local namespaces as dictionaries. Python comes with
the globals() and locals() methods that allow us to access global and local namespace
dictionaries.
What is the Python import module?
A file is considered as a module in python. To use the module, you have to import it
using the import keyword. The function or variables present inside the file can be used in
another file by importing the module. This functionality is available in other languages, like
typescript, JavaScript, java, ruby, etc.
Here is the flow to create and import the module as shown in the screenshot:
modtest/
test.py
display.py
Step 1) Create a file and name it test.py
Def display_message():
return "Welcome to Guru99 Tutorials!"
Step 3) Now create another file display.py.
import test
While importing, you don’t have to mention the test.py but just the name of the file.
Step5)
Then you can call the function display_message() from test.py inside display.py, you need to
make use of module_name.function_name.
Import test
print(test.display_message())
Step 6)
When you execute display.py, you will get the following output:
Earlier, we have seen a simple module with a function. Here will create a class and refer the
class inside another file.
myproj/
Car.py
display.py
Create a file called Car.py with the following code:
Filename : Car.py
class Car:
brand_name = "BMW"
model = "Z4"
manu_year = "2020"
def car_details(self):
print("Car brand is ", self.brand_name)
print("Car model is ", self.model)
print("Car manufacture year is ", self.manu_year)
def get_Car_brand(self):
print("Car brand is ", self.brand_name)
def get_Car_model(self):
print("Car model is ", self.model)
In the file Car.py , there are attributes brand_name, model and manu_year. The functions
defined inside the class are car_details(), get_Car_brand(), get_Car_model().
Let us now use the file Car.py as a module in another file called display.py.
Filename : display.py
import Car
car_det = Car.Car("BMW","Z5", 2020)
print(car_det.brand_name)
print(car_det.car_details())
print(car_det.get_Car_brand())
print(car_det.get_Car_model())
Output:
BMW
Car brand is BMW
Car model is Z5
Car manufacture year is 2020
Car brand is BMW
Car model is Z5
So we can access all the variables and functions from Car.py using Car module.
When you want only specific things to be imported, you can make use of “from” keyword to
import what you want.
So the syntax is
modtest/
test.py
display.py
In test.py there are 2 functions as shown :
Filename : test.py
defdisplay_message():
return "Welcome to Guru99 Tutorials!"
def display_message1():
return "All about Python!"
Now you want display_message() function. The function or variable that you are importing
can be directly accessed as shown below:
Output:
Welcome!
Now if you happen to use the function display_message1() ,it will throw an error that the
function is not defined as shown below:
Syntax:
Import module
Or by using
modtest/
test.py
display.py
Following are the code details inside test.py
my_name = "Python"
my_address = "Mumbai"
defdisplay_message():
return "Welcome!"
def display_message1():
return "All about Python!"
Example
Filename : display.py
Import test
print(test.display_message())
print(test.display_message1())
print(test.my_name)
print(test.my_address)
The module name test is used to refer to the function and variables inside module test.
Output:
Teacher!
All about Python!
Python
Mumbai
Using import *
Let us see an example using import *. Using import *, the functions and variables are
directly accessible, as shown in the example below:
print(display_message())
print(display_message1())
print(my_name)
print(my_address)
Output:
Teacher!
All about Python!
Python
Mumbai
A package is a container that contains various functions to perform specific tasks. For
example, the math package includes the sqrt() function to perform the square root of a
number.
While working on big projects, we have to deal with a large amount of code, and writing
everything together in the same file will make our code look messy. Instead, we can separate
our code into multiple files by keeping the related code together in packages.
Now, we can use the package whenever we need it in our projects. This way we can also
reuse our code.
Suppose we are developing a game. One possible organization of packages and modules
could be as shown in the figure below.
In Python, we can import modules from packages using the dot (.) operator.
For example, if we want to import the start module in the above example, it can be done as
follows:
import Game.Level.start
Now, if this module contains a function named select_difficulty(), we must use the full name
to reference it.
Game.Level.start.select_difficulty(2)
If this construct seems lengthy, we can import the module without the package prefix as
follows:
start.select_difficulty(2)
Import Required Functionality Only
Another way of importing just the required function (or class or variable) from a module
within a package would be as follows:
select_difficulty(2)
Although easier, this method is not recommended. Using the full namespace avoids confusion
and prevents two same identifier names from colliding.
While importing packages, Python looks in the list of directories defined in sys.path, similar
as for module search path.