{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyNVSTz5T278cKz5J00UKIgS", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# **Operator: Commands**\n", "\n", "Module of functions that provide the functionality of operators.\n", "\n", "from operator import add, sub, mul, truediv, floordiv, mod, pow, neg, abs\n", "\n", "from operator import eq, ne, lt, le, gt, ge\n", "\n", "from operator import and_, or_, not_\n", "\n", "from operator import itemgetter, attrgetter, methodcaller\n", "\n", "import operator as op\n", "\n", "sorted_by_second = sorted(, key=op.itemgetter(1))\n", "\n", "sorted_by_both = sorted(, key=op.itemgetter(1, 0))\n", "\n", "product_of_elems = functools.reduce(op.mul, )\n", "\n", "LogicOp = enum.Enum('LogicOp', {'AND': op.and_, 'OR' : op.or_})\n", "\n", "last_el = op.methodcaller('pop')()" ], "metadata": { "id": "nC-ZCLEhPR9g" } }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "rfiv-8LZPFT-", "outputId": "aafe0d37-ea53-49a2-be26-d8aca49b3f34" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "x + y = 25\n", "x - y = 5\n", "x * y = 150\n", "x / y = 1.5\n", "x % y = 5\n", "x // y = 1\n", "x ** y = 576650390625\n", "x< y is True\n", "x == y is False\n", "x != y is True\n", "x >= y is False\n", "x<= y is True\n", "a and b is False\n", "a or b is True\n", "not a is False\n", "False\n", "True\n", "False\n", "True\n", "True\n", "True\n", "False\n", "10\n", "15\n", "25\n", "15\n", "5\n", "50\n", "2.0\n", "1\n", "8\n" ] } ], "source": [ "x = 15\n", "y = 10\n", "print('x + y =', x+y)\n", "Output: x + y = 25\n", "\n", "\n", "print('x - y =', x-y)\n", "Output: x - y = 5\n", "\n", "\n", "print('x * y =', x*y)\n", "Output: x * y = 150\n", "\n", "\n", "print('x / y =', x/y)\n", "Output: x / y = 1.5\n", "\n", "\n", "print('x % y =', x%y)\n", "Output: x % y = 5\n", "\n", "\n", "print('x // y =', x//y)\n", "Output: x // y = 1\n", "\n", "\n", "print('x ** y =', x**y)\n", "\n", "\n", "\n", "x = 8\n", "y = 15\n", "('x>y is',x>y)\n", "Output: x > y is False\n", "\n", "\n", "print('x< y is', x= y is', x>=y)\n", "Output: x >= y is False\n", "\n", "\n", "print('x<= y is', x<=y)\n", "\n", "\n", "\n", "a = True\n", "b = False\n", "print('a and b is', a and b)\n", "Output: a and b is False\n", "\n", "\n", "print('a or b is', a or b)\n", "Output: a or b is True\n", "\n", "\n", "print('not a is', not a)\n", "\n", "a1 = 3\n", "b1 = 3\n", "a2 = \"Python\"\n", "b2 = \"Python\"\n", "\n", "a3 = [4,5,6]\n", "b3 = [4,5,6]\n", "\n", "print(a1 is not b1)\n", "Output: False\n", "\n", "print(a2 is b2)\n", "Output: True\n", "\n", "\n", "print(a3 is b3)\n", "\n", "a = \"Python operators\"\n", "b = {1:'x',2:'y'}\n", "\n", "print(\"P\" in a)\n", "Output: True\n", "\n", "\n", "print(\"python\" not in a)\n", "Output: False\n", "\n", "\n", "print(1 in b)\n", "Output: True\n", "\n", "\n", "print('y' in b)\n", "\n", "import operator\n", "\n", "n=5+5\n", "print(n)\n", "\n", "n=operator.add(5, 10)\n", "print(n)\n", "\n", "n=operator.__add__(5, 20)\n", "print(n)\n", "\n", "\n", "# Addition\n", "a = 5\n", "b = 10\n", "c = a + b\n", "print(c) # Output: 15\n", "\n", "# Subtraction\n", "a = 10\n", "b = 5\n", "c = a - b\n", "print(c) # Output: 5\n", "\n", "# Multiplication\n", "a = 5\n", "b = 10\n", "c = a * b\n", "print(c) # Output: 50\n", "\n", "# Division\n", "a = 10\n", "b = 5\n", "c = a / b\n", "print(c) # Output: 2.0\n", "\n", "# Modulo\n", "a = 10\n", "b = 3\n", "c = a % b\n", "print(c) # Output: 1\n", "\n", "# Exponentiation\n", "a = 2\n", "b = 3\n", "c = a ** b\n", "print(c) # Output: 8\n", "\n" ] }, { "cell_type": "markdown", "source": [ "# **Operators** in Python are special symbols that are used to perform various mathematical or logical operations on values or variables. Python provides a variety of operators that can be used to manipulate values and perform operations on them." ], "metadata": { "id": "mwcpIjguQUWR" } }, { "cell_type": "code", "source": [ "x= 5\n", "y=3\n", "print(x+y)\n", "\n", "x= 5\n", "y=3\n", "print(x-y)\n", "\n", "x= 5\n", "y=3\n", "print(x*y)\n", "\n", "x= 5\n", "y=3\n", "print(x/y)\n", "\n", "x= 5\n", "y=3\n", "print(x//y)\n", "\n", "x= 5\n", "y=3\n", "print(x%y)\n", "\n", "x= 5\n", "y=3\n", "print(x**y)\n", "\n", "x = 5\n", "y = 2\n", "\n", "print('Addition:', x + y)\n", "print('Subtraction:', x - y)\n", "print('Multiplication:', x * y)\n", "print('Division:', x / y)\n", "print('Floor Division:', x // y)\n", "print('Exponentiation:', x ** y)\n", "print('Modulus:', x % y)\n", "\n", "#Initialising the variables with values\n", "x =5; y=3\n", "# Addition (+): add both x&y and return the sum\n", "# 5 + 3 = 8\n", "print (\"Add: \",x + y)\n", "# Subtraction (-): subtract the two values and return the difference\n", "# 5 - 3 = 2\n", "print (\"Diff: \", x - y)\n", "# Multiplication (*): Finds the product of two values and return it\n", "# 5 * 3 = 15\n", "print(\"Prod:\", x * y)\n", "\n", "# Division (/): Divides the left value with right value and return the quotient\n", "# 5 / 3 = 1.66\n", "#print(\"div:\", x /y)\n", "\n", "# Modulus(%): Divides the left value with right value and return the remainder\n", "# 5 % 3 = 2\n", "print(\"Remainder:\", x % y)\n", "# Floor Division (//): Divides the left value with right value and return the nearest low whole number\n", "# 5 // 3 =1\n", "print(\"Floor:\", 5 // 3)\n", "# Exponent (**): Raises the left value to the power of right value\n", "# 5 ** 3 = 125\n", "print(\"exp:\", 5**3)\n", "\n", "# =\n", "x = 5\n", "print(x)\n", "# x+=5 --> x = x+5\n", "x+=5\n", "print(x)\n", "# x-=5 --> x = x-5\n", "x -= 5\n", "print(x)\n", "\n", "# x*=5 --> x = x*5\n", "x *= 5\n", "print(x)\n", "# x/=5 --> x = x/5\n", "x /= 5\n", "print(x)\n", "# x %=5 --> x =x %5\n", "x %= 5\n", "print(x)\n", "x = 5\n", "# x **= 5 --> x = x**5\n", "x **= 3;\n", "print(x)\n", "\n", "# x //= 5 --> x = x // 5\n", "x //= 5\n", "print(x)\n", "\n", "a = 4 # 0100\n", "b = 5 # 0101\n", "# binary AND\n", "print(a & b) # 0100 * 0101 --> 0100 (4)\n", "# binary OR\n", "print(a | b) # 0100 + 0101 --> 0101 (5)\n", "# binary XOR\n", "print(a ^ b) #0100 xor 0101 --> 0001 (1)\n", "# binary Ones Complement\n", "print(~a) #0100 --> -(0100 + 1) --> -(0101) -->-5\n", "\n", "# binary left shift\n", "print(a<<2) #0000 0100 --> 0001 0000 (16)\n", "# binary right shift\n", "print(a >> 3) #0000 0100 --> 0000 0000 (0)\n", "\n", "a = 4\n", "b = 5\n", "print(a == b) # True if a is equal to b else it returns false\n", "print(a > b) # True if a is greater than b else it return false\n", "print(a < b) # True if a is less than b else it returns false\n", "print(a != b) # True if a is not equal to b else it returns false\n", "#print(a <> b) # Similar to !=\n", "print(a >= b) #True if a is greter than or equal to b else it #returns false\n", "print(a <= b) # True if a is less than or equal to b else it return\n", "#False\n", "\n", "a = True\n", "b = False\n", "print(a and b) #False if any of the two operands is False\n", "print(a or b) # False if both operands are False\n", "print(not a) # Inverse the logical state if True return False vice-#versa\n", "\n", "print(7 in [1, 2, 3, 4]) #returns false since 7 is not in the list\n", "ls = 'Homer J Simpson' #returns true since the letter r is present\n", "print('r' in ls)\n", "\n", "print('j' not in ls) # returns true since j (not J) is not present #in the string\n", "\n", "a = 'hello'\n", "b = 'hello'\n", "print(a is b)\n", "a = 'Hello'\n", "print(a is b)\n", "print( a is not b)\n", "\n", "# Assigning values to variables\n", "a = 10\n", "b = 5\n", "# Addition\n", "print('a + b =', a + b)\n", "# Subtraction\n", "print('a - b =', a - b)\n", "# Multiplication\n", "print('a * b =', a * b)\n", "# Division\n", "print('a / b =', a / b)\n", "# Modulus\n", "print('a % b =', a % b)\n", "# Floor Division\n", "print('a // b =', a // b)\n", "# Exponent\n", "print('a ** b =', a ** b)\n", "\n", "# Assigning values to variables\n", "a = 10\n", "b = 5\n", "# Greater than\n", "print('a > b =', a > b)\n", "# Lesser than\n", "print('a < b =', a < b)\n", "# Equal to\n", "print('a == b =', a == b)\n", "# Not equal to\n", "print('a != b =', a != b)\n", "# Greater than or equal to\n", "print('a >= b =', a >= b)\n", "# Lesser than or equal to\n", "print('a <= b =', a <= b)\n", "\n", "# Assigning values to variable\n", "a = True\n", "b = False\n", "# Logical and\n", "print('a and b is',a and b)\n", "# Logical or\n", "print('a or b is',a or b)\n", "# Logical not\n", "print('not a is',not a)\n", "\n", "# Assigning values to variables\n", "a = 10\n", "b = 11\n", "# Bitwise AND\n", "print('a & b is',a & b)\n", "# Bitwise OR\n", "print('a | b is',a | b)\n", "# Bitwise XOR\n", "print('a ^ b is',a ^ b)\n", "# Bitwise NOT\n", "print('~a is',~a)\n", "# Bitwise Left Shift\n", "print('a << b is',a << b)\n", "# Bitwise Right Shift\n", "print('a >> b is',a >> b)\n", "\n", "# Assigning the values to variables\n", "a = 15\n", "b = 5\n", "# Simple assignment operator\n", "b = a\n", "print('b = a: ',b)\n", "# ADD AND operator\n", "b += a\n", "print('b += a: ', b)\n", "# SUBTRACT AND operatpr\n", "b -= a\n", "print('b -= a: ', b)\n", "# MULTIPLICATION AND operator\n", "b *= a\n", "print('b *= a: ', b)\n", "# DIVISION AND operator\n", "b /= a\n", "print('b /= a: ', b)\n", "# FLOOR AND operator\n", "b //= a\n", "print('b //= a: ', b)\n", "# MODULUS AND operator\n", "b %= a\n", "print('b %= a: ', b)\n", "# EXPONENT AND operator\n", "b **= a\n", "print('b **= a: ', b)\n", "# LESS THAN AND operator\n", "b <= a\n", "print('b <= a: ', b)\n", "# GREATOR THAN AND operator\n", "b >= a\n", "print('b >= a: ', b)\n", "# BINARY AND operator\n", "a &= 5\n", "print('a &= 5: ', a)\n", "# BINARY OR operator\n", "a |= 5\n", "print('a |= 5: ', a)\n", "\n", "# Assigning values to variables\n", "a = 10\n", "b = 11\n", "# Identity is operator\n", "print('a is b is',a is b)\n", "# Identity is not operator\n", "print('a is not b is',a is not b)\n", "\n", "# Assigning the values to variables\n", "a = 5\n", "b = 5\n", "c = a\n", "# Getting the id of the variables\n", "print(id(a))\n", "print(id(b))\n", "print(id(c))\n", "# Comparing the id of a and c\n", "print(a is c)\n", "\n", "# Assigning a string value to a variable\n", "a = \"Python\"\n", "# Type of the variable\n", "print(type(a))\n", "# Checking whether 'y' is present in the variable a or not\n", "print('y' in a)\n", "# Checking whether 'P' is present in the variable a or not\n", "print('p' not in a)\n", "\n", "# Dictionary with key as 1, 2 and values as 'A' and 'B'\n", "a = {1: \"A\", 2: 'B'}\n", "# Using 'in' operator\n", "print(2 in a)\n", "# Using 'not in' operator\n", "print(3 not in a)\n", "\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "YD75_JwBQuIU", "outputId": "0a7f0c7a-968d-4e10-f4aa-8c2d463d2c9e" }, "execution_count": 5, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "8\n", "2\n", "15\n", "1.6666666666666667\n", "1\n", "2\n", "125\n", "Addition: 7\n", "Subtraction: 3\n", "Multiplication: 10\n", "Division: 2.5\n", "Floor Division: 2\n", "Exponentiation: 25\n", "Modulus: 1\n", "Add: 8\n", "Diff: 2\n", "Prod: 15\n", "Remainder: 2\n", "Floor: 1\n", "exp: 125\n", "5\n", "10\n", "5\n", "25\n", "5.0\n", "0.0\n", "125\n", "25\n", "4\n", "5\n", "1\n", "-5\n", "16\n", "0\n", "False\n", "False\n", "True\n", "True\n", "False\n", "True\n", "False\n", "True\n", "False\n", "False\n", "True\n", "True\n", "True\n", "False\n", "True\n", "a + b = 15\n", "a - b = 5\n", "a * b = 50\n", "a / b = 2.0\n", "a % b = 0\n", "a // b = 2\n", "a ** b = 100000\n", "a > b = True\n", "a < b = False\n", "a == b = False\n", "a != b = True\n", "a >= b = True\n", "a <= b = False\n", "a and b is False\n", "a or b is True\n", "not a is False\n", "a & b is 10\n", "a | b is 11\n", "a ^ b is 1\n", "~a is -11\n", "a << b is 20480\n", "a >> b is 0\n", "b = a: 15\n", "b += a: 30\n", "b -= a: 15\n", "b *= a: 225\n", "b /= a: 15.0\n", "b //= a: 1.0\n", "b %= a: 1.0\n", "b **= a: 1.0\n", "b <= a: 1.0\n", "b >= a: 1.0\n", "a &= 5: 5\n", "a |= 5: 5\n", "a is b is False\n", "a is not b is True\n", "11654504\n", "11654504\n", "11654504\n", "True\n", "\n", "True\n", "True\n", "True\n", "True\n" ] } ] }, { "cell_type": "markdown", "source": [ "# **Operators and Expressions**\n", "Most statements (logical lines) that you write will contain expressions. A simple example of an expression is 2 + 3. An expression can be broken down into operators and operands.\n", "\n", "Operators are functionality that do something and can be represented by symbols such as + or by special keywords. Operators require some data to operate on and such data is called operands. In this case, 2 and 3 are the operands." ], "metadata": { "id": "GWSSPPGfSJdG" } } ] }