Python Modules and Packages, Functions, Exceptional Handling, Data Structures and Testing

In this blog, we will discuss Python Modules and Packages, Functions, Exceptional Handling, Data Structures, Testing, and so on.

About Python?

Python is a readable, flexible, fast, dynamic, pleasant, powerful, object-oriented, high-level programming language. 

Python uses an interpreter which means the codes in Python are executed line by line. 

Python supports multiple programming styles (object-oriented, functional, and procedural). It mainly focuses on readability and productivity so they are easy to learn and are simple enough. 

The library of python is large and standard. 

Python can be used for multipurpose like scientific computing, Artificial Intelligence, Data analysis, and so on. 

Since it is easier and simple compared to other languages so anyone can easily access this language. 

It also supports cross-platform as it works on Windows, Mac, Linux, etc. 

It accepts and strictly follows indentation, use of whitespace, description of the scope, etc. 

Python helps to generate a large and bulky program into a simple and short one by using its features like functions, modules, and so on. 

There are various versions of python. The latest version of python is python 3.

Modules and Packages

In python modules are simply python files with a .py extension that has python executable code. 

The name of a file is the name of a module. 

The module is a file that consists of python functions, global variables, or classes defined and executed. 

To implement the function from another module, the module must be imported. 

The collection of those modules is called packages. 

Packages contain a special file i.e. __init__.py.
The syntax is represented as;
    <import name of module>
    <variable name> = <name of module><function name(parameter)>

Like this, we can use the function of another module by importing that respective module. Such programming is known as modular programming in python. 

Modules help to make our program short, simple, and easy to understand.

User-defined functions

In python, users can define or declare functions with the keyword “def” in the beginning then function name which may or may not consist of parameters/arguments. 

After this, a semicolon is entered and after that line, if we press enter indentation occurs below the def statement which defines the scope of the function. 

A function may consist of different functions in itself. 

Functions are called by their function name. 

A function may or may not return a value depending on the user. If the user wants to return a value, then the return keyword is used otherwise not.
Syntax:
    def function name(argument1, argument2, …..):
    statement _1
    statement _2 ………
Example;
def multiply(x,y)                          # declaration of function 
    result = x*y                             # operation with return keyword 
    return result

In the above example, the multiplication of two numbers passed as arguments are returned when add function is called. 

Since we have not passed the integer value to variables x and y and the print function is also not introduced so the value will not be printed. 

But in program 5 will the value displayed in the console will be returned to the same function where we called it i.e. to the multiply function.

def multiply(x,y):                         # declaration of function with function name multiply and with                                                         two-parameter x and y 

    result = x*y                             # multiplication operation is performed and the obtained value                                                          is assigned to the variable result

    return result                           # returns result to the state where the function is called                  print(multiply(2,3))                    # multiply function is called and x is assigned to value 2 and y                                                         to 3 and multiplication operation is carried out which is                                                                 then printed and suitable output is obtained 
#Output is 6

Exceptional Handling

The feature in python that helps us to tackle the unexpected error is called exception handling. 

Even if the expression is correct syntax-wise but it may cause an error while executing it. 

Exceptions refer to the errors that are detected during the execution and such errors can be handled by the appropriate use of exception handling. 

The errors may be zero division error, out of the index, a variant in user-entered input, and so on. 

For example;
info= True                                 # Boolean value True is assigned to variable info 
while info == True:                    # while loop is executed till variable info is True 
    try:                                        # exception handling is introduced 
        number_1 = int(input(“Enter a number”))     # input from user is asked 
        info = False                      # now info is set to False Boolean value 
    except:                                 # if try finds error then except is executed
        print(“You entered and invalid input!!”)         # suitable message is displayed 
    info=True                             # info becomes True and again ask to enter number

In the above program how and in which case the use of exception handling is appropriate is explained. 

In the normal program if the user enters the value of variable number_1 as the string then the program displays a respective error and the remaining code terminates.

But with the use of exception handling the try function executes first and if there is not an error then it runs but if the try function creates an error then the program does not terminate instead it directly jumps to the except function and executes the except function with the suitable message as invalid input and since while loop is used so the loop runs until the data is entered right.

Algorithm

The algorithm is a set of step-by-step instructions for accomplishing a task. 

It is considered a development of a pattern for solving a program.

It is basically a plan to write a program. 

It helps us to solve our problems in a short period. 

