Aiffel Day 3

2195 ワード

LMS & Python master class day 3


What I've learned


  • Try - Except

  • Lambda expression

  • Class, Module, Pacakge
  • 1. Try - Except


    With the help of try-except and try-except-else, you can avoid many unknown problems that could arise from your code.
    The try block is used to check some code for errors. The code inside the try block will execute when there is no error in the program.
    Whereas the code inside the except block will execute whenever the program encounters some error in the preceding try block.
    try:
        # Some Code
    except:
        # Executed if error in the
        # try block
    else() and finally() are used also with try() and except() .

    Seems not too difficult but it's not the best way to handle all of the errors. There are plenty of other ways to do so.
    Errors and Exceptions

    Lambda expression


    A lambda function is a small anonymous function. It can take any number of arguments, but can only have one expression. The form may look like lambda argument(s): expression .
    #Normal python function
    def a_name(x):
        return x+x
        
    #Lambda function
    lambda x: x+x
    Lambda expressions are used for lots of different occasions but I'll get into that later.
    higher-order fucntions look this up

    Class, Module, Package


  • Class: templates for creating objects, contain variables and functions which define the class objects
  • Module: python programs that can be imported into another python program, importing a module enables the usage of the module's functions and variables into another program.
  • You can download packages from here .