Found 10476 Articles for Python

casefold() string in Python Program

Hafeezul Kareem
Updated on 02-Jan-2020 06:31:14

169 Views

In this tutorial, we are going to discuss the string method str.casefold(). It doesn't take any arguments. The return value of the method is a string that is suitable for the caseless comparisons.What are caseless comparisons? For example, the german lower case letter ß is equivalent to ss. The str.casefold() method returns the ß as ss. It converts all the letters to lower case.Example Live Demo# initialising the string string = "TUTORIALSPOINT" # printing the casefold() version of the string print(string.casefold())OutputIf run the above program, you will get the following result.tutorialspointLet's see the example where caseless comparison works. If you directly compare ... Read More

Python - fmod() function

Pradeep Elance
Updated on 30-Dec-2019 10:39:05

558 Views

the fmod()in python implements the math modulo operation. The remainder obtained after the division operation on two operands is known as modulo operation. It is a part of standard library under the math module. In the below examples we will see how the modulo operation gives different out puts under various scenarios.Positive numbersFor positive numbers, the result is the remainder of the operation after the first integer is divided by the second. Interesting the result always comes as a float as we can see from the type of the result.Example Live Demofrom math import fmod print(fmod(6, 7)) print(type(fmod(6, 7))) print(fmod(0, 7)) ... Read More

Multi-Line printing in Python

Pavitra
Updated on 30-Dec-2019 10:26:04

877 Views

We have usually seen the print command in python printing one line of output. But if we have multiple lines to print, then in this approach multiple print commands need to be written. This can be avoided by using another technique involving the three single quotes as seen below.Example Live Demoprint(''' Motivational Quote : Sometimes later becomes never, Do it now. Great things never come from comfort zones. The harder you work for something, the greater you'll feel when you achieve it. ''') Running the above code gives us the following result:Motivational Quote : Sometimes later becomes never, Do ... Read More

Adding two Python lists elements

Farhan Muhamed
Updated on 14-Jul-2025 19:06:05

3K+ Views

In Python, a list is a built-in data structure that is used to store an ordered collection of multiple items in a single variable. Lists are mutable that means we can add, remove, and change its elements. In this article, we will learn how we can add the corresponding elements of two Python Lists. You are given two equal sized lists in Python and your task is to create a new list containing sum of corresponding elements of the lists.Consider the following input output scenario:Scenario Input: List1 = [3, 6, 9, 45, 6] List2 = [11, 14, 21, ... Read More

Python-program-to-convert-pos-to-sop

Pavitra
Updated on 24-Dec-2019 05:55:35

234 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given pos form we need to convert it into its equivalent sop formThe conversion can be done by first counting the number of alphabets in the pos form and then calculating all the max and the minterms.Now let’s observe the concept in the implementation below−Example# Python code to convert standard POS form # to standard SOP form # to calculate number of variables def count_no_alphabets(POS):    i = 0    no_var = 0    # total no. of alphabets before will be ... Read More

Python program to print all Prime numbers in an Interval

Pavitra
Updated on 24-Dec-2019 05:46:13

533 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an interval we need to compute all the prime numbers in a given rangeHere we will be discussing a brute-force approach to get the solution i.e. the basic definition of a prime number. Prime numbers are the number which has 1 and itself as a factor and rests all the numbers are not its factors.Each time the condition of a prime number is evaluated to be true computation is performed.Now let’s observe the concept in the implementation below−Example Live Demostart = 1 ... Read More

Python program to interchange first and last elements in a list

Pavitra
Updated on 11-Jul-2020 12:01:53

729 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list, we need to swap the last element with the first element.There are 4 approaches to solve the problem as discussed below−Approach 1 − The brute-force approachExample Live Demodef swapLast(List):    size = len(List)    # Swap operation    temp = List[0]    List[0] = List[size - 1]    List[size - 1] = temp    return List # Driver code List = ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l'] print(swapLast(List))Output['t', 'u', 't', 'o', 'r', 'i', 'a', 'l']Approach 2 − The ... Read More

Python program to insert an element into sorted list

Pavitra
Updated on 24-Dec-2019 05:30:25

1K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list, we need to insert an element in a list without changing sorted orderThere are two approaches as discussed below−Approach 1: The brute-force methodExample Live Demodef insert(list_, n):    # search    for i in range(len(list_)):       if list_[i] > n:          index = i          break    # Insertion    list_ = list_[:i] + [n] + list_[i:]    return list_ # Driver function list_ = ['t', 'u', 't', 'o', 'r'] n = ... Read More

Python program to get all subsets of a given size of a set

Pavitra
Updated on 24-Dec-2019 05:26:57

912 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a set, we need to list all the subsets of size nWe have three approaches to solve the problem −Using itertools.combinations() methodExample Live Demo# itertools module import itertools def findsubsets(s, n):    return list(itertools.combinations(s, n)) #main s = {1, 2, 3, 4, 5} n = 4 print(findsubsets(s, n))Output[(1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 4, 5), (1, 3, 4, 5), (2, 3, 4, 5)]Using map() and combination() methodExample# itertools module from itertools import combinations def findsubsets(s, n):    return ... Read More

Python Program to find whether a no is power of two

Pavitra
Updated on 24-Dec-2019 05:18:26

2K+ 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.We can solve this using two approaches as discussed below.Approach 1: Taking the log of the given number on base 2 to get the powerExample Live Demo# power of 2 def find(n):    if (n == 0):       return False    while (n != 1):       if (n % 2 != 0):          return False       n = ... Read More

Advertisements