The algorithm makes a program in a descriptive format. 

The algorithm is the first step of the program so it should be written in such a way that everyone who knows a bit about programming must understand your steps and the logic of the program. 

Finally, a perfect algorithm results in a perfect and errorless program.

Flowchart

The flowchart refers to the diagrammatic representation of a program. 

As we always feel easy to remember the visual or diagrammatic things rather than theoretical ones, since a flowchart is the diagram of the flow of the program so it is easy to understand than the algorithm. 

It represents the program step by step with the help of its own symbols.

In the flowchart, the start and end of the program are indicated with the oval shape, input/output are represented with the parallelogram, the process is carried out in a rectangle, decision making through a diamond structure, an arrow or connector shows the relation between these shapes, etc.

Pseudocode

Pseudocode is not an exact programming code that is meant to be executed but it’s an informal way of explaining the program which does not exhibit any strict language syntax. 

Since it is not an actual program so it does not comply and is not executed. 

It refers to the outline or a rough code of a program. 

The main purpose of writing the pseudocode of a program is that it can be understood by any programmer. 

It also contributes to decreasing the chance of errors in a program while writing the actual code.

Data Structures

Data structures in python are the method of storing, arranging, and organizing the data so that they can be easily accessed, implemented, and worked with full efficiency. 

These data structures hold the data for the program and also help in defining the interrelationship between the data and their operations. 

Many data structures help in solving the larger obstacles with ease and in a short period. 

The data types applied in this program include:
  •     Integer
  •     Float
  •     String
  •     List
  •     Boolean

Integer

Integers are the positive or negative whole numbers with no decimal point. 

They are represented as “int” in the program. It can be converted to other data types like; float, long, etc. 

For example; they are used to assign numeric values to the respective variables. 
    num_1 = 7                 # integer value i.e. 7 is assigned to the variable num_1 

Float

Float is considered as the number with the decimal points. 

These numbers are called floating-point numbers. 

Type conversion is also possible in it. 

Examples of floating numbers include; 0.0, 2.6,3.0, and so on. 
type(2.0)                       #checking the type of number 2.0 
<type 'float'>                 #type of 2.0 is float 

String

The string is one of the data types that store a bunch of characters or text data. 

In python, strings are represented by either single or double-quotes. 

Strings can perform different operations like slicing, concatenation, etc. 
Example; ‘hi’ or “hi” (both represent string)


List

The list is one of the most flexible and versatile collection data types which can store multiple values separated by commas and are enclosed by square brackets. 

The list can contain elements of the same data type or mixed data type.

List works based on indexing. Each and every element in the list contains its respective index. 

List indices start at 0 and it also supports negative indexing which starts at -1 from the end. 

Since lists are mutable so it contains different features such as append, extend, slicing, concatenation, etc. and the list also has its own attributes like remove (), clear (), len (), pop (), del (), and so on.

list_1 = [1, 2, 0, 9]                                                  # a list of same data type 
list_2 = [7, 2.0,” hello”, True]                                 # a list of different data types 
list_1.append (8)                                                  # list_1 is now [1, 2, 0, 9, 8] 
list_2. extend ([3,5])                                             # list_2 is now [7, 2.0, “hello”, True, 3, 5] 
list_3 = [2, 1, 0, 6]                                                # a list of numbers 
list_3.remove (0)                                                 # mutates list_3 = [2, 1, 6] 
del (list_3[0])                                                       #mutates list_3 = [1,6] 
list_3.pop ()                                                        #returns 6 and mutates list_3 = [1]


Boolean

Boolean in python contains only the two values either True or False. 

As python is case sensitive so python does not consider true and false as Boolean values. 
Example; print(type(False))                         #gives output as , representing as Boolean

Testing

The main purpose of testing our program is for gaining an error-free program. 

Testing is not only done after the whole completion of the program but also can be done between the programs to know whether the program is providing the expected result or not. 

More test results in a high chance of an error-free program. 

Testing is done for the sake of the program. 

It helps in saving our time and hard work. 

In this program also, I performed many tests to make my program error-free.

Black-Box Testing

Black box testing is the method of testing in which the functionality of the program is tested without concerning the internal code details and recognizing the internal paths of the application. 

The main concern of implementing the black box test is on the input and output of the program ignoring the interior knowledge of the program.

Post a Comment

0 Comments