Time module

Python offers a module called ‘time’ to create varying functions that are related to time as well as to achieve time-related tasks. Consider a function from the ‘time’ module which returns the number of seconds passed since ‘epoch’. Epoch is the time passed since January the first of 1970.  import time # This prints time…

datetime module

Python does not have a direct implementation to work with dates and time but we can use a built-in module called “datetime” to work with dates and time as objects. Consider an example: import datetime current_date_time = datetime.datetime.now() print(current_date_time) When we run this, we get: The output is displayed in the format of Year-Month-Day Hour:Minute:Second.Microsecond. Each…

Shallow and Deep Copy

In Python, we can use the ‘=’ operator to copy one value of a variable to another. However, when we copy a value what happens underneath is that Python is simply creating another reference to the same variable. When one reference changes its internal value, the change is reflected in all references. Another important aspect…

Garbage Collection

Garbage Collection Python uses Garbage Collection to free up memory that was given to the program after it has completed execution, and this also includes memory that was once given to a program at a certain time but is no longer in use by the program. Garbage collection helps us in memory management and also…

Assert Statement

Assert Statement Python offers a keyword known as the ‘assert’ keyword and we use it to run conditional checks to handle different errors. If the condition is not satisfied an error is raised and the program halts. Assert also tells us where the error happened. This condition is known as the ‘Assertion Condition’ and the…

Decorator & Property

Decorators To better understand Decorators, consider a scenario of painting a house. If the house is already painted in one color let’s say white, we also have the option of adding another color of paint to a specific part of the house, without having to remove the entire paint of the house. Likewise, Decorators give…

Generators & Clousers

Generators Generators are functions and a function is deemed as a Generator function if it has one or more yield statements. Generators also provide us with a very simplistic way of creating iterator objects. They also do not store all of the values instead they generate one value at a time. In a practical setting,…

__name__ Variables

__name__ Variable Dunder In Python, we have what we call ‘dunder’ names, and it refers to a naming convention that uses double underscores at the start and end of the variable or function. The term ‘dunder’ means double underscores. The term ‘__name__’ is a special variable in Python which refers to the current script that…

Logging Classes and Functions

Logging Classes & Functions So far, we have seen how we can create logs as well as how to write logs to a file. When creating logs we saw the term ‘logger’ when we discussed the name of the log, as well as creating a log using the FileHandler() function. Let’s dive deeper into what…

Logging Varriable Data

Variable Data When creating logs it will be useful if we had the option to use variables to make a more meaningful log record. While we can create variables that hold our string, we can also create logs that directly access a variable we require. import logging user_name = “Christopher” logging.warning(“%s encountered a warning message”,…