{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Hello World" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, World!\n" ] } ], "source": [ "# dòng code mà ai cũng sẽ thử\n", "print(\"Hello, World!\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, World!\n" ] } ], "source": [ "# viết comment 1 dòng\n", "print(\"Hello, World!\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, World!\n" ] } ], "source": [ "\"\"\"\n", "viết comment nhiều dòng\n", "\"\"\"\n", "print(\"Hello, World!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Variables\n", "\n", "Variables là objects trong Python dùng để lưu giá trị nào đó như số hoặc chuỗi.\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# gán giá trị cho biến\n", "male = 70\n", "family_status = \"Single\"\n", "amt_income = 135000.0" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('male =', 70)\n" ] } ], "source": [ "# dùng dấu ',' để ghép nhiều mệnh đề\n", "print(\"male =\", male)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "family status = Single\n", "male = 70, family status = Single\n", "male = 00070\n", "amount incode = 135000.00\n", "amount incode = 135000.0 Wow!\n" ] } ], "source": [ "# dùng {fieldname:conversion} để định dạng chuỗi\n", "# link 1: https://docs.python.org/3.5/library/string.html#string.Formatter.format\n", "# link 2: https://www.python-course.eu/python3_formatted_output.php\n", "# link 3: https://www.digitalocean.com/community/tutorials/how-to-use-string-formatters-in-python-3\n", "\n", "# mặc định {}\n", "print(\"family status = {}\".format(family_status))\n", "\n", "# {fieldname} có thứ tự\n", "print(\"male = {1}, family status = {0}\".format(family_status, male))\n", "\n", "# format integer {:conversion}\n", "print(\"male = {:05d}\".format(male))\n", "\n", "# format float {:conversion}\n", "print(\"amount incode = {:08.2f}\".format(amt_income))\n", "\n", "# placeholder arg\n", "print(\"amount incode = {} {info}\".format(amt_income, info=\"Wow!\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Có nhiều loại kiểu dữ liệu trong variables. Trong biến số ta có thể có kiểu integers (int), floats (float), etc. " ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\n" ] } ], "source": [ "# kiểm tra kiểu dữ liệu\n", "print(type(male))\n", "print(type(family_status))\n", "print(type(amt_income))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# List\n", "\n", "Lists là objects trong Python dùng để lưu chuỗi có thứ tự số hoặc text. Các phần tử cùng kiểu hoặc khác kiểu dữ liệu\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "type: , ls_id = [100065, 100005, 100150], length = 3\n" ] } ], "source": [ "# tạo list\n", "ls_id = [100065, 100005, 100150]\n", "print(\"type: {}, ls_id = {}, length = {}\".format(type(ls_id), ls_id, len(ls_id)))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[100065, 100005, 100150, 100152]\n" ] } ], "source": [ "# thêm phần tử vào list\n", "ls_id.append(100152)\n", "print(ls_id)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sk_id_curr:100107, name_contract:Cash loans, code_gender:M, amt_income:180000.0\n" ] } ], "source": [ "ls_account_info = [100107, \"Cash loans\", \"M\", 180000.0]\n", "print(\"sk_id_curr:{}, name_contract:{}, code_gender:{}, amt_income:{}\".format(ls_account_info[0], \n", " ls_account_info[1], \n", " ls_account_info[2], \n", " ls_account_info[3]))\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100107\n", "M\n" ] } ], "source": [ "# truy xuất list\n", "print(ls_account_info[0])\n", "print(ls_account_info[-2])" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['M', 180000.0]\n" ] } ], "source": [ "# slicing list\n", "print(ls_account_info[2:4])" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[100065, 100005, 100150, 100152, 100107, 'Cash loans', 'M', 180000.0]\n" ] } ], "source": [ "# nối 2 list\n", "ls_z = ls_id + ls_account_info\n", "print(ls_z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Tuples\n", "\n", "Immutable list (không thể thay đổi giá trị của phần tử)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['N', 'Y', 'N', 'Y', 'N', 'N']\n", "Reason: 'tuple' object does not support item assignment\n" ] } ], "source": [ "ls_flag_own_car = [\"Y\", \"Y\", \"N\", \"Y\", \"N\", \"N\"]\n", "ls_flag_own_car[0] = \"N\"\n", "print(ls_flag_own_car)\n", "\n", "tpl_flag_own_car = (\"Y\", \"Y\", \"N\", \"Y\", \"N\", \"N\")\n", "try:\n", " tpl_flag_own_car[0] = \"N\"\n", "except Exception as err: \n", " print(\"Reason: {}\".format(err))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('Y', 'Y', 'N', 'Y', 'N', 'N', 'Unknown')\n" ] } ], "source": [ "# thêm phần tử vào tuple\n", "tpl_flag_own_car = tpl_flag_own_car + (\"Unknown\",)\n", "print (tpl_flag_own_car)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Dictionary\n", "\n", "List of key-value pairs" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'name_contract': 'Cash loans', 'amt_income': 180000.0, 'code_gender': 'M', 'sk_id_curr': 100107}\n" ] } ], "source": [ "dict_account_info = {\n", " \"sk_id_curr\": 100107, \n", " \"name_contract\": \"Cash loans\", \n", " \"code_gender\": \"M\", \n", " \"amt_income\": 180000.0\n", "}\n", "print(dict_account_info)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'name_contract': 'Cash loans', 'amt_income': 27000.5, 'code_gender': 'M', 'sk_id_curr': 100107}\n" ] } ], "source": [ "# thay đổi giá trị của key\n", "dict_account_info[\"amt_income\"] = 27000.5\n", "print(dict_account_info)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'name_contract': 'Cash loans', 'cnt_children': 2, 'amt_income': 27000.5, 'code_gender': 'M', 'sk_id_curr': 100107}\n" ] } ], "source": [ "# thêm cặp key-value mới\n", "dict_account_info[\"cnt_children\"] = 2\n", "print(dict_account_info)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "# độ dài của dictionary\n", "print(len(dict_account_info))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Mệnh đề if\n", "\n", "Ta dùng mệnh đề **if** để rẽ nhánh câu lệnh theo các điều kiện chỉ định" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gender: Male\n" ] } ], "source": [ "code_gender = \"M\"\n", "if code_gender == \"M\":\n", " print(\"Gender: Male\")\n", "elif code_gender == \"F\":\n", " print(\"Gender: Female\")\n", "else:\n", " print(\"Unknown\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Loops\n", "\n", "Ta dùng **for** loop để duyệt từng phần tử trong list hoặc tuple" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "N\n", "Y\n", "N\n", "Y\n", "N\n", "N\n", "(0, 'N')\n", "(1, 'Y')\n", "(2, 'N')\n", "(3, 'Y')\n", "(4, 'N')\n", "(5, 'N')\n" ] } ], "source": [ "# for loop in list\n", "for flag in ls_flag_own_car:\n", " print(flag)\n", "\n", "# for loop with enumerate \n", "for i, flag in enumerate(ls_flag_own_car):\n", " print(i, flag)\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name_contract: Cash loans\n", "cnt_children: 2\n", "amt_income: 27000.5\n", "code_gender: M\n", "sk_id_curr: 100107\n" ] } ], "source": [ "# for loop in dictionary\n", "for k, v in dict_account_info.items():\n", " print(\"{}: {}\".format(k, v))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# List comprehension" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4, 2, 6, 8]\n" ] } ], "source": [ "num_cars = [2, 1, 3, 4]\n", "double_num_cars = [num * 2 for num in num_cars]\n", "print(double_num_cars)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Many people struggle to get loans due to insufficient or non-existent credit histories. And, unfortunately, this population is often taken advantage of by untrustworthy lenders. Home Credit strives to broaden financial inclusion for the unbanked population by providing a positive and safe borrowing experience. In order to make sure this underserved population has a positive loan experience, Home Credit makes use of a variety of alternative data--including telco and transactional information--to predict their clients' repayment abilities.\n" ] } ], "source": [ "ls_paragraph = [\n", " ['Many',\n", " 'people',\n", " 'struggle',\n", " 'to',\n", " 'get',\n", " 'loans',\n", " 'due',\n", " 'to',\n", " 'insufficient',\n", " 'or',\n", " 'non-existent',\n", " 'credit',\n", " 'histories.',\n", " 'And,',\n", " 'unfortunately,',\n", " 'this',\n", " 'population',\n", " 'is',\n", " 'often',\n", " 'taken',\n", " 'advantage',\n", " 'of',\n", " 'by',\n", " 'untrustworthy',\n", " 'lenders.'],\n", " ['Home',\n", " 'Credit',\n", " 'strives',\n", " 'to',\n", " 'broaden',\n", " 'financial',\n", " 'inclusion',\n", " 'for',\n", " 'the',\n", " 'unbanked',\n", " 'population',\n", " 'by',\n", " 'providing',\n", " 'a',\n", " 'positive',\n", " 'and',\n", " 'safe',\n", " 'borrowing',\n", " 'experience.',\n", " 'In',\n", " 'order',\n", " 'to',\n", " 'make',\n", " 'sure',\n", " 'this',\n", " 'underserved',\n", " 'population',\n", " 'has',\n", " 'a',\n", " 'positive',\n", " 'loan',\n", " 'experience,',\n", " 'Home',\n", " 'Credit',\n", " 'makes',\n", " 'use',\n", " 'of',\n", " 'a',\n", " 'variety',\n", " 'of',\n", " 'alternative',\n", " 'data--including',\n", " 'telco',\n", " 'and',\n", " 'transactional',\n", " 'information--to',\n", " 'predict',\n", " 'their',\n", " \"clients'\",\n", " 'repayment',\n", " 'abilities.']\n", "]\n", "flatten_paragraph = [word for paragraph in ls_paragraph for word in paragraph]\n", "print(\" \".join(flatten_paragraph))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Functions" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "6\n" ] } ], "source": [ "# viết hàm tính toán\n", "def your_function_name(arg_here=2):\n", " return_value = 2 * arg_here\n", " return return_value\n", "\n", "print(your_function_name())\n", "print(your_function_name(3))" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "6\n" ] } ], "source": [ "# viết hàm anonymous\n", "f01 = lambda x: x * 2\n", "print(f01(2))\n", "print(f01(3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Tìm hiểu thêm\n", "\n", "Những đoạn code trong notebook này đủ để bạn làm một project Data Science cơ bản. Để tìm hiểu thêm bạn có thể tham gia khoá học này https://www.codecademy.com/learn/learn-python" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.15" } }, "nbformat": 4, "nbformat_minor": 2 }