**datetime basics**
```python
from datetime import datetime, date, time, timezone
# Current UTC time (naive vs aware)
datetime.now() # naive — no timezone, uses local clock
datetime.utcnow() # naive UTC — deprecated in Python 3.12
datetime.now(timezone.utc) # aware UTC — preferred
# Creating specific dates
d = date(2024, 3, 15) # year, month, day
dt = datetime(2024, 3, 15, 10, 30) # year, month, day, hour, minute
```
**Naive vs aware datetimes**
A *naive* datetime has no timezone info — Python doesn't know if it's UTC, New York, or Tokyo. An *aware* datetime carries a `tzinfo` and can be compared across time zones:
```python
from datetime import datetime, timezone, timedelta
naive = datetime(2024, 3, 15, 10, 0) # no tzinfo
aware = datetime(2024, 3, 15, 10, 0, tzinfo=timezone.utc) # UTC
# You cannot compare naive and aware:
naive < aware # TypeError: can't compare offset-naive and offset-aware
# Custom offset timezone:
tz_kyiv = timezone(timedelta(hours=3)) # UTC+3
dt_kyiv = datetime(2024, 3, 15, 13, 0, tzinfo=tz_kyiv)
```
**zoneinfo — named timezones (Python 3.9+)**
```python
from zoneinfo import ZoneInfo
tz = ZoneInfo('Europe/Kyiv')
dt = datetime(2024, 3, 15, 10, 0, tzinfo=tz)
dt.isoformat() # '2024-03-15T10:00:00+02:00'
# Convert between zones:
dt_ny = dt.astimezone(ZoneInfo('America/New_York'))
# Same moment, different local time
```
**Parsing and formatting**
```python
# Parse from string
dt = datetime.strptime('2024-03-15 10:30', '%Y-%m-%d %H:%M')
# Format to string
dt.strftime('%d %B %Y') # '15 March 2024'
dt.isoformat() # '2024-03-15T10:30:00'
# Parse ISO format directly (Python 3.7+):
datetime.fromisoformat('2024-03-15T10:30:00') # no Z support until 3.11
```
Common `strftime` / `strptime` codes:
```
%Y 4-digit year %m month (01-12) %d day (01-31)
%H hour (00-23) %M minute (00-59) %S second (00-59)
%A weekday name %B month name %f microseconds
```
timedelta arithmetic, calendar module, and isoweekday
Parse the string '2024-03-15' into a `datetime.date` object and return it.
from datetime import date
def parse_date(s):
# your code here
pass
print(parse_date('2024-03-15')) # 2024-03-15
print(type(parse_date('2024-03-15'))) # <class 'datetime.date'>
Solution
from datetime import date
def parse_date(s):
return date.fromisoformat(s)
Given a `datetime` object, format it as 'March 15, 2024' (e.g., full month name, day, year).
from datetime import datetime
def format_date(dt):
# your code here
pass
from datetime import datetime
print(format_date(datetime(2024, 3, 15))) # March 15, 2024
print(format_date(datetime(2024, 11, 7))) # November 7, 2024
Solution
from datetime import datetime
def format_date(dt):
return dt.strftime('%B %-d, %Y')
Serialize a dictionary to a JSON string. The dict may contain date objects — convert them to ISO strings (YYYY-MM-DD) before serializing.
import json
from datetime import date
def serialize(data):
# your code here
pass
d = {'name': 'Alice', 'birthday': date(1990, 5, 21), 'score': 42}
print(serialize(d)) # '{"name": "Alice", "birthday": "1990-05-21", "score": 42}'
Solution
import json
from datetime import date
def serialize(data):
def default(obj):
if isinstance(obj, date):
return obj.isoformat()
raise TypeError(f'Object of type {type(obj)} is not JSON serializable')
return json.dumps(data, default=default)
Parse a JSON string into a Python object. If a key ends with '_date', convert its value from a string to a `datetime.date`.
import json
from datetime import date
def deserialize(s):
# your code here
pass
s = '{"name": "Alice", "start_date": "2024-01-15", "score": 42}'
result = deserialize(s)
print(result['start_date']) # 2024-01-15
print(type(result['start_date'])) # <class 'datetime.date'>
print(result['score']) # 42
Solution
import json
from datetime import date
def deserialize(s):
def object_hook(d):
for k, v in d.items():
if k.endswith('_date') and isinstance(v, str):
d[k] = date.fromisoformat(v)
return d
return json.loads(s, object_hook=object_hook)
Given a date, return the date of the first day of that month.
from datetime import date
def first_of_month(d):
# your code here
pass
from datetime import date
print(first_of_month(date(2024, 3, 15))) # 2024-03-01
print(first_of_month(date(2024, 12, 31))) # 2024-12-01
Solution
from datetime import date
def first_of_month(d):
return d.replace(day=1)
Given a list of JSON records (each with an 'amount' field), compute the total. Return 0 if the list is empty.
import json
def total_amount(records_json):
# your code here
pass
records = '[{"item": "book", "amount": 12.5}, {"item": "pen", "amount": 3.0}, {"item": "bag", "amount": 25.0}]'
print(total_amount(records)) # 40.5
print(total_amount('[]')) # 0
Solution
import json
def total_amount(records_json):
records = json.loads(records_json)
return sum(r['amount'] for r in records)
No split tab
Cookie preferences
We use necessary cookies to run the site. With your permission, we can also save your site preferences and use analytics and advertising cookies to understand usage and support the project.
Open tools in tabs.Exercises, IDE tools, and trainers stay available as site tabs.
Switch without losing context.Move between explanations, code, and utilities while keeping your place.
Use the sidebar as your map.The left panels hold navigation, settings, files, libraries, and tool controls.
PythonJavaScriptSQLite
One IDE, three practical modes
Python in the browser.Run small scripts, try libraries, and practice API requests without installing anything.
JavaScript for quick experiments.Test browser-friendly code and compare ideas next to your learning materials.
SQLite for data practice.Open the database explorer to inspect tables, write queries, and learn SQL workflows locally.
TopicIDE
Work side by side with split tabs
Keep instructions visible.Open an exercise or reference page beside the IDE instead of jumping back and forth.
Compare tools while you learn.Place regex checks, explanations, and code experiments next to each other when the task needs it.
Close the split when you are done.The workspace returns to a single focused tab, and your open site tabs remain available.
Code Typing Trainer
Or plain text
This trainer is designed for a physical keyboard.Open this section on a laptop or desktop with a wide screen. Touch typing practice will not work correctly on a phone.
Speed: 0 chars/min
0 words/min
Best speed (60s): 0 chars/min
0 words/min
Errors: 0
Total time: 0.0 s
To practice touch typing, avoid looking at your physical keyboard.