This question already has answers here:
Adding Values From Tuples of Same Length
(6 answers)
Closed 4 years ago.
This is an example of the code
elif longitude > longitudel and latitude < latitudel:
thor = 1,-1 + thor
I want it to change if say thor is = 1,1 I want it to become 2,0 I have been messing around with it for a while but I cant seem to find a way to get this to work and keep it as a tuple. Is there a way to keep it as a tuple or do I have to set seperate integers to get this to work ?
You can do it like this:
thor = tuple(sum(x) for x in zip(thor, (1,-1)))
A shorter way would be:
thor = tuple(map(sum, zip(thor, (1,-1))))
Taken from here.
Related
This question already has answers here:
What is the difference between '/' and '//' when used for division?
(16 answers)
Closed last year.
Is there any difference between int(a//x) and int(a/x) in python3 if both a and x are integers.
Recently I got a wrong answer in a contest when I used int(a/x) but my code was accepted when I used int(a//x) .
x, y = 3, 4
print(int(x/y))
print(x//y)
returns
0
0
However:
x, y = -2, 4
print(int(x/y))
print(x//y)
returns
0
-1
So yes. In case one of your input variables is negative an integer, the output of your variable differs.
int(a/x) cuts off the decimals (truncates the number). It doesn't actually do division in the intfunction.
a//x divides to floor (rounds down). It uses the BINARY_FLOOR_DIVIDE in bytecode.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
when i try to add 1 to answer python adds 10^-16 to the answer tried printing in different ways but i think it will not help. Any answers ?
x = 0.8475
print(1 + x)
print(1 + 0.8475)
print(1.0 + 0.8475)
y = 1 + x
print(y)
output :
1.8475000000000001
1.8475000000000001
1.8475000000000001
1.8475000000000001
Its called a floating point error, and isn't specific to python.
A float can't represent all values perfectly accurately, so you get these weird inaccuracies like you're seeing.
Use Decimal instead of float if you need your numbers to be that accurate. In most cases though, its acceptable to just round your answer to a few decimal places.
https://docs.python.org/2/library/decimal.html
This question already has answers here:
Big O, how do you calculate/approximate it?
(24 answers)
Closed 4 years ago.
I have been asked to explaine about Big O notation and to calculate Big o Notation for an algorithm. I'm done with the defining part but I'm still wondering how I can calculate it. Can someone help me to calculate the Big O for the below given code?
new = int (input("enter number" ))
if new <= 10000:
comm=new*2/100
print (comm)
else :
comm= new*5/100
print (comm)
Since there is no loop, it is O(1).
This question already has answers here:
Smooth color transition algorithm
(5 answers)
Closed 5 years ago.
How to get a list of color values between two given ones? For example, I have #ab7878 and #da9933, what if I want the first color to be gradually transformed into the second color, how could I get those values in between?
You can do this by incrementing the values for R, G and B at the same time.
#4477AA
#5588BB
#6699CC
#77AADD
#88BBEEE
var a = 0xab7878;
var b = 0xda9933;
while(b > a)
{
a = a + 0xFF;
console.log(a.toString(16));
}
Full output:
Pastebin
This question already has answers here:
How to format a floating number to fixed width in Python
(10 answers)
Closed 6 years ago.
I'm making a percentage finder, pretty basic. But, I would only like to print 4 characters in the float or int. Can anyone help?
Here is my code so far:
numer = int(input("What's the numerator?"))
denom = int(input("What's the denominator?"))
percent = (numer / denom * 100, "%")
print(percent)
I want the output for 5/6 to be:
83.33%
But it actually prints:
83.33333333333334%
you can use % in print function
print("%.2f"%(a/b))