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

Department of Data Science

\n", "

Course: Tools and Techniques for Data Science

\n", "\n", "---\n", "

Instructor: Muhammad Arif Butt, Ph.D.

" ] }, { "cell_type": "markdown", "id": "3b9f2264", "metadata": {}, "source": [ "

Lecture 2.3

" ] }, { "cell_type": "markdown", "id": "4d88ae71", "metadata": {}, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "ddecc238", "metadata": {}, "source": [ "## _03-operators.ipynb_\n", "#### [Learn more about Python Operators](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex)" ] }, { "cell_type": "markdown", "id": "b0e8cbe3", "metadata": {}, "source": [ "## Learning agenda of this notebook\n", "\n", " " ] }, { "cell_type": "markdown", "id": "92f688b7", "metadata": {}, "source": [ "## 1. Arithmetic operators in Python\n", "- Python supports the following arithmetic operators:\n", "\n", "| Operator | Purpose | Example | Result |\n", "|------------|-------------------|-------------|-----------|\n", "| `+` | Addition | `2 + 3` | `5` |\n", "| `-` | Subtraction | `3 - 2` | `1` |\n", "| `*` | Multiplication | `8 * 12` | `96` |\n", "| `/` | Division | `100 / 7` | `14.28..` |\n", "| `//` | Floor Division | `100 // 7` | `14` | \n", "| `%` | Modulus/Remainder | `100 % 7` | `2` |\n", "| `**` | Exponent | `5 ** 3` | `125` |\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "5dc01fb7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x + y = 13\n", "x - y = 7\n", "x * y = 30\n", "x / y = 3.3333333333333335\n", "x // y = 3\n", "x ** y = 1000\n", "x % y = 1\n" ] } ], "source": [ "x = 10\n", "y = 3\n", "\n", "# Output: x + y \n", "print('x + y =',x+y)\n", "\n", "# Output: x - y \n", "print('x - y =',x-y)\n", "\n", "# Output: x * y \n", "print('x * y =',x*y)\n", "\n", "# Output: x / y \n", "print('x / y =',x/y)\n", "\n", "# Floor Division: Output: x // y \n", "print('x // y =',x//y)\n", "\n", "# Output: x ^ y \n", "print('x ** y =',x**y)\n", "\n", "\n", "# Output: x % y \n", "print('x % y =',x%y)" ] }, { "cell_type": "markdown", "id": "16a60ba4", "metadata": {}, "source": [ "## 2. Assignment Operators in Python" ] }, { "cell_type": "code", "execution_count": 2, "id": "943aebc8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n" ] } ], "source": [ "#Assignment operators in Python\n", "x = 4\n", "\n", "\n", "x += 5 # <-> x = x + 5\n", "print(x)\n", "\n", "# x = x - 5\n", "# x -= 5 \n", "\n", "# x = x * 5\n", "# x *= 5 \n", "\n", "# x = x / 5\n", "# x /= 5 \n", "\n", "# x = x % 5\n", "# x %= 5 \n", "\n", "# x = x // 5\n", "# x //= 5 \n", "\n", "# x = x ** 5\n", "# x **= 5 \n", "\n", "# x = x & 5\n", "# x &= 5 \n", "\n", "# x = x | 5\n", "# x |= 5 \n", "\n", "# x = x ^ 5\n", "# x ^= 5 \n", "\n", "# x = x >> 5\n", "# x >>= 5 \n", "\n", " # x = x << 5\n", "x <<= 5" ] }, { "cell_type": "markdown", "id": "f7cec4ab", "metadata": {}, "source": [ "## 3. Comparison Operators in Python\n", "- Comparison operators compare the contents in a field to either the contents in another field or a constant. " ] }, { "cell_type": "code", "execution_count": 3, "id": "d735d6bc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x > y is False\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", "True\n" ] } ], "source": [ "#Comparison operators in Python\n", "\n", "x = 10\n", "y = 12\n", "\n", "# Output: x > y \n", "print('x > y is',x>y)\n", "\n", "# Output: x < y \n", "print('x < y is',x= y \n", "print('x >= y is',x>=y)\n", "\n", "# Output: x <= y \n", "print('x <= y is',x<=y)\n", "\n", "\n", "a= x<=y\n", "print(a)" ] }, { "cell_type": "markdown", "id": "68d9b695", "metadata": {}, "source": [ "## 4. Logical operators in Python\n", "- The logical operators `and`, `or` and `not` operate upon conditions and `True` & `False` values. The `and` and `or` operate on two conditions, whereas `not` operates on a single condition.\n", "\n", "- The `and` operator returns `True` when both the conditions evaluate to `True`. Otherwise, it returns `False`.\n", "\n", "| `a` | `b` | `a and b` |\n", "|---------|--------|-----------|\n", "| `True` | `True` | `True` |\n", "| `True` | `False`| `False` |\n", "| `False`| `True` | `False` |\n", "| `False`| `False`| `False` |\n", "\n", "- The `or` operator returns `True` if at least one of the conditions evaluates to `True`. It returns `False` only if both conditions are `False`.\n", "\n", "| `a` | `b` | `a or b` |\n", "|---------|--------|-----------|\n", "| `True` | `True` | `True` |\n", "| `True` | `False`| `True` |\n", "| `False`| `True` | `True` |\n", "| `False`| `False`| `False` |\n", "\n", "- The `not` operator returns `False` if a condition is `True` and `True` if the condition is `False`." ] }, { "cell_type": "code", "execution_count": null, "id": "9b6803b2", "metadata": {}, "outputs": [], "source": [ "x = True\n", "y = False\n", "\n", "print('x and y is',x and y)\n", "\n", "print('x or y is',x or y)\n", "\n", "print('not x is',not x)" ] }, { "cell_type": "markdown", "id": "84fa7d3b", "metadata": {}, "source": [ "Logical operators can be combined to form complex conditions. Use round brackets or parentheses `(` and `)` to indicate the order in which logical operators should be applied." ] }, { "cell_type": "code", "execution_count": null, "id": "1bbd7785", "metadata": {}, "outputs": [], "source": [ "numb = 3\n", "(2 > 3 and 4 <= 5) or not (numb < 0 and True)" ] }, { "cell_type": "markdown", "id": "f7b3d4a0", "metadata": {}, "source": [ "### - Short Circuit Evaluation of Logical Expressions\n", "- When Python is processing a logical expression such as `x >= 2 and (x/y) > 2` , it evaluates the expression from left to right.\n", "- The evaluation of a logical expression stops when the overall value is already known. it is called short-circuiting the evaluation." ] }, { "cell_type": "code", "execution_count": null, "id": "9330e389", "metadata": {}, "outputs": [], "source": [ "# A short circuit happens in 'and' operation, when the first condition evaluates to False\n", "x = 3\n", "y = 0\n", "z = ((x>=6) and (x/y))\n", "z" ] }, { "cell_type": "code", "execution_count": 2, "id": "5f98f487", "metadata": {}, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "division by zero", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/var/folders/1t/g3ylw8h50cjdqmk5d6jh1qmm0000gn/T/ipykernel_36190/1253562860.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m8\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mz\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m>=\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mz\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" ] } ], "source": [ "x = 8\n", "y = 0\n", "z = ((x>=6) and (x/y))\n", "z" ] }, { "cell_type": "code", "execution_count": 1, "id": "b3a791d0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# A short circuit happens in 'or' operation, when the first condition evaluates to True\n", "x = 7\n", "y = 0\n", "z = ((x>=6) or (x/y))\n", "z" ] }, { "cell_type": "code", "execution_count": null, "id": "cf80f7d7", "metadata": {}, "outputs": [], "source": [ "# Now short circuit will not happen\n", "x = 3\n", "y = 0\n", "z = ((x<=6) and (x/y))\n", "z" ] }, { "cell_type": "code", "execution_count": null, "id": "62c8d2f5", "metadata": {}, "outputs": [], "source": [ "# to overcome the above scenario use guard evaluation\n", "\n", "x = 3\n", "y = 0\n", "z = ((x <= 6) and (y != 0) and (x/y))\n", "z" ] }, { "cell_type": "markdown", "id": "b8f41edc", "metadata": {}, "source": [ "## 5. Bitwise Operators in Python\n", "- A bitwise operator is an operator used to perform bitwise operations on bit patterns or binary numerals that involve the manipulation of individual bits." ] }, { "cell_type": "code", "execution_count": null, "id": "a7f38ad7", "metadata": {}, "outputs": [], "source": [ "a = -5\n", "b = a >> 1\n", "b" ] }, { "cell_type": "code", "execution_count": 3, "id": "150256a7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x & y is 0\n", "x | y is 14\n", "~x is -11\n", "x^y is 14\n", "x>>3 is 1\n", "x<<3 is 80\n" ] } ], "source": [ "#Bitwise operators in Python\n", "x = 10 # 00001010\n", "y = 4 # 00000100\n", "\n", "\n", "# Bitwise and\n", "print('x & y is',x&y)\n", "\n", "# Bitwise or\n", "print('x | y is',x|y)\n", "\n", "# Bitwise not\n", "print('~x is',~x)\n", "\n", "# Bitwise XOR\n", "print('x^y is',x^y)\n", "\n", "# Bitwise right shift\n", "print('x>>3 is',x>>3)\n", "\n", "# Bitwise left shift\n", "print('x<<3 is',x<<3)" ] }, { "cell_type": "code", "execution_count": 7, "id": "a4bfc65d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n", "-14\n", "-2\n" ] } ], "source": [ "#Bitwise operators in Python\n", "x = -10 \n", "y = 4 \n", "\n", "print(~x)\n", "print(x^y)\n", "print(x>>3)" ] }, { "cell_type": "markdown", "id": "cf10f921", "metadata": {}, "source": [ "## 6. Identity Operators in Python\n", "- Identity operators are used to compare the ID of objects (not their values)\n", "- Returns True if both objects refer to same memory location\n", "- The two identity operators in Python are `is` and `is not`" ] }, { "cell_type": "code", "execution_count": null, "id": "cb150232", "metadata": {}, "outputs": [], "source": [ "#Identity operators in Python\n", "a = 5\n", "b = 5.0\n", "\n", "print(a is b)\n", "print(a==b)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "3141880d", "metadata": {}, "outputs": [], "source": [ "a = 'Hello'\n", "b = 'Hello'\n", "\n", "# Output: False\n", "print(a is not b)\n", "\n", "# Output: True\n", "print(a is b)" ] }, { "cell_type": "markdown", "id": "ec7ba936", "metadata": {}, "source": [ "## 7. Membership Operators in Python\n", "- Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples.\n", "- The two membership operators in Python are `in` and `not in`" ] }, { "cell_type": "code", "execution_count": null, "id": "da3a469f", "metadata": {}, "outputs": [], "source": [ "a = 10\n", "b = 4\n", "list = [1, 2, 3, 4, 5 ]\n", "\n", "rv = a in list\n", "print(rv)\n", "rv = b in list\n", "print(rv)" ] }, { "cell_type": "markdown", "id": "61c4d920", "metadata": {}, "source": [ "## 8. Operators Precedence and Associativity: \n", " \n", "\n", "- The **precedence of operators** determines which operator is executed first if there are more than one operator in an expression. \n", "- Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator.\n", "- Operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom.\n", "- The **associativity of operators** is the order in which Python evaluates an expression containing multiple operators of the same precedence.\n", "- Almost all the operators have left-to-right associativity.\n", " >- may be associative (means the operations can be grouped arbitrarily)\n", " >-left-associative (means the operations are grouped from the left)\n", " >-right-associative (Exponent operator ** has right-to-left associativity in Python)\n", " >- non-associative (meaning operations cannot be chained, often because the output type" ] }, { "cell_type": "code", "execution_count": null, "id": "d8669ef1", "metadata": {}, "outputs": [], "source": [ "# Run interactive help and type \"OPERATORS\" to get information about precedence\n", "help('OPERATORS')" ] }, { "cell_type": "code", "execution_count": null, "id": "efe3dddf", "metadata": {}, "outputs": [], "source": [ "print(5 + 3 * 2)\n", "print((5 + 3) * 2)" ] }, { "cell_type": "code", "execution_count": 10, "id": "329c2627", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "512\n", "64\n" ] } ], "source": [ "print(2 ** 3 ** 2)\n", "print((2 ** 3) ** 2)" ] }, { "cell_type": "code", "execution_count": null, "id": "9392b97d", "metadata": {}, "outputs": [], "source": [ "num1, num2, num3 = 2, 3, 4\n", "print ((num1 + num2) * num3)" ] }, { "cell_type": "code", "execution_count": 9, "id": "e12cfa9d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12\n" ] } ], "source": [ "num1, num2, num3 = 2, 3, 4\n", "print (num1 ** num2 + num3)" ] }, { "cell_type": "code", "execution_count": 8, "id": "a5a74189", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-13\n" ] } ], "source": [ "num1, num2 = 15, 3\n", "print (~num1 + num2)" ] }, { "cell_type": "markdown", "id": "fc264563", "metadata": {}, "source": [ "## Check your Concepts\n", "\n", "Try answering the following questions to test your understanding of the topics covered in this notebook:\n", "\n", "1. What the different arithmetic operations supported in Python?\n", "2. How do you perform arithmetic operations using Python?\n", "3. What is the difference between the `/` and the `//` operators?\n", "4. What is the difference between the `*` and the `**` operators?\n", "5. What do you mean by precedence, associativity and arity of operators?\n", "5. What is the order of precedence for arithmetic operators in Python?\n", "6. How do you specify the order in which arithmetic operations are performed in an expression involving multiple operators?\n", "7. How do you solve a multi-step arithmetic word problem using Python?\n", "8. What are variables? Why are they useful?\n", "9. How do you create a variable in Python?\n", "10. What is the assignment operator in Python?\n", "11. What are the rules for naming a variable in Python?\n", "12. How do you view the value of a variable?\n", "13. How do you store the result of an arithmetic expression in a variable?\n", "14. What happens if you try to access a variable that has not been defined?\n", "15. How do you display messages in Python?\n", "16. What type of inputs can the print function accept?\n", "17. What are code comments? How are they useful?\n", "18. What are the different ways of creating comments in Python code?\n", "19. What are the different comparison operations supported in Python?\n", "20. What is the result of a comparison operation?\n", "21. What is the difference between `=` and `==` in Python?\n", "22. What are the logical operators supported in Python?\n", "23. What is the difference between the `and` and `or` operators?\n", "24. Can you use comparison and logical operators in the same expression?\n", "25. What is the purpose of using parentheses in arithmetic or logical expressions?\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }