Posts

Showing posts from December, 2020

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.14...

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...

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))        prin...

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 h...

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("Sati...

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  ...

Python3 Functions introduction

Image
Python  Functions   Functions:        Function is a group of related statements that perform a specific task.        Functions helps us to break our program into smaller and modular chunks. As our program grows larger and larger, functions make organized and manageable.    **   It avoid repetition and makes code reusable.   Syntax::           def functi on_name(parameters):              " " "                     Doc String              " " "                Statement(s)        1. keyword "def" marks the start of function header        2. Parameters(arguments) through which we pass values to a function. These are optional        3. A colon(:) to make the end of function...