when I type code as int(input()) and input(int()) for same input let say 12,output are 12 and 012 respectively . So why this,what is difference in syntax?
input() can take some text as parameter as description of the input-line.
From the manual:
>>> s = input('--> ')
--> Monty Python's Flying Circus
>>> s
"Monty Python's Flying Circus"
int() is zero and therefore you pass zero to input if you do that:
# --> same as input(0):
input(int())
If you write int(input()) you first get the input which is a string and then cast it to an int:
>>> type(input())
5
<class 'str'>
>>> type(int(input()))
5
<class 'int'>
>>>
int() returns the integer 0. input() is using this as the prompt argument (which it prints out to stdout). Hence the extra 0 being printed in the front.
Related
This question already has answers here:
Formatting floats without trailing zeros
(21 answers)
Closed 2 years ago.
In the same function, I have tried to use integer, float, and rounding, but I could not get this result. What did I do wrong?
The goal is:
10*12.3 = 123
3*12.3= 36.9
my code:
def multi(n1, n2):
x = n1*n2
return x
I have tried int(n1*n2), but I got 123 and 36. Then I tried float(n1*n2) and I got 123.0 and 36.9. What did I do wrong and how can I fix it?
You are always multiplying an integer with a float which will always output a float.
If you want the number that your function returns to be a float with 1 decimal point you can use round(num, 1).
def multi(n1, n2):
x = n1*n2
return round(x, 1)
print(multi(10, 12.3)) # outputs '123.0'
print(multi(3, 12.3)) # outputs '36.9'
To escape the .0 you could probably use an if statement although I don't see the use of it, since doing calculations with floats have the same output as integers (when they are .0)
def multi(n1, n2):
x = n1 * n2
return round(x, 1)
output = []
output.append(multi(10, 12.3)) # outputs '123.0'
output.append(multi(3, 12.3)) # outputs '36.9'
for index, item in enumerate(output):
if int(item) == float(item):
output[index] = int(item)
print(output) # prints [129, 36.9]
This should probably help you but it shouldn't matter all that match to you
The number is not the representation of the number. For example, all these representations are 123:
123
12.3E1
123.0
123.0000000000000000000
My advice is to do them as floating point and either use output formatting to get them all in a consistent format:
>>> for i in (12.3 * 10, 42., 36.9 / 10):
... print(f"{i:8.2f}")
...
123.00
42.00
3.69
or string manipulation to remove useless suffixes:
>>> import re
>>> x = 12.3 * 10
>>> print(x)
123.0
>>> print(re.sub(r"\.0*$", "", str(x)))
123
I generated a hash value for a file in python. The value consists of both characters and numbers. If i check the data type of each value, it is showing as string for both letters and numbers. How do I convert the data type of numbers from string to int?
Thanks in Advance!
import hashlib
myl = []
fobj = open('akash.txt','r')
buffer = fobj.read()
hsh = hashlib.sha512()
hsh.update(buffer.encode('utf-8'))
val = hsh.hexdigest()
for i in val:
print(type(i))
fobj.close()
The hash value generated by the code is:
cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
I expect the output to be as
<class 'str'>
<class 'str'>
<class 'int'>
...
But this is the output Im getting
<class 'str'>
<class 'str'>
<class 'str'>
...
I truly have no idea why you would want to do this, but you can try if you can convert a character in the hash string to an integer first and then print the type. Just change your loop as follows
for i in val:
try:
n = int(i)
except ValueError:
n = i
print(f'character: {n} is {type(n)}')
In Python 3, I want to return the units place of an integer value, then tens, then hundreds and so on. Suppose I have an integer 456, first I want to return 6, then 5 then 4. Is there any way? I tried floor division and for loop but didn't work.
If you look at the list of basic operators from the documentation, for example here,
Operator Description Example
% Modulus Divides left hand operand by right hand operand and returns remainder b % a = 1
// Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity): 9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0
With that knowledge, you can get what you want as follows:
In [1]: a = 456
In [2]: a % 10
Out[2]: 6
In [3]: (a % 100) // 10
Out[3]: 5
In [4]: a // 100
Out[4]: 4
Write a generator if you want to retrieve digits in different places of your code based on requirement as follows.
If you are not much familiar with Python's generator, have a quick look at https://www.programiz.com/python-programming/generator.
» Here get_digits() is a generator.
def get_digits(n):
while str(n):
yield n % 10
n = n // 10
if not n:
break
digit = get_digits(1729)
print(next(digit)) # 9
print(next(digit)) # 2
print(next(digit)) # 7
print(next(digit)) # 1
» If you wish to iterate over digits, you can also do so as follows.
for digit in get_digits(74831965):
print(digit)
# 5
# 6
# 9
# 1
# 3
# 8
# 4
# 7
» Quick overview about its usage (On Python3's Interactive terminal).
>>> def letter(name):
... for ch in name:
... yield ch
...
>>>
>>> char = letter("RISHIKESH")
>>>
>>> next(char)
'R'
>>>
>>> "Second letter is my name is: " + next(char)
'Second letter is my name is: I'
>>>
>>> "3rd one: " + next(char)
'3rd one: S'
>>>
>>> next(char)
'H'
>>>
I want to ask the user to enter a number which can be float, integer and complex number, but Python 3 considers it as string if I use the input function. If I don't know what type of number the user will enter, how do I calculate the half of the input number?
You can use ast.literal_eval to safely interpret strings as though they were Python literals:
from ast import literal_eval
print(type(literal_eval("1"))) # <class 'int'>
print(type(literal_eval("1.0"))) # <class 'float'>
print(type(literal_eval("1+1j"))) # <class 'complex'>
I would parse it as complex, then only print out the real part if the imaginary part is zero:
try:
num = complex(input('Enter something: '))
except ValueError:
print('Invalid input')
if num.imag:
print(num / 2)
else:
print(num.real / 2)
Another option is a nested try-except:
num = input('Enter something: ')
try:
print(int(num) / 2)
except ValueError:
try:
print(float(num) / 2)
except ValueError:
try:
print(complex(num) / 2)
except ValueError:
print('Invalid input')
my code for pattern:
def pattern(n):
if n==1:
return '1'
else:
return pattern(n-int(n/2))*2+str(n)
print(pattern(n))
i need:
>>> pattern(1)
1
>>> pattern(2)
112
>>> pattern(4)
1121124
>>> pattern(8)
112112411211248
but i get:
>>> pattern(1)
'1'
>>> pattern(2)
'112'
>>> pattern(4)
'1121124'
>>> pattern(8)
'112112411211248'
i have tried a lot but nothing is working to get rid of those pesky quotes.
The quotes are from the REPL printing the representation of the result of the function call, which is a string. If you do not want the representation then just print the result explicitly instead.