Python Tutorials

Overview

A date in Python is not a data type of its own, but a module called datetime can be imported to work with dates as date objects.

Importing the datetime Module for Use

import datetime

x = datetime.datetime.now() #get the current date and time
print(x)

Output:

2025-04-04 10:42:55.315652

Note

The result “2025-04-04 10:42:55.315652” contains the year, month, day, hour, minute, second, and microsecond.

Creating a Date Object

To create a date, the datetime() class of the datetime module is used.

The datetime() class requires three parameters to create a date: year, month, day.

import datetime

x = datetime.datetime(2025, 11, 22)
print(x)

Output:

2025-11-22 00:00:00

Note

The datetime() class also takes parameters for time and timezone (hour, minute, second, microsecond, tzone), but they are optional, and has a default value of 0, (None for timezone).

Using the strftime() Method

The datetime object has a strftime() method for formatting date objects into readable strings. It takes one parameter (format) to specify the format of the returned string.

import datetime

x = datetime.datetime(2025, 4, 15)
print(x.strftime("%B"))

Output:

April

Parameters

Format CodeDescriptionExample
%aWeekday, short versionMon
%AWeekday, full versionMonday
%wWeekday as a number 0-6, 0 is Sunday4
%dDay of month 01-3122
%bMonth name, short versionJan
%BMonth name, full versionJanuary
%mMonth as a number 01-1201
%yYear, short version, without century25
%YYear, full version2025
%HHour 00-2316
%IHour 00-125
%pAM/PMPM
%MMinute 00-5933
%SSecond 00-592
%fMicrosecond 000000-999999345223
%zUTC offset100
%ZTimezoneCST
%jDay number of year 001-366365
%UWeek number of year, Sunday as the first day of week, 00-5352
%WWeek number of year, Monday as the first day of week, 00-5352
%cLocal version of date and timeMon Jan 22 16:33:02 2025
%CCentury21
%xLocal version of date01/22/25
%XLocal version of time16:33:02
%%A % character%
%GISO 8601 year2025
%uISO 8601 weekday (1-7)1
%VISO 8601 weeknumber (01-53)1

Python Notes:

  • The most recent major version of Python is Python 3; however, Python 2 is still in use and quite popular, although not being updated with anything other than security updates
  • Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses
  • Python relies on indentation, using whitespace to define scope, such as the scope of loops, functions, and classes; other programming languages often use curly-brackets for this purpose
  • Python string methods return new values, and DO NOT change the original string
  • Python tuples are unchangeable after created (their items CANNOT be changed or re-ordered at a later point)
  • Python sets are unordered (may appear in random orders when called), unchangeable (the value of individual items cannot be changed after creation), unindexed (items cannot be referred to by index or key), and duplicates are NOT ALLOWED
  • As of v3.7, Python dictionaries are ordered and duplicates ARE ALLOWED; in v3.6 and earlier, dictionaries were unordered (did not have a defined order and could not be referred to using an index)
  • Python does not have built-in support for arrays, but Python lists can be used as pseudo “arrays”; therefore, all Python list methods will work with these pseudo “arrays”

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.