{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# The Zen of Python" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "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": [ "# 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": 2, "metadata": {}, "outputs": [], "source": [ "x = 2\n", "y = 5\n", "xy = 'Hey'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "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": 4, "metadata": {}, "outputs": [], "source": [ "x = y = 1" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 1\n" ] } ], "source": [ "print(x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Operators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Arithmetic Operators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "| Symbol | Task Performed |\n", "|----|---|\n", "| + | Addition |\n", "| - | Subtraction |\n", "| / | division |\n", "| % | mod (modulo)|\n", "| * | multiplication |\n", "| // | floor division |\n", "| ** | to the power of |" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + 2" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 - 1" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 * 2" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "1.6666666666666667" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 / 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`//` if floor division also referred to as integer division. The resultant value is a whole integer, though the result’s type is not necessarily int. The modulo operation `%` finds the remainder after division of one number by another (sometimes called modulus)." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 // 3" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 // 3.0" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "13" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b = 13, 3\n", "b * (a // b) + (a % b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 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": 13, "metadata": {}, "outputs": [], "source": [ "z = 1" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z == 1" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z > 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 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": 16, "metadata": {}, "outputs": [], "source": [ "a = 2 #0b10\n", "b = 3 #0b11" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "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": 18, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 18, "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": 19, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 19, "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": [ "# Built-in Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python comes loaded with pre-built functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 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 **0o** to the octal value or vice versa by using built in function **oct( )**." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "'0xaa'" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(170)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "170" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0xAA" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "'0o10'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "oct(8)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0o10" ] }, { "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": 24, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8\n", "170\n", "10\n" ] } ], "source": [ "print(int('0o10',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": 25, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "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": 26, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "'b'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chr(98)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "98" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ord('b')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 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": 28, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": 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": 29, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(3+4j) 5.0\n" ] } ], "source": [ "c = complex('3+4j')\n", "print(c, 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": 30, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "(4, 1)" ] }, "execution_count": 30, "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": 31, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "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": [ "**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": 32, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "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(start, stop, step)`** 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. Function **`range`** generates an interator, which can be converted to a list using `list(...)`" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "range(0, 3) [0, 1, 2]\n", "range(2, 9) [2, 3, 4, 5, 6, 7, 8]\n", "range(2, 27, 8) [2, 10, 18, 26]\n" ] } ], "source": [ "print(range(3), list(range(3)))\n", "print(range(2,9), list(range(2,9)))\n", "print(range(2,27,8), list(range(2,27,8)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accepting User Inputs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**input( )** accepts input and stores it as a string. Hence, if the user inputs a integer, the code should convert the string to an integer and then proceed." ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Type something here and it will be stored in variable abc: Test\n" ] } ], "source": [ "abc = input(\"Type something here and it will be stored in variable abc: \")" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(abc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that **type( )** returns the format or the type of a variable or a number" ] } ], "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.7.4" } }, "nbformat": 4, "nbformat_minor": 4 }