{ "cells": [ { "cell_type": "markdown", "id": "e545f9aa", "metadata": {}, "source": [ "# Combine Operations" ] }, { "cell_type": "code", "execution_count": 1, "id": "8debcab5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}\n" ] } ], "source": [ "dic = {}\n", "for x in range(1, 6):\n", " dic[x] = x ** 2\n", " # dic.update({x: x ** 2})\n", "print(dic)" ] }, { "cell_type": "code", "execution_count": 2, "id": "19abd5c1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}\n" ] } ], "source": [ "squares = {x: x ** 2 for x in range(1, 6)}\n", "print(squares)" ] }, { "cell_type": "code", "execution_count": 3, "id": "e842f3d7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{2: 4, 4: 16, 6: 36, 8: 64, 10: 100}\n" ] } ], "source": [ "dic = {}\n", "for x in range(1, 11):\n", " if x % 2 == 0:\n", " dic[x] = x ** 2\n", " # dic.update({x: x ** 2})\n", "print(dic)" ] }, { "cell_type": "code", "execution_count": 4, "id": "44546b3d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{2: 4, 4: 16, 6: 36, 8: 64, 10: 100}\n" ] } ], "source": [ "squares = {x: x ** 2 for x in range(1, 11) if x % 2 == 0}\n", "print(squares)" ] }, { "cell_type": "code", "execution_count": 5, "id": "56cdc396", "metadata": {}, "outputs": [], "source": [ "users = [\n", " {\n", " \"_id\": \"u12345\",\n", " \"name\": \"Rahman\",\n", " \"email\": \"rahman@example.com\",\n", " \"age\": 28,\n", " \"isActive\": True,\n", " \"address\": {\n", " \"street\": \"123 Lakeview Road\",\n", " \"city\": \"Dhaka\",\n", " \"zip\": \"1207\"\n", " },\n", " \"hobbies\": [\"reading\", \"gaming\", \"traveling\"],\n", " \"orders\": [\n", " {\n", " \"order_id\": \"ord1001\",\n", " \"date\": \"2025-05-01\",\n", " \"items\": [\n", " {\"product\": \"Book\", \"price\": 12.99, \"quantity\": 1},\n", " {\"product\": \"Pen\", \"price\": 1.99, \"quantity\": 3}\n", " ],\n", " \"total\": 18.96\n", " },\n", " {\n", " \"order_id\": \"ord1002\",\n", " \"date\": \"2025-05-04\",\n", " \"items\": [\n", " {\"product\": \"Shoes\", \"price\": 45.00, \"quantity\": 1}\n", " ],\n", " \"total\": 45.00\n", " }\n", " ]\n", " },\n", " {\n", " \"_id\": \"u12346\",\n", " \"name\": \"Fatima\",\n", " \"email\": \"fatima@example.com\",\n", " \"age\": 32,\n", " \"isActive\": False,\n", " \"address\": {\n", " \"street\": \"78 Rose Lane\",\n", " \"city\": \"Chittagong\",\n", " \"zip\": \"4000\"\n", " },\n", " \"hobbies\": [\"painting\", \"yoga\"],\n", " \"orders\": [\n", " {\n", " \"order_id\": \"ord1003\",\n", " \"date\": \"2025-05-02\",\n", " \"items\": [\n", " {\"product\": \"Canvas\", \"price\": 25.50, \"quantity\": 2}\n", " ],\n", " \"total\": 51.00\n", " }\n", " ]\n", " },\n", " {\n", " \"_id\": \"u12347\",\n", " \"name\": \"Karim\",\n", " \"email\": \"karim@example.com\",\n", " \"age\": 40,\n", " \"isActive\": True,\n", " \"address\": {\n", " \"street\": \"10 Green Street\",\n", " \"city\": \"Khulna\",\n", " \"zip\": \"9100\"\n", " },\n", " \"hobbies\": [\"fishing\", \"cycling\", \"chess\"],\n", " \"orders\": []\n", " },\n", " {\n", " \"_id\": \"u12348\",\n", " \"name\": \"Ayesha\",\n", " \"email\": \"ayesha@example.com\",\n", " \"age\": 25,\n", " \"isActive\": True,\n", " \"address\": {\n", " \"street\": \"56 Mango Street\",\n", " \"city\": \"Sylhet\",\n", " \"zip\": \"3100\"\n", " },\n", " \"hobbies\": [\"dancing\", \"baking\"],\n", " \"orders\": [\n", " {\n", " \"order_id\": \"ord1004\",\n", " \"date\": \"2025-05-05\",\n", " \"items\": [\n", " {\"product\": \"Dress\", \"price\": 30.00, \"quantity\": 1},\n", " {\"product\": \"Handbag\", \"price\": 40.00, \"quantity\": 1}\n", " ],\n", " \"total\": 70.00\n", " }\n", " ]\n", " }\n", "]" ] }, { "cell_type": "code", "execution_count": 6, "id": "11151195", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'_id': 'u12345',\n", " 'name': 'Rahman',\n", " 'email': 'rahman@example.com',\n", " 'age': 28,\n", " 'isActive': True,\n", " 'address': {'street': '123 Lakeview Road', 'city': 'Dhaka', 'zip': '1207'},\n", " 'hobbies': ['reading', 'gaming', 'traveling'],\n", " 'orders': [{'order_id': 'ord1001',\n", " 'date': '2025-05-01',\n", " 'items': [{'product': 'Book', 'price': 12.99, 'quantity': 1},\n", " {'product': 'Pen', 'price': 1.99, 'quantity': 3}],\n", " 'total': 18.96},\n", " {'order_id': 'ord1002',\n", " 'date': '2025-05-04',\n", " 'items': [{'product': 'Shoes', 'price': 45.0, 'quantity': 1}],\n", " 'total': 45.0}]},\n", " {'_id': 'u12347',\n", " 'name': 'Karim',\n", " 'email': 'karim@example.com',\n", " 'age': 40,\n", " 'isActive': True,\n", " 'address': {'street': '10 Green Street', 'city': 'Khulna', 'zip': '9100'},\n", " 'hobbies': ['fishing', 'cycling', 'chess'],\n", " 'orders': []},\n", " {'_id': 'u12348',\n", " 'name': 'Ayesha',\n", " 'email': 'ayesha@example.com',\n", " 'age': 25,\n", " 'isActive': True,\n", " 'address': {'street': '56 Mango Street', 'city': 'Sylhet', 'zip': '3100'},\n", " 'hobbies': ['dancing', 'baking'],\n", " 'orders': [{'order_id': 'ord1004',\n", " 'date': '2025-05-05',\n", " 'items': [{'product': 'Dress', 'price': 30.0, 'quantity': 1},\n", " {'product': 'Handbag', 'price': 40.0, 'quantity': 1}],\n", " 'total': 70.0}]}]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "active_users = [user for user in users if user[\"isActive\"]]\n", "active_users" ] }, { "cell_type": "code", "execution_count": 7, "id": "5d4e9086", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'name': 'Rahman', 'email': 'rahman@example.com'},\n", " {'name': 'Fatima', 'email': 'fatima@example.com'},\n", " {'name': 'Ayesha', 'email': 'ayesha@example.com'}]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "users_with_orders = [\n", " {\"name\": user[\"name\"], \"email\": user[\"email\"]}\n", " for user in users if len(user[\"orders\"]) > 0\n", "]\n", "users_with_orders" ] }, { "cell_type": "code", "execution_count": 8, "id": "75657de8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'name': 'Rahman', 'total_spent': 63.96},\n", " {'name': 'Fatima', 'total_spent': 51.0},\n", " {'name': 'Karim', 'total_spent': 0},\n", " {'name': 'Ayesha', 'total_spent': 70.0}]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "user_spending = [\n", " {\n", " \"name\": user[\"name\"],\n", " \"total_spent\": sum(order[\"total\"] for order in user[\"orders\"])\n", " }\n", " for user in users\n", "]\n", "user_spending" ] }, { "cell_type": "code", "execution_count": 9, "id": "04860cbc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Book\n", "Pen\n", "Shoes\n" ] } ], "source": [ "for user in users:\n", " if user['name'] == 'Rahman':\n", " for i in user['orders']:\n", " for j in i['items']:\n", " print(j['product'])" ] }, { "cell_type": "code", "execution_count": 12, "id": "940feb5c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "31.25" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "avg_age = sum(user[\"age\"] for user in users) / len(users)\n", "avg_age" ] } ], "metadata": { "kernelspec": { "display_name": "all", "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.12.9" } }, "nbformat": 4, "nbformat_minor": 5 }