How to limit decimal places when using complex numbers [duplicate] - python

This question already has answers here:
Formatting Complex Numbers
(6 answers)
Closed 8 years ago.
sqrtnum_ = 0
sqrtnum_ = cmath.sqrt(snum_)
print "Using cmath.sqrt:"
print " sqrt(", snum_, ") = ", '%.12f' %sqrtnum_
How can I limit the number of decimal places. Normally I would use the above but it does not work with complex numbers

You can use imag and real attributes of a complex number to get their respective values.
>>> a = cmath.sqrt(-1000)
>>> "%.12f + %.12fj"%(a.real,a.imag)
'0.000000000000 + 31.622776601684j'
This works for completely real numbers also
>>> a = cmath.sqrt(1000)
>>> "%.12f + %.12fj"%(a.real,a.imag)
'31.622776601684 + 0.000000000000j'

This looks to be a duplicate of Formatting Complex Numbers
hopefully that helps!
(would have followed duplicate marking/commenting/etc protocol, but lack the rep for it)

Related

When Rounding to nearest hundreds, how do I include 0s [duplicate]

This question already has answers here:
Rounding a number in Python but keeping ending zeros
(6 answers)
Closed 6 years ago.
So let's say I have this code:
num = 1.29283
round(num, 2)
That rounds to 1.29, but if I do this:
num = 1.30293
round(num, 2)
That rounds to 1.3. I want to know if there is a way to have it round to 1.30; I know it is the same number, but I need it to print 1.30.
You can use string formatting for this. A number in python does not have such a thing as trailing zeros. So your question only make sense for strings.
Example:
>>> num = 1.30293
>>> "{:.2f}".format(num)
'1.30'
The .2f says that this is a float (f) and that you want two digits after the point .2. Read more about string formatting here

How can I stop printing a float 3 spaces after decimal? [duplicate]

This question already has answers here:
How to display a float with two decimal places?
(13 answers)
Pad python floats
(4 answers)
Closed 8 years ago.
I'm sorry, I know this must be a duplicate, I can't find where else it's posted. Please feel free to link me to the original question and mark this as duplicate.
I would like to print a 3 digits of a number AFTER the decimal point in it.
For example:
number = 523.637382
I would like to print: 523.637
I have a feeling I can use something similar to this
print(str(number)[:7])
>>>523.637
However, this will not work if the number before the decimal is not 3 decimals.
Bonus points:
Would this be easy?
number = 500.220
#magic
>>>500.22
number = 500.2000003
#magic
>>>500.2
A (built-in) function that could do this is round:
>>> number = 523.637382
>>> rounded = round(number, 3) # 3 decimal places, for example
>>> rounded
523.637
This has already been answered for example here.
The good news, to answer the second part of your question, is that the round function automatically removes trailing zeroes. It's much harder to retain the zeros if you're defining a new variable: you need the decimal module; but it looks that that isn't necessary here.
>>> number = 523.60000001
>>> rounded = round(number, 3)
>>> rounded
523.6
print("%.3f" % number)
or, using the new-style formatting,
print("{0:.3f}".format(number))
If you're printing a str like above you can use string interpolation:
number = 33.33333
print("{0:.3f}".format(number))
#=> 33.333

Binary in Python [duplicate]

This question already has answers here:
What do numbers starting with 0 mean in python?
(9 answers)
Closed 8 years ago.
When I write run the following code in Python:
m = 010001110110100101110110011001010010000001101101011001010010000001100001011011100010000001000001
print "m = ", m
I receive the output:
m = 7772840013437408857694157721741838884340237826753178459792706472536593002623651807233
What's happening? Is Python automatically converting from base 2 to base 10?
You're starting m with 0. Python assumes it's an Octal (see this)
What you're seeing is the decimal representation of the octal number.
If you want to work with that as binary numbers, I'd recommend setting it as an str and then parse it to int specifying that you're parsing something in base 2:
>>> m='0100011'
>>> int(m, 2)
35
Kind of, it interprets a number starting with 0 as octal, so it actually isn't binary in the first place.
In any case, you can make it output the integer as binary like so:
print "m = ", "{0:b}".format(m)

What does percentage sign do in python if not working as a module [duplicate]

This question already has answers here:
What does % do to strings in Python?
(4 answers)
Closed 8 years ago.
def getresults(candidates, parties, votes, percentageofvote):
results = "/=\/=\=/=\=/=\=/=\=/=\Election Results/=\=/=\=/=\=/=\=/=\=/=\=/=\/\n\n Candidate Party Votes Percent\n\n\n"
for x in range(len(candidates)):
results = results + "%12.12s"%candidates[x] + "\t%3.3s"%parties[x] + "\t%3.3s"% votes[x] + "\t" + str(percentageofvote[x]) + "\n"
return(results)
So my question is what the %12.12, and other percents are doing in this code. I understand everything else about the code, what its doing etc but I don't quite understand what the percent does. I know that normally it works as a modulus and will tell you the remainder between two numbers being divided. Clarification on this would be much appreciated.
It does formatting of strings.
More info on it:
http://docs.python.org/2/library/string.html#format-examples

convert string rep of hexadecimal into actual hexadecimal [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
how to parse hex or decimal int in Python
i have a bunch of Hexadecimal colors in a database stored as strings.
e.g. '0xFFFF00'
when i get them from the database i need to convert this string into an actual hexadecimal number, so
0xFFFF00
how can i do this in python
This is one way to do it:
>>> s = '0xFFFF00'
>>> i = int(s, 16)
>>> print i
hex(int('0xFFFF00', 16))
Also this works
number = int('0xFFFF00',0)
print("%x follows %x" % (number+1, number))
0 argument tells interpreter to follow the Python rules of numbers to decide the used format of number so this expression will work right for all numbers.

Categories