Posts

Python Packages

Image
Python  Packages  Packages :               Packages are a ways of structuring(organizing) Python's module namespace by using "dotted module names".  A directory must contain a file named init.py in order for python to consider it as a package. This file can be left empty but we generally place the initialization code for that packages in this file.           directory structure: Importing module from a packages We can import module from packages using the dot ( . ) operator.         #import Game.Image.open Packages in Python, is basically group of modules.

Python3 Modules

Python  Modules Modules :              Modules refer to a file containing Python statements and definitions.    A file containing Python code, for eg: abc.py, is called a module and its module name would be "abc"   We use module to break down a large programs into simpler manageable and organized files. Furthermore, module provide reusability of code.   We can define our most used functions in a module  and import it, instead of copying their definitions into different programs. How to import a module?  We use the import keyword to do this.        import example      #imported example module   Using the module name we can access the function using dot( . ) operation.        example.add(10,10) output :  20 Python has a lot of standard modules available. Example:        import math        print(math.pi) output: 3.141592653589793        import datetime        datetime.datetime.now( ) output: datetime.datetime(2020,10,18,47,20,606228) import with renaming        import math as

Python3 File Handling

Python  File Handling FILE  I/O          File is a named location on disk to store related information. It is used to store data permanently in a non-volatile memory (eg. hard disk).  Since, random access memory (RAM) is volatile which loses its data when computer is turned off ,we use files for the future use of the data    When we want to read from or write to a file we need to open it first. When we are done, it needs to be closed, so that resources that are tied with the file are freed.  File operation :   1.  Open a file   2.  Read or write(perform operation)   3.  Close the file Opening a File        Python has a built-in function open() to open a file. This function returns a file object, also called a handle, as it is used to read or modify the file accordingly.        #open file in current directory            f=open('example.txt')          we can specify the mode while opening a file. In mode, we specify whether we want to read 'r' , write 'w' or appen

Python3 Lambda Function

Python    Lambda Function  Anonymous/Lambda Function :    In Python, anonymous function is a function that is defined without a name.      While normal functions are defined using the def keyword, in Python anonymous functions are defined using the lambda keyword.     Lambda functions are used extensively along with built-in functions like filter(),map(),reduce() . Syntax:                    lambda arguments: expression Example        double=lambda x: x*2        print(double(5)) output : 10        def double(x):              return x*2        print(double(5)) output: 10         #example using filter() function        lst=[1,2,3,4,5]        even_lst=list(filter(lambda x: (x%2==0),lst))        print(even_lst) output:  [2,4]        #example using map() function        lst=[1,2,3,4,5]        new_lst=list(map(lambda x: (x+2),lst))        print(new_lst) output: [3,4,5,6,7]         #example using reduce() function        from functools import reduce        lst=[1,2,3,4,5]        new_lst=li

Python3 Recursive Function

Python Recursion  Recursion :      We all know in Python, a function can call other functions. It is even possible to call itself. These type of construct are termed as recursive functions. Example        def factorial(num):              return 1 if num==1 else (num*factorial (num-1))        num=5        print("factorial of {0} is {1}".format(num, factorial(num))) output : factorial of 5 is 120 Advantages    1. Recursive functions make code look clean and elegant.    2. A complex task can be broken down into simpler sub-problems using recursion.    3. Sequence generation is easier with recursion than some nested iteration. Disadvantages   1. Sometimes the logic behind recursion is hard to follow through.   2. Recursive calls are expensive (inefficient) as they take up  a lot of memory and time.   3. Recursive functions are hard to debug.     Python program to display the fibonacci sequence up to n-th term using recursive function        def fibonacci(num):              return

Python3 Function Arguments

Python Function Arguments 1 . Function Arguments :        def greet(name, msg):              print("Hello {0}, {1} ".format(name, msg))        #call a function with arguments        greet("Satish", "good morning") output: Hello Satish, good morning         #suppose if we pass one argument        greet("satish") output:   ----------------------------------------------------------------------               ---               TypeError               TypeError : greet( ) missing 1 required positional argument: 'msg' Different Forms of Arguments :        def greet(name, msg="good morning"):              print("Hello {0} , {1}".format(name, msg))            greet("Satish", "good night") output :   Hello Satish , good night         #with out msg argument        greet("Satish")   output:   Hello Satish , good morning   Once we have a default argument , all the arguments to its right must also have

Python3 Types of functions

Python  Functions Types of Functions:        1.  Built-in Functions        2.  User-defined Functions   Built-in Functions : 1 . abs()         #find the absolute value        num=-100        print(abs(num))   output: 100 2 . all()   #return value of all() function   True : if all elements in an iterable are true   False : if any element in an iterable is false Eg:        lst=[1,2,3,4]        print(all(lst))               lst1=[0,2,3,4]        print(all(lst1)) output: True               False           lst=[ ]        print(all(lst))           lst1=[False,1,2]      #False present in a list, then all(lst) is also False        print(all(lst1)) output:  True                False   3 . dir()   The dir() tries to return a list of valid attributes of the object.   If the object has dir() method, the method will be called and must return the list of attributes.   If the object doesn't have dir() method, the method tries to find information from the dict attribute (if defined) ,and from ty