Typecasting in Python

Which of the following Python2 pieces of code given below give(s) error?

1.

1
a = int ('4.0')

2.

1
b = float ('-9')

3.

1
c = str (12)

2 1 and 3 1, 2, and 3 1 1 and 2 3 2 and 3 None of these

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Viki Zeta
May 17, 2017
1
2
a = int("4.0")
#          ^ decimal system is not a base 10 number system.

Zeeshan Ali
May 17, 2017
1
a = int ('4.0')


ValueError Traceback (most recent call last)

<ipython-input-41-8073d7eae12d> in <module>()

----> 1 int('4.0')

ValueError: invalid literal for int() with base 10: '4.0'

Reason: 4.0 is a float value in the string '4.0'

Solution: Convert the string into float, then the float to int as follow

1
a = int (float ('4.0'))

4

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...