perform arithmetic operations python [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
given the number 12345 = 1x10^4 + 2x10^3 + 3x10^2 + 4x10^1 + 5x10^0, how to perform some arithmetic operations to leave you with just the digit at position 5 (from the left) and output it to the screen? Thanks for the help!

Assuming you want to stick to arithmetic operations (and not strings), use the modulo operator with 10 to get the remainder of division by 10, i.e. the unit:
12345%10
output: 5
For an arbitrary number, you need to compute the position, you can use log10 and ceil:
from math import log10, ceil
N = 5
number = 1234567
number//10**(ceil(log10(number))-N)%10
output: 5

Related

I am trying to convert a string from a list into an integer without losing the decimal places [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I want to convert a string to integer without rounding. For example
s = "99.7"
x = s(int(float(s))
Output:
99
But I want the output to be 99.7
I was thinking of just adding all the strings to a list and somehow converting the list to an integer but I am not sure how to do that or how to even do it individually.
Desired output:
x = '99.7'
z = int(x)
output:
99.7
An integer in python can not have a floating point. To show this you should use
float(x)
This will prevent any rounding.

Using the (x%y) in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Why in python the x%y result is x when x is less than y. should the result be zero?
for example when I try 4%5, python give me 4 as a result of and I believe it should be zero
% -> refers to the modulo operator
So you would get the remainder if you use this operator
4%5 will give 4 as 4 is the remainder when you divide 5 by 4
If you wish to get the quotient then you can use // which is used for absolute division
so 4%5 gives 4
and 4//5 gives 0
The % symbol in Python is called the Modulo Operator.
It returns the remainder of dividing the left hand operand by right hand operand. It's used to get the remainder of a division problem.
Example:
5%6
# results in 5
See the long division below
_____0__
6 | 5
| 0
|_____
| 5
Performing 5 / 6 results in reminder of 5, thus 5%6 is 5

Check if the length of the range divided by range element has no division remainder [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
The code below shall show a specific comment if the number of elements in the array isn't integer-type number.
It works for dr=0.5 but not for dr=0.1 because print (4%0.1) returns 0.09999999999999978 instead of 0.
How can I change the code to get 4%0.1==0?
import math
limits_real=(-2,2)
dr=0.1
if (limits_real[1]-limits_real[0])%dr!=0:
print ('Inapropriate limits or elements')
It works for dr=0.5 but not for dr=0.1 because print (4%0.1) returns 0.09999999999999978 instead of 0.
Because floating point numbers have limitations that every programmer should know, see this:
https://docs.python.org/3/tutorial/floatingpoint.html
As showed at the end of above documentation, you can use decimal module, which works exacly right but is slower than normal floating point arihtmetics:
from decimal import Decimal
limits_real=(-2,2)
dr = Decimal("0.1")
if (limits_real[1] - limits_real[0]) % dr != 0:
print ('Inapropriate limits or elements')
Note that you should use a str while constructing the Decimal instance, do not use a float.
I found out only this solution :) At least it works
import math
limits_real=(-2,2)
dr=0.1
if (((limits_real[1] - limits_real[0]) * 1000) % (dr * 1000)) / 1000 != 0:
print ('Inapropriate limits or elements')

how to extract digits from a string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
i have a list
a=['invoice β invoice # 2018-33167-2 β date 03/21/2018 β total due $8,804.90']
i want to extract only digits from this list of the form
a=['2018-33167-2 03/21/2018 8,804.90']
i have used regex to extract
for i in a:
res = re.sub("\D", "", i)
but the result is
res=201833167203212018880490
Based on the expected output, you want more than just digits. The digits can have /, - and ,.
Maybe, perhaps, you wanted something that starts with and ends with a digit, but anything in the middle other than a space?
import re
a='invoice β invoice # 2018-33167-2 β date 03/21/2018 β total due $8,804.90'
print(re.findall(r'\d[^ ]+\d', a))
output
python3 test.py
['2018-33167-2', '03/21/2018', '8,804.90']

how to restrict user to input only upto two decimal point float numbers in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am new in python, i want to do is limit the float input by the user only upto two decimal point, eg: 1.11, user is not allowed to input 1.111 or more than to after two decimal point . Thank you
You cannot restrict what the user inputs, but you can convert a float to have 2 decimal points:
value = float(input("Input your number: "))
print ("You inputted " + str(value))
new_value = "{:.2f}".format(value)
print ("After formatting, your number has become: " + str(new_value))

Categories

Resources