{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"function1.ipynb","provenance":[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"vVsM2n8JMbyU","colab_type":"text"},"source":["# function1"]},{"cell_type":"markdown","metadata":{"id":"hKLVh_SgMb_x","colab_type":"text"},"source":["## [Python Program To Display Powers of 2 Using Anonymous Function](https://www.programiz.com/python-programming/examples/power-anonymous)\n","In this program, you'll learn to display powers of the integer 2 using Python anonymous function.\n","\n","In the program below, we have used anonymous (lambda) function inside the map() built-in function to find the powers of 2.\n","###Source Code"]},{"cell_type":"code","metadata":{"id":"QiJdW7a5MnnE","colab_type":"code","colab":{}},"source":["# Python Program to display the powers of 2 using anonymous function\n","\n","# Change this value for a different result\n","terms = 10\n","\n","# Uncomment to take number of terms from user\n","#terms = int(input(\"How many terms? \"))\n","\n","# use anonymous function\n","result = list(map(lambda x: 2 ** x, range(terms)))\n","\n","# display the result\n","\n","print(\"The total terms is:\",terms)\n","for i in range(terms):\n"," print(\"2 raised to power\",i,\"is\",result[i])"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"B8mdH3ANMrLe","colab_type":"text"},"source":["Note: To test this program, change the value of terms."]},{"cell_type":"markdown","metadata":{"id":"Sf_VTz3kMwuj","colab_type":"text"},"source":["## [Python Program to Find Numbers Divisible by Another Number](https://www.programiz.com/python-programming/examples/number-divisible)\n","In this program, you'll learn to find the numbers divisible by another number and display it.\n","\n","In the program below, we have used anonymous (lambda) function inside the filter() built-in function to find all the numbers divisible by 13 in the list.\n","\n","## Source Code"]},{"cell_type":"code","metadata":{"id":"eOtbIQo-M0hR","colab_type":"code","colab":{}},"source":["# Python Program to find numbers divisible by thirteen from a list using anonymous function\n","\n","# Take a list of numbers\n","my_list = [12, 65, 54, 39, 102, 339, 221,]\n","\n","# use anonymous function to filter\n","result = list(filter(lambda x: (x % 13 == 0), my_list))\n","\n","# display the result\n","print(\"Numbers divisible by 13 are\",result)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"oMMSMWqnM6pK","colab_type":"text"},"source":["## [Python Program to Convert Decimal to Binary, Octal and Hexadecimal](https://www.programiz.com/python-programming/examples/conversion-binary-octal-hexadecimal)\n","In this program, you'll learn to convert decimal to binary, octal and hexadecimal, and display it.\n","\n","Decimal system is the most widely used number system. But computer only understands binary. Binary, octal and hexadecimal number systems are closely related and we may require to convert decimal into these systems. Decimal system is base 10 (ten symbols, 0-9, are used to represent a number) and similarly, binary is base 2, octal is base 8 and hexadecimal is base 16.\n","\n","A number with the prefix '0b' is considered binary, '0o' is considered octal and '0x' as hexadecimal. For example:\n","\n","\n","\n","```\n","60 = 0b11100 = 0o74 = 0x3c\n","```\n","\n","\n","### Source Code"]},{"cell_type":"code","metadata":{"id":"I13MD-kcNBRp","colab_type":"code","colab":{}},"source":["# Python program to convert decimal number into binary, octal and hexadecimal number system\n","\n","# Change this line for a different result\n","dec = 344\n","\n","print(\"The decimal value of\",dec,\"is:\")\n","print(bin(dec),\"in binary.\")\n","print(oct(dec),\"in octal.\")\n","print(hex(dec),\"in hexadecimal.\")"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"zZx5agG9NC03","colab_type":"text"},"source":["Note: To test the program, change the value of dec in the program.\n","\n","In this program, we have used built-in functions bin(), oct() and hex() to convert the given decimal number into respective number systems.\n","\n","These functions take an integer (in decimal) and return a string."]},{"cell_type":"markdown","metadata":{"id":"R-BW0BC4NFjo","colab_type":"text"},"source":["##P[ython Program to Find ASCII Value of Character](https://www.programiz.com/python-programming/examples/ascii-character)\n","In this program, you'll learn to find the ASCII value of a character and display it.\n","\n","ASCII stands for American Standard Code for Information Interchange.\n","\n","It is a numeric value given to different characters and symbols, for computers to store and manipulate. For example: ASCII value of the letter 'A' is 65.\n","\n","###Source Code"]},{"cell_type":"code","metadata":{"id":"ko-FKqbUNKPm","colab_type":"code","colab":{}},"source":["# Program to find the ASCII value of the given character\n","\n","# Change this value for a different result\n","c = 'p'\n","\n","# Uncomment to take character from user\n","#c = input(\"Enter a character: \")\n","\n","print(\"The ASCII value of '\" + c + \"' is\",ord(c))"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"DamuNTixNM54","colab_type":"text"},"source":["Note: To test the program, change the value of c.\n","\n","\n","Here we have used ord() function to convert a character to an integer (ASCII value). This function actually returns the Unicode code point of that character.\n","\n","Unicode is also an encoding technique that provides a unique number to a character. While ASCII only encodes 128 characters, current Unicode has more than 100,000 characters from hundreds of scripts.\n","\n","Your turn: Modify the code above to get character from the ASCII value using the chr() function as shown below.\n","\n","```\n",">>> chr(65)\n","'A'\n",">>> chr(120)\n","'x'\n",">>> chr(ord('S') + 1)\n","'T'\n","```\n","\n","Here, ```ord()``` and ```chr()``` are built-in functions. Visit here to know more about [built-in functions in Python](https://www.programiz.com/python-programming/built-in-function)."]},{"cell_type":"markdown","metadata":{"id":"7IyO1m3yNcgR","colab_type":"text"},"source":["##[Python Program to Find HCF or GCD](https://www.programiz.com/python-programming/examples/hcf)\n","In this example, you will learn to find the GCD of two numbers using two different methods: function and loops and, Euclidean algorithm\n","\n","The highest common factor (H.C.F) or greatest common divisor (G.C.D) of two numbers is the largest positive integer that perfectly divides the two given numbers. For example, the H.C.F of 12 and 14 is 2.\n","\n","###Source Code: Using Loops"]},{"cell_type":"code","metadata":{"id":"rhomM-hZNhtA","colab_type":"code","colab":{}},"source":["# Python program to find the H.C.F of two input number\n","\n","# define a function\n","def computeHCF(x, y):\n","\n","# choose the smaller number\n"," if x > y:\n"," smaller = y\n"," else:\n"," smaller = x\n"," for i in range(1, smaller+1):\n"," if((x % i == 0) and (y % i == 0)):\n"," hcf = i\n"," \n"," return hcf\n","\n","num1 = 54 \n","num2 = 24\n","\n","# take input from the user\n","# num1 = int(input(\"Enter first number: \"))\n","# num2 = int(input(\"Enter second number: \"))\n","\n","print(\"The H.C.F. of\", num1,\"and\", num2,\"is\", computeHCF(num1, num2))"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"jFHo-4TiNjbS","colab_type":"text"},"source":["Here, two integers stored in variables num1 and num2 are passed to a function which returns the H.C.F.\n","\n","In the function, we first determine the smaller of the two number since the H.C.F can only be less than or equal to the smallest number. We then use a for loop to go from 1 to that number.\n","\n","In each iteration, we check if our number perfectly divides both the input numbers. If so, we store the number as H.C.F. At the completion of the loop we end up with the largest number that perfectly divides both the numbers.\n","\n","The above method is easy to understand and implement but not efficient. A much more efficient method to find the H.C.F. is the Euclidean algorithm.\n","\n","### Euclidean algorithm\n","\n","This algorithm is based on the fact that H.C.F. of two numbers divides their difference as well.\n","\n","In this algorithm, we divide the greater by smaller and take the remainder. Now, divide the smaller by this remainder. Repeat until the remainder is 0.\n","\n","For example, if we want to find the H.C.F. of 54 and 24, we divide 54 by 24. The remainder is 6. Now, we divide 24 by 6 and the remainder is 0. Hence, 6 is the required H.C.F.\n","\n","###Source Code: Using Euclidean Algorithm"]},{"cell_type":"code","metadata":{"id":"QG1KXw43Nnyp","colab_type":"code","colab":{}},"source":["def computeHCF(x, y):\n","\n"," # This function implements the Euclidian algorithm to find H.C.F. of two numbers\n"," while(y):\n"," x, y = y, x % y\n","\n"," return x\n","\n","computeHCF(300, 400)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"SDwOsVGSNqF5","colab_type":"text"},"source":["Here we loop until y becomes zero. The statement x, y = y, x % y does swapping of values in Python. Click here to learn more about swapping variables in Python.\n","\n","In each iteration, we place the value of y in x and the remainder (x % y) in y, simultaneously. When y becomes zero, we have H.C.F. in x."]},{"cell_type":"markdown","metadata":{"id":"EMGGVjZ1NvQd","colab_type":"text"},"source":["##[Python Program to Find LCM](https://www.programiz.com/python-programming/examples/lcm)\n","In this program, you'll learn to find the LCM of two numbers and display it.\n","\n","The least common multiple (L.C.M.) of two numbers is the smallest positive integer that is perfectly divisible by the two given numbers.\n","\n","For example, the L.C.M. of 12 and 14 is 84.\n","\n","### Source Code: Without using GCD function\n"]},{"cell_type":"code","metadata":{"id":"qLXHAJvCN2tj","colab_type":"code","colab":{}},"source":["# Python Program to find the L.C.M. of two input number\n","\n","# define a function\n","def lcm(x, y):\n"," \"\"\"This function takes two\n"," integers and returns the L.C.M.\"\"\"\n","\n"," # choose the greater number\n"," if x > y:\n"," greater = x\n"," else:\n"," greater = y\n","\n"," while(True):\n"," if((greater % x == 0) and (greater % y == 0)):\n"," lcm = greater\n"," break\n"," greater += 1\n","\n"," return lcm\n","\n","# change the values of num1 and num2 for a different result\n","num1 = 54\n","num2 = 24\n","\n","# uncomment the following lines to take input from the user\n","#num1 = int(input(\"Enter first number: \"))\n","#num2 = int(input(\"Enter second number: \"))\n","\n","print(\"The L.C.M. of\", num1,\"and\", num2,\"is\", lcm(num1, num2))"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"AWLyC2bcNyqP","colab_type":"text"},"source":["Note: To test this program, change the values of num1 and num2.\n","\n","This program stores two number in num1 and num2 respectively, and passes them to a function which returns the L.C.M.\n","\n","In the function, we first determine the greater of the two number since the L.C.M. can only be greater than or equal to the largest number. We then use an infinite while loop to go from that number and beyond.\n","\n","In each iteration, we check if both the input numbers perfectly divides our number. If so, we store the number as L.C.M. and break from the loop. Otherwise, the number is incremented by 1 and the loop continues.\n","\n","The above program is slower to run. We can make it more efficient by using the fact that the product of two numbers is equal to the product of least common multiple and greatest common divisor of those two numbers.\n","\n","```\n","Number1 * Number2 = L.C.M. * G.C.D.\n","\n","```\n","Here is a Python program to implement this.\n","### Source Code: Using GCD function\n"]},{"cell_type":"code","metadata":{"id":"Sw5Xpi_YN_v4","colab_type":"code","colab":{}},"source":["# Python program to find the L.C.M. of two input number\n","\n","# define gcd function\n","def gcd(x, y):\n"," \"\"\"This function implements the Euclidian algorithm\n"," to find G.C.D. of two numbers\"\"\"\n","\n"," while(y):\n"," x, y = y, x % y\n","\n"," return x\n","\n","# define lcm function\n","def lcm(x, y):\n"," \"\"\"This function takes two\n"," integers and returns the L.C.M.\"\"\"\n","\n"," lcm = (x*y)//gcd(x,y)\n"," return lcm\n","\n","# change the values of num1 and num2 for a different result\n","num1 = 54\n","num2 = 24 \n","\n","# uncomment the following lines to take input from the user\n","#num1 = int(input(\"Enter first number: \"))\n","#num2 = int(input(\"Enter second number: \"))\n","\n","print(\"The L.C.M. of\", num1,\"and\", num2,\"is\", lcm(num1, num2))"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"oVQS_ffZOBX5","colab_type":"text"},"source":["Note: To test this program, change the values of num1 and num2.\n","\n","The output of this program is same as before. We have two functions gcd() and lcm(). We require G.C.D. of the numbers to calculate its L.C.M.\n","\n","So, lcm() calls the function gcd() to accomplish this. G.C.D. of two numbers can be calculated efficiently using the Euclidean algorithm.\n","\n","Click here to learn more about methods to [calculate G.C.D in Python](https://www.programiz.com/python-programming/examples/hcf)."]}]}