{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Cơ bản về Lập trình và Ngôn ngữ lập trình Python\n", "\n", "## Giới thiệu" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Yankees rule!\n", "But not in Boston!\n", "Yankees rule, but not in Boston!\n" ] } ], "source": [ "print('Yankees rule!')\n", "print('But not in Boston!')\n", "print('Yankees rule,', 'but not in Boston!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Kiểu dữ liệu cơ bản (Scalar type)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.27" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3.27" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(5)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(3.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Chuyển đổi kiểu dữ liệu " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(3)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(3.9)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Xuất dữ liệu ra màn hình (Printing to Console)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3+2" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "print(3+2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Các toán tử cho int và float" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5+3" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8.0" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5.0+3" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5-3" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5*3" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.5" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5/2" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.5" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5/2.0" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5//2" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5//2.0" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5%2" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Toán tử mở và đóng ngoặc" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1+5*3" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1+5)*3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Biến" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.14159\n" ] } ], "source": [ "pi = 3.14159\n", "print(pi)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.142857142857143\n" ] } ], "source": [ "pi_approx = 22/7\n", "print(pi_approx)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "78.53975\n" ] } ], "source": [ "pi = 3.14159\n", "radius = 5\n", "# công thức diện tích của hình tròn <- this is a comment\n", "area = pi * (radius ** 2)\n", "print(area)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15.205295600000001\n", "15.205295600000001\n", "32.169881600000004\n" ] } ], "source": [ "pi = 3.14159\n", "radius = 2.2\n", "# công thức diện tích của hình tròn <- this is a comment\n", "area = pi * (radius ** 2)\n", "print(area)\n", "\n", "# thay đổi giá trị của bán kính <- another comment\n", "# sử dụng các comment để giúp người khác hiểu bạn đang làm gì\n", "radius = radius + 1 # tăng giá trị của biến lên 1 đơn vị\n", "print(area) # diện tích chưa thay đổi\n", "area = pi*(radius**2)\n", "print(area)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "#############################\n", "#### AUTOCOMPLETE #######\n", "#############################\n", "# Spyder can autocomplete names for you\n", "# start typing a variable name defined in your program and hit tab \n", "# before you finish typing -- try it below\n", "\n", "# define a variable\n", "a_very_long_variable_name_dont_name_them_this_long_pls = 1\n", "\n", "# below, start typing a_ve then hit tab... cool, right!\n", "# use autocomplete to change the value of that variable to 1\n", "\n", "# use autocomplete to write a line that prints the value of that long variable\n", "# notice that Spyder also automatically adds the closed parentheses for you!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Multiple assigment\n", "x, y = 2, 3\n", "x, y = y, x # swap variables\n", "print('x =', x)\n", "print('y =', y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lệnh rẽ nhánh\n", "\n", "### Toán tử so sánh cho kiểu int và float" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 > 3" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 >= 5" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 < 5" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 <= 3" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 == 3" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 == 3.0" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 != 3.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Toán tử logic cho kiểu bool" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = True\n", "not a" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = True\n", "b = False\n", "a and b" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = True\n", "b = False\n", "a or b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lệnh if" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter an integer:3\n", "\n", "Odd\n", "Done with conditional\n" ] } ], "source": [ "x = int(input('Enter an integer:'))\n", "if x % 2 == 0:\n", " print('')\n", " print('Even')\n", "else:\n", " print('')\n", " print('Odd')\n", "print('Done with conditional')" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter an integer:6\n", "Divisible by 2 and 3\n" ] } ], "source": [ "x = int(input('Enter an integer:'))\n", "if x % 2 == 0:\n", " if x % 3 == 0:\n", " print('Divisible by 2 and 3')\n", " else:\n", " print('Divisible by 2 and not by 3')\n", "elif x % 3 == 0:\n", " print('Divisible by 3 and not by 2')\n", "else:\n", " print('Indivisible by 2 and 3')" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x is least\n" ] } ], "source": [ "x = 2\n", "y = 3\n", "z = 4\n", "if x < y and x < z:\n", " print('x is least')\n", "elif y < z:\n", " print('y is least')\n", "else:\n", " print('z is least')" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a number for x:3\n", "Enter a number for y:3\n", "x and y are equal\n", "therefore, x / y is 1.0\n", "thanks!\n" ] } ], "source": [ "x = float(input('Enter a number for x:'))\n", "y = float(input('Enter a number for y:'))\n", "if x == y:\n", " print('x and y are equal')\n", " if y != 0:\n", " print('therefore, x / y is', x/y)\n", "elif x < y:\n", " print('x is smaller')\n", "else:\n", " print('y is smaller')\n", "print('thanks!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Bài tập\n", "\n", "Write a program that examines three variables—x, y, and z—and prints the largest odd number among them. If none of them are odd, it should print a message to that effect." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- - -\n", "[Mục lục](http://nbviewer.jupyter.org/github/manleviet/introCSusingPython/blob/master/index.ipynb) | \n", "[Tiếp: Chuỗi, rẽ nhành và lặp](http://nbviewer.jupyter.org/github/manleviet/introCSusingPython/blob/master/2%20-%20Chu%E1%BB%97i%2C%20r%E1%BA%BD%20nh%C3%A1nh%20v%C3%A0%20l%E1%BA%B7p.ipynb)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }