{ "cells": [ { "cell_type": "markdown", "id": "35483a7e", "metadata": {}, "source": [ "# # Python3 program to add two numbers\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "4db7dd9e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sum of 15 and 12 is 27\n" ] } ], "source": [ "num1 = 15\n", "num2 = 12\n", " \n", "sum = num1 + num2\n", " \n", "print(\"Sum of {0} and {1} is {2}\" .format(num1, num2, sum))" ] }, { "cell_type": "code", "execution_count": 3, "id": "53336f61", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First number: 10\n", "\n", "Second number: 20\n", "The sum of 10 and 20 is 30.0\n" ] } ], "source": [ "number1 = input(\"First number: \")\n", "number2 = input(\"\\nSecond number: \")\n", "\n", "sum = float(number1) + float(number2)\n", " \n", "print(\"The sum of {0} and {1} is {2}\" .format(number1, number2, sum))" ] }, { "cell_type": "code", "execution_count": 6, "id": "fe3559fc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sum of 15 and 12 is 27;\n" ] } ], "source": [ "if __name__ == \"__main__\" :\n", " \n", " num1 = 15\n", " num2 = 12\n", "\n", "sum_twoNum = lambda num1, num2 : num1 + num2\n", " \n", "print(\"Sum of {0} and {1} is {2};\" .format(num1, num2, sum_twoNum(num1, num2)))" ] }, { "cell_type": "markdown", "id": "464a5723", "metadata": {}, "source": [ "# Maximum of two numbers in Python" ] }, { "cell_type": "code", "execution_count": 7, "id": "b0a31d7a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "def maximum(a, b):\n", " \n", " if a >= b:\n", " return a\n", " else:\n", " return b\n", " \n", "a = 2\n", "b = 4\n", "print(maximum(a, b))" ] }, { "cell_type": "code", "execution_count": 8, "id": "556e70d8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ " \n", "a = 2\n", "b = 4\n", " \n", "maximum = max(a, b)\n", "print(maximum)" ] }, { "cell_type": "code", "execution_count": 9, "id": "70ba3b02", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "a = 2\n", "b = 4\n", "\n", "print(a if a >= b else b)" ] }, { "cell_type": "code", "execution_count": 10, "id": "be071989", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4 is a maximum number\n" ] } ], "source": [ "a=2;b=4\n", "maximum = lambda a,b:a if a > b else b\n", "print(f'{maximum(a,b)} is a maximum number')" ] }, { "cell_type": "code", "execution_count": 11, "id": "1211ceea", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "maximum number is: [4]\n" ] } ], "source": [ "a=2;b=4\n", "x=[a if a>b else b]\n", "print(\"maximum number is:\",x)" ] }, { "cell_type": "markdown", "id": "54f1c21a", "metadata": {}, "source": [ "# Python Program for factorial of a number\n" ] }, { "cell_type": "code", "execution_count": 14, "id": "32aae2e6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Factorial of 5 is 120\n" ] } ], "source": [ "def factorial(n):\n", "\n", " return 1 if (n==1 or n==0) else n * factorial(n - 1);\n", "\n", "num = 5;\n", "print(\"Factorial of\",num,\"is\",\n", "factorial(num))" ] }, { "cell_type": "code", "execution_count": 15, "id": "b623a26d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Factorial of 5 is 120\n" ] } ], "source": [ "def factorial(n):\n", " if n < 0:\n", " return 0\n", " elif n == 0 or n == 1:\n", " return 1\n", " else:\n", " fact = 1\n", " while(n > 1):\n", " fact *= n\n", " n -= 1\n", " return fact\n", " \n", "num = 5;\n", "print(\"Factorial of\",num,\"is\",\n", "factorial(num))" ] }, { "cell_type": "code", "execution_count": 16, "id": "8f7c592b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Factorial of 5 is 120\n" ] } ], "source": [ "def factorial(n):\n", " \n", " res = 1\n", " \n", " for i in range(2, n+1):\n", " res *= i\n", " return res\n", "num = 5;\n", "print(\"Factorial of\", num, \"is\",\n", "factorial(num))" ] }, { "cell_type": "code", "execution_count": 17, "id": "c31af606", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Factorial of 5 is 120\n" ] } ], "source": [ "def factorial(n):\n", " \n", " return 1 if (n==1 or n==0) else n * factorial(n - 1)\n", " \n", " \n", "num = 5\n", "print (\"Factorial of\",num,\"is\",\n", " factorial(num))" ] }, { "cell_type": "code", "execution_count": 18, "id": "98a69992", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Factorial of 5 is 120\n" ] } ], "source": [ "import math\n", " \n", "def factorial(n):\n", " return(math.factorial(n))\n", " \n", " \n", "num = 5\n", "print(\"Factorial of\", num, \"is\",\n", " factorial(num))" ] }, { "cell_type": "code", "execution_count": 19, "id": "ac75e7e2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "120\n" ] } ], "source": [ "import numpy\n", "n=5\n", "x=numpy.prod([i for i in range(1,n+1)])\n", "print(x)" ] }, { "cell_type": "markdown", "id": "51dfa435", "metadata": {}, "source": [ "# Python Program for simple interest\n" ] }, { "cell_type": "code", "execution_count": 20, "id": "33a911f8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The principal is 8\n", "The time period is 6\n", "The rate of interest is 8\n", "The Simple Interest is 3.84\n" ] }, { "data": { "text/plain": [ "3.84" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def simple_interest(p,t,r):\n", " print('The principal is', p)\n", " print('The time period is', t)\n", " print('The rate of interest is',r)\n", " \n", " si = (p * t * r)/100\n", " \n", " print('The Simple Interest is', si)\n", " return si\n", " \n", "simple_interest(8, 6, 8)" ] }, { "cell_type": "markdown", "id": "fc433c00", "metadata": {}, "source": [ "# Python Program for compound interest\n" ] }, { "cell_type": "code", "execution_count": 21, "id": "78133ea7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Compound interest is 6288.946267774416\n" ] } ], "source": [ " \n", "def compound_interest(principal, rate, time):\n", " \n", " Amount = principal * (pow((1 + rate / 100), time))\n", " CI = Amount - principal\n", " print(\"Compound interest is\", CI)\n", " \n", " \n", "compound_interest(10000, 10.25, 5)" ] }, { "cell_type": "code", "execution_count": 22, "id": "fae87376", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "133.0992000000001\n" ] } ], "source": [ "p= 1200 # principal amount\n", "t= 2 # time\n", "r= 5.4 # rate\n", "# calculates the compound interest\n", "a=p*(1+(r/100))**t # formula for calculating amount\n", "ci=a-p # compound interest = amount - principal amount\n", "print(ci)" ] }, { "cell_type": "markdown", "id": "87a9cb23", "metadata": {}, "source": [ "# Python Program to check Armstrong Number\n" ] }, { "cell_type": "code", "execution_count": 23, "id": "6160fcf0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n" ] } ], "source": [ "def power(x, y):\n", " \n", " if y == 0:\n", " return 1\n", " if y % 2 == 0:\n", " return power(x, y // 2) * power(x, y // 2)\n", " \n", " return x * power(x, y // 2) * power(x, y // 2)\n", " \n", "# Function to calculate order of the number\n", "def order(x):\n", " \n", " # Variable to store of the number\n", " n = 0\n", " while (x != 0):\n", " n = n + 1\n", " x = x // 10\n", " \n", " return n\n", " \n", "# Function to check whether the given\n", "# number is Armstrong number or not\n", "def isArmstrong(x):\n", " \n", " n = order(x)\n", " temp = x\n", " sum1 = 0\n", " \n", " while (temp != 0):\n", " r = temp % 10\n", " sum1 = sum1 + power(r, n)\n", " temp = temp // 10\n", " \n", " # If condition satisfies\n", " return (sum1 == x)\n", " \n", "# Driver code\n", "x = 153\n", "print(isArmstrong(x))\n", " \n", "x = 1253\n", "print(isArmstrong(x))" ] }, { "cell_type": "code", "execution_count": 25, "id": "fb997082", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The given number 153 is armstrong number\n" ] } ], "source": [ "n = 153 # or n=int(input()) -> taking input from user\n", "s = n # assigning input value to the s variable\n", "b = len(str(n))\n", "sum1 = 0\n", "while n != 0:\n", " r = n % 10\n", " sum1 = sum1+(r**b)\n", " n = n//10\n", "if s == sum1:\n", " print(\"The given number\", s, \"is armstrong number\")\n", "else:\n", " print(\"The given number\", s, \"is not armstrong number\")\n" ] }, { "cell_type": "markdown", "id": "411e0e64", "metadata": {}, "source": [ "# Python Program for Program to find area of a circle" ] }, { "cell_type": "code", "execution_count": 26, "id": "1163fef8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Area is 78.550000\n" ] } ], "source": [ " \n", "def findArea(r):\n", " PI = 3.142\n", " return PI * (r*r);\n", " \n", "# Driver method\n", "print(\"Area is %.6f\" % findArea(5));\n", " " ] }, { "cell_type": "code", "execution_count": 27, "id": "c6c48d8e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Area of circle is: 50.26548245743669\n" ] } ], "source": [ "import math\n", "def area(r):\n", " area = math.pi* pow(r,2)\n", " return print('Area of circle is:' ,area)\n", "area(4)" ] }, { "cell_type": "markdown", "id": "4443eb06", "metadata": {}, "source": [ "# Python program to print all Prime numbers in an Interval" ] }, { "cell_type": "code", "execution_count": 28, "id": "06b67a4a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The prime numbers in this range are: [2, 3, 5]\n" ] } ], "source": [ "def prime(x, y):\n", " prime_list = []\n", " for i in range(x, y):\n", " if i == 0 or i == 1:\n", " continue\n", " else:\n", " for j in range(2, int(i/2)+1):\n", " if i % j == 0:\n", " break\n", " else:\n", " prime_list.append(i)\n", " return prime_list\n", " \n", "# Driver program\n", "starting_range = 2\n", "ending_range = 7\n", "lst = prime(starting_range, ending_range)\n", "if len(lst) == 0:\n", " print(\"There are no prime numbers in this range\")\n", "else:\n", " print(\"The prime numbers in this range are: \", lst)" ] }, { "cell_type": "markdown", "id": "210bfd47", "metadata": {}, "source": [ "# Python program to check whether a number is Prime or not" ] }, { "cell_type": "code", "execution_count": 29, "id": "8fd39fbb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11 is a prime number\n" ] } ], "source": [ "num = 11\n", "# If given number is greater than 1\n", "if num > 1:\n", " # Iterate from 2 to n / 2\n", " for i in range(2, int(num/2)+1):\n", " # If num is divisible by any number between\n", " # 2 and n / 2, it is not prime\n", " if (num % i) == 0:\n", " print(num, \"is not a prime number\")\n", " break\n", " else:\n", " print(num, \"is a prime number\")\n", "else:\n", " print(num, \"is not a prime number\")" ] }, { "cell_type": "code", "execution_count": 30, "id": "1c107c48", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "from math import sqrt\n", "# n is the number to be check whether it is prime or not\n", "n = 1\n", " \n", "# this flag maintains status whether the n is prime or not\n", "prime_flag = 0\n", " \n", "if(n > 1):\n", " for i in range(2, int(sqrt(n)) + 1):\n", " if (n % i == 0):\n", " prime_flag = 1\n", " break\n", " if (prime_flag == 0):\n", " print(\"True\")\n", " else:\n", " print(\"False\")\n", "else:\n", " print(\"False\")" ] }, { "cell_type": "markdown", "id": "d3534bc4", "metadata": {}, "source": [ "# Python Program for n-th Fibonacci number\n" ] }, { "cell_type": "code", "execution_count": 31, "id": "25f509d4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "34\n" ] } ], "source": [ "def Fibonacci(n):\n", " if n<= 0:\n", " print(\"Incorrect input\")\n", " # First Fibonacci number is 0\n", " elif n == 1:\n", " return 0\n", " # Second Fibonacci number is 1\n", " elif n == 2:\n", " return 1\n", " else:\n", " return Fibonacci(n-1)+Fibonacci(n-2)\n", " \n", " \n", "print(Fibonacci(10))" ] }, { "cell_type": "code", "execution_count": 32, "id": "64e7c92c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "21\n" ] } ], "source": [ "FibArray = [0, 1]\n", " \n", "def fibonacci(n):\n", " if n<0:\n", " print(\"Incorrect input\")\n", " elif n<= len(FibArray):\n", " return FibArray[n-1]\n", " else:\n", " temp_fib = fibonacci(n-1)+fibonacci(n-2)\n", " FibArray.append(temp_fib)\n", " return temp_fib\n", " \n", " \n", "print(fibonacci(9))" ] }, { "cell_type": "code", "execution_count": 33, "id": "0e583ea4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "21\n" ] } ], "source": [ "def fibonacci(n):\n", " a = 0\n", " b = 1\n", " if n < 0:\n", " print(\"Incorrect input\")\n", " elif n == 0:\n", " return a\n", " elif n == 1:\n", " return b\n", " else:\n", " for i in range(2, n):\n", " c = a + b\n", " a = b\n", " b = c\n", " return b\n", " \n", " \n", "print(fibonacci(9))" ] }, { "cell_type": "code", "execution_count": 35, "id": "58f3aebf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "21\n" ] } ], "source": [ "def fibonacci(n):\n", " if n <= 0:\n", " return \"Incorrect Output\"\n", " data = [0, 1]\n", " if n > 2:\n", " for i in range(2, n):\n", " data.append(data[i-1] + data[i-2])\n", " return data[n-1]\n", " \n", "print(fibonacci(9))" ] }, { "cell_type": "code", "execution_count": 36, "id": "f45525ce", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "144 is 12th fibonacci number\n" ] } ], "source": [ "from math import sqrt\n", "# import square-root method from math library\n", "def nthFib(n):\n", " res = (((1+sqrt(5))**n)-((1-sqrt(5)))**n)/(2**n*sqrt(5))\n", " # compute the n-th fibonacci number\n", " print(int(res),'is',str(n)+'th fibonacci number')\n", " # format and print the number\n", " \n", "nthFib(12)\n", " " ] }, { "cell_type": "markdown", "id": "264f1958", "metadata": {}, "source": [ "# Python Program for How to check if a given number is Fibonacci number?" ] }, { "cell_type": "code", "execution_count": 38, "id": "ef1ad757", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 is a Fibonacci Number\n", "2 is a Fibonacci Number\n", "3 is a Fibonacci Number\n", "4 is a not Fibonacci Number \n", "5 is a Fibonacci Number\n", "6 is a not Fibonacci Number \n", "7 is a not Fibonacci Number \n", "8 is a Fibonacci Number\n", "9 is a not Fibonacci Number \n", "10 is a not Fibonacci Number \n" ] } ], "source": [ "import math\n", " \n", "# A utility function that returns true if x is perfect square\n", " \n", " \n", "def isPerfectSquare(x):\n", " s = int(math.sqrt(x))\n", " return s*s == x\n", " \n", "# Returns true if n is a Fibonacci Number, else false\n", " \n", " \n", "def isFibonacci(n):\n", " \n", " # n is Fibonacci if one of 5*n*n + 4 or 5*n*n - 4 or both\n", " # is a perfect square\n", " return isPerfectSquare(5*n*n + 4) or isPerfectSquare(5*n*n - 4)\n", " \n", " \n", "for i in range(1, 11):\n", " if (isFibonacci(i) == True):\n", " print(i, \"is a Fibonacci Number\")\n", " else:\n", " print(i, \"is a not Fibonacci Number \")" ] }, { "cell_type": "markdown", "id": "0d123ed2", "metadata": {}, "source": [ "# Python Program for Sum of squares of first n natural numbers" ] }, { "cell_type": "code", "execution_count": 39, "id": "4847b2c0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "30\n" ] } ], "source": [ "def squaresum(n) :\n", " \n", " # Iterate i from 1 \n", " # and n finding \n", " # square of i and\n", " # add to sum.\n", " sm = 0\n", " for i in range(1, n+1) :\n", " sm = sm + (i * i)\n", " \n", " return sm\n", " \n", "# Driven Program\n", "n = 4\n", "print(squaresum(n))" ] }, { "cell_type": "code", "execution_count": 41, "id": "fb789a1f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "30\n" ] } ], "source": [ "def squaresum(n) :\n", " return (n * (n + 1) * (2 * n + 1)) // 6\n", " \n", "n = 4\n", "print(squaresum(n))" ] }, { "cell_type": "markdown", "id": "7c61509c", "metadata": {}, "source": [ "# Python Program for cube sum of first n natural numbers" ] }, { "cell_type": "code", "execution_count": 43, "id": "e762cdd5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "225\n" ] } ], "source": [ "def sumOfSeries(n):\n", " sum = 0\n", " for i in range(1, n+1):\n", " sum +=pow(i,3)\n", " \n", " return sum\n", " \n", " \n", "n = 5\n", "print(sumOfSeries(n))\n", " " ] }, { "cell_type": "code", "execution_count": 44, "id": "a39f6844", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "225\n" ] } ], "source": [ "def sumOfSeries(n):\n", " x = 0\n", " if n % 2 == 0 :\n", " x = (n/2) * (n+1)\n", " else:\n", " x = ((n + 1) / 2) * n\n", " \n", " return (int)(x * x)\n", " \n", " \n", "n = 5\n", "print(sumOfSeries(n))\n", " " ] }, { "cell_type": "markdown", "id": "c768c63d", "metadata": {}, "source": [ "# Advance Python" ] }, { "cell_type": "markdown", "id": "bfd0a32a", "metadata": {}, "source": [ "# Python Program to find sum of array" ] }, { "cell_type": "code", "execution_count": 45, "id": "5cab1132", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sum of the array is 34\n" ] } ], "source": [ "def _sum(arr):\n", " \n", " # initialize a variable\n", " # to store the sum\n", " # while iterating through\n", " # the array later\n", " sum = 0\n", " \n", " # iterate through the array\n", " # and add each element to the sum variable\n", " # one at a time\n", " for i in arr:\n", " sum = sum + i\n", " \n", " return(sum)\n", " \n", " \n", "arr = []\n", "# input values to list\n", "arr = [12, 3, 4, 15]\n", " \n", "# calculating length of array\n", "n = len(arr)\n", " \n", "ans = _sum(arr)\n", " \n", "print('Sum of the array is ', ans)" ] }, { "cell_type": "code", "execution_count": 47, "id": "87a9e11e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sum of the array is 34\n" ] } ], "source": [ "arr = []\n", " \n", "arr = [12, 3, 4, 15]\n", "ans = _sum(arr)\n", " \n", "print('Sum of the array is ', ans)" ] }, { "cell_type": "markdown", "id": "3a3710d0", "metadata": {}, "source": [ "# Python Program to find largest element in an array" ] }, { "cell_type": "code", "execution_count": 48, "id": "a143090e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Largest in given array 9808\n" ] } ], "source": [ "def largest(arr, n):\n", " \n", " # Initialize maximum element\n", " max = arr[0]\n", " \n", " # Traverse array elements from second\n", " # and compare every element with\n", " # current max\n", " for i in range(1, n):\n", " if arr[i] > max:\n", " max = arr[i]\n", " return max\n", " \n", " \n", "arr = [10, 324, 45, 90, 9808]\n", "n = len(arr)\n", "Ans = largest(arr, n)\n", "print(\"Largest in given array \", Ans)\n", " \n" ] }, { "cell_type": "code", "execution_count": 49, "id": "7202b9fc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Largest in given array 9808\n" ] } ], "source": [ "def largest(arr, n):\n", " ans = max(arr)\n", " return ans;\n", " \n", "# Driver code\n", "if __name__ == '__main__':\n", " arr = [10, 324, 45, 90, 9808]\n", " n = len(arr)\n", " print (\"Largest in given array \", largest(arr, n))" ] }, { "cell_type": "markdown", "id": "a8f15a62", "metadata": {}, "source": [ "# Python Program for array rotation\n" ] }, { "cell_type": "code", "execution_count": 51, "id": "275b30db", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original array: [1, 2, 3, 4, 5, 6, 7, 8]\n", "Rotated array: [2, 3, 4, 5, 6, 7, 8, 1]\n" ] } ], "source": [ "def reverse(start,end,arr):\n", " \n", " #No of iterations needed for reversing the list\n", " no_of_reverse=end-start+1\n", " \n", " #By incrementing count value swapping of first and last elements is done.\n", " count=0\n", " while((no_of_reverse)//2!=count):\n", " arr[start+count],arr[end-count]=arr[end-count],arr[start+count]\n", " count+=1\n", " return arr\n", " \n", "#Function takes array, length of array and no of rotations as input\n", "def left_rotate_array(arr,size,d):\n", " \n", " #Reverse the Entire List\n", " start=0\n", " end=size-1\n", " arr=reverse(start,end,arr)\n", " \n", " #Divide array into twosub-array based on no of rotations.\n", " #Divide First sub-array\n", " #Reverse the First sub-array\n", " start=0\n", " end=size-d-1\n", " arr=reverse(start,end,arr)\n", " \n", " #Divide Second sub-array\n", " #Reverse the Second sub-array\n", " start=size-d\n", " end=size-1\n", " arr=reverse(start,end,arr)\n", " return arr\n", " \n", "arr=[1,2,3,4,5,6,7,8]\n", "size=8\n", "d=1\n", "print('Original array:',arr)\n", " \n", "#Finding all the symmetric rotation number\n", "if(d<=size):\n", " print('Rotated array: ',left_rotate_array(arr,size,d))\n", "else:\n", " d=d%size\n", " print('Rotated array: ',left_rotate_array(arr,size,d))\n" ] }, { "cell_type": "code", "execution_count": 52, "id": "32506975", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]\n" ] } ], "source": [ "def rotateArray(arr, n, d):\n", " temp = []\n", " i = 0\n", " while (i < d):\n", " temp.append(arr[i])\n", " i = i + 1\n", " i = 0\n", " while (d < n):\n", " arr[i] = arr[d]\n", " i = i + 1\n", " d = d + 1\n", " arr[:] = arr[: i] + temp\n", " return arr\n", " \n", " \n", "# Driver function to test above function\n", "arr = [1, 2, 3, 4, 5, 6, 7]\n", "print(\"Array after left rotation is: \", end=' ')\n", "print(rotateArray(arr, len(arr), 2))" ] }, { "cell_type": "markdown", "id": "4443a3ea", "metadata": {}, "source": [ "# Python Program for Reversal algorithm for array rotation " ] }, { "cell_type": "code", "execution_count": 53, "id": "1e27f08f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "4\n", "5\n", "6\n", "7\n", "1\n", "2\n" ] } ], "source": [ "def rverseArray(arr, start, end):\n", " while (start < end):\n", " temp = arr[start]\n", " arr[start] = arr[end]\n", " arr[end] = temp\n", " start += 1\n", " end = end-1\n", " \n", "# Function to left rotate arr[] of size n by d\n", "def leftRotate(arr, d):\n", " n = len(arr)\n", " rverseArray(arr, 0, d-1)\n", " rverseArray(arr, d, n-1)\n", " rverseArray(arr, 0, n-1)\n", "\n", "# Function to print an array\n", "def printArray(arr):\n", " for i in range(0, len(arr)):\n", " print (arr[i])\n", " \n", "# Driver function to test above functions\n", "arr = [1, 2, 3, 4, 5, 6, 7]\n", "leftRotate(arr, 2) # Rotate array by 2\n", "printArray(arr)" ] }, { "cell_type": "markdown", "id": "ef287b1f", "metadata": {}, "source": [ "# Python Program to Split the array and add the first part to the end" ] }, { "cell_type": "code", "execution_count": 54, "id": "e814fc7d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 6 52 36 12 10 " ] } ], "source": [ "def splitArr(arr, n, k):\n", " for i in range(0, k):\n", " x = arr[0]\n", " for j in range(0, n-1):\n", " arr[j] = arr[j + 1]\n", " \n", " arr[n-1] = x\n", " \n", " \n", "# main\n", "arr = [12, 10, 5, 6, 52, 36]\n", "n = len(arr)\n", "position = 2\n", " \n", "splitArr(arr, n, position)\n", " \n", "for i in range(0, n):\n", " print(arr[i], end = ' ')\n", " \n" ] }, { "cell_type": "markdown", "id": "f423b221", "metadata": {}, "source": [ "# Python Program for Find remainder of array multiplication divided by n" ] }, { "cell_type": "code", "execution_count": 55, "id": "e7d44f81", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n" ] } ], "source": [ "\n", "arr = [100, 10, 5, 25, 35, 14];lens = len(arr);n = 11\n", "mul = 1\n", "for i in range(lens):\n", " mul = (mul * (arr[i] % n)) % n\n", "print(mul % n)" ] }, { "cell_type": "markdown", "id": "cb6178b2", "metadata": {}, "source": [ "# Python Program to check if given array is Monotonic" ] }, { "cell_type": "code", "execution_count": 58, "id": "d223c74d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "def isMonotonic(A):\n", " x, y = [], []\n", " x.extend(A)\n", " y.extend(A)\n", " x.sort()\n", " y.sort(reverse=True)\n", " if(x == A or y == A):\n", " return True\n", " return False\n", " \n", " \n", "A = [6, 5, 4, 4]\n", " \n", "print(isMonotonic(A))" ] }, { "cell_type": "markdown", "id": "eb4ddd81", "metadata": {}, "source": [ "# List Programs:" ] }, { "cell_type": "markdown", "id": "495b080c", "metadata": {}, "source": [ "# Python program to interchange first and last elements in a list" ] }, { "cell_type": "code", "execution_count": 10, "id": "3c540d7a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[24, 35, 9, 56, 12]\n" ] } ], "source": [ "def swapList(newList):\n", " size = len(newList)\n", " \n", " # Swapping\n", " temp = newList[0]\n", " newList[0] = newList[size - 1]\n", " newList[size - 1] = temp\n", " \n", " return newList\n", " \n", "newList = [12, 35, 9, 56, 24]\n", " \n", "print(swapList(newList))" ] }, { "cell_type": "markdown", "id": "2875adbc", "metadata": {}, "source": [ "# Python program to swap two elements in a list\n" ] }, { "cell_type": "code", "execution_count": 11, "id": "a808cc0d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[19, 65, 23, 90]\n" ] } ], "source": [ "def swapPositions(list, pos1, pos2):\n", " \n", " list[pos1], list[pos2] = list[pos2], list[pos1]\n", " return list\n", " \n", "# Driver function\n", "List = [23, 65, 19, 90]\n", "pos1, pos2 = 1, 3\n", " \n", "print(swapPositions(List, pos1-1, pos2-1))" ] }, { "cell_type": "markdown", "id": "e3750721", "metadata": {}, "source": [ "# Python | Ways to find length of list\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "b0866ee7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The list is : [1, 4, 5, 7, 8]\n", "Length of list using naive method is : 5\n" ] } ], "source": [ "test_list = [1, 4, 5, 7, 8]\n", " \n", "# Printing test_list\n", "print(\"The list is : \" + str(test_list))\n", " \n", "# Finding length of list\n", "# using loop\n", "# Initializing counter\n", "counter = 0\n", "for i in test_list:\n", " \n", " # incrementing counter\n", " counter = counter + 1\n", " \n", "print(\"Length of list using naive method is : \" + str(counter))" ] }, { "cell_type": "markdown", "id": "2fdaf4d3", "metadata": {}, "source": [ "# Check if element exists in list in Python\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "0337b8a0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "exist\n" ] } ], "source": [ "lst=[ 1, 6, 3, 5, 3, 4 ]\n", "i=6\n", "if i in lst:\n", " print(\"exist\")\n", "else:\n", " print(\"not exist\")" ] }, { "cell_type": "markdown", "id": "3802e2db", "metadata": {}, "source": [ "# Different ways to clear a list in Python\n" ] }, { "cell_type": "code", "execution_count": 17, "id": "0fbf8fea", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "clear: [6, 0, 4, 1]\n", "clear: []\n" ] } ], "source": [ "all = [6, 0, 4, 1]\n", "print('clear:', all)\n", " \n", "all.clear()\n", "print('clear:', all)" ] }, { "cell_type": "markdown", "id": "8f209656", "metadata": {}, "source": [ "# Reversing a List in Python\n" ] }, { "cell_type": "code", "execution_count": 18, "id": "b2a73e0e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using reverse() [15, 14, 13, 12, 11, 10]\n", "Using reversed() [10, 11, 12, 13, 14, 15]\n" ] } ], "source": [ "lst = [10, 11, 12, 13, 14, 15]\n", "lst.reverse()\n", "print(\"Using reverse() \", lst)\n", " \n", "print(\"Using reversed() \", list(reversed(lst)))" ] }, { "cell_type": "code", "execution_count": 19, "id": "6be5c33d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[7, 6, 5, 4, 3, 2, 1]\n" ] } ], "source": [ "def reverse_list(arr):\n", " left = 0\n", " right = len(arr)-1\n", " while (left < right):\n", " # Swap\n", " temp = arr[left]\n", " arr[left] = arr[right]\n", " arr[right] = temp\n", " left += 1\n", " right -= 1\n", " \n", " return arr\n", " \n", "arr = [1, 2, 3, 4, 5, 6, 7]\n", "print(reverse_list(arr))" ] }, { "cell_type": "code", "execution_count": 20, "id": "95a60408", "metadata": {}, "outputs": [], "source": [ "arr = [1, 2, 3, 4, 5, 6, 7]\n", "list1 = arr[::-1]" ] }, { "cell_type": "code", "execution_count": 21, "id": "de357230", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[7, 6, 5, 4, 3, 2, 1]\n" ] } ], "source": [ "print(list1)" ] }, { "cell_type": "markdown", "id": "ec0da00c", "metadata": {}, "source": [ "# Python program to find sum of elements in list\n" ] }, { "cell_type": "code", "execution_count": 22, "id": "11d375c9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sum of all elements in given list: 74\n" ] } ], "source": [ "total = 0\n", " \n", "list1 = [11, 5, 17, 18, 23]\n", " \n", "for ele in range(0, len(list1)):\n", " total = total + list1[ele]\n", " \n", "print(\"Sum of all elements in given list: \", total)" ] }, { "cell_type": "code", "execution_count": 23, "id": "9c8a9fac", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sum of all elements in given list: 74\n" ] } ], "source": [ "list1 = [11, 5, 17, 18, 23]\n", "\n", "def sumOfList(list, size):\n", " if (size == 0):\n", " return 0\n", " else:\n", " return list[size - 1] + sumOfList(list, size - 1)\n", " \n", " \n", "total = sumOfList(list1, len(list1))\n", " \n", "print(\"Sum of all elements in given list: \", total)" ] }, { "cell_type": "markdown", "id": "4657233a", "metadata": {}, "source": [ "# Python | Multiply all numbers in the list" ] }, { "cell_type": "code", "execution_count": 24, "id": "610bd835", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n", "24\n" ] } ], "source": [ "def multiplyList(myList):\n", " \n", " result = 1\n", " for x in myList:\n", " result = result * x\n", " return result\n", " \n", " \n", "# Driver code\n", "list1 = [1, 2, 3]\n", "list2 = [3, 2, 4]\n", "print(multiplyList(list1))\n", "print(multiplyList(list2))" ] }, { "cell_type": "code", "execution_count": 25, "id": "8071d70d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n", "24\n" ] } ], "source": [ "import math\n", "list1 = [1, 2, 3]\n", "list2 = [3, 2, 4]\n", " \n", " \n", "result1 = math.prod(list1)\n", "result2 = math.prod(list2)\n", "print(result1)\n", "print(result2)" ] }, { "cell_type": "markdown", "id": "b15b355d", "metadata": {}, "source": [ "# Python program to find smallest number in a list\n" ] }, { "cell_type": "code", "execution_count": 26, "id": "5caff6a9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Smallest element is: 4\n" ] } ], "source": [ "list1 = [10, 20, 4, 45, 99]\n", " \n", "list1.sort()\n", " \n", "print(\"Smallest element is:\", list1[0])" ] }, { "cell_type": "code", "execution_count": 27, "id": "46a14b6c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Smallest element is: 1\n" ] } ], "source": [ "list1 = [10, 20, 1, 45, 99]\n", "print(\"Smallest element is:\", min(list1))\n" ] }, { "cell_type": "markdown", "id": "b57b6e15", "metadata": {}, "source": [ "# Python program to find largest number in a list\n" ] }, { "cell_type": "code", "execution_count": 28, "id": "52f84894", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Largest element is: 99\n" ] } ], "source": [ "list1 = [10, 20, 4, 45, 99]\n", "list1.sort()\n", "print(\"Largest element is:\", list1[-1])" ] }, { "cell_type": "code", "execution_count": 29, "id": "0d27c4e1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Largest element is: 99\n" ] } ], "source": [ "list1 = [10, 20, 4, 45, 99]\n", "print(\"Largest element is:\", max(list1))" ] }, { "cell_type": "code", "execution_count": 6, "id": "7d608c5b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100\n" ] } ], "source": [ "lst = [20, 10, 20, 4, 100]\n", "print(max(lst, key=lambda value: int(value)) )" ] }, { "cell_type": "markdown", "id": "4818e012", "metadata": {}, "source": [ "# Python program to find second largest number in a list" ] }, { "cell_type": "code", "execution_count": 14, "id": "2a41b663", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Second largest element is: 45\n" ] } ], "source": [ "list1 = [10, 20, 20, 4, 45, 45, 45, 99, 99]\n", "list2 = list(set(list1))\n", "list2.sort()\n", "print(\"Second largest element is:\", list2[-2])" ] }, { "cell_type": "markdown", "id": "cbca5321", "metadata": {}, "source": [ "# Python program to print negative numbers in a list" ] }, { "cell_type": "code", "execution_count": 3, "id": "59b9fd99", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-21 -93 " ] } ], "source": [ "list1 = [11, -21, 0, 45, 66, -93]\n", "for num in list1:\n", " if num < 0:\n", " print(num, end=\" \")" ] }, { "cell_type": "code", "execution_count": 4, "id": "f6f8507f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-10, -21, -4, -66]\n" ] } ], "source": [ "list1 = [-10, -21, -4, 45, -66, 93]\n", "number = [num for num in list1 if num < 0]\n", "print(number)-" ] }, { "cell_type": "markdown", "id": "fc2bf939", "metadata": {}, "source": [ "# Python program to print all positive numbers in a range" ] }, { "cell_type": "code", "execution_count": 5, "id": "0715f4de", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 " ] } ], "source": [ "\n", "start, end = -4, 19\n", " \n", "for num in range(start, end + 1):\n", " \n", " if num >= 0:\n", " print(num, end=\" \")" ] }, { "cell_type": "markdown", "id": "95e6a6eb", "metadata": {}, "source": [ "# Matrix Programs:" ] }, { "cell_type": "markdown", "id": "6eaecc28", "metadata": {}, "source": [ "# Python program to add two Matrices\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "fc3e147a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[10, 10, 10]\n", "[10, 10, 10]\n", "[10, 10, 10]\n" ] } ], "source": [ "X = [[1,2,3],\n", " [4 ,5,6],\n", " [7 ,8,9]]\n", " \n", "Y = [[9,8,7],\n", " [6,5,4],\n", " [3,2,1]]\n", " \n", " \n", "result = [[0,0,0],\n", " [0,0,0],\n", " [0,0,0]]\n", " \n", "# iterate through rows\n", "for i in range(len(X)): \n", "# iterate through columns\n", " for j in range(len(X[0])):\n", " result[i][j] = X[i][j] + Y[i][j]\n", " \n", "for r in result:\n", " print(r)" ] }, { "cell_type": "markdown", "id": "bc68e05b", "metadata": {}, "source": [ "# Python program to multiply two matrices\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "eafb8321", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[114, 160, 60, 27]\n", "[74, 97, 73, 14]\n", "[119, 157, 112, 23]\n" ] } ], "source": [ "A = [[12, 7, 3],\n", " [4, 5, 6],\n", " [7, 8, 9]]\n", " \n", "# take a 3x4 matrix \n", "B = [[5, 8, 1, 2],\n", " [6, 7, 3, 0],\n", " [4, 5, 9, 1]]\n", " \n", "result = [[0, 0, 0, 0],\n", " [0, 0, 0, 0],\n", " [0, 0, 0, 0]]\n", " \n", "# iterating by row of A\n", "for i in range(len(A)):\n", " \n", " # iterating by column by B\n", " for j in range(len(B[0])):\n", " \n", " # iterating by rows of B\n", " for k in range(len(B)):\n", " result[i][j] += A[i][k] * B[k][j]\n", " \n", "for r in result:\n", " print(r)" ] }, { "cell_type": "markdown", "id": "50f1cf4f", "metadata": {}, "source": [ "# String Program" ] }, { "cell_type": "markdown", "id": "80a906bd", "metadata": {}, "source": [ "# Python program to check if a string is palindrome or not" ] }, { "cell_type": "code", "execution_count": 8, "id": "d1020844", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Yes\n" ] } ], "source": [ "\n", "def isPalindrome(s):\n", " return s == s[::-1]\n", " \n", " \n", "# Driver code\n", "s = \"malayalam\"\n", "ans = isPalindrome(s)\n", " \n", "if ans:\n", " print(\"Yes\")\n", "else:\n", " print(\"No\")" ] }, { "cell_type": "markdown", "id": "630f064b", "metadata": {}, "source": [ "# Reverse words in a given String in Python\n" ] }, { "cell_type": "code", "execution_count": 10, "id": "e7c8a149", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python a is This\n" ] } ], "source": [ "string = \"This is a Python\"\n", "s = string.split()[::-1]\n", "l = []\n", "for i in s:\n", " l.append(i)\n", "print(\" \".join(l))" ] }, { "cell_type": "markdown", "id": "4af8d334", "metadata": {}, "source": [ "# Dictionary Programs:" ] }, { "cell_type": "markdown", "id": "2efed14c", "metadata": {}, "source": [ "# Python – Extract Unique values dictionary values\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "24cca7d8", "metadata": {}, "outputs": [], "source": [ "# initializing dictionary\n", "test_dict = {'gfg': [5, 6, 7, 8],\n", " 'is': [10, 11, 7, 5],\n", " 'best': [6, 12, 10, 8],\n", " 'for': [1, 2, 5]}" ] }, { "cell_type": "code", "execution_count": 2, "id": "9e78e7cb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The original dictionary is : {'gfg': [5, 6, 7, 8], 'is': [10, 11, 7, 5], 'best': [6, 12, 10, 8], 'for': [1, 2, 5]}\n" ] } ], "source": [ "print(\"The original dictionary is : \" + str(test_dict))" ] }, { "cell_type": "code", "execution_count": 3, "id": "ec261694", "metadata": {}, "outputs": [], "source": [ "res = list(sorted({ele for val in test_dict.values() for ele in val}))\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "cf5e5c98", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The unique values list is : [1, 2, 5, 6, 7, 8, 10, 11, 12]\n" ] } ], "source": [ "print(\"The unique values list is : \" + str(res))" ] }, { "cell_type": "markdown", "id": "a23168cb", "metadata": {}, "source": [ "# Python program to find the sum of all items in a dictionary" ] }, { "cell_type": "code", "execution_count": 5, "id": "a1661798", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sum : 600\n" ] } ], "source": [ "def returnSum(myDict):\n", " \n", " list = []\n", " for i in myDict:\n", " list.append(myDict[i])\n", " final = sum(list)\n", " \n", " return final\n", " \n", " \n", "# Driver Function\n", "dict = {'a': 100, 'b': 200, 'c': 300}\n", "print(\"Sum :\", returnSum(dict))" ] }, { "cell_type": "code", "execution_count": 6, "id": "4ebeb32f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sum : 600\n" ] } ], "source": [ "def returnSum(dict):\n", " \n", " sum = 0\n", " for i in dict.values():\n", " sum = sum + i\n", " \n", " return sum\n", " \n", " \n", "# Driver Function\n", "dict = {'a': 100, 'b': 200, 'c': 300}\n", "print(\"Sum :\", returnSum(dict))" ] }, { "cell_type": "markdown", "id": "d5dba4cc", "metadata": {}, "source": [ "# Python | Ways to remove a key from dictionary\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "e14efc61", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The dictionary before performingremove is : {'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}\n", "The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}\n" ] } ], "source": [ "test_dict = {\"Arushi\": 22, \"Anuradha\": 21,\n", " \"Mani\": 21, \"Haritha\": 21}\n", " \n", "# Printing dictionary before removal\n", "print(\"The dictionary before performing\\\n", "remove is : \" + str(test_dict))\n", " \n", "# Using items() + dict comprehension to remove a dict. pair\n", "# removes Mani\n", "new_dict = {key: val for key,\n", " val in test_dict.items() if key != 'Mani'}\n", " \n", "# Printing dictionary after removal\n", "print(\"The dictionary after remove is : \" + str(new_dict))" ] }, { "cell_type": "code", "execution_count": 8, "id": "0292c74b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Current Time = 09:43:09.855923\n" ] } ], "source": [ "from datetime import datetime\n", "\n", "# Time object containing\n", "# the current time.\n", "time = datetime.now().time()\n", "\n", "print(\"Current Time =\", time)\n" ] }, { "cell_type": "code", "execution_count": 9, "id": "fb4c746d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Yesterday = 27-12-2022\n", "Today = 28-12-2022\n", "Tomorrow = 29-12-2022\n" ] } ], "source": [ "from datetime import datetime,timedelta\n", "\n", "\n", "# Get today's date\n", "presentday = datetime.now() # or presentday = datetime.today()\n", "\n", "# Get Yesterday\n", "yesterday = presentday - timedelta(1)\n", "\n", "# Get Tomorrow\n", "tomorrow = presentday + timedelta(1)\n", "\n", "\n", "# strftime() is to format date according to\n", "# the need by converting them to string\n", "print(\"Yesterday = \", yesterday.strftime('%d-%m-%Y'))\n", "print(\"Today = \", presentday.strftime('%d-%m-%Y'))\n", "print(\"Tomorrow = \", tomorrow.strftime('%d-%m-%Y'))\n" ] }, { "cell_type": "code", "execution_count": 11, "id": "2b4e10f0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Wednesday\n" ] } ], "source": [ "import datetime\n", "import calendar\n", " \n", "def findDay(date):\n", " born = datetime.datetime.strptime(date, '%d %m %Y').weekday()\n", " return (calendar.day_name[born])\n", " \n", "# Driver program\n", "date = '28 12 2022'\n", "print(findDay(date))" ] }, { "cell_type": "code", "execution_count": null, "id": "ac0196d6", "metadata": {}, "outputs": [], "source": [] } ], "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 }