Notes of Python Workshop, Python December 08_211209_063152.pdf - Study Material
Page 1 :
Day – 5, Class – Object,, Modules and, Package, , DECEMBER 08, Team Dealslover, Authored by: Shashank Kashyap, 1
Page 2 :
*args and *kwargs, Arbitrary Arguments (*args):, If you don’t know how many arguments will be passed to function, add * before the, parameter name., In this way the argument will become a tuple element., Example:, def create(*items):, print(f“Name of the product is {items[2]}”), , Arbitrary Keyword Arguments (*kwargs):, If you don’t know how many keyword arguments will be passed to function, add **, before the parameter name., This way function will receive a dictionary of argument., Example:, def create(**items):, print(f“ Item name is items[“name”]”), , Recursion and Lambda Function, Recursion:, A defined Function calling itself is called recursion., , 2
Page 3 :
Example:, def recur(k):, if (k>0):, result = k + recur(k-1), print(result), else:, result = 0, return result, , Lambda Function:, Lambda is an anonymous function that can take only one statement and will execute it., Syntax:, Lambda arguments : expression, Example:, X = lambda a, b : a*b, print(X(5, 6)), , Create Class and Object, Python Classes/Object:, Classes are basically collection of functions and properties., Objects are instances/ image of class which can be called using a variable., , Create Class:, We use the keyword class followed by name of the class to create class., , 3
Page 4 :
Example:, class MyClass:, x=5, , Creating Object:, Class name can be used to create object of the class by assigning it to variable., Example:, P1 = MyClass(), print(p1.x), , The __init__() Function:, This is the function which gets initiated when the class is being called in the form of, object., We usually use this function to create properties of object., Example:, class Person:, def __init__(self, name, age):, self.name = name, self.age = age, self Parameter:, The self parameter is used to refer to the current object., , Inheritance:, A class known as the child class can inherit the properties and methods from its parent, class by using the concept of inheritance., , 4
Page 5 :
Example:, class Student(Person):, pass, , Modules, Definition:, A module is basically a .py file which contains variables, functions and classes which, can be imported., , Various ways of Importing a Module:, import Math, import * from Math, from Math import pow, , Renaming the Module while importing, import numpy as np, , Try, Except and Finally, try, except and finally keywords are used for debugging purposes., , Try:, try block helps you to check a programming block for errors., , Except:, except block helps you to handle the error., , Finally:, finally block helps you to run the code regardless of the try….except block., 5
Page 6 :
Example:, try:, print(x), except Exception as e:, print(e), finally:, print(“Try an Except blocks are over.”), , Package, Definition:, A package is a directory which contains the __init__.py file along with other python, modules present in it., , 6