Found 27104 Articles for Server Side Programming

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 get signal names from numbers in Python?

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

331 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?

Ankith Reddy
Updated on 17-Jun-2020 14:54:08

282 Views

To print Narcissistic Numbers, let's first look at the definition of it. It is a number that is the sum of its own digits each raised to the power of the number of digits. For example, 1, 153, 370 are all Narcissistic numbers. You can print these numbers by running the following codedef print_narcissistic_nums(start, end): for i in range(start, end + 1):    # Get the digits from the number in a list:    digits = list(map(int, str(i)))    total = 0    length = len(digits)    for d in digits:       total += d ** length   ... Read More

How to clamp floating numbers in Python?

George John
Updated on 17-Jun-2020 14:51:13

6K+ Views

Clamp function limits a value to a given range. Python doesn't have such a function in built. You can create this function likedef clamp(num, min_value, max_value):    return max(min(num, max_value), min_value) print(clamp(5, 1, 20)) print(clamp(1, 10, 20)) print(clamp(20, 1, 10))This will give the output5 10 10

How can we generate Strong numbers in Python?

Sravani S
Updated on 17-Jun-2020 14:52:33

151 Views

To print Strong Numbers, let's first look at the definition of it. It is a number that is the sum of factorials of its own digits. For example, 145 is a Strong number. First, create a function to calculate factorial:def fact(num):    def factorial(n):    num = 1    while n >= 1:       num = num * n       n = n - 1    return numYou can print these numbers by running the following code:def factorial(n):    num = 1    while n >= 1:       num = num * n   ... Read More

How to convert numbers to words using Python?

Arjun Thakur
Updated on 17-Jun-2020 14:48:33

1K+ Views

The constructor for the string class in python, ie, str can be used to convert a number to a string in python. For example, i = 10050 str_i = str(i) print(type(str_i))This will give the output:But if you want something that converts integers to words like 99 to ninety-nine, you have to use an external package or build one yourself. The pynum2word module is pretty good at this task. You can install it using$ pip install pynum2wordThen use it in the following way>>> import num2word >>> num2word.to_card(16) 'sixteen' >>> num2word.to_card(23) 'twenty-three' >>> num2word.to_card(1223) 'one thousand, two hundred and twenty-three'Read More

How to bitwise XOR of hex numbers in Python?

Ramu Prasad
Updated on 17-Jun-2020 14:47:12

2K+ Views

You can get the XOR of any type of numbers using the ^ operator. Specifically for hex numbers, you can use:a = 0x12ef b = 0xabcd print(hex(a ^ b))This will give the output:0xb922The 0x at the beginning of the numbers implies that the number is in hex representation. You can use the ^ operator for other integer representations as well.

How to divide large numbers using Python?

George John
Updated on 30-Jul-2019 22:30:22

908 Views

You can divide large numbers in python as you would normally do. But this has a lot of precision issues as such operations cannot be guaranteed to be precise as it might slow down the language. You would be better off using a numeric computation library like bigfloat to perform such operations.You can read more about floating point issues that you might face with precision on https://docs.python.org/3/tutorial/floatingpoint.html

How to multiply large numbers using Python?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:22

2K+ Views

You can multiply large numbers in python directly without worrying about speed. Python supports a "bignum" integer type which can work with arbitrarily large numbers. In Python 2.5+, this type is called long and is separate from the int type, but the interpreter will automatically use whichever is more appropriate. As long as you have version 2.5 or better, just perform standard math operations and any number which exceeds the boundaries of 32-bit math will be automatically (and transparently) converted to a bignum. For example, a = 15421681351 b = 6184685413848 print(a * b) This will give the output − 95378247708541418748648

Advertisements