{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"function2.ipynb","provenance":[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"xaYFgHYxOSpY","colab_type":"text"},"source":["# function2"]},{"cell_type":"markdown","metadata":{"id":"7oy6zkSbOUfU","colab_type":"text"},"source":[""]},{"cell_type":"markdown","metadata":{"id":"sGI5SV9oOY_-","colab_type":"text"},"source":["## [Python Program to Find Factors of Number](https://www.programiz.com/python-programming/examples/factor-number)\n","In this program, you'll learn to find the factors of a number using a for loop and display it.\n","\n","### Source Code\n"]},{"cell_type":"code","metadata":{"id":"6_J675qYOcUH","colab_type":"code","colab":{}},"source":["# Python Program to find the factors of a number\n","\n","# define a function\n","def print_factors(x):\n"," # This function takes a number and prints the factors\n","\n"," print(\"The factors of\",x,\"are:\")\n"," for i in range(1, x + 1):\n"," if x % i == 0:\n"," print(i)\n","\n","# change this value for a different result.\n","num = 320\n","\n","# uncomment the following line to take input from the user\n","#num = int(input(\"Enter a number: \"))\n","\n","print_factors(num)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"4CkKcKYxOeau","colab_type":"text"},"source":["Note: To test the program, change the value of num.\n","\n","In this program, the number whose factor is to be found is stored in num.\n","\n","hen we display its factors using the function print_factors(). In the function, we use a for loop to iterate from 1 to that number and only print it if, it perfectly divides our number. Here, print_factors() is a user-defined function.\n","\n","Visit here to learn more about [user-defined function in Python](https://www.programiz.com/python-programming/user-defined-function)."]},{"cell_type":"markdown","metadata":{"id":"Dn-EBDttOtS1","colab_type":"text"},"source":["## [Python Program to Make a Simple Calculator](https://www.programiz.com/python-programming/examples/calculator))\n","In this example you will learn to create a simple calculator that can add, subtract, multiply or divide depending upon the input from the user.\n","\n","### Simple Calculator by Making Functions"]},{"cell_type":"code","metadata":{"id":"ewDDXC9QO3Br","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"9R_u778PPCYd","colab_type":"code","colab":{}},"source":["# This function divides two numbers\n","def divide(x, y):\n"," return x / y\n","print(\"Select operation.\")\n","print(\"1.Add\")\n","print(\"2.Subtract\")\n","print(\"3.Multiply\")\n","print(\"4.Divide\")\n","# Take input from the user \n","choice = input(\"Enter choice(1/2/3/4):\")\n","num1 = int(input(\"Enter first number: \"))\n","num2 = int(input(\"Enter second number: \"))\n","if choice == '1':\n"," print(num1,\"+\",num2,\"=\", add(num1,num2))\n","elif choice == '2':\n"," print(num1,\"-\",num2,\"=\", subtract(num1,num2))\n","elif choice == '3':\n"," print(num1,\"*\",num2,\"=\", multiply(num1,num2))\n","elif choice == '4':\n"," print(num1,\"/\",num2,\"=\", divide(num1,num2))\n","else:\n"," print(\"Invalid input\")\n"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"-W5MFdslPD2c","colab_type":"text"},"source":["In this program, we ask the user to choose the desired operation. Options 1, 2, 3 and 4 are valid. Two numbers are taken and an if...elif...else branching is used to execute a particular section. User-defined functions add(), subtract(), multiply() and divide() evaluate respective operations."]},{"cell_type":"markdown","metadata":{"id":"6LxFkSxkPKBM","colab_type":"text"},"source":["## [Python Program to Shuffle Deck of Cards](https://www.programiz.com/python-programming/examples/shuffle-card)\n","In this program, you'll learn to shuffle a deck of cards using random module.\n","\n","### Source Code"]},{"cell_type":"code","metadata":{"id":"_172R8j0POjr","colab_type":"code","colab":{}},"source":["# Python program to shuffle a deck of card using the module random and draw 5 cards\n","\n","# import modules\n","import itertools, random\n","\n","# make a deck of cards\n","deck = list(itertools.product(range(1,14),['Spade','Heart','Diamond','Club']))\n","\n","# shuffle the cards\n","random.shuffle(deck)\n","\n","# draw five cards\n","print(\"You got:\")\n","for i in range(5):\n"," print(deck[i][0], \"of\", deck[i][1])"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"46fWpOGvPQTY","colab_type":"text"},"source":["Note: Run the program again to shuffle the cards.\n","\n","In program, we used the product() function in itertools module to create a deck of cards. This function performs the Cartesian product of the two sequence.\n","\n","The two sequence are, numbers from 1 to 13 and the four suits. So, altogether we have 13 * 4 = 52 items in the deck with each card as a tuple. For e.g.``` deck[0] = (1, 'Spade').```\n","\n","Our deck is ordered, so we shuffle it using the function shuffle() in random module.\n","\n","Finally, we draw the first five cards and display it to the user. We will get different output each time you run this program as shown in our two outputs.\n","\n","Here we have used the standard modules itertools and random that comes with Python.\n"]},{"cell_type":"markdown","metadata":{"id":"-0N5jIL6PdRW","colab_type":"text"},"source":["## [Python Program to Display Calendar](https://www.programiz.com/python-programming/examples/display-calendar)\n","Python has a built-in function, calendar to work with date related tasks. You will learn to display the calendar of a given date in this example.\n","\n","In the program below, we import the calendar module. The built-in function month() inside the module takes in the year and the month and displays the calendar for that month of the year.\n","\n","###Source Code"]},{"cell_type":"code","metadata":{"id":"Jxk7fFF3PiQ_","colab_type":"code","colab":{}},"source":["# Python program to display calendar of given month of the year\n","\n","# import module\n","import calendar\n","\n","yy = 2014\n","mm = 11\n","\n","# To ask month and year from the user\n","# yy = int(input(\"Enter year: \"))\n","# mm = int(input(\"Enter month: \"))\n","\n","# display the calendar\n","print(calendar.month(yy, mm))"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"GSwtH5qpPkUU","colab_type":"text"},"source":["You can change the value of variables yy and mm and run it to test this program for other dates."]},{"cell_type":"markdown","metadata":{"id":"1NPqxeqYPmL7","colab_type":"text"},"source":["## [Python Program to Display Fibonacci Sequence Using Recursion](https://www.programiz.com/python-programming/examples/fibonacci-recursion)\n","In this program, you'll learn to display Fibonacci sequence using a recursive function.\n","\n","A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8....\n","\n","The first two terms are 0 and 1. All other terms are obtained by adding the preceding two terms.This means to say the nth term is the sum of (n-1)th and (n-2)th term.\n","\n","### Source Code"]},{"cell_type":"code","metadata":{"id":"b1RlJRQvPrtk","colab_type":"code","colab":{}},"source":["# Python program to display the Fibonacci sequence up to n-th term using recursive functions\n","\n","def recur_fibo(n):\n"," \"\"\"Recursive function to\n"," print Fibonacci sequence\"\"\"\n"," if n <= 1:\n"," return n\n"," else:\n"," return(recur_fibo(n-1) + recur_fibo(n-2))\n","\n","# Change this value for a different result\n","nterms = 10\n","\n","# uncomment to take input from the user\n","#nterms = int(input(\"How many terms? \"))\n","\n","# check if the number of terms is valid\n","if nterms <= 0:\n"," print(\"Plese enter a positive integer\")\n","else:\n"," print(\"Fibonacci sequence:\")\n"," for i in range(nterms):\n"," print(recur_fibo(i))"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"j040xGRRPt-0","colab_type":"text"},"source":["Note: To test the program, change the value of nterms.\n","\n","\n","In this program, we store the number of terms to be displayed in nterms.\n","\n","A recursive function recur_fibo() is used to calculate the nth term of the sequence. We use a for loop to iterate and calculate each term recursively.\n","\n","Visit here to know more about [recursion in Python.](https://www.programiz.com/python-programming/recursion)"]},{"cell_type":"markdown","metadata":{"id":"jEwzDfGyPy8n","colab_type":"text"},"source":["## [Python Program to Find Sum of Natural Numbers Using Recursion](https://www.programiz.com/python-programming/examples/natural-number-recursion)\n","In this program, you'll learn to find the sum of natural numbers using recursive function.\n","\n","In the program below, we've used a recursive function recur_sum() to compute the sum up to the given number.\n","\n","###Source Code"]},{"cell_type":"code","metadata":{"id":"N37XmYuhP39u","colab_type":"code","colab":{}},"source":["# Python program to find the sum of natural numbers up to n using recursive function\n","\n","def recur_sum(n):\n"," \"\"\"Function to return the sum\n"," of natural numbers using recursion\"\"\"\n"," if n <= 1:\n"," return n\n"," else:\n"," return n + recur_sum(n-1)\n","\n","# change this value for a different result\n","num = 16\n","\n","# uncomment to take input from the user\n","#num = int(input(\"Enter a number: \"))\n","\n","if num < 0:\n"," print(\"Enter a positive number\")\n","else:\n"," print(\"The sum is\",recur_sum(num))"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"LwNlY4sXP52q","colab_type":"text"},"source":["Note: To test the program, change the value of num. Try negative numbers as well."]},{"cell_type":"markdown","metadata":{"id":"Y1-iN1B6P8EO","colab_type":"text"},"source":["## [Python Program to Find Factorial of Number Using Recursion](https://www.programiz.com/python-programming/examples/factorial-recursion)\n","In this program, you'll learn to find the factorial of a number using recursive function.\n","\n","The factorial of a number is the product of all the integers from 1 to that number.\n","\n","For example, the factorial of 6 (denoted as 6!) is 1*2*3*4*5*6 = 720. Factorial is not defined for negative numbers and the factorial of zero is one, 0! = 1.\n","\n","### Source Code\n"]},{"cell_type":"code","metadata":{"id":"1JlIe6hmQB0J","colab_type":"code","colab":{}},"source":["# Python program to find the factorial of a number using recursion\n","\n","def recur_factorial(n):\n"," \"\"\"Function to return the factorial\n"," of a number using recursion\"\"\"\n"," if n == 1:\n"," return n\n"," else:\n"," return n*recur_factorial(n-1)\n","\n","# Change this value for a different result\n","num = 7\n","\n","# uncomment to take input from the user\n","#num = int(input(\"Enter a number: \"))\n","\n","# check is the number is negative\n","if num < 0:\n"," print(\"Sorry, factorial does not exist for negative numbers\")\n","elif num == 0:\n"," print(\"The factorial of 0 is 1\")\n","else:\n"," print(\"The factorial of\",num,\"is\",recur_factorial(num))"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"l8tNq5OIQDZD","colab_type":"text"},"source":["Note: To test the program, change the value of num.\n","\n","Here, the number is stored in num and use recursive function recur_factorial() to compute the product up to that number."]},{"cell_type":"markdown","metadata":{"id":"tq0AwnfFQFWg","colab_type":"text"},"source":["## [Python Program to Convert Decimal to Binary Using Recursion](https://www.programiz.com/python-programming/examples/decimal-binary-recursion)\n","In this program, you will learn to convert decimal number to binary using recursive function.\n","\n","Decimal number is converted into binary by dividing the number successively by 2 and printing the remainder in reverse order.\n","\n","![alt text](https://cdn.programiz.com/sites/tutorial2program/files/decimal-to-binary-conversion_0.jpg)\n","\n","### Source Code\n"]},{"cell_type":"code","metadata":{"id":"jCULMW4zQO-K","colab_type":"code","colab":{}},"source":["def convertToBinary(n):\n"," # Function to print binary number for the input decimal using recursion\n"," if n > 1:\n"," convertToBinary(n//2)\n"," print(n % 2,end = '')\n","\n","# decimal number\n","dec = 34\n","\n","convertToBinary(dec)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"Khb2f_stQQhZ","colab_type":"text"},"source":["You can change the variable dec in the above program and run it to test out for other values.\n","\n","This program works only for whole numbers. It doesn't work for real numbers having fractional values such as: 25.5, 45.64 and so on. We encourage you to create Python program that converts decimal numbers to binary for all real numbers on your own. "]}]}