{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Identifiers and Keywords" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ " Python is a case sensitive language.\n", " Python identifier is a name used to identify a varibale, function,class, module ot other object.\n", "\n", "Rules for Creating identifier:\n", "1. starts with alphabet or an userscore\n", "2. Followed by zero or more letter, _ and digits\n", "3. keywords cannot be used as identifer\n", "\n", "All keywords are lowercase.\n", "\n", "Python2 has 33 keywords\n", "Python3 has 35 keywords.\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']\n" ] } ], "source": [ "# you can obtain a list of python keywords through the statements:\n", "\n", "import keyword\n", "print(keyword.kwlist)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Data Types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Basic types - int, float, complex, bool, string, bytes\n", "2. Container types - list, tuple, set, dictionary\n", "3. User defined class" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20\n" ] } ], "source": [ "# Integer Data type\n", "a = 20\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "30\n" ] } ], "source": [ "a = 30\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.566\n", "35600.0\n" ] } ], "source": [ "# Float data type\n", "a = 3.566 # Float number using decimal point\n", "print(a)\n", "\n", "a = 3.56e4 # float number using mantissa and exponent\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9387482374982734892497029837453475982309845709832745894375984357\n" ] } ], "source": [ "# Integer and Float data types ranges?\n", "a = 9387482374982734892497029837453475982309845709832745894375984357\n", "print(a)\n", "\n", "# there is no limit for int range. Do not worry about underflow or overflow" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.7e+300\n", "inf\n" ] } ], "source": [ "a = 1.7e300\n", "print(a) \n", "\n", "# a number greater than 1.8e308 is referred as inf (infinity)\n", "\n", "a = 1.9e308\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1+2j)\n", "(3+5j)\n", "(4+7j)\n", "4.0\n", "7.0\n", "(4-7j)\n" ] } ], "source": [ "# Complex Data Type\n", "a = 1 + 2j\n", "b = 3 + 5j\n", "c = a + b\n", "print(a)\n", "print(b)\n", "print(c)\n", "print(c.real) # prints the real part of complex number\n", "print(c.imag) # prints the imaginary part of complex number\n", "print(c.conjugate()) # prints the conjugate of complex number" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n" ] } ], "source": [ "# bool data type\n", "\n", "a = True\n", "\n", "b = False\n", "\n", "print(a)\n", "print(b)" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "b'\\x00\\x00\\x00\\x00'\n" ] } ], "source": [ "# bytes data type\n", "x = 4\n", "\n", "a = bytes(x) # create a array of x bytes\n", "print(type(a))\n", "\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'\\x01\\x05\\x07\\x08'\n" ] } ], "source": [ "x = [1,5,7,8]\n", "\n", "a = bytes(x) # create a array of bytes using the list elements\n", "\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'Python is interesting.'\n" ] } ], "source": [ "string = \"Python is interesting.\"\n", "\n", "# string with encoding 'utf-8'\n", "arr = bytes(string, 'utf-8')\n", "print(arr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Varibale Type and Assignment" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "No need to define type of variable. Type of variable is taken from the context in which the variable is being used." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "a = 25 # type of a is taken as int\n", "a = 3.14 # type of a is taken as float\n", "a = 'Hi' # type of a is taken as str" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "# Type of a variable can be checked using the built-in function type()\n", "a = 'PYTHON'\n", "print(type(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Variable assignment" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "#Simple Variable assignment\n", "a = 10\n", "pi = 3.14\n", "name = \"Ganesh\"\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "#Multiple Variable assignment\n", "\n", "a=10;pi=3.14;name='Ganesh'\n", "\n", "# OR\n", "a,pi,name = 10,3.14,'Ganesh'\n", "\n", "a=b=c=d=5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Arithmetic Operators: + - * / % // **\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "2\n", "6\n", "2.0\n" ] } ], "source": [ "a = 2 + 3\n", "b = 3 - 1\n", "c = 2 * 3\n", "d = 4 / 2\n", "\n", "print(a)\n", "print(b)\n", "print(c)\n", "print(d)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "4\n", "8\n" ] } ], "source": [ "e = 5 % 2\n", "f = 9 // 2\n", "g = 2 ** 3\n", "\n", "print(e)\n", "print(f)\n", "print(g)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Operator Precedence\n", "\n", "Operators in decreasing order of their priority\n", "\n", " ### ( ) Parenthesis\n", " ### ** Exponentiation\n", " ### * / // % Multiplication, Division\n", " ### + - Addition, subtraction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Type Conversions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "int(float/numeric string)\n", "\n", "int(numeric string, base)\n", "\n", "float(int/numeric string)\n", "\n", "complex(int/float)\n", "\n", "complex(int/float,int/float)\n", "\n", "bool(int/float)\n", "\n", "str(int/float/bool)\n", "\n", "chr(int) # get the character associated with given ascii value\n", "\n", "ord(char) # get the ascii value for a given character" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# BUILT-IN functions" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "# Built-in mathematical functions:\n", "x = 3\n", "y = 5\n", "z = 2\n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(x) # absolute value of x" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "243" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pow(x,y) # value of x raised to y" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "min(x,y,z) # smallest argument" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max(x,y,z) # largest argument" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(0, 3)" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "divmod(x,y) # returns a pair(x//y, x%y)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b11'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(x) # Binary equivalent" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0o3'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "oct(x) # Octal equivalent" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0x3'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(x) # Hexadecimal equivalent" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.57" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 4.568785\n", "round(x,2) # x rounded to n digits after decimal point" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Library Functions" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.141592653589793\n" ] } ], "source": [ "# Mathematical functions in math module\n", "import math\n", "print(math.pi) # Get the Pi constant" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.718281828459045\n" ] } ], "source": [ "print(math.e) # Get the e constant" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "120\n" ] } ], "source": [ "x = 5\n", "print(math.factorial(x)) # factorial of x" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.45\n" ] } ], "source": [ "x = -3.45\n", "print(math.fabs(x)) # absolute value of float x" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.8066624897703196\n" ] } ], "source": [ "x = 45\n", "print(math.log(x)) # natural log of x(log x to the base e)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.6532125137753437\n" ] } ], "source": [ "print(math.log10(x)) # base-10 logarithm of x" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.4934271057485095e+19\n" ] } ], "source": [ "print(math.exp(x)) # e raised to x" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "46\n" ] } ], "source": [ "x =46.78\n", "print(math.trunc(x)) # truncates to integer" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "x = 3.56\n", "print(math.ceil(x)) # smallest integer >=x" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "x = 3.56\n", "print(math.floor(x)) # largest integer <=x" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0.56, 3.0)\n" ] } ], "source": [ "x = 3.56\n", "print(math.modf(x)) # returns the tuple of fractional and integer parts of x as a seperate" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "57.29577951308232\n" ] } ], "source": [ "# Trignometric functions in math module\n", "import math\n", "x = 1\n", "print(math.degrees(x)) # radians to degree" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.7853981633974483\n" ] } ], "source": [ "x = 45\n", "print(math.radians(x)) # degrees to radians" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.8414709848078965\n" ] } ], "source": [ "x =1\n", "print(math.sin(x)) # sine of x radians" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.5403023058681398\n" ] } ], "source": [ "print(math.cos(x)) # cosine of x radians" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.557407724654902\n" ] } ], "source": [ "print(math.tan(x)) # tan of x radians" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.1752011936438014\n" ] } ], "source": [ "print(math.sinh(x)) # hyperbolic sine of x" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.5430806348152437\n" ] } ], "source": [ "print(math.cosh(x)) # hyperbolic cosine of x" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.7615941559557649\n" ] } ], "source": [ "print(math.tanh(x)) # hperbolic tan of x" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.5707963267948966\n" ] } ], "source": [ "print(math.asin(x)) # sine inverse of x in radians" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.0\n" ] } ], "source": [ "print(math.acos(x)) # cosine inverse of x in radians" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.7853981633974483\n" ] } ], "source": [ "print(math.atan(x)) # tan inverse of x in radians" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.6055512754639896\n" ] } ], "source": [ "x = 3\n", "y = 2\n", "print(math.hypot(x,y)) # sqrt(x*x + y*y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Random Number generation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Random number can be generate using the random module\n" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [], "source": [ "random.seed() # sets the current time as seed for random number generation" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [], "source": [ "random.seed(x) # set x as seed for random number generation logic" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.23796462709189137\n" ] } ], "source": [ "print(random.random()) # random number between 0 and 1" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "print(random.randint(1,10)) # random integer number between the given range" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Comments and indentation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Comments begins with #" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multi-line comments should be written in a pair of ''' or \"\"\"" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [], "source": [ "# This is a single line comment" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " this is a multiline comment or string\n", " and second line\n", " and third line\n", " and fourth line\n" ] } ], "source": [ "str1 = \"\"\" this is a multiline comment or string\n", " and second line\n", " and third line\n", " and fourth line\"\"\" \n", "print(str1)\n", "# this can be used for generating docstring " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Identation Matters" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [], "source": [ "a = 20 # Works fine" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "ename": "IndentationError", "evalue": "unexpected indent (, line 2)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m b = 20# doesn't work\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unexpected indent\n" ] } ], "source": [ "a = 20\n", " b = 20# doesn't work\n", "\n", "# Use either 4 spaces or a tab for identation. \n", "# identation is mostly used to define the statement block in python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multilining" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "31.0\n" ] } ], "source": [ "# If the statements are long, they can be written as multilines with each line except the last ending with a \\\n", "\n", "total = a + \\\n", " b + \\\n", " c + \\\n", " d + \\\n", " e\n", "print(total)" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Mon', 'Tue', 'WedThu', 'Fri', 'Sat', 'Sun']\n" ] } ], "source": [ "# Multiline statements within [], {} or () doesn't need \\\n", "\n", "days = ['Mon','Tue','Wed'\n", " 'Thu','Fri','Sat',\n", " 'Sun']\n", "print(days)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }