Found 34494 Articles for Programming

Python program to convert an array to an ordinary list with the same items

Arushi
Updated on 30-Jul-2019 22:30:23

123 Views

The array is given. Our task is to convert an array to an ordinary list. We solve this problem with the help of tolist() function. This function return the array as a (possibly nested) list. Algorithm Step 1: Given an array. Step 2: convert the array to a list using tolist() function. Step 3: Display list Example Code #Python program to convert an array to an ordinary #list with the same items from array import * def arraytolist(A): lst = A.tolist() # list print("The List Is ::>",lst) # Driver code A= array('i', [20,30,60]) # array arraytolist(A) Output The List Is ::> [20, 30, 60] The List Is ::> [20, 30, 60]

Python program to count total set bits in all number from 1 to n.

Paul Richard
Updated on 23-Jun-2020 16:12:51

265 Views

Given a positive integer n, then we change to its binary representation and count the total number of set bits.ExampleInput : n=3 Output : 4AlgorithmStep 1: Input a positive integer data. Step 2: then convert it to binary form. Step 3: initialize the variable s = 0. Step 4: traverse every element and add. Step 5: display sum.Example Code# Python program to count set bits # in all numbers from 1 to n. def countbits(n):    # initialize the counter    c = 0    for i in range(1, n + 1):    c += bitsetcount(i)    return c    def bitsetcount(x):       if (x

Create Word Cloud using Python

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

1K+ Views

In this problem, there is a file with some texts. We have to create Word Clouds from those texts and one masking image. The program will store the word cloud image as png format. To implement this problem, we need to use some libraries of python. The libraries are matplotlib, wordcloud, numpy, tkinter and PIL. To install these libraries, we need to follow these commands − Setup the Libraries $ sudo pip3 install matplotlib $ sudo pip3 install wordcloud $ sudo apt-get install python3-tk After adding these libraries, we can write the python code to perform the task. ... Read More

How to convert HTML to PDF using Python

Arushi
Updated on 30-Jul-2019 22:30:23

1K+ Views

Python provides Pdfcrowd API v2 which is convert HTML documents to PDF. This API is very easy to use and the integration takes only a couple of lines of code. Installation Install the client library from PyPI $ pip install pdfcrowd Conversion will be completed in following 3 Steps from Webpage/HTML to PDF Step 1 − Download the library pdfkit $ pip install pdfkit Step 2 − Now download wkhtmltopdf For Ubuntu/Debian − sudo apt-get install wkhtmltopdf For Windows − (a) Download link : WKHTMLTOPDF (b) Set : PATH variable set binary folder ... Read More

Python program to sort a list according to the second element in the sublist.

AmitDiwan
Updated on 11-Aug-2022 08:56:19

1K+ Views

In this article, we will sort a list according to the second element in the sublist. Let’s say we have the following list − [['jack', 50], ['antony', 20], ['jones', 87], ['gary', 70], ['tom', 90], ['sam', 110], ['warner', 65]] The output should be the following i.e. sorted according to the second element − [['antony', 20], ['jack', 50], ['warner', 65], ['gary', 70], ['jones', 87], ['tom', 90], ['sam', 110]] Python program to sort a list according to the second element in the sublist using the sort() method Example # Custom Function def SortFunc(sub_li): sub_li.sort(key = lambda x: x[1]) return sub_li ... Read More

Python program addition of two matrix

Samual Sam
Updated on 30-Jul-2019 22:30:23

11K+ Views

Given two user input matrix. Our task is to display the addition of two matrix. In these problem we use nested List comprehensive. Algorithm Step1: input two matrix. Step 2: nested for loops only to iterate through each row and columns. Step 3: At each iterationshall add the corresponding elements from two matrices and shall store the result. Example code # Program to add two matrices using nested loop A=[] n=int(input("Enter N for N x N matrix : ")) #3 here #use list for storing 2D array #get the ... Read More

Python program to calculate BMI(Body Mass Index) of your Body

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

788 Views

We have to enter our height and weight. Our task is to calculate BMI using formula. Algorithm Step 1: input height and weight of your body. Step 2: then applying the formula for calculation BMI. Step 3: display BMI. Example Code height = float(input("Enter your height(m): ")) weight = float(input("Enter your weight(kg): ")) print("Your BMI is: ", round(weight / (height * height), 2)) Output Enter your height (m): 5.8 Input your weight (kg): 64 Your body mass index is: 1.9

Python program multiplication of two matrix.

Samual Sam
Updated on 30-Jul-2019 22:30:23

18K+ Views

Given two user input matrix. Our task is to display the addition of two matrix. In these problem we use nested List comprehensive. Algorithm Step1: input two matrix. Step 2: nested for loops to iterate through each row and each column. Step 3: take one resultant matrix which is initially contains all 0. Then we multiply each row elements of first matrix with each elements of second matrix, then add all multiplied value. That is the value of resultant matrix. Example Code # Program to multiply two matrices A=[] n=int(input("Enter N for N x N matrix: ")) ... Read More

Python program maximum of three.

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

10K+ Views

Given three number a b and c, our task is that we have to find the maximum element in among in given number. Example Input: a = 2, b = 4, c = 3 Output: 4 Algorithm Step 1: input three user input number. Step2: Add three numbers to list. Step 3: Using max() function to find the greatest number max(lst). Step 4: And finally we will print maximum number. Example Code def maximum(a, b, c): list = [a, b, c] return max(list) # ... Read More

Python program for Modular Exponentiation

Samual Sam
Updated on 30-Jul-2019 22:30:23

593 Views

Given three numbers x, y and z, our task is to calculate (x^y) % z Example Input: x = 2, y = 3, p = 3 Output: 2 Explanation : 2^3 % 3= 8 % 3 = 2. Algorithm Step 1: Input three numbers. Step 2: then we use pow() to calculating power and % for modular. Step 3: display result. Example Code x = int(input("Enter First Value ::>")) y = int(input("Enter Second Value ::>")) z= (int)(1e9+7) # pow function use d = pow(x, y) % z print ("Value Is=",d) Output Enter First Value ::> 2 Enter Second Value ::> 3 Value Is= 8

Advertisements