Essential Python Classroom Notes – 10/08/2021

Functions

  • A function is a reusable piece of code. Refer Here for the notebook
  • Importing the function and calling it from the shell. Refer Here for the function with positional arguments Preview
  • Keyword Arguments: Refer Here

Preview

  • Exercise: Try to write a python function in a module to calculate whether the year passed to the function is leap or not Refer Here
def is_leap(year):
    """This function finds if the year passed is leap or not
    Arguments:
      year (int): year 

    Return: True if leap false other wise
    """
    pass
  
  • Solution is
def is_leap(year):
    """This function finds if the year passed is leap or not
    Arguments:
      year (int): year 

    Return: True if leap false other wise
    """
    if year % 4 == 0:
        if year % 100 == 0:
            if year % 400 == 0:
                return True
            else:
                return False
        return True
    else:
        return False

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner