Found 10476 Articles for Python

Python Program for Difference between sums of odd and even digits

Pavitra
Updated on 25-Sep-2019 13:20:27

611 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given an integer, we need to calculate if the difference between the sum of odd digits and sum of even digits is 0 or not.The brute-force approach will be calculating the sum of all even and odd digits in the numbers and subtracting them to compute the answer.To reduce the computational time we use the concept of mental mathematics .The above constraints holds true only if the numbers are divisible by 11. So here in the implementation given below we check the ... Read More

Python Program for cube sum of first n natural numbers

Pavitra
Updated on 25-Sep-2019 13:16:16

2K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given an input n, we need to print the sum of series 13 + 23 + 33 + 43 + …….+ n3 till n-th term.Here we will discuss two approach to reach the solution of the problem statement −Brute-force approach using loops.Mathematical solution of sum of n numbers.Approach 1 −Computing sum of each term by adding by iterating over the numbersExample Live Demodef sumOfSeries(n):    sum = 0    for i in range(1, n+1):       sum +=i*i*i    return sum # ... Read More

Python Program for compound interest

Pavitra
Updated on 25-Sep-2019 13:13:09

578 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −We are given with three input values i.e principle, rate & time and we need to compute compound interest.The code given below shows the process of computation of compound interest.The formula used here isCompound Interest = P(1 + R/100)rWhere, P is the principal amountR is the rate andT is the time spanThe implementation is given belowExample Live Demodef compound_interest(principle, rate, time):    CI = principle * (pow((1 + rate / 100), time))    print("Compound interest : ", CI) # main compound_interest(10000, 7.78, 2)OutputCompound ... Read More

Python Program for Bubble Sort

Pavitra
Updated on 25-Sep-2019 12:59:03

2K+ Views

In this article, we will learn about the implementation of bubble sort sorting technique.The figure shown below illustrates the working of this algorithm −ApproachStarting with the first element(index = 0), compare the current element with the next element of the array.If the current element is greater than the next element of the array, swap them.If the current element is less than the next element, move to the next element.Repeat Step 1.Now let’s see the implementation below −Exampledef bubbleSort(ar):    n = len(arr)    # Traverse through all array elements    for i in range(n):    # Last i elements are ... Read More

Python Program for Binary Search

Farhan Muhamed
Updated on 22-Aug-2025 15:53:46

6K+ Views

The binary search algorithm is the fastest algorithm to search for an element in a sorted array. It works by dividing the array into two halves and checking if the target element is in the left or right half, thus reducing the search space by half with each iteration. Implementing Binary Search Algorithm Given a sorted array of integers and a target element, and our task is to implement a python program to search for the target element in the array using the binary search algorithm. Here are some example scenarios: Scenario 1 Input: arr[] = {1, ... Read More

Python Program to Print Matrix in Z form

Pavitra
Updated on 25-Sep-2019 12:26:13

382 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given a square matrix of order n*n, we need to display elements of the matrix in Z form.Z form is traversing the matrix in the following steps −Traverse the first rowNow, traverse the second principal diagonalFinally, traverse the last row.We will take an input matrix here implicitly taken to demonstrate the flow of code.demostrateExample Live Demoarr = [[1, 2, 6, 9],    [1, 2, 3, 1],    [7, 1, 3, 5],    [1, 8, 7, 5]] n = len(arr[0]) i = 0 ... Read More

Python Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!

Pavitra
Updated on 25-Sep-2019 12:21:44

3K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given an integer input n, we need to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!Here we are implementing for loop, therefore, we get O(n) as the time complexity.Here to reach the efficiency we calculate factorial within the same loop.Here we frame a sumofseries function as described below −Example Live Demodef sumOfSeries(num):    res = 0    fact = 1    for i in range(1, num+1):       fact *= i       res ... Read More

Python Program to find the area of a circle

Pavitra
Updated on 25-Sep-2019 12:18:21

759 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given the radius of a circle, we need to find a circle.The area of a circle can simply be evaluated using the following formula.Area = Pi*r*rLet’s see the implementation below −Example Live Demodef findArea(r):    PI = 3.142    return PI * (r*r); # Driver method print("Area is %.6f" % findArea(5));OutputArea is 78.550000All variables and functions are declared in the global scope as shown in the figure below.ConclusionIn this article, we learned about the approach to find whether it is possible to make ... Read More

Python Program to calculate the area of a Tetrahedron

Pavitra
Updated on 25-Sep-2019 12:37:59

399 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given the side of a tetrahedron, we need to find a tetrahedron.A Tetrahedron is a geometric figure which looks like a pyramid with a triangular base. It is a solid object with four triangular faces, three on the sides, one on the bottom of the base and four vertices or corners.Here we frame an area function as shown below −Exampleimport math def areatetrahedron(side):    return (math.sqrt(3) * (side * side)) # Driver Code side = 20 print("Area of Tetrahedron = ", area_of_tetrahedron(side))OutputArea ... Read More

Python Implementing Web Scraping with Scrapy

Pavitra
Updated on 25-Sep-2019 12:04:21

230 Views

In this article, we will learn about the web scraping technique using the Scrappy module available in Python.What is web scraping?Web scraping is used to obtain/get the data from a website with the help of a crawler/scanner. Web scrapping comes handy to extract the data from a web page that doesn't offer the functionality of an API. In python, web scraping can be done by the help of various modules namely Beautiful Soup, Scrappy & lxml.Here we will discuss web scraping using the Scrappy module.For that, we first need to install Scrappy.Type in the terminal or command prompt>>> pip install ... Read More

Advertisements