Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 210 additions & 0 deletions m1-01-ipython-python-basics-lab.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "1e14eece",
"metadata": {},
"outputs": [],
"source": [
"int1 = 15\n",
"flt1 = 13.5\n",
"str1 = \"hello world\"\n",
"ls1 = [3, 4, 6, 1,]\n",
"dct1 = {\"name1\": 1, \"name2\": 2, \"name3\": 3}\n",
"\n",
"print(type(int1))\n",
"print(type(flt1))\n",
"print(type(str1))\n",
"print(type(ls1))\n",
"print(type(dct1))\n",
"\n",
"str1 = str1.upper() #Made all characters have uppercase\n",
"ls1.append(7) #Added an element '7'\n",
"dct1.pop('name1') #Removed the element 'name': 1\n",
"\n",
"str1.capitalize() # Short note as help from the software: \"Return a capitalized version of the string. More specifically make the first character have upper case and the rest lower case\"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "45541603",
"metadata": {},
"outputs": [],
"source": [
"values = [1, 2, 3, 4, 5]\n",
"alias = values\n",
"alias.append(8)\n",
"print(values, alias)\n",
"\n",
"# The output of both lists: [1, 2, 3, 4, 5, 8] [1, 2, 3, 4, 5, 8]\n",
"\n",
"\n",
"alias = values.copy()\n",
"alias.append(9)\n",
"print(values, alias)\n",
"\n",
"#The output of values and alias respectively: [1, 2, 3, 4, 5, 8] [1, 2, 3, 4, 5, 8, 9]\n",
"\n",
"\n",
"record = {'p1': 1, 'p2': 2, 'p3': 3}\n",
"record_copy = record\n",
"record_copy['p5'] = 5\n",
"print(record, record_copy)\n",
"\n",
"#The output of both dictionaries: {'p1': 1, 'p2': 2, 'p3': 3, 'p5': 5} {'p1': 1, 'p2': 2, 'p3': 3, 'p5': 5}\n",
"\n",
"\n",
"record_copy = record.copy()\n",
"record_copy['p6'] = 6\n",
"print(record, record_copy)\n",
"\n",
"#The output of record and record_copy respectively: {'p1': 1, 'p2': 2, 'p3': 3, 'p5': 5} {'p1': 1, 'p2': 2, 'p3': 3, 'p5': 5, 'p6': 6}\n",
"\n",
"\n",
"\n",
"#During the first step when we use a simple \"=\" which instead of copying a collection's elements, only acts as a reference to it.\n",
"#That's why when we make changes to the second collection, the first one gets affected.\n",
"#However when we use .copy() method, it creates a shallow copy of the original, which then gets assigned to the second collection,\n",
"#thus making it independent from the first. That's why when changes are made once more, the original collection remains unaffected\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cb299f65",
"metadata": {},
"outputs": [],
"source": [
"p1 = {'user_id': 230104001, 'event_type': 'event1', 'duration_seconds': 102}\n",
"p2 = {'user_id': 230104002, 'event_type': 'event1', 'duration_seconds': 235}\n",
"p3 = {'user_id': 230104003, 'event_type': 'event1', 'duration_seconds': -320}\n",
"p4 = {'user_id': 230104004, 'event_type': 'event2', 'duration_seconds': 427}\n",
"p5 = {'user_id': 230104005, 'event_type': 'event5', 'duration_seconds': -2698}\n",
"p6 = {'user_id': 230104006, 'event_type': 'event5', 'duration_seconds': 4653}\n",
"p7 = {'user_id': 230104007, 'event_type': 'event5', 'duration_seconds': 3800}\n",
"p8 = {'user_id': 230104008, 'event_type': 'event2', 'duration_seconds': 546}\n",
"p9 = {'user_id': 230104009, 'event_type': 'event9', 'duration_seconds': 1032}\n",
"p10 = {'user_id': 230104010, 'event_type': 'event10', 'duration_seconds': 789}\n",
"p11 = {'user_id': 230104011, 'event_type': 'event2', 'duration_seconds': \"N/A\"}\n",
"p12 = {'user_id': 230104012, 'event_type': 'event2', 'duration_seconds': 600}\n",
"\n",
"lsP = [p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12]\n",
"cleaned_events = []\n",
"\n",
"for i in lsP:\n",
" if isinstance(i['duration_seconds'], int):\n",
" if i['duration_seconds'] > 0:\n",
" new_record = i.copy()\n",
" new_record['duration_minutes'] = round(i['duration_seconds'] / 60, 1)\n",
" cleaned_events.append(new_record)\n",
"\n",
"\n",
"print(len(cleaned_events) == 9)\n",
"\n",
"required_keys = {'user_id', 'event_type', 'duration_seconds', 'duration_minutes'}\n",
"\n",
"for record in cleaned_events:\n",
" print(required_keys.issubset(record.keys()))\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a5cf3c83",
"metadata": {},
"outputs": [],
"source": [
"event_counts = {}\n",
"sum_time = {}\n",
"\n",
"for i in cleaned_events:\n",
"\n",
" etype = i['event_type']\n",
" duration = i['duration_minutes']\n",
"\n",
" if etype in event_counts:\n",
"\n",
" event_counts[etype] += 1\n",
"\n",
" else:\n",
" event_counts[etype] = 1\n",
" \n",
"\n",
" if etype in sum_time:\n",
" sum_time[etype] += duration\n",
" else:\n",
" sum_time[etype] = duration\n",
"\n",
"\n",
"\n",
"avg_time = {}\n",
"for etype in sum_time:\n",
" avg_time[etype] = sum_time[etype] / event_counts[etype]\n",
" avg_time[etype] = round(avg_time[etype], 1)\n",
"\n",
"\n",
"unique_users = len({record['user_id'] for record in cleaned_events})\n",
"validation = sum(event_counts.values()) == len(cleaned_events)\n",
"\n",
"print(\"Event counts:\", event_counts)\n",
"print(\"Average duration (minutes):\", avg_time)\n",
"print(\"Number of unique users:\", unique_users)\n",
"print(\"Validation (counts match cleaned_events?):\", validation)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80cf80ef",
"metadata": {},
"outputs": [],
"source": [
"csv_string = \"metric,key,value\\n\"\n",
"\n",
"for i in event_counts:\n",
" csv_string += f\"count,{i},{event_counts[i]}\\n\"\n",
"\n",
"for i in avg_time:\n",
" csv_string += f\"average_time,{i},{avg_time[i]}\\n\"\n",
"\n",
"csv_string += f\"unique_users,all,{unique_users}\"\n",
"\n",
"print(csv_string)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e1304306",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
20 changes: 20 additions & 0 deletions m1-01-task-3-functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def strFloat(str1):
try:

return float(str1)

except (ValueError, TypeError):

return None

def cleanStr(str1):

nstr = str1.strip()
nstr = nstr.lower()
return nstr


inputList = [" sdFaEDei ", "12.4", " 123. ", ".hEllO.12.3 ! ", "3.1_data_structures_lists", "168.5893 "]

for i in inputList:
print(strFloat(i),"\t", cleanStr(i))