{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "

Python Basics

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Today Objectives\n", "\n", "\n", "- Ouput & Input\n", "- Variables in Python\n", "- Type Conversions in Python\n", "- Operators in Python\n", "- Conditional Statements" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n" ] } ], "source": [ "print(\"Hello World\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Todays date is 29\n" ] } ], "source": [ "print(\"Todays date is\",29)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Todays date is-29\n" ] } ], "source": [ "print(\"Todays date is\",29, sep = '-')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\t2\t3\t4\t5\t6\n" ] } ], "source": [ "print(1,2,3,4,5,6, sep = '\\t')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1abc2abc3abc4abc5abc6\n" ] } ], "source": [ "print(1,2,3,4,5,6, sep = 'abc')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n", "Hello World\n", "Hello World3\n" ] } ], "source": [ "print(\"Hello World\")\n", "print(\"Hello World\")\n", "print(\"Hello World3\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\tHello World\n", "Hello World3\n" ] } ], "source": [ "print(\"Hello World\", end = '\\t')\n", "print(\"Hello World\")\n", "print(\"Hello World3\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Variables\n", "\n", "It is the named memory location which holds some value\n", "\n", "\n", "### Properties of declaring a variable\n", "\n", "- It can contain AZaz09_\n", "- It shouldn't start with a number\n", "- No special characters should included in variable name except `_`\n", "- Keywords are not allowed\n", "- Case Sensitive" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "a = 10\n", "A = 5" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "a = 55\n", "_a = 2256\n", "# 1a = 66\n", "# a-b = 23+66\n", "_ = 625\n", "a9 = 'Python'\n", "a9_ = 'APSSDC'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Data Types in Python\n", "\n", "\n", "- Primary Data Types -> int, float, complex, string, boolean\n", "- Secondary Data Types -> Containers -> List, Tuple, Dictoinary, set" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "a = 55\n", "b = 55.66\n", "c = 5 + 6j\n", "d = True\n", "e = 'a+5/.,;'\n", "f = ['python', 65, 5+6j, 55.665]\n", "g = ('python', 65, 5+6j, 55.665)\n", "h = {'Name': 'Python'}\n", "i = {1,2,3,4}" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(a))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "a = 55.66\n", "\n", "print(type(a))" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n" ] } ], "source": [ "print(type(c), type(i))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "123\n" ] } ], "source": [ "a = input()" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "abc = 556.6" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(a))" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n" ] } ], "source": [ "b = input()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3] \n" ] } ], "source": [ "print(b, type(b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Type Conversions" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "123\n", "123 \n" ] } ], "source": [ "a = int(input())\n", "\n", "\n", "print(a, type(a))" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "256.66\n" ] }, { "ename": "ValueError", "evalue": "invalid literal for int() with base 10: '256.66'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: '256.66'" ] } ], "source": [ "a = int(input())\n", "\n", "\n", "print(a, type(a))" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "125.336\n", "125.336 \n" ] } ], "source": [ "a = float(input())\n", "\n", "\n", "print(a, type(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Number System\n", "\n", "- Decimal -> 10 -> 0-9\n", "- Binary -> 2 -> 0,1 \n", "- Octal -> 8 -> 0-7\n", "- Hexadecimal -> 16 -> 0-F" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0b110111 0o67 0x37\n" ] } ], "source": [ "a = 55\n", "\n", "print(bin(a), oct(a), hex(a))" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "55 55 55\n" ] } ], "source": [ "print(int('110111', 2), int('67', 8), int('37', 16))" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "97\n" ] } ], "source": [ "print(ord('a'))" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n" ] } ], "source": [ "print(chr(97))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Operators in Python\n", "\n", "\n", "- Arthematic Operator -> +, -, *, /, %, //, **\n", "- Logical -> and, or, not\n", "- Comparision/relational -> <, >, <=, >=, ==, !=\n", "- Bitwise Operator -> &(and), |(or), ~(Not), ^(XOR), >>(right shift), <<(left shift)\n", "- Assignment Operator -> +=, -=, /=, %=, //=, **=\n", "- Identity operators -> is, not is\n", "- Membership operators -> in, not in" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "14->-6->40->0.4->4->0->16\n" ] } ], "source": [ "a = 4\n", "b = 10\n", "\n", "\n", "print(a+b, a-b, a*b, a/b, a % b, a//b, a ** 2, sep = '->')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### and (inp1 * inp2)\n", "\n", "|inp1| inp2|inp1 and inp2|\n", "|----|----|----|\n", "|T|T|T|\n", "|T|F|F|\n", "|F|T|F|\n", "|F|F|F|\n", "\n", "### or (inp1 + inp2)\n", "\n", "|inp1| inp2|inp1 or inp2|\n", "|----|----|----|\n", "|T|T|T|\n", "|T|F|T|\n", "|F|T|T|\n", "|F|F|F|\n", "\n", "### not \n", "\n", "|inp1|not inp1 |\n", "|----|----|\n", "|F|T|\n", "|T|F|\n" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0->1->False->0->->False\n" ] } ], "source": [ "a = 1\n", "b = 0\n", "c = '0'\n", "\n", "print(a and b, a or b, not a, a and c, type(a and c), not c, sep = '->')" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 1\n" ] } ], "source": [ "print(c and a, a or c)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n" ] } ], "source": [ "a = 5\n", "b = 6\n", "c = 0\n", "\n", "print(a and b)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0\n" ] } ], "source": [ "print(a and c, c and a)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True False False\n" ] } ], "source": [ "a = 5\n", "b = 6\n", "\n", "print(a < b, a>b, a == b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Bitwise Operators\n", "\n", "\n", "&, |, ~, ^\n", "\n", "```\n", "a = 4\n", "b = 10\n", "\n", "\n", "\n", " 0000 -> 0 -> and\n", " 1110 -> 14 -> or\n", " \n", "~a\n", "4 -> 0100\n", " 0001\n", " -0101 -> *5\n", " \n", "```" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 14 -5\n" ] } ], "source": [ "a = 4\n", "b = 10\n", "\n", "print(a & b, a | b, ~a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### \n", "\n", "XOR -> A'B + AB'\n", "\n", "|inp1| inp2|inp1 and inp2|\n", "|----|----|----|\n", "|T|T|F|\n", "|T|F|T|\n", "|F|T|T|\n", "|F|F|F|\n", "\n", "```\n", "a = 4 -> 0100\n", "b = 10 -> 1010\n", " _____\n", " 1110 -> 14\n", "```" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "14\n" ] } ], "source": [ "print(a ^ b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Left Shift and right shift\n", "\n", "\n", "\n", "a = 4 -> 0100\n", "\n", "\n", "a << 2 -> 010000 -> 16\n", "\n", "a << 2 -> 0001 -> 1" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "16 1\n" ] } ], "source": [ "a = 4\n", "\n", "print( a << 2, a >> 2)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15\n", "25\n", "20\n", "400\n" ] } ], "source": [ "a = 5\n", "\n", "a = a + 10\n", "print(a)\n", "a += 10\n", "print(a)\n", "a -= 5\n", "print(a)\n", "a **= 2\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Membership Operators" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False False True\n" ] } ], "source": [ "a = 'Python'\n", "b = 'Py'\n", "c = 'py'\n", "\n", "print(a in b, a in c, a not in c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Identity Operators" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [], "source": [ "a = 5\n", "b = 5.5\n", "c = 5\n", "d = [1,2,3]\n", "e = [1,2,3]\n", "f = d" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n" ] } ], "source": [ "print(f)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True True True\n" ] } ], "source": [ "print(a == c, d == e, d == f)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "140719521806240\n", "140719521806240\n", "2762794741312\n", "2762794754112\n", "2762794741312\n" ] } ], "source": [ "print(id(a))\n", "print(id(c))\n", "print(id(d))\n", "print(id(e))\n", "print(id(f))" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 'Python', 'Python']\n", "[1, 2, 3, 'Python', 'Python']\n" ] } ], "source": [ "d.append('Python')\n", "\n", "print(d)\n", "print(f)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3] [1, 2, 3] [1, 2, 3]\n", "False True False True\n" ] } ], "source": [ "print(d,e,f)\n", "print(d is e, d is f, e is f, d is not e)" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 4 }