{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chuỗi, rẽ nhành và lặp\n", "\n", "## Ôn tập về phép toán gán" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x = 4\n", "y = 5\n" ] } ], "source": [ "x = 2\n", "x = x * x\n", "y = x + 1\n", "print('x =', x)\n", "print('y =', y)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x = 1\n", "y = 1\n" ] } ], "source": [ "# Swap variables - NOT DO\n", "x = 1\n", "y = 2\n", "y = x\n", "x = y\n", "print('x =', x)\n", "print('y =', y)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x = 2\n", "y = 1\n" ] } ], "source": [ "# Swap variables - DO\n", "x = 1\n", "y = 2\n", "temp = y\n", "y = x\n", "x = temp\n", "print('x =', x)\n", "print('y =', y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Chuỗi ký tự (String)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello thereeric\n", "hello there, eric\n" ] } ], "source": [ "hi = \"hello there\"\n", "greetings = 'hello'\n", "\n", "foo = \"this isn't right\"\n", "\n", "name = 'eric'\n", "greet = hi + name\n", "greeting = hi + \", \" + name\n", "\n", "print(greet)\n", "print(greeting)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Toán tử trên chuỗi" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abcd'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'ab' + 'cd'" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ericericeric'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 * 'eric'" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len('eric')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len('hi there')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'r'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'eric'[1]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'e'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'eric'[0]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'e'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "name = 'eric'\n", "name[0]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "string index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mname\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: string index out of range" ] } ], "source": [ "name[4]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ri'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'eric'[1:3]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'eri'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'eric'[:3]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ric'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'eric'[1:]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'eric'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'eric'[:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Xuất, nhập dữ liệu" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "my fav num is 1 . x = 1\n", "my fav num is1.x = 1\n", "my fav num is 1. x = 1\n" ] } ], "source": [ "x = 1\n", "print(x)\n", "x_str = str(x)\n", "print(\"my fav num is\", x, \".\", \"x = \", x)\n", "print(\"my fav num is\" + x_str + \".\" + \"x = \" + x_str)\n", "print(\"my fav num is \" + x_str + \". \" + \"x = \" + x_str)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Type something...hello\n", "hellohellohellohellohello\n" ] }, { "data": { "text/plain": [ "str" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text = input(\"Type something...\")\n", "print(5 * text)\n", "type(text)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Type a number...10\n", "50\n" ] } ], "source": [ "num = int(input(\"Type a number...\"))\n", "print(5 * num)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lệnh lặp" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "You are in the Lost Forest. Go left or right?right\n", "You are in the Lost Forest. Go left or right?right\n", "You are in the Lost Forest. Go left or right?left\n", "You got out of the Lost Forest!\n" ] } ], "source": [ "n = input('You are in the Lost Forest. Go left or right?')\n", "while n == 'right':\n", " n = input('You are in the Lost Forest. Go left or right?')\n", "print('You got out of the Lost Forest!')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "####################\n", "## EXAMPLE: while loops \n", "## Try expanding this code to show a sad face if you go right\n", "## twice and flip the table any more times than that.\n", "## Hint: use a counter\n", "####################\n", "#n = input(\"You are in the Lost Forest\\n****************\\n****************\\n :)\\n****************\\n****************\\nGo left or right? \")\n", "#while n == \"right\" or n == \"Right\":\n", "# n = input(\"You are in the Lost Forest\\n****************\\n****** ***\\n (╯°□°)╯︵ ┻━┻\\n****************\\n****************\\nGo left or right? \")\n", "#print(\"\\nYou got out of the Lost Forest!\\n\\o/\")" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "n = 0\n", "while n < 5:\n", " print(n)\n", " n = n + 1" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for n in range(5):\n", " print(n)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "24\n" ] } ], "source": [ "mysum = 0\n", "for i in range(7, 10):\n", " mysum += i\n", "print(mysum)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "21\n" ] } ], "source": [ "mysum = 0\n", "for i in range(5, 11, 2):\n", " mysum += i\n", "print(mysum)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "mysum = 0\n", "for i in range(5, 11, 2):\n", " mysum += i\n", " if mysum == 5:\n", " break\n", "print(mysum)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "45\n" ] } ], "source": [ "total = 0\n", "for c in '123456789':\n", " total = total + int(c)\n", "print(total)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3*3 = 9\n" ] } ], "source": [ "x = 3\n", "ans = 0\n", "itersLeft = x\n", "while (itersLeft != 0):\n", " ans = ans + x\n", " itersLeft = itersLeft - 1\n", "print(str(x) + '*' + str(x) + ' = ' + str(ans))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Bài tập\n", "\n", "Replace the comment in the following code with a while loop." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "How many times should I print the letter X? 4\n", "\n" ] } ], "source": [ "numXs = int(input('How many times should I print the letter X? '))\n", "toPrint = ''\n", "#concatenate X to toPrint numXs times\n", "print(toPrint)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Bài tập\n", "\n", "Write a program that asks the user to input 10 integers, and then prints the largest odd number that was entered. If no odd number was entered, it should print a message to that effect." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Thử, sai (Guess and check)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter an integer: 27\n", "Cube root of 27 is 3\n" ] } ], "source": [ "# Find a cube root of x\n", "x = int(input('Enter an integer: '))\n", "ans = 0\n", "while ans ** 3 < x:\n", " ans = ans + 1\n", "if ans ** 3 != x:\n", " print(str(x) + ' is not a perfect cube')\n", "else:\n", " print('Cube root of ' + str(x) + ' is ' + str(ans))" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter an integer: 29\n", "29 is not a perfect cube\n" ] } ], "source": [ "# Find a cube root of x\n", "x = int(input('Enter an integer: '))\n", "ans = 0\n", "while ans ** 3 < abs(x):\n", " ans = ans + 1\n", "if ans ** 3 != abs(x):\n", " print(str(x) + ' is not a perfect cube')\n", "else:\n", " if x < 0:\n", " ans = -ans\n", " print('Cube root of ' + str(x) + ' is ' + str(ans))" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Cube root of 27 is 3\n" ] } ], "source": [ "cube = 27\n", "for guess in range(cube + 1):\n", " if guess**3 == cube:\n", " print('Cube root of', cube, 'is', guess)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Cube root of 8 is 2\n" ] } ], "source": [ "cube = 8\n", "for guess in range(abs(cube) + 1):\n", " if guess**3 >= abs(cube):\n", " break\n", "if guess**3 != abs(cube):\n", " print(cube, 'is not a perfect cube')\n", "else:\n", " if cube < 0:\n", " guess = -guess\n", " print('Cube root of ' + str(cube) + ' is ' + str(guess))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Bài tập\n", "\n", "Write a program that asks the user to enter an integer and prints two integers, root and pwr, such that 0 < pwr < 6 and root**pwr is equal to the integer entered by the user. If no such pair of integers exists, it should print a message to that effect." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Bài tập\n", "\n", "Let s be a string that contains a sequence of decimal numbers separated by commas, e.g., s = '1.23,2.4,3.123'. Write a program that prints the sum of the numbers in s." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- - -\n", "[Trước: Cơ bản về Lập trình và Ngôn ngữ lập trình Python](http://nbviewer.jupyter.org/github/manleviet/introCSusingPython/blob/master/1%20-%20C%C6%A1%20b%E1%BA%A3n%20v%E1%BB%81%20L%E1%BA%ADp%20tr%C3%ACnh%20v%C3%A0%20Ng%C3%B4n%20ng%E1%BB%AF%20l%E1%BA%ADp%20tr%C3%ACnh%20Python.ipynb) | \n", "[Mục lục](http://nbviewer.jupyter.org/github/manleviet/introCSusingPython/blob/master/index.ipynb) | \n", "[Tiếp: Các phương pháp cơ bản giải quyết bài toán trên máy tính](http://nbviewer.jupyter.org/github/manleviet/introCSusingPython/blob/master/3%20-%20C%C3%A1c%20ph%C6%B0%C6%A1ng%20ph%C3%A1p%20c%C6%A1%20b%E1%BA%A3n%20gi%E1%BA%A3i%20quy%E1%BA%BFt%20b%C3%A0i%20to%C3%A1n%20tr%C3%AAn%20m%C3%A1y%20t%C3%ADnh.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 }