Found 10476 Articles for Python

Python Program to convert Kilometers to Miles

Pavitra
Updated on 23-Dec-2019 07:37:08

350 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given distance in kilometers and we need to convert it into milesAs we know that 1 kilometer equals 0.62137 miles.Formula UsedMiles = kilometer * 0.62137Now let’s observe the concept in the implementation below−Example Live Demokilometers = 5.5 # conversion factor as 1 km = 0.621371 miles conv = 0.621371 # calculation miles = kilometers * conv print(kilometers, "kilometers is equal to ", miles, "miles")Output5.5 kilometers is equal to 3.4175405 milesAll the variables are declared in the local scope and their references are seen ... Read More

Python program to convert hex string to decimal

Pavitra
Updated on 23-Dec-2019 07:35:02

521 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a hexadecimal string, we need to convert it into its decimal equivalent.We have two approaches to solve the problem−Brute-force ApproachUsing Built-in moduleBrute-Force MethodHere we take the help of explicit type casting function i.e. integer. This function takes two arguments i.e. hex equivalent and base i.e. (16). This function is used to convert a hexadecimal string to its equivalent decimal in an integer type, which can be further typecasted back to string format.Example Live Demo#input string string = 'F' # converting hexadecimal string ... Read More

Convert a list to string in Python program

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

241 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

459 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

502 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

573 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

1K+ 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

639 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

325 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

517 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

Advertisements