Saturday 20 March 2021

Python strptime() vs Python strftime()

The strftime() method returns a string representing date and time using date , time or datetime object. So it is used to convert the date-time object into string format.

Let’s take a look at an example:

    from datetime import datetime
    t = datetime.now()
    print('current-time--->', t)
 #output
    current-time---> 2023-01-10 23:16:42.511274

now let's play around strftime
 
    print(t.strftime('%y-%m-%d'))
output:-->  
    23-01-10
    print(t.strftime('%Y-%m-%d'))
output:--> 
    2023-01-10
    print(t.strftime('%y-%m-%d %H:%M'))
output:--> 
    23-01-10 23:16

So basically we can extract any kind of data from date-time object in string format.

The strptime() method creates a datetime object from the given string. It used to convert a given string into python datetime object.

In the same way, let’s look at an example for strptime.

    from datetime import datetime
    t_date = '2023-01-10'
    t_date_obj = datetime.strptime(t_date, '%Y-%m-%d')
output:-->
    datetime.datetime(2023, 1, 10, 0, 0)
 lets add a time as well.
    t_date_time = '2023-01-10T10:10'
    t_date_time_obj = datetime.strptime(t_date, '%Y-%m-%dT%H:%M')
output:-->
    datetime.datetime(2023, 1, 10, 10, 10)

Here t_date_obj and t_date_time_obj now became regular python datetime object and you can start treating them as datetime object further in your program.

It’s really helpful in time formatting, ingesting data in different time format, date-to-string conversion etc.

Do check out the official python documentation for the full list of abbreviations.

Directive Meaning Example
%a Weekday as locale’s abbreviated name. Sun, Mon, …, Sat (en_US);So, Mo, …, Sa (de_DE)
%A Weekday as locale’s full name. Sunday, Monday, …, Saturday (en_US);Sonntag, Montag, …, Samstag (de_DE)
%w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. 0, 1, …, 6
%d Day of the month as a zero-padded decimal number. 01, 02, …, 31

https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

Thursday 4 March 2021

Python Debugging Techniques for Beginners

Debugging is an important part of the software development process, and Python provides several techniques for beginners to debug their code. Here are some common techniques:

  1. Print statements: One of the simplest and most widely used techniques is to insert print statements in your code to check the values of variables and expressions at different points in the execution. This can help you identify where the problem is occurring and what the state of the program is at that point.
  2. The pdb library: Python includes a built-in library called pdb that provides a command-line interface for debugging. You can insert the statement import pdb; pdb.set_trace() at the point where you want to start debugging, and the program will enter the pdb interactive mode, allowing you to step through the code and inspect variables.
  3. The ipdb library: ipdb is an improved version of pdb library, it allows you to use the same interface as pdb but with some added features like syntax highlighting and tab completion.
  4. The breakpoint() function: Python 3.7 introduces a built-in function breakpoint() that is similar to pdb.set_trace() but it does not require importing pdb, it is built-in to the interpreter.
  5. IDEs and text editors with debugging support: Many IDEs and text editors have built-in support for debugging Python code, such as PyCharm, VSCode, and Sublime Text. These tools provide a graphical user interface that allows you to set breakpoints, step through code, and inspect variables.
  6. Assert statements: Using assert statements you can check if a certain condition is true during runtime and if it isn't it will raise an AssertionError, you can use this to check if the code is working as expected.
  7. logging: Logging is a way to record information about your code's execution. It can be used to record messages that can help you understand what is happening in your code.
  8. try-except block: Using try-except blocks to catch and handle specific errors, can help you isolate and fix the issue, and also can provide the user with a meaningful message.

While debugging can be a frustrating process, these techniques can help you quickly identify and fix errors in your code, allowing you to move on to the next step in your development process. It's important to try different techniques to find the one that works best for you and your specific needs.

 Do Comment what's your favorite? 

Sunday 28 February 2021

Python’s Inbuilt Modules to Make Life Easier

Python has a number of built-in modules and packages that are included with the standard distribution. Here are some popular ones:

  1. math: A module that provides mathematical functions and constants, such as trigonometric functions, logarithms, and the constant pi.
  2. random: A module that provides various random number generators and probability distributions.
  3. os: A module that provides a way to interact with the operating system, such as navigating the file system, creating and removing directories, and executing shell commands.
  4. time: A module that provides functions for working with time and dates, such as measuring time intervals and parsing and formatting date and time strings.
  5. datetime: A module that provides classes for working with dates and times, such as date, time, datetime, timedelta, etc.
  6. json: A module for working with JSON data, it provides functions for encoding and decoding JSON data.
  7. re : A module that provides regular expression matching operations.
  8. sys: A module that provides access to some variables and functions that are used or maintained by the Python interpreter, such as the command-line arguments passed to a script.
  9. statistics: A module for mathematical statistics functions, like mean, median, variance and etc.
  10. sys: A module that provides access to system-specific parameters and functions, like interpreter version, command line arguments and etc.
  11. urllib : A module that provides an API for using the basic HTTP and FTP protocols, it also used for opening and reading URLs.
  12. argparse : A module that makes it easy to write user-friendly command-line interfaces.

These are just a few examples of the built-in modules and packages available in Python. Each of them provides a specific set of functionalities that can be used to solve different problems.

Do comment your favorite module.