Found 10400 Articles for Python

Find all the patterns of \"10+1\" in a given string using Python Regex

Sumana Challa
Updated on 09-May-2025 12:17:34

184 Views

The term "10plus1" is a specific pattern in a binary string that starts with a digit '1' followed by at least one or more '0' and ending with a '1'. In regular expressions this pattern is represented as - 10+ 1 Using re.findall() The re.findall() method accepts a pattern and a string as parameters, finds the given pattern in the entire string, and returns all the matches in the form of a list.To find the patterns of "10+1" in a given string, we just need to pass the same as pattern to the findall() method along with the string.Example ... Read More

How can I remove the same element in the list by Python

Sumana Challa
Updated on 09-May-2025 10:30:40

2K+ Views

A list is a built-in Python data structure that is used to store an ordered collection of items of different data types. It often occurs that lists contain duplicate values, i.e., the same element repeating multiple times, which causes data inaccuracies. In this article, we will discuss the approaches that can be used to remove the repeated elements from a list. Using set() Using List Comprehension Using a For Loop Using Dictionary fromkeys() Using set() Function The set() function accepts an iterable ... Read More

Can someone help me fix this Python Program?

Arnab Chakraborty
Updated on 24-Jun-2020 07:26:01

102 Views

The first problem u are getting in the bold portion is due to non-indent block, put one indentation there.second problem is name variable is not definedfollowing is the corrected one -print ("Come-on in. Need help with any bags?") bag=input ('(1) Yes please  (2) Nah, thanks   (3) Ill get em later  TYPE THE NUMBER ONLY') if bag == ('1'): print ("Ok, ill be right there!") if bag == ('2'): print ("Okee, see ya inside. Heh, how rude of me? I'm Daniel by the way, ya?") name="Daniel" print (name + ": Um, Names " + name) print ("Dan: K, nice too ... Read More

Reply to user text using Python

Arnab Chakraborty
Updated on 16-Jun-2020 08:28:13

2K+ Views

You can solve this problem by using if-elif-else statements. And to make it like, it will ask for a valid option until the given option is on the list, we can use while loops. When the option is valid, then break the loop, otherwise, it will ask for the input repeatedly.You should take the input as an integer, for that you need to typecast the input to an integer using int() method.ExamplePlease check the code to follow the given points.print("Come-on in. Need help with any bags?") while True: # loop is used to take option until it is not valid. ... Read More

Python - How to convert this while loop to for loop?

Pythonista
Updated on 20-Jun-2020 07:41:33

1K+ Views

Usin count() function in itertools module gives an iterator of evenly spaced values. The function takes two parameters. start is by default 0 and step is by default 1. Using defaults will generate infinite iterator. Use break to terminate loop.import itertools percentNumbers = [ ] finish = "n" num = "0" for x in itertools.count() :     num = input("enter the mark : ")     num = float(num)     percentNumbers.append(num)     finish = input("stop? (y/n) ")     if finish=='y':break print(percentNumbers)Sample output of the above scriptenter the mark : 11 stop? (y/n) enter the mark : 22 stop? (y/n) enter the mark : 33 stop? (y/n) y [11.0, 22.0, 33.0]

How to find Square root of complex numbers in Python?

Chandu yadav
Updated on 30-Apr-2025 13:06:05

591 Views

Complex numbers are numbers that have both real and imaginary components in the structure,  a+bi. You can find the square root of complex numbers in Python using the cmath module. This module in Python is exclusively used to deal with complex numbers.Square Root of Complex Numbers Using cmath.sqrt() The cmath.sqrt() function is a part of Python's cmath module, that takes a number which is an integer or float (real or complex) and returns the complex square root of x. Below are some examples of scenarios where the function can be used - Example - Basic Complex Number In the below ... Read More

How to get signal names from numbers in Python?

Govinda Sai
Updated on 17-Jun-2020 14:55:56

478 Views

There is no straightforward way of getting signal names from numbers in python. You can use the signal module to get all its attributes. Then use this dict to filter the variables that start with SIG and finally store them in a dice. For example,Exampleimport signal sig_items = reversed(sorted(signal.__dict__.items())) final = dict((k, v) for v, k in sig_items if v.startswith('SIG') and not v.startswith('SIG_')) print(final)OutputThis will give the output:{: 'SIGTERM', : 'SIGSEGV', : 'SIGINT', : 'SIGILL', : 'SIGFPE', : 'SIGBREAK', : 'SIGABRT'}

How to print Narcissistic(Armstrong) Numbers with Python?

Sumana Challa
Updated on 16-May-2025 19:37:20

421 Views

A narcissistic number (also known as an Armstrong number) is a number that equals the sum of its digits, each raised to the power of the number of digits. For example, 370 - 33+73+03 = 370. The algorithm to check for an Armstrong number is as follows - Determine the number of digits for the mentioned number. Extract each digit and calculate the power of that digit with the exponent equal to the number of digits. Calculate the sum of the power. Compare ... Read More

How to clamp floating numbers in Python?

Sumana Challa
Updated on 06-May-2025 18:53:39

6K+ Views

Clamping refers to limiting a number to a specific range, i.e., making sure that the number lies between the minimum and maximum value mentioned. This method is used in applications like graphics and statistical computations, as it requires the data to stick to specific limits. Clamping Floating Numbers in Python The following are some of the approaches to clamp floating numbers in Python - Creating a User-Defined Function Since Python has no built-in clamp function, in the following program, we will create our clamp() function, which takes three parameters - n (number to be clamped), min (minimum value), and max (maximum ... Read More

How to convert numbers to words using Python?

Sumana Challa
Updated on 30-Apr-2025 18:10:50

1K+ Views

The given task is to convert the numerical values to their respective word representation (i.e, we need to spell the numbers in text ). For example, if the given numbers are 1, 2, 29, the resultant values would be: one, two, twenty-nine, respectively. We can do so, using the function(s) available in the num2word library. Converting Numbers to Words Using num2word() The num2words() is a function of the Python library with the same name (num2words). This is used to convert numbers like 56 to words like fifty-six. In addition to the numerical parameters, this function accepts two optional parameters - ... Read More

Advertisements