|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015 |
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Python Basic"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [],
- "source": [
- "a = 10\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 1. import & Zen of Python\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['1_Basics.ipynb',\n",
- " '2_Print_Statement.ipynb',\n",
- " '3_Data_Structure_1.ipynb',\n",
- " '4_Data_Structure_2.ipynb',\n",
- " '5_Control_Flow.ipynb',\n",
- " '6_Function.ipynb',\n",
- " '7_Class.ipynb',\n",
- " 'Python.pdf',\n",
- " 'README.md',\n",
- " '.ipynb_checkpoints']"
- ]
- },
- "execution_count": 1,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# 导入库\n",
- "import os\n",
- "os.listdir('.')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "The Zen of Python, by Tim Peters\n",
- "\n",
- "Beautiful is better than ugly.\n",
- "Explicit is better than implicit.\n",
- "Simple is better than complex.\n",
- "Complex is better than complicated.\n",
- "Flat is better than nested.\n",
- "Sparse is better than dense.\n",
- "Readability counts.\n",
- "Special cases aren't special enough to break the rules.\n",
- "Although practicality beats purity.\n",
- "Errors should never pass silently.\n",
- "Unless explicitly silenced.\n",
- "In the face of ambiguity, refuse the temptation to guess.\n",
- "There should be one-- and preferably only one --obvious way to do it.\n",
- "Although that way may not be obvious at first unless you're Dutch.\n",
- "Now is better than never.\n",
- "Although never is often better than *right* now.\n",
- "If the implementation is hard to explain, it's a bad idea.\n",
- "If the implementation is easy to explain, it may be a good idea.\n",
- "Namespaces are one honking great idea -- let's do more of those!\n"
- ]
- }
- ],
- "source": [
- "import this"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 2. Variables"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "A name that is used to denote something or a value is called a variable. In python, variables can be declared and values can be assigned to it as follows,"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [],
- "source": [
- "x = 2\n",
- "y = 5\n",
- "xy = 'Hey'"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "7 Hey\n"
- ]
- }
- ],
- "source": [
- "print(x+y, xy)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Multiple variables can be assigned with the same value."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [],
- "source": [
- "x = y = 1"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1 1\n"
- ]
- }
- ],
- "source": [
- "print(x,y)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 3. Operators"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 3.1 Arithmetic Operators"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "| Symbol | Task Performed |\n",
- "|----|---|\n",
- "| + | Addition |\n",
- "| - | Subtraction |\n",
- "| / | division |\n",
- "| % | mod |\n",
- "| * | multiplication |\n",
- "| // | floor division |\n",
- "| ** | to the power of |"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "3"
- ]
- },
- "execution_count": 10,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "1+2"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "1"
- ]
- },
- "execution_count": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "2-1"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "2"
- ]
- },
- "execution_count": 12,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "1*2"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "0.5"
- ]
- },
- "execution_count": 5,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "1/2"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "0? This is because both the numerator and denominator are integers but the result is a float value hence an integer value is returned. By changing either the numerator or the denominator to float, correct answer can be obtained."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "0.5"
- ]
- },
- "execution_count": 14,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "1.0/2"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "0.5"
- ]
- },
- "execution_count": 10,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "1/2.0"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "5"
- ]
- },
- "execution_count": 15,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "15%10"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Floor division is nothing but converting the result so obtained to the nearest integer."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "1.0"
- ]
- },
- "execution_count": 16,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "2.8//2.0"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 3.2 Relational Operators"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "| Symbol | Task Performed |\n",
- "|----|---|\n",
- "| == | True, if it is equal |\n",
- "| != | True, if not equal to |\n",
- "| < | less than |\n",
- "| > | greater than |\n",
- "| <= | less than or equal to |\n",
- "| >= | greater than or equal to |"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [],
- "source": [
- "z = 1"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "True"
- ]
- },
- "execution_count": 7,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "z == 1"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "False"
- ]
- },
- "execution_count": 8,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "z > 1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 3.3 Bitwise Operators"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "| Symbol | Task Performed |\n",
- "|----|---|\n",
- "| & | Logical And |\n",
- "| l | Logical OR |\n",
- "| ^ | XOR |\n",
- "| ~ | Negate |\n",
- "| >> | Right shift |\n",
- "| << | Left shift |"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {},
- "outputs": [],
- "source": [
- "a = 2 #10\n",
- "b = 3 #11"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "2\n",
- "0b10\n"
- ]
- }
- ],
- "source": [
- "print(a & b)\n",
- "print(bin(a&b))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "2"
- ]
- },
- "execution_count": 23,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "5 >> 1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "0000 0101 -> 5 \n",
- "\n",
- "Shifting the digits by 1 to the right and zero padding\n",
- "\n",
- "0000 0010 -> 2"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 24,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "10"
- ]
- },
- "execution_count": 24,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "5 << 1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "0000 0101 -> 5 \n",
- "\n",
- "Shifting the digits by 1 to the left and zero padding\n",
- "\n",
- "0000 1010 -> 10"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 4. Built-in Functions"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Python comes loaded with pre-built functions"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 4.1 Conversion from one system to another"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Conversion from hexadecimal to decimal is done by adding prefix **0x** to the hexadecimal value or vice versa by using built in **hex( )**, Octal to decimal by adding prefix **0** to the octal value or vice versa by using built in function **oct( )**."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 25,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'0xaa'"
- ]
- },
- "execution_count": 25,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "hex(170)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 26,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "170"
- ]
- },
- "execution_count": 26,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "0xAA"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'0o10'"
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "oct(8)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**int( )** accepts two values when used for conversion, one is the value in a different number system and the other is its base. Note that input number in the different number system should be of string type."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "8\n",
- "170\n",
- "10\n"
- ]
- }
- ],
- "source": [
- "print(int('010',8))\n",
- "print(int('0xaa',16))\n",
- "print(int('1010',2))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**int( )** can also be used to get only the integer value of a float number or can be used to convert a number which is of type string to integer format. Similarly, the function **str( )** can be used to convert the integer back to string format"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "7\n",
- "7\n"
- ]
- }
- ],
- "source": [
- "print(int(7.7))\n",
- "print(int('7'))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Also note that function **bin( )** is used for binary and **float( )** for decimal/float values. **chr( )** is used for converting ASCII to its alphabet equivalent, **ord( )** is used for the other way round."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'b'"
- ]
- },
- "execution_count": 10,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "chr(98)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "98"
- ]
- },
- "execution_count": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "ord('b')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 4.2 Simplifying Arithmetic Operations"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**round( )** function rounds the input value to a specified number of places or to the nearest integer. "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {
- "scrolled": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "6\n",
- "4.56\n"
- ]
- }
- ],
- "source": [
- "print(round(5.6231))\n",
- "print(round(4.55892, 2))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**complex( )** is used to define a complex number and **abs( )** outputs the absolute value of the same."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 35,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "5.385164807134504\n"
- ]
- }
- ],
- "source": [
- "c =complex('5+2j')\n",
- "print(abs(c))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**divmod(x,y)** outputs the quotient and the remainder in a tuple(you will be learning about it in the further chapters) in the format (quotient, remainder). "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 36,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "(4, 1)"
- ]
- },
- "execution_count": 36,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "divmod(9,2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**isinstance( )** returns True, if the first argument is an instance of that class. Multiple classes can also be checked at once."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "True\n",
- "False\n",
- "True\n"
- ]
- }
- ],
- "source": [
- "print(isinstance(1, int))\n",
- "print(isinstance(1.0,int))\n",
- "print(isinstance(1.0,(int,float)))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**cmp(x,y)**\n",
- "\n",
- "|x ? y|Output|\n",
- "|---|---|\n",
- "| x < y | -1 |\n",
- "| x == y | 0 |\n",
- "| x > y | 1 |"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 38,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "True\n"
- ]
- }
- ],
- "source": [
- "print(1<2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**pow(x,y,z)** can be used to find the power $x^y$ also the mod of the resulting value with the third specified number can be found i.e. : ($x^y$ % z)."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 33,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "27\n",
- "2\n"
- ]
- }
- ],
- "source": [
- "print(pow(3,3))\n",
- "print(pow(3,3,5))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**range( )** function outputs the integers of the specified range. It can also be used to generate a series by specifying the difference between the two numbers within a particular range. The elements are returned in a list (will be discussing in detail later.)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[0, 1, 2]\n",
- "[2, 3, 4, 5, 6, 7, 8]\n",
- "[2, 10, 18, 26]\n"
- ]
- }
- ],
- "source": [
- "print(list(range(3)))\n",
- "print(list(range(2,9)))\n",
- "print(list(range(2,27,8)))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 4.3 Accepting User Inputs"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**input( )** accepts input and stores it as a string. "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Type something here and it will be stored in variable abc \tHello world!\n"
- ]
- }
- ],
- "source": [
- "abc = input(\"Type something here and it will be stored in variable abc \\t\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "str"
- ]
- },
- "execution_count": 16,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "type(abc)"
- ]
- }
- ],
- "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.8"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 1
- }
|