{"id":23632,"date":"2023-01-06T13:47:47","date_gmt":"2023-01-06T08:17:47","guid":{"rendered":"https:\/\/copyassignment.com\/?p=23632"},"modified":"2023-01-06T13:48:08","modified_gmt":"2023-01-06T08:18:08","slug":"python-programming-examples-fundamental-programs-in-python","status":"publish","type":"post","link":"https:\/\/copyassignment.com\/python-programming-examples-fundamental-programs-in-python\/","title":{"rendered":"Python Programming Examples | Fundamental Programs in Python"},"content":{"rendered":"\n<p>Python is a popular, widely-used programming language and easy to learn for beginners because of its short syntax and readability, and learning to code in python creates a lot of opportunities in today&#8217;s time. Practicing Programs in Python can help you become more familiar with the language and its syntax, which will make it easier for you to write and understand code in the future.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">60 fundamental Programs in Python<\/h2>\n\n\n\n<p>In this article, we will see 60 fundamental Programs in <a href=\"https:\/\/www.python.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python<\/a> i.e. Python programming examples that you must learn and solve as they will help you improve your problem-solving and critical thinking skills, and give you an idea of how you must break down complex problems into smaller, more manageable pieces and then find a solution for each piece.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Python program to find the sum of elements<\/h3>\n\n\n\n<p>A list in python is like an array in any other programming language. We will write a Python program to find the sum of all elements of a list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def arraySum(arr):\n    sum = 0\n    for i in arr:\n        sum += i\n    return sum\n\nprint(arraySum(&#91;1, 2, 3, 4, 5]))<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">15<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Python program to check whether a number is even or odd<\/h3>\n\n\n\n<p>Even numbers are divisible by 2 and odd numbers are not divisible by 2. We will use a modulo operator to check if a number is completely divisible by 2 or not.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def is_even(n):\n    if n%2==0:\n        return True\n    else:\n        return False\n\nprint(is_even(3))<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">False, True<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. Python program to find the average of all elements<\/h3>\n\n\n\n<p>Take a list and print the average of all the elements in it. The average of numbers is calculated by adding all the numbers and then dividing by the count of those numbers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def average(arr):\n    sum = 0\n    for i in arr:\n        sum += i\n    return sum \/ len(arr)\nprint(average(&#91;1, 2, 3, 4, 5]))<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">3.0<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. Python program to count the number of Vowels in a String<\/h3>\n\n\n\n<p>Vowels are &#8220;a, e, i, o, u&#8221;. We will write a python program to check the number of vowels present in a string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def countVowels(str):\n    vowels = &#91;'a', 'e', 'i', 'o', 'u']\n    count = 0\n    for i in str:\n        if i in vowels:\n            count += 1\n    return count\n \nprint('number of vowels in hello is '+str(countVowels('hello')))<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">number of vowels in hello is 2<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. Python Program to check if a string is a palindrome<\/h3>\n\n\n\n<p>A palindrome is a word that reads the same forward and backward. For example &#8211; \u201cLevel\u201d, \u201cNun\u201d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_str = 'malayalam'\nprint(my_str==my_str&#91;::-1])<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">True<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. Python Program to reverse a list<\/h3>\n\n\n\n<p>We will write a program to reverse the order of a list in python using the two-pointer approach.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def reverse_list(arr):\n    left = 0\n    right = len(arr)-1\n    while (left &lt; right):\n        temp = arr&#91;left]\n        arr&#91;left] = arr&#91;right]\n        arr&#91;right] = temp\n        left += 1\n        right -= 1\n    return arr\narr = &#91;1, 2, 3, 4, 5, 6, 7]\nprint(reverse_list(arr))\n# another and short approach\nprint(arr&#91;::-1])<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[7, 6, 5, 4, 3, 2, 1]<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. Python program to find the factorial of a number<\/h3>\n\n\n\n<p>The factorial of a number is the product of all the positive integers from 1 up to that number. For example, the factorial of 5 is 5! = 5*4*3*2*1 = 120<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def factorial(num):\n    if num == 1:\n        return 1\n    else:\n        return num * factorial(num - 1)\nprint(factorial(5))<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">120<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">8. Python program to concatenate all the words in a list<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>arr = &#91;'hello', 'world']\nst = ''\nfor i in arr:\n  st += i\nprint(st)<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">helloworld<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">9. Python program to remove all the vowels in a string<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>s = 'hello' \nvowels = &#91;'a', 'e', 'i', 'o', 'u']\nnewStr = ''\nfor i in s:\n  if i not in vowels:\n    newStr += i\nprint(newStr)<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">hll<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">10. Armstrong Number Program In Python<\/h3>\n\n\n\n<p>An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>n = 371\nnum_digits = len(str(n))\nsum = 0\nfor digit in str(n):\n  sum += int(digit)**num_digits\nprint(sum == n)<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">True<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">11. &nbsp;Python Program to remove duplicates from a list<\/h3>\n\n\n\n<p>A set is a data structure that only contains unique elements. We will use a set in python to remove all the duplicate items.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;1, 2, 3, 2, 1]\nnew_list = list(set(my_list))\nprint(new_list)<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[1, 2, 3]<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">12. Fibonacci Series&nbsp;program<\/h3>\n\n\n\n<p>A Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding numbers. For example, the first few numbers in the Fibonacci sequence are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, etc.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>n = 10\na, b = 0, 1\nprint(a)\nprint(b)\nfor i in range(2, n):\n  c = a + b\n  print(c)\n  a, b = b, c<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">0 1 1 2 3 5 8 13 21 34<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">13. Remove all odd elements in a list<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>arr = &#91;1, 2, 3, 4, 5]\nnewArr = &#91;]\nfor i in arr:\n  if i % 2 == 0:\n    newArr.append(i)\nprint(newArr)<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[2, 4]<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">14. Leap Year Program in Python<\/h3>\n\n\n\n<p>A leap year is a year that has an extra day in it (February 29th). Leap year has 366 days.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>year = 2000\nif year % 400 == 0:\n  print('leap year')\nelif year % 4 == 0:\n  print('leap year')\nelif year % 100 == 0:\n  print('not a leap year')\nelse:\n  print('not a leap year')<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">leap year<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">15. Prime Number Program in Python<\/h3>\n\n\n\n<p>A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. The first few prime numbers are 2, 3, 5, 7, and 11.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>n = 7\nis_prime = True\nif n &lt; 2:\n  is_prime = False\nelse:\n  for i in range(2, n):\n    if n % i == 0:\n      is_prime = False\n      break\nif is_prime:\n  print(\"Prime\")\nelse:\n  print(\"Not prime\")<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Prime<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">16. LCM program in python<\/h3>\n\n\n\n<p>The least common multiple (LCM) of two or more numbers is the smallest number which is a multiple of all of the numbers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 6\nb = 9\nlcm = a\nwhile lcm % b != 0:\n  lcm += a\nprint(f\"LCM of {a} and {b} is {lcm}.\")<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">LCM of 6 and 9 is 18.<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">17. HCF program in python<\/h3>\n\n\n\n<p>The highest common factor (HCF) of two or more numbers is the largest number that divides all of the numbers exactly. For example, the HCF of 15 and 20 is 5, because 5 is the largest number that divides both 15 and 20 exactly.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 15\nb = 20\nif a &gt; b:\n  smaller = b\nelse:\n  smaller = a\nfor i in range(1, smaller+1):\n  if (a % i == 0) and (b % i == 0):\n    hcf = i\nprint(f\"HCF of {a} and {b} is {hcf}\")<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">HCF of 15 and 20 is 5<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">18. Python program to swap two numbers<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>first = 27\nsecond = 4\ntemp = first\nfirst = second\nsecond = temp\nprint(f\"After swapping, the first number is {first} and the second number is {second}\")<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">After swapping, the first number is 4 and the second number is 27.<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">19. Python program to print all prime numbers up to n<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>n = 10\nfor i in range(2, n+1):\n  is_prime = True\n  for j in range(2, i):\n    if i % j == 0:\n      is_prime = False\n      break\n  if is_prime:\n    print(i)<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">2 3 5 7<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">20. Python program to find the greatest of three numbers<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>num1 = 10\nnum2 = 14\nnum3 = 12\nif (num1 &gt;= num2) and (num1 &gt;= num3):\n   largest = num1\nelif (num2 &gt;= num1) and (num2 &gt;= num3):\n   largest = num2\nelse:\n   largest = num3\nprint(largest)<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">14<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">21. Multiplication table program<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>num = 19\nfor i in range(1, 11):\n   print(num*i)<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">19 38 57 76 95 114 133 152 171 190<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">22. Python program to sort an array(list)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>numbers = &#91;2, 3, 4, 1]\nnumbers.sort()\nprint(numbers)<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[1, 2, 3, 4]<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">23. Python program to convert all characters to uppercase<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>s = 'follow us'\nnewStr = ''\nfor i in s:\n  newStr += i.upper()\nprint(newStr)<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">FOLLOW US<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">24. Python program to capitalize the first letter of each word<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>s = 'copy assignment'\nnewStr = ''\nfor i in range(len(s)):\n  if i == 0 or s&#91;i - 1] == ' ':\n    newStr += s&#91;i].upper()\n  else:\n    newStr += s&#91;i]\nprint(newStr)<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Copy Assignment<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">25. Python program to convert all characters to lowercase<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>s = 'COPYASSIGNMENT'\nnewStr = ''\nfor i in s:\n  newStr += i.lower()\nprint(newStr)<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">copyassignment<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">26. Python program to reverse a string<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>s = 'tnemngissAypoC'\nnewStr = \"\"\nfor i in s:\n  newStr = i + newStr\nprint(newStr)<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CopyAssignment<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">27. Calculator Program in Python<\/h3>\n\n\n\n<p>We will write a basic calculator program that can add, subtract, multiply and divide.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add(a,b):\n     return a+b\ndef sub(a,b):\n     return a-b\ndef multi(a,b):\n     return a * b\ndef div(a,b):\n     return a \/ b\nprint(add(10,15))\nprint(sub(13,10))<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">25 3<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">28. Python program for Pyramid pattern<\/h3>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">n=5\nfor i in range(0, n):\n  for j in range(0, i+1):\n    print(\"* \",end=\"\")\n  print(\"\\r\")<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"151\" height=\"157\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/pyramid-pattern.jpg\" alt=\"right side half pyramid\" class=\"wp-image-23673 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 151px; --smush-placeholder-aspect-ratio: 151\/157;\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">29. Reverse Pyramid pattern<\/h3>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">n=5\nk = 2*n - 2\nfor i in range(0, n):\n  for j in range(0, k):\n      print(end=\" \")\n  k = k - 2\n  for j in range(0, i+1):\n    print(\"* \", end=\"\")\n  print(\"\\r\")<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"153\" height=\"148\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/reverse_pyramid.jpg\" alt=\"left side half pyramid\" class=\"wp-image-23674 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 153px; --smush-placeholder-aspect-ratio: 153\/148;\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">30. Continuous Character pattern program in Python<\/h3>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">n = 5\nnum = 65\nfor i in range(0, n):\n  for j in range(0, i+1):\n    ch = chr(num)\n    print(ch, end=\" \")\n    num = num + 1\n  print(\"\\r\")<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"126\" height=\"153\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/char-pattern-1.jpg\" alt=\"continuous characters\" class=\"wp-image-23669 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 126px; --smush-placeholder-aspect-ratio: 126\/153;\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">31. Python program to print Triangle<\/h3>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">n=5\nk = n - 1\nfor i in range(0, n):\n  for j in range(0, k):\n      print(end=\" \")\n  k = k - 1\n  for j in range(0, i+1):\n    print(\"* \", end=\"\")\n  print(\"\\r\")<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"156\" height=\"151\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/triangle-pattern.jpg\" alt=\"triangle\" class=\"wp-image-23671 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 156px; --smush-placeholder-aspect-ratio: 156\/151;\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">32. Reverse Triangle program<\/h3>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">rows = 4\nk = 2 * rows - 2  \nfor i in range(rows, -1, -1):  \n  for j in range(k, 0, -1):  \n      print(end=\" \")  \n  k = k + 1  \n  for j in range(0, i + 1):  \n      print(\"*\", end=\" \")  \n  print(\"\")  <\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"166\" height=\"154\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/reverse_triangle-1.jpg\" alt=\"reverse triangle\" class=\"wp-image-23678 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 166px; --smush-placeholder-aspect-ratio: 166\/154;\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">33. Python program to print a Diamond shape<\/h3>\n\n\n\n<p>To print a diamond shape, first, we will print the upper half of triangle and then the lower half triangle.<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">n = 4\nk = 2 * n - 2  \nfor i in range(0, n):  \n  for j in range(0, k):  \n      print(end=\" \")  \n  k = k - 1  \n  for j in range(0, i + 1):  \n      print(\"* \", end=\"\")  \n  print(\"\")  \nk = n - 2  \nfor i in range(n, -1, -1):  \n  for j in range(k, 0, -1):  \n      print(end=\" \")  \n  k = k + 1  \n  for j in range(0, i + 1):  \n      print(\"* \", end=\"\")  \n  print(\"\")  <\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/diamond_shape.jpg\" alt=\"diamond shape\" class=\"wp-image-23679 lazyload\" width=\"165\" height=\"253\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 165px; --smush-placeholder-aspect-ratio: 165\/253;\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">34. Print Hourglass Pattern program<\/h3>\n\n\n\n<p>To print the hourglass shape, first, we will print the downward triangle and then the upward triangle.<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">row = 5\nfor i in range(row, 0, -1):\n  for j in range(row-i):\n    print(\" \", end=\"\")\n  for j in range(1, 2*i):\n    print(\"*\", end=\"\")\n  print()\nfor i in range(2, row+1):\n  for j in range(row-i):\n    print(\" \", end=\"\")\n  for j in range(1, 2*i):\n    print(\"*\", end=\"\")\n  print()<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"132\" height=\"256\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/horglass_shape.jpg\" alt=\"hourglass pattern\" class=\"wp-image-23684 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 132px; --smush-placeholder-aspect-ratio: 132\/256;\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">35. Print Butterfly Pattern program<\/h3>\n\n\n\n<p>To print the Butterfly pattern, we will follow the same approach of printing the upper half first and then the lower half.<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">row = 8\nn = row\/\/2\nfor i in range(1,n+1):\n  for j in range(1, 2*n+1):\n    if j>i and j&lt; 2*n+1-i:\n      print(\"  \", end=\"\")\n    else:\n      print(\"* \", end=\"\")\n  print()\nfor i in range(n,0,-1):\n  for j in range(2*n,0,-1):\n    if j>i and j&lt; 2*n+1-i:\n      print(\"  \", end=\"\")\n    else:\n      print(\"* \", end=\"\")\n  print()   <\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"207\" height=\"232\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/butterfly_shape.jpg\" alt=\"butterfly pattern\" class=\"wp-image-23686 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 207px; --smush-placeholder-aspect-ratio: 207\/232;\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">36. Print Christmas Tree Pattern using python program<\/h3>\n\n\n\n<p>To print a Christmas tree pattern, we can divide it into three parts. First is the upper triangle, second is the mid triangle and third is a vertical rectangle.<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">n = 5\n#upper triangle\nfor i in range(n):\n  for j in range(n-i):\n    print(' ', end=' ')\n  for k in range(2*i+1):\n    print('*',end=' ')\n  print()\n#mid triangle\nfor i in range(n):\n  for j in range(n-i):\n    print(' ', end=' ')\n  for k in range(2*i+1):\n    print('*',end=' ')\n  print()\n#pole shape\nfor i in range(n):\n  for j in range(n-1):\n    print(' ', end=' ')\n  print('* * *')<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/christmas_tree_shape.jpg\" alt=\"Christmas tree pattern\" class=\"wp-image-23688 lazyload\" width=\"185\" height=\"299\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/christmas_tree_shape.jpg 247w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/christmas_tree_shape-186x300.jpg 186w\" data-sizes=\"(max-width: 185px) 100vw, 185px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 185px; --smush-placeholder-aspect-ratio: 185\/299;\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">37. Python program to Reverse words in a String<\/h3>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">string = \"more for copyassignment visit\"\ns = string.split()[::-1]\nl = []\nfor i in s:\n  l.append(i)\nprint(\" \".join(l))<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">visit copyassignment for more<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">38. Python program to Check if Two Strings are Anagram<\/h3>\n\n\n\n<p>An anagram is a word formed by rearranging the letters of one word. For example &#8220;listen&#8221; and &#8220;silent&#8221; are anagrams since they contain the same letters but are arranged differently.<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">s1='listen'\ns2='silent'\nif(sorted(s1)== sorted(s2)):\n  print(\"The strings are anagrams\")\nelse:\n  print(\"The strings aren't anagrams\")    <\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">The strings are anagrams<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">39. Python program to print number divisible by 3 or 5 in a given range<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>n = 30\nfor i in range(1, n):\n    if i % 3 == 0 or i % 5 == 0:\n        print(i)<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">3 5 6 9 10 12 15 18 20 21 24 25 27<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">40. Python program to sort all words alphabetically<\/h3>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">arr = ['hello', 'world', 'its', 'copyassignment', 'website']\nfor i in range(len(arr)):\n  for j in range(i + 1, len(arr)):\n    if arr[i] > arr[j]:\n      arr[i], arr[j] = arr[j], arr[i]\nprint(arr)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">['copyassignment', 'hello', 'its', 'website', 'world']<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">41. Python program for two sum problem<\/h3>\n\n\n\n<p>Given a list of numbers, we have to find two numbers whose sum is equal to the given target<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">def twoSum(num_arr, pair_sum):\n  for i in range(len(num_arr) - 1):\n    for j in range(i + 1, len(num_arr)):\n      if num_arr[i] + num_arr[j] == pair_sum:\n        print(\"Pair with sum\", pair_sum,\"is: \", num_arr[i],\",\",num_arr[j])\nnum_arr = [3, 5, 2, 4, 8, 1]\npair_sum = 4\ntwoSum(num_arr, pair_sum)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Pair with sum 4 is: 3 , 1<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">42. Python program for Perfect number<\/h3>\n\n\n\n<p>A perfect number is a positive integer that is equal to the sum of its proper positive divisors excluding the number itself. For example, the proper positive divisors of 6 are 1, 2, and 3, which add up to 6. So 6 is a perfect number.<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">n = 6\nsum1 = 0\nfor i in range(1, n):\n    if(n % i == 0):\n        sum1 = sum1 + i\nif (sum1 == n):\n    print(\"Perfect number\")\nelse:\n    print(\"Not a Perfect number\")<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Perfect number<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">43. Python program for Strong Number<\/h3>\n\n\n\n<p>A strong number is a number in which the sum of the factorials of its digits is equal to the number itself. For example, 145 is a strong number because 1! + 4! + 5! = 1 + 24 + 120 = 145.<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">sum=0\nnum=145\ntemp=num\nwhile(num):\n    i=1\n    fact=1\n    r=num%10\n    while(i&lt;=r):\n        fact=fact*i\n        i=i+1\n    sum=sum+fact\n    num=num\/\/10\nif(sum==temp):\n    print(\"Strong number\")\nelse:\n    print(\"Not a Strong number\")<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Strong number<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">44. Python program to find Prime Factors of a number<\/h3>\n\n\n\n<p>The prime factors of a number are the prime numbers that divide the number completely. For example, the prime factors of 15 are 3 and 5, because 3 and 5 are prime numbers, and 3 x 5 = 15.<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">n=15\ni=1\nprint(\"Factors:\")\nwhile(i&lt;=n):\n    k=0\n    if(n%i==0):\n        j=1\n        while(j&lt;=i):\n            if(i%j==0):\n                k=k+1\n            j=j+1\n        if(k==2):\n            print(i)\n    i=i+1<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Factors: 3 5<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">45. Program to reverse a number in Python<\/h3>\n\n\n\n<p>To reverse a number we follow these steps.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Obtain the last digit of the number by using the modulus(%) operator.<\/li>\n\n\n\n<li>Store this last digit in a new variable at the one&#8217;s place.<\/li>\n\n\n\n<li>Remove the last digit from the current number by diving(\/) it with 10.<\/li>\n\n\n\n<li>Repeat the above steps till the current number becomes empty.<\/li>\n<\/ul>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">n= 123456\nreversed=0\nwhile(n>0):\n    dig=n%10\n    reversed=reversed*10+dig\n    n=n\/\/10\nprint(\"Reversed number is:\",reversed)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Reversed number is: 654321<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">46. Python program to find the Sum of all digits in a number<\/h3>\n\n\n\n<p>To sum all the digits of a number we follow the same steps as reversing a number with a minor change. Instead of storing the resultant number, we keep on adding these numbers to the sum variable.<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">n=12345\ntotal=0\nwhile(n>0):\n    lastDigit=n%10\n    total=total+lastDigit\n    n=n\/\/10\nprint(\"Total sum of digits in number:\",total)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Total sum of digits in number: 15<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">47. Python program to check Amicable numbers or not<\/h3>\n\n\n\n<p>Two numbers are amicable numbers if the sum of the divisors of the first number is equal to the second number, and the sum of the divisors of the second number is equal to the first number. For example, 220 and 284 are Amicable numbers.<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">a=220\nb=284\nsum1=0\nsum2=0\nfor i in range(1,a):\n    if a%i==0:\n        sum1+=i\nfor j in range(1,b):\n    if b%j==0:\n        sum2+=j\nif(sum1==b and sum2==a):\n    print('Amicable')\nelse:\n    print('Not Amicable')<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Amicable<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">48. Python program to print Perfect Squares in a range<\/h3>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">start = int(input(\"Enter the Lower bound\"))\nend = int(input(\"Enter the upper bound\"))\n \nfor n in range(start, end+1):\n    sum = 0\n\n    for i in range(1, n):\n      if n%i == 0:\n        sum += i\n    if n == sum:\n      print(n)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<pre class=\"wp-block-preformatted\">Enter the Lower bound 1\nEnter the upper bound 50\n6 28<\/pre>\n<\/div><\/div>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">49. Python program to print Identity Matrix<\/h3>\n\n\n\n<p>An identity matrix is a square matrix with 1s on the diagonal (from the top left to the bottom right) and 0s everywhere else.<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">rows=3\nfor i in range(0,rows):\n    for j in range(0,rows):\n        if(i==j):\n            print(\"1\",sep=\" \",end=\" \")\n        else:\n            print(\"0\",sep=\" \",end=\" \")\n    print()<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"106\" height=\"105\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/identity_matrix.jpg\" alt=\"identity matrix\" class=\"wp-image-23747 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/identity_matrix.jpg 106w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/identity_matrix-100x100.jpg 100w\" data-sizes=\"(max-width: 106px) 100vw, 106px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 106px; --smush-placeholder-aspect-ratio: 106\/105;\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">50. Python Program to Swap Two Numbers without using Third Variable<\/h3>\n\n\n\n<p>This is a famous interview question that you must learn once. To swap two numbers without any third variable we follow these steps<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Add both numbers and assign it to the first number<\/li>\n\n\n\n<li>Subtract both numbers and assign them to the second number<\/li>\n\n\n\n<li>Again, subtract both numbers and assign them to the first number<\/li>\n<\/ul>\n\n\n\n<p>We can also use division and multiplication to swap two numbers just like this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a=4\nb=10\na=a+b\nb=a-b\na=a-b\nprint(\"a is\",a,\"and b is\",b)<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a is 10 and b is 4<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">51. Print Numbers in a Range Without using Loops<\/h3>\n\n\n\n<p>We will use a simple recursive program, to print numbers in the range of 1 to 10 without loops<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def printno(upper):\n    if(upper&gt;0):\n        printno(upper-1)\n        print(upper)\nprintno(10)<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1 2 3 4 5 6 7 8 9 10<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">52. Python program to find Simple Interest<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/simple-interest.png\" alt=\"simple interest formula\" class=\"wp-image-23749 lazyload\" width=\"200\" height=\"200\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/simple-interest.png 400w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/simple-interest-300x300.png 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/simple-interest-150x150.png 150w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/simple-interest-100x100.png 100w\" data-sizes=\"(max-width: 200px) 100vw, 200px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 200px; --smush-placeholder-aspect-ratio: 200\/200;\" \/><\/figure>\n<\/div>\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">principle=float(input(\"Principle amount:\"))\ntime=int(input(\"Time(years):\"))\nrate=float(input(\"Rate(%):\"))\nsimple_interest=(principle*time*rate)\/100\nprint(\"Simple interest is:\",simple_interest)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"315\" height=\"129\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/simple-interest-python.jpg\" alt=\"simple interest\" class=\"wp-image-23750 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/simple-interest-python.jpg 315w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/simple-interest-python-300x123.jpg 300w\" data-sizes=\"(max-width: 315px) 100vw, 315px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 315px; --smush-placeholder-aspect-ratio: 315\/129;\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">53. Python program to find Compound Interest<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/compound-interest.png\" alt=\"compound interest formula\" class=\"wp-image-23754 lazyload\" width=\"200\" height=\"200\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/compound-interest.png 400w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/compound-interest-300x300.png 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/compound-interest-150x150.png 150w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/compound-interest-100x100.png 100w\" data-sizes=\"(max-width: 200px) 100vw, 200px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 200px; --smush-placeholder-aspect-ratio: 200\/200;\" \/><\/figure>\n<\/div>\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">principal = float(input(\"Principle amount:\"))\ntime=int(input(\"Time(years):\"))\nrate=float(input(\"Rate(%):\"))\nAmount = principal * (pow((1 + rate \/ 100), time))\nCI = Amount - principal\nprint(\"Compound interest is\", CI)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"348\" height=\"126\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/compound-interest-oython.jpg\" alt=\"compound interest\" class=\"wp-image-23756 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/compound-interest-oython.jpg 348w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/compound-interest-oython-300x109.jpg 300w\" data-sizes=\"(max-width: 348px) 100vw, 348px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 348px; --smush-placeholder-aspect-ratio: 348\/126;\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">54. Python program to Convert Celsius to Fahrenheit<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>celsius=32\nf=(celsius*1.8)+32\nprint(\"Temperature in farenheit is:\",f)<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Temperature in farenheit is: 89.6<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">55. Python program to Calculate the Grade of a Student<\/h3>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">eng=int(input(\"Enter marks of English: \"))\nhindi=int(input(\"Enter marks of Hindi: \"))\nmath=int(input(\"Enter marks of Math: \"))\nsci=int(input(\"Enter marks of Science: \"))\nsanskrit=int(input(\"Enter marks of588 Sanskrit: \"))\navg=(eng+hindi+math+sci+sanskrit)\/5\nif(avg>=90):\n    print(\"Grade: A\")\nelif(avg>=80 and avg&lt;90):\n    print(\"Grade: B\")\nelif(avg>=70 and avg&lt;80):\n    print(\"Grade: C\")\nelif(avg>=60 and avg&lt;70):\n    print(\"Grade: D\")\nelse:\n    print(\"Grade: F\")<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/grading-system.jpg\" alt=\"grade of a student\" class=\"wp-image-23763 lazyload\" width=\"287\" height=\"136\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/grading-system.jpg 382w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/grading-system-300x142.jpg 300w\" data-sizes=\"(max-width: 287px) 100vw, 287px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 287px; --smush-placeholder-aspect-ratio: 287\/136;\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">56. Find the largest element in an array(list)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>list = &#91;2, 4, 1, 16, 25, 3]\nmax = list&#91;0]\nfor x in list:\n  if x &gt; max:\n    max = x\nprint(max)<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">25<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">57. Python program to find the intersection of two lists in python<\/h3>\n\n\n\n<p>The intersection of two objects means to include only those elements that are common in both objects.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list1 = &#91;4, 9, 1, 17, 11, 26, 28, 54, 69]\nlist2 = &#91;9, 9, 74, 21, 45, 11, 63, 28, 26]\nlist3 = &#91;value for value in list1 if value in list2]\nprint(list3) <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[9, 11, 26, 28]<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">58. Python program to Print Pascal Triangle<\/h3>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">n=5\na=[]\nfor i in range(n):\n    a.append([])\n    a[i].append(1)\n    for j in range(1,i):\n        a[i].append(a[i-1][j-1]+a[i-1][j])\n    if(n!=0):\n        a[i].append(1)\nfor i in range(n):\n    print(\"   \"*(n-i),end=\" \",sep=\" \")\n    for j in range(0,i+1):\n        print('{0:6}'.format(a[i][j]),end=\" \",sep=\" \")\n    print()<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/pas_triangle.jpg\" alt=\"pascal triangle\" class=\"wp-image-23772 lazyload\" width=\"418\" height=\"154\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/pas_triangle.jpg 418w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/01\/pas_triangle-300x111.jpg 300w\" data-sizes=\"(max-width: 418px) 100vw, 418px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 418px; --smush-placeholder-aspect-ratio: 418\/154;\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">59. Python program to print Quotient and Remainder<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>a=325\nb=15\nprint(\"Quotient:\",a\/\/b)\nprint(\"Remainder:\",a%b)<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Quotient: 21 Remainder: 10<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">60. Python program to print the sum of first n Numbers<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>n = int(input(\"Enter a number: \"))\nsum = 0  \nwhile(n &gt; 0):  \n  sum += n  \n  n -= 1  \nprint(\"Total sum is\",sum)<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Enter a number: 10                                                                                                                                      Total sum is 55<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we have covered 60 Python Programming Examples and various fundamental Programs in Python on various python topics which will help you to learn and apply these concepts to many different problems. We recommend you start solving all the questions in this article to get a good hold of problem-solving in python.<\/p>\n\n\n\n<p>We hope you enjoyed our list of Python programs. Thank you for visiting <a href=\"https:\/\/copyassignment.com\/\">our website<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python is a popular, widely-used programming language and easy to learn for beginners because of its short syntax and readability, and learning to code in&#8230;<\/p>\n","protected":false},"author":62,"featured_media":23841,"comment_status":"open","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,1896,1061,1930,1403,1873,1928],"tags":[],"class_list":["post-23632","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-allcategorites","category-general-python","category-gui-python-projects","category-programs-in-python","category-python-projects","category-python-simple-programs","category-python-short-tutorial","wpcat-22-id","wpcat-1896-id","wpcat-1061-id","wpcat-1930-id","wpcat-1403-id","wpcat-1873-id","wpcat-1928-id"],"_links":{"self":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/23632","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/users\/62"}],"replies":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/comments?post=23632"}],"version-history":[{"count":0,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/23632\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media\/23841"}],"wp:attachment":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media?parent=23632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/categories?post=23632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/tags?post=23632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}