Found 10784 Articles for Python

Convert a list to string in Python program

Pavitra
Updated on 23-Dec-2019 07:28:22

170 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list iterable, we need to convert it into a string type iterable.There are four approaches to solve the given problem. Let’s see them one by one−Brute-force ApproachExample Live Demodef listToString(s):    # initialize an empty string    str_ = ""    # traverse in the string    for ele in s:       str_ += ele    # return string    return str_ # main s = ['Tutorials', 'Point'] print(listToString(s))OutputTutorialsPointUsing Built-In join() methodExampledef listToString(s):    # initialize an empty string ... Read More

Python program to check if the given string is vowel Palindrome

Pavitra
Updated on 23-Dec-2019 07:17:47

357 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given string (containing both vowel and consonant letters), remove all consonants, then check if the resulting string is a palindrome or not.Here we first remove all the consonants present in the string. A loop to calculate the divisors by computed by dividing each value from 1 to the minimum computedEach time the condition is evaluated to be true counter is incremented by one.Remove all the consonants in the string. Now we check whether the vowel string is a palindrome or not i.e. ... Read More

Python program to check if a given string is Keyword or not

Pavitra
Updated on 23-Dec-2019 07:13:45

323 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a number, we need to check that the number is a power of two or not.Keywords are the special words reserved by any language with specific usage and cannot be used as an identifier.To check whether the given string is a keyword we used the keyword module as discussed below.Example Live Demo# keyword module import keyword # Function def isKeyword(word) :    # list of all keywords    keyword_list = keyword.kwlist    # check the presence    if word in keyword_list : ... Read More

Python Program for Number of elements with odd factors in the given range

Pavitra
Updated on 23-Dec-2019 07:03:27

433 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a range, we need to find the number of odd factors in the range.ApproachAs we all know that all perfect squares have an odd number of factors in a range. So here we will compute a number of perfect squares.As m and n both are inclusive, so to avoid error in case of n being a perfect square we take n-1 in the formulae.Now let’s see the implementation below−Example Live Demo# count function def count(n, m):    return int(m**0.5) - int((n-1)**0.5) # ... Read More

Python Program for nth multiple of a number in Fibonacci Series

Pavitra
Updated on 23-Dec-2019 07:00:09

823 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement− We are given a number, we need to find the nth multiple of a number k in Fibonacci number.The solution to the problem is discussed below−Example Live Demo# find function def find(k, n):    f1 = 0    f2 = 1    i =2;    #fibonacci recursion    while i!=0:       f3 = f1 + f2;       f1 = f2;       f2 = f3;       if f2%k == 0:          return n*i     ... Read More

Find sum of even factors of a number in Python Program

Utkarsha Naithani
Updated on 23-Aug-2022 14:01:49

450 Views

Let’s start our article with an explanation of how to find the sum of even factors of a number but what are factors? Factors are the numbers which completely divide the given number leaving zero as remainder or we can say factors are the multiples of the number. Example 1 − If we are given a number 60 Factors of 60 (6*10=2*3*2*5) are 2, 5 and 3 But according to the question, we must find out the sum of even factors so, in the above given example, the even factor will be only 2. Also, if the given number is an ... Read More

Python Program for Efficient program to print all prime factors of a given number

Pavitra
Updated on 23-Dec-2019 06:43:38

255 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a number, we need to find all the prime factors of a given number.The efficient solution to the problem is discussed below −Example Live Demo# Python program to print prime factors import math # prime def primeFactors(n):    # no of even divisibility    while n % 2 == 0:       print (2),       n = n / 2    # n reduces to become odd    for i in range(3, int(math.sqrt(n))+1, 2):       # while ... Read More

Python Program for Common Divisors of Two Numbers

Pavitra
Updated on 23-Dec-2019 06:40:33

414 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given two integers, we need to display the common divisors of two numbersHere we are computing the minimum of the two numbers we take as input. A loop to calculate the divisors by computed by dividing each value from 1 to the minimum computedEach time the condition is evaluated to be true counter is incremented by one.Now let’s observe the concept in the implementation below−Example Live Demoa = 5 b = 45 count = 0 for i in range(1, min(a, b)+1):   ... Read More

Boolean Indexing in Python

Pradeep Elance
Updated on 20-Dec-2019 11:26:46

2K+ Views

The Boolean values like True & false and 1&0 can be used as indexes in panda dataframe. They can help us filter out the required records. In the below exampels we will see different methods that can be used to carry out the Boolean indexing operations.Creating Boolean IndexLet’s consider a data frame desciribing the data from a game. The various points scored on different days are mentioned in a dictionary. Then we can create an index on the dataframe using True and False as the indexing values. Then we can print the final dataframe.Example Live Demoimport pandas as pd # dictionary ... Read More

html5lib and lxml parsers in Python

Pradeep Elance
Updated on 20-Dec-2019 11:19:19

650 Views

html5lib is a pure-python library for parsing HTML. It is designed to conform to the WHATWG HTML specification, as is implemented by all major web browsers. It can parse almost all the elements of an HTML doc, breaking it down into different tags and pieces which can be filtered out for various use cases. It parses the text the same way as done by the major browsers. It can also tackle broken HTML tags and add some necessary tags to complete the structure. Also it is written in pure python code.lxml is also a similar parser but driven by XML ... Read More

Advertisements