Unit 3.pdf
Document Details
Uploaded by Deleted User
Tags
Full Transcript
FE- Programming and Problem Solving Prof. Sarang Joshi Coding with Python Framework Functions and Modules U3 Need of Functions l Divide-and-Conquer helps identify the need of function including – Decomposing complex problems into simple...
FE- Programming and Problem Solving Prof. Sarang Joshi Coding with Python Framework Functions and Modules U3 Need of Functions l Divide-and-Conquer helps identify the need of function including – Decomposing complex problems into simpler pieces l To improve the readability of code l Improves the reusability of the code l --same function can be used in any program rather than writing the same code from scratch l Reducing duplication of code and avoid coding cost and testing time (Tool used to identify the duplication using scatter diagram) l Debugging of the code would be easier with use of functions, as errors are easy to be traced Need of Functions l functions can help inducing concurrency, parallel and embedded coding related to program design l Functions helps in drawing function dependency diagrams, reliability testing l Functions help in developing framework and useful reference documents l Easy to write test scripts and functions can be tested independently thereby improving testing time, l Testing can be overlapped with coding l Information hiding l Fooling or delaying the code thept of interpretation resulting in delaying the copy of product and defeating it by version-ing l Function can exhibit behavioral properties including morphism, overloading etc. Function defined l A function is a block of code which only runs when it is called l In other words, Function is a independently addressed space which encloses a sequence of code that is executed when called for with/ without input parameters and can return the outcomes to the original calling code program l Code can pass data, known as input parameters, into a function l A function can return data as a result Function CALL l "Function call" means to make a reference to a function name space(Address space) code that is written elsewhere l This function "call" can be made to the standard Python library (functions that are installed when a Python installed), third-party libraries (function written by other people that are used while coding), or own functions written while code development l Any function name is a separate address space l Convention method to access function name space l is call Function call Function local gobal variables l Global and Local variables l x=5 l Keywords: global, local Access to global variables is l valid in function and outside l def test1(): function l x = 10 l Access to local variable to l print("local x:", x) function are accessible within the function l Outcomes l test1() l Local x: 10 l print("global x:", x) l Global x: 5 Non global non local variables [Ref: programiz.com] l Nonlocal variables are l # works with python 3.x and used in nested functions not 2.7 l def outer(): (function inside another l x = "local" function) whose local scope is not defined. l def inner(): # nested This means that the function variable can be neither l nonlocal x in the local nor the l x = "nonlocal" l print("inner:", x) global scope l Keyword: nonlocal l inner() l Outcome l print("outer:", x) l linner: nonlocal l outer: nonlocal l outer() Lifetime of variables l The scope of a variable refers to the context in which that variable is visible/accessible to the Python interpreter l Variables get memory(address space) allocation(namespace(address) allocation) run time l The lifetime of a variable is the period throughout which the variable exits in the memory of your Python program l The lifetime of variables inside a function is as long as the function executes l These local(inside a function) variables are destroyed as soon as the function returns or terminates return Statement in Python Coding l A return statement is used to end the l def MyFunc(X): execution of the function call and “returns” to the assignment address and l M=X the result (value of the expression l #explicit return following the return keyword) assigned l return M+10 to the variable assigned to a calling address(function) l M1 = MyFunc(15) l The statements after the return statements are not executed l If the return statement is without any expression, then the special value None is returned l Using a return inside of a loop will break the loop and exit the function even if the iteration is still not finished return Statement in Python Coding l Implied value of return statement is l #implicit return “None” l def MyNext(x): l When there is no explicit value or l m=x computation returned by a function then it implicitly returns None l print(MyNext(10)) l In implicit return, return keyword can also be omitted as shown in example l Outcome l Or in a function it can be simply return l None statement without anything to return then implicitly “None” is returned l return keyword can be considered as exit statement in Python Revising functions [ref: programiz.com] l In Python, a function is a group of related statements that performs a specific task. l Functions help break the program into smaller and modular chunks l As the program grows larger and larger, functions make it more organized and manageable l Furthermore, it avoids repetition and makes the code reusable l Functions can be independently tested l It can contribute in Data-Flow-architecture of software design Revising functions [ref: programiz.com] l Syntax of Function l def function_name(parameters): l ["""docstring"""] l statement(s) or None l [return] l In python, the function definition should always be present before the function call. Otherwise, an error is displayed Revising functions [ref: programiz.com] l Keyword def that marks the start of the function header l A function name to uniquely identify the function. l Function naming follows the same rules of writing identifiers in Python Parameters (arguments) through which passes values to a function. They are optional l A colon (:) to mark the end of the function header l Optional documentation string (docstring) to describe what the function does l One or more valid python statements that make up the function body. l Statements must have the same indentation level (usually 4 spaces) l An optional return statement to return a value from the function Revising functions [ref: programiz.com] l Once a function is defined, it can be called from another function, program, or even the Python prompt l To call a function simply type the function name with appropriate parameters l In python, the function definition should always be present before the function call. Otherwise, an error occurs l The return statement is used to exit a function and go back to the place from where it was called Revising functions [ref: programiz.com] l Docstrings l # function definition l def greet(name): l The first string after the function header is l """ called the docstring and is short for l This function greets documentation string. It is briefly used to to the person passed in as a parameter explain what a function does l """ l Its optional, but documentation is a good l print("Hello, " + programming practice name) l In this example, a docstring is written l Output immediately below the function header l >>> l Usually use triple quotes so that docstring print(greet.__doc__) can extend up to multiple lines. This string l This function greets is available as the __doc__ attribute of the to the person passed function in as a parameter Lambda functions/ Anonymous function[ref: programiz] l the anonymous function, also known as lambda functions l In Python, an anonymous function is a function that is defined without a name l While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword l Hence, anonymous functions are also called lambda functions l Syntax: l lambda arguments: expression Lambda functions/ Anonymous function l Lambda functions can have any number of arguments but only one expression l Example l # Program to show the The expression is evaluated and l returned use of lambda functions l double = lambda x: x * 2 l Lambda functions can be used wherever function objects are required l print(double(5)) l In the this example, lambda x: x * 2 is the lambda function. Here x is the argument and x * 2 is the expression that gets l Output evaluated and returned l 10 l This function has no name. It returns a function object which is assigned to the identifier double Lambda functions/ Anonymous function l In Python, generally use l Example lambda function as an l # Program to show the argument to a higher-order use of lambda functions function (a function that l double = lambda x: x * 2 takes in other functions as arguments) l print(double(5)) l Lambda functions can also be used along with built-in l Output functions like filter(), map() l 10 etc. Lambda functions/ Anonymous function l The filter() function in Python l Example with built-in takes in a function and a list function “filter” as arguments. l # Program to filter out only The function is called with all the even items from a list l my_list = [1, 5, 4, 6, 8, 11, 3, the items in the list and a new 12] list is returned which contains items for which the function l new_list = evaluates to True. list(filter(lambda x: (x%2 == 0) , my_list)) l Given example uses filter() print(new_list) function to filter out only Output even numbers from a list [4, 6, 8, 12] l Lambda functions/ Anonymous function l Example use with map() l Example with builtin l Map() function can be considered function “map” as mapping of Domain to range in l # Program to double each SET theory item in a list using map() l The map() function in Python takes in a function and a list. l my_list = [1, 5, 4, 6, 8, 11, 3, l The function is called with all the 12] items in the list and a new list is returned which contains items l new_list = list(map(lambda returned by that function for each x: x * 2 , my_list)) item. l Here is an example use of map() l print(new_list) function to double all the items in l Output a list. l [2, 10, 8, 12, 16, 22, 6, 24] Python docstrings l Comments and docstrings are bit different in the sense single line comments start with symbol “#” and are completely ignored by the Python line interpreter l whereas docstrings can be assessed using an attribute construct __doc__ l Whenever string literals are present just after the definition of a function, module, class or method, they are associated with the object as their __doc__ attribute Python docstrings l def square(n): l '''Takes in a number n, returns the square of n''' l return n**2 l print(square.__doc__) l Output l Takes in a number n, returns the square of n Python docstrings: Study of print function l Docstrings for the built-in print() function l Its just like –help on print statement, its a better way to study Python functions l print(print.__doc__) l Output l print(value,..., sep=' ', end='\n', file=sys.stdout, flush=False) l Prints the values to a stream, or to sys.stdout by default. l Optional keyword arguments: l file: a file-like object (stream); defaults to the current sys.stdout. l sep: string inserted between values, default a space. l end: string appended after the last value, default a newline. l flush: whether to forcibly flush the stream. l Standard conventions to write single-line docstrings: l Even though they are single-lined, still use the triple quotes around these docstrings as they can be expanded easily later l The closing quotes are on the same line as the opening quotes. l There's no blank line either before or after the docstring l They should not be descriptive, rather they must follow "Do this, return that" structure ending with a period. Good Program(ming) (Coding) Practices l 1>>>>Writing Well-Structured Code l There are two programming concepts leading to technologies: Structured programming(C- style ) and Un-structured programming (Object Oriented Programming) l with proper names of the modules(input modules, Output Modules, Processing Modules). Modules are independent. Some times Dependent Good Program(ming) (Coding) Practices l 1>>>>Writing Well-Structured Code l correct indentations, and documentation improves the code's usability in the future l include a README file for describing the project l the setup.py file for properly setting up the project in a new environment l the requirements.txt for describing the dependencies that are needed l documentation describing how all the components of the projects work l tests in the repository for better understanding Good Program(ming) (Coding) Practices l 2>>>>Having Proper Comments and Documentation l Incorporating comments in a code can be the first step of having proper documentation l This may include, what is the role of a specific module in a code, how a function works, or the role of a function etc l In Python, two types of comments are available. l 1>>single-line comments which start with a hash symbol (#). l These comments are helpful to describe a specific line of code like what an expression does Good Program(ming) (Coding) Practices l 2>>>>Having Proper Comments and Documentation l e.g print(“hello world”) #one line comment: this prints hello world l 2> a multiline comment which starts and ends with triple quotes (‘’’). l They are more useful while describing a module or a function l Having meaningful comments and proper documentation improves the readability and usability of a code Good Program(ming) (Coding) Practices l 3>>>>Proper Naming of Variables, Classes, Functions and Modules l One of the most common mistakes among beginner Python developers is improper naming of variables, classes, functions etc. l It is always necessary to avoid writing single-letter variable names for heavily abbreviated function or class names Good Program(ming) (Coding) Practices l 3>>>>Proper Naming of Variables, Classes, Functions and Modules l A long variable should be separated using underscores(_), e.g. long_variable_name l While writing class name CamelCase should be used, e.g. LongClassName l For writing functions using underscores is advised e.g. function_name() Good Program(ming) (Coding) Practices l 4>>>>Writing Modular Code l The module is a collection of functions to be used when needed l Python has many modules and libraries under a repository known as PyPI (python package index) l It contains numerous models written by Python developers l These Modules can be used by installing them from the internet. Good Program(ming) (Coding) Practices l 4>>>>Writing Modular Code l The module is a collection of functions to be used when needed l Python has many modules and libraries under a repository known as PyPI (python package index) l This prevents from implementing all the logic from scratch and makes program script compact and readable l Since these modules are written by very experienced programmers, It gives the best possible solution and reduced lead time Good Program(ming) (Coding) Practices l 4>>>>Using Virtual Environments l The main purpose of a virtual environment in Python is to create a separate environment for Python projects. This becomes particularly useful when using any third-party module or library in a project. l If two projects use different versions of the same module (third party), e.g. clear screen module developed by say two different developers Good Program(ming) (Coding) Practices l 4>>>>Using Virtual Environments l it becomes difficult for python to understand which version is required for which project as both the versions are saved in the same directory with the same filename l This problem can be easily tackled by using a virtual environment for each project where the dependencies are stored independently Good Program(ming) (Coding) Practices l 4>>>>Using Virtual Environments l To use the virtual environment, it is necessary to l 1> install it using the following line of code l $pip install virtualenv l 2> and to create a new environment, use l $python3 -m venv env l This makes working on different projects at the same time,...easy Good Program(ming) (Coding) Practices l Writing Well-Structured Code l Having Proper Comments and Documentation l Proper Naming of Variables, Classes, Functions and Modules l Writing Modular Code l Using Virtual Environments Python Modules ⚫ Modules are simply files with the “.py” extension containing Python code, data definitions that can be imported inside another Python Program ⚫ Python packages (collection of Modules or directory) provide a beginner-friendly and efficient way to solve complex problems in scientific computing, data visualization, data modeling, and many other fields ⚫ There are two types of Modules ⚫ Built-in Modules (Modules that are installed automatically during installation of Python or bundled with installation package ) ⚫ User defined Modules Python Modules ⚫ A Python code that is used frequently in different program/s is a candidate member to be written as a module OR ⚫ Its a special function/ equations/ computations in other domains of science, arts, commerce which can not be written efficiently by other domain coders. ⚫ Python package is a directory with a collection of python modules ⚫ Module may have Packages/Sub-Packages/Sub- Modules ⚫ Standard Practice of writing a module ⚫ Each module usually(as a standard practice) include a file __init__.py (its like a constructor of a class on oo- Python Modules [Ref: learnpython.com] ⚫ Here's an example of the my_model package with three sub-packages: training, submission, and metrics Python Modules ⚫ Most Popularly used packages in 2021, 2022 ⚫ NumPy - Package for scientific computing ⚫ Pandas- If you work with tabular, time series, or matrix data, pandas is your go-to Python package ⚫ Matplotlib - Matplotlib is the most common data exploration and visualization library ⚫ Seaborn - Seaborn is a high-level interface for drawing attractive statistical graphics with just a few lines of code ⚫ Scikit-learn – used for computing Regression or data classification problems ⚫ Requests – This is used to make HTTP requests, a web interface with Python code Python Modules ⚫ Most Popularly used packages in 2021, 2022 ⚫ Urllib3 – Its an important HTTP client (to generate HTTP requests) for Python, For example, write a Py program to generate an email ⚫ NLTK - Natural Language Toolkit (NLTK) is one of the leading Python platforms for processing language data. It is a set of language processing libraries and programs that provide a toolkit ⚫ Pillow - Its an image data processing module, called Pillow package. It is a fork of PIL (Python Image Library) that developed into an easy-to-use and efficient tool for image manipulation in Python ⚫ Pytest - This package provides a variety of modules for testing new code, including small unit tests and complex functional tests for applications and libraries Built-in Modules with Python [Ref: geeksforgeeks] ⚫ >># importing module calc.py ⚫ import calc ⚫ print(calc.add(10, 2)) ⚫ >># importing sqrt() and factorial from the module math ⚫ from math import sqrt, factorial ⚫ # if simply do "import math", then math.sqrt(16) and math.factorial() are required. ⚫ print(sqrt(16)) ⚫ print(factorial(6)) Built-in Modules with Python [Ref: geeksforgeeks] ⚫ >>from math import * ⚫ >># importing sys module ⚫ import sys ⚫ # importing sys.path ⚫ print(sys.path) ⚫ >> Renaming a module maths ⚫ import math as gfg ⚫ print(gfg.sqrt(16)) ⚫ print(gfg.factorial(6)) Built-in Modules with Python [Ref: geeksforgeeks] ⚫ >>The dir() function ⚫ The dir() built-in function returns a sorted list of strings containing the names defined by a module. ⚫ The list contains the names of all the modules, variables, and functions that are defined in a module. ⚫ # Import built-in module random ⚫ import random ⚫ print(dir(random))