Posts Tagged ‘Convert’

How to convert an integer to a float in Python?

Python provides a float() function that will attempt to convert any value to a float (including strings). n1 = 5 n2 = 2   # This performs integer arithmetic result = n1 / n2 assert result == 2   # Convert the numerator to a float, which results in floating point arithmetic result = float(n1) [...]

How to convert a tuple to a list in Python?

Python provides a list() function that will convert any iterable into a list: colour_tuple = ("Red","Green","Blue") colour_list = list(colour_tuple)   assert colour_list == ["Red","Green","Blue"]