Python Classroom Series – 26/Jun/2021

Exceptions and Errors

  • Errors in python can be of two types
    • Syntax Error: This error is caused by wrong syntax in the code. This leads toe termination of the program
    • Exceptions: These are raised when the program is syntactically correct, but the code resulted in error
  • Try and except Blocks in python for handling exceptions
  • Refer Here for the exception handling sample
  • Syntax of try
try:
    # some code block
except :
    # optional block
    # Handle Exceptions
else:
    # optional block
    # executes if no exceptions occur
finally:
    # optional block
    # always executed
  • Refer Here for the code block with all the blocks added. Try debugging this code

  • For python built-in exceptions Refer Here

  • Python Built-in exception hierarchy Preview

  • Refer Here for printing the error information

  • Generally the best practice is handle errors separtely Refer Here

  • We can raise Exception from our programs Refer Here

Calenders and Clocks

  • Python’s standard library has many date and time modules including

    • datetime
    • time
    • calendar
    • dateutil
  • Leap Year Preview

  • The datetime Module:

    • The standard module dateime Refer Here handles dates and times. It defines four major object classes, each with many methods
      • date for years, months and days
      • time for hours, minutes, seconds and fractions
      • datetime for dates and times together
      • timedelta for date and/or time intervals
    • date class: Preview
    • timedelta: We can use this to add some time intervals to date Preview
    • time: This object is used to represent time of the day Preview
    • datetime: this includes both date and time Preview Preview

Using the time module

  • The time module is used to represent epoch time.
  • Epoch time is Unix time which represents number of seconds since midnight on Jan 1, 1970
  • Preview
  • note: Refer Here for different timezones and the UTC values

Read and Write Dates and Times

  • We can convert dates and times to strings by using strftime().
  • This is provided as a method in the datetime, time and date objects and as function in the time module.
  • strftime() uses format strings to specify the outputs
Format Date/time unit Range
%Y year 1900…
%m month 01 -12
%B month Name January…
%b month abbrev Jan…
%d day of the month 01 – 31
%A week day name Sunday..
%a weed day abbrev Sun..
%H Hour (24 hr) 00-23
%I hour (12 hr) 01-12
%p AM/PM AM, PM
%M minute 00-59
%S second 00-59

Preview Preview

Leave a Comment

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

About learningthoughtsadmin