Python, I can't understand this statement what does that mean - python

Write a python program to accept 2 "string" numbers for calculation.
Note : convert string number to an Integer before perform the calculation
Any examples answer?

You have to accept input in string, use raw_input(), and you have to parse them in int. And perform your calculation

Related

problem during the conversion with to_numeric, I lose decimal I don't want to lose

I have a problem, I would like to keep all decimals when converting string to float. How can I do this?
Each time, I have like a round() of values.
As you can see, the column named 'price_to_numeric' had less decimal than the column 'price'.
enter image description here
If you want to convert from string to float (a number with decimals) you can use the float() function.
To do this the string just needs to be passed into the function as follows: float('6.5') and the output of the function will be 6.5
If that is what you meant then enjoy, if not please upload the code for us to see.

Why does input() only return strings?

My question is very simple, I want to know that when we initialize a variable in python it recognize it whether it is string or integer, But when we use input function it takes input as string and if we need integer or float we have to type cast it. why?
Because input() always returns a str. In another words, input() "recognizes" everything (what is entered to it) as a string.
There is a difference between "123", which is string and 123, which is int. To make int from string, you should use type casting - int(input('Number: ').
input() function is not designed to autodetect type, like Python does, because it is inconvenient default behavior. In all usual cases, a program except certain input type from user. You can implement such thing by yourself (if needed).
Python CAN recognize type of variable. But most of the time python doesn't NEED to.
As mentioned, input always returns str. You need to cast it into int only if you're gonna do something integer-specific with it. In most cases python doesn't care about type of variables. It is called duck typing
https://realpython.com/lessons/duck-typing/

Converting String Expression to Float

I am sorry if this question has been answered already, I just couldn't find it.
I have scraped a table with pandas.read_html. In the table, I have a couple of expressions I am interested in the format of "20.15 X 200", where the type is a string. I want to convert them to floats in order to make the multiplication and later compare the values. The issue I have is that the expressions are with the "X" sign for multiplication and I am not able to find any information ho to convert such an expression.
When I try the function to float() I get - ValueError: could not convert string to float.

Getting error in python: Value Error: invalid literal for int() with base 10: '470.21'

i want adding and subtracting this type of data: $12,587.30.which returns answer in same format.how can do this ?
Here is my code example:
print(int(col_ammount2.lstrip('$'))-int(col_ammount.lstrip('$')))
I removed $ sign and convert it to int but it gives me base 10 error.
You mentioned you want to do arithmetic operations to the numbers (addition/subtraction) so you probably want them in float instead. The difference between an integer (int) and float is that integers do not carry decimal points.
Additionally, as #officialaimm mentioned you need to remove the commas too, for example
float('$3,333.33'.replace('$', '').replace(',', ''))
will give you
3333.33
So putting it into your code
print(float(col_ammount2.lstrip('$').replace(',', ''))
- float(col_ammount.lstrip('$').replace(',', '')))
An additional note for when you parse a floating point number (same applies to integers too), you may want to watch out for empty values, i.e.
float('')
is bad. One of the things u can do in case col_amount and col_amount2 may be empty at some point is default them to 0 if that happens
float(col_amount.lstrip(...).replace(...) or 0)
You also want to read this to know about workaround to problems you may face with floating point arithmetic https://docs.python.org/3/tutorial/floatingpoint.html
There are two things you are missing here. Firstly python int(...) cannot parse numbers with commas so you will need to remove commas as well by using .replace(',',''). Secondly int() cannot parse floating point values you will have to use float(...) first and after that maybe typecast it to int using int or math.ceil, math.floor appropriately as per your choice and needs.
Maybe something like this will solve your problem:
col_ammount2='$1,587.30'
col_ammount = '$2,567.67'
print(int(float(col_ammount2.lstrip('$').replace(',','')))-int(float(col_ammount.lstrip('$').replace(',',''))))
If you are doing these sorts of things quite often in your code, making a function as such might be handy:
integerify_currency = lambda x:int(float(x.lstrip('$').replace(',','')))

Alternative ways for binary conversion in python

I often need to convert status code to bit representation in order to determine what error/status are active on analyzers using plain-text or binary communication protocol.
I use python to poll data and to parse it. Sometime I really get confuse because I found that there is so many ways to solve a problem. Today I had to convert a string where each character is an hexadecimal digit to its binary representation. That is, each hexadecimal character must be converted into 4 bits, where the MSB start from left. Note: I need a char by char conversion, and leading zero.
I managed to build these following function which does the trick in a quasi one-liner fashion.
def convertStatus(s, base=16):
n = int(math.log2(base))
b = "".join(["{{:0>{}b}}".format(n).format(int(x, base)) for x in s])
return b
Eg., this convert the following input:
0123456789abcdef
into:
0000000100100011010001010110011110001001101010111100110111101111
Which was my goal.
Now, I am wondering what another elegant solutions could I have used to reach my goal? I also would like to better understand what are advantages and drawbacks among solutions. The function signature can be changed, but usually it is a string for input and output. Lets become imaginative...
This is simple in two steps
Converting a string to an int is almost trivial: use int(aString, base=...)
the first parameter is can be a string!
and with base, almost every option is possible
Converting a number to a string is easy with format() and the mini print language
So converting hex-strings to binary can be done as
def h2b(x):
val = int(x, base=16)
return format(val, 'b')
Here the two steps are explicitly. Possible it's better to do it in one line, or even in-line

Categories