How to convert Python date string mm/dd/yyyy to datetime?

Rajendra Dharmkar
Updated on 28-Sep-2023 02:02:47

7K+ Views

In Python, you can convert a string to date object using the strptime() function. Provide the date string and the format in which the date is specified.  Example import datetime date_str = '29/12/2017' # The date - 29 Dec 2017 format_str = '%d/%m/%Y' # The format datetime_obj = datetime.datetime.strptime(date_str, format_str) print(datetime_obj.date())OutputThis will give the output −2017-12-29

How to find only Monday's date with Python?

Vikram Chiluka
Updated on 28-Sep-2023 01:59:51

6K+ Views

In this article, we will show you how to find only Monday's date using Python. We find the last Monday, next Monday, nth Monday's dates using different methods− Using timedelta() function Using relativedelta() function to get Last Monday Using relativedelta() function to get next Monday Using relativedelta() function to get next nth Monday Using timedelta() function to get previous nth Monday Method 1: Using timedelta Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task − Use the import keyword, to import the, datetime (To work with Python dates and times) module. Use ... Read More

How to store and retrieve a date into MySQL database using Python?

Rajendra Dharmkar
Updated on 28-Sep-2023 01:40:28

3K+ Views

To insert a date in a MySQL database, you need to have a column of Type date or datetime in your table. Once you have that, you'll need to convert your date in a string format before inserting it to your database. To do this, you can use the datetime module's strftime formatting function.Example from datetime import datetime now = datetime.now() id = 1 formatted_date = now.strftime('%Y-%m-%d %H:%M:%S') # Assuming you have a cursor named cursor you want to execute this query on: cursor.execute('insert into table(id, date_created) values(%s, %s)', (id, formatted_date))Running this will try to insert the tuple (id, date) ... Read More

How to compare time in different time zones in Python?

Vikram Chiluka
Updated on 28-Sep-2023 01:37:36

3K+ Views

In this article, we will show you how to compare time to different timezones in Python using the below methods. Comparing the given Timezone with the local TimeZone Comparing the Current Datetime of Two Timezones Comparing Two Times with different Timezone Method 1: Comparing the given Timezone with the local TimeZone Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task – Use the import keyword, to import the datetime, pytz modules. Use the timezone() function (gets the time zone of a specific location) of the pytz module, to get the timezone ... Read More

How do nested functions work in Python?

Vikram Chiluka
Updated on 28-Sep-2023 01:30:40

4K+ Views

In this article, we will explain nested/inner functions in Python and how they work with examples. Nested (or inner) functions are functions defined within other functions that allow us to directly access the variables and names defined in the enclosing function. Nested functions can be used to create closures and decorators, among other things. Defining an inner/nested Function Simply use the def keyword to initialize another function within a function to define a nested function. The following program is to demonstrate the inner function in Python − Example # creating an outer function def outerFunc(sample_text): sample_text ... Read More

How to raise an exception in Python?

Manogna
Updated on 28-Sep-2023 01:26:59

1K+ Views

We can force raise an exception using the raise keyword. Here is the syntax for calling the raise method. raise [Exception [, args [, traceback]]] Where, the Exception is the name of the exception; the optional "args" represents the value of the exception argument. The also optional argument, traceback, is the traceback object used for the exception. #raise_error.py try: i = int ( input ( "Enter a positive integer value: " ) ) if i

How can we create recursive functions in Python?

Rajendra Dharmkar
Updated on 28-Sep-2023 01:19:18

1K+ Views

Recursion is a programming technique, in which a function calls itself one or more times in its body. Usually, it is returning the return value of this function call. If a function definition follows recursion, we call this function a recursive function. A recursive function has to be terminated before it can be used in a program. It terminates, if, with every recursive call, the solution of the problem becomes smaller and moves towards a base case, where the problem can be solved without further recursion. A recursion can lead to an infinite loop, if the base case is not ... Read More

How can we overload a Python function?

Rajendra Dharmkar
Updated on 28-Sep-2023 01:15:57

2K+ Views

In Python, you can define a method in such a way that there are multiple ways to call it. Depending on the function definition, it can be called with zero, one, two or more parameters. This is known as method overloading. In the given code, there is a class with one method, sayHello(). We rewrite as shown below. The first parameter of this method is set to None, which gives us the option to call it with or without a parameter. An object is created based on the class, and we call its method using zero and one parameter. To ... Read More

Centered Pentadecagonal Number

Rinish Patidar
Updated on 27-Sep-2023 16:04:39

32 Views

The problem includes printing the N-th centered pentadecagonal number for any input number N. A centered pentadecagonal number is a number that can be represented in the form of a figure with a dot in the centre and surrounded by successive layers of the pentadecagon i.e. 15-sided polygon. Here the successive layers of the pentadecagon depict that the first layer surrounding the dot in the centre will be 15-sided polygon, the next layer will be 30-sided polygon followed by a 45-sided polygon and so on. We can understand the concept of centered pentadecagonal with the below figures. The first ... Read More

Centered Octagonal Number

Rinish Patidar
Updated on 27-Sep-2023 15:51:40

19 Views

The problem statement includes printing the N-th centered octagonal number for some positive integer N, which will be given by the user. A centered octagonal number is a type of number which can be represented in a pattern of figures. Every centered octagonal number can be represented as a dot in the centre surrounded by the successive layers of an Octagon. An octagon is a type of polygon in geometry which has 8 sides in it. The successive layers of an octagon means that the first layer surrounding the dot in the centre will be an octagon, the second ... Read More

1 2 3 4 5 ... 11330 Next
Advertisements