Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In Python, you might often need to convert data from one type to another, such as converting a string to an integer or a float to an integer. This tutorial will guide you through converting data types in Python.
Converting between numeric types:
Convert a float to an int: Use the int()
function to convert a float to an integer. Note that this will truncate the decimal part of the float, not round it.
float_num = 5.7 int_num = int(float_num) print(int_num) # Output: 5
Convert an int to a float: Use the float()
function to convert an integer to a float.
int_num = 5 float_num = float(int_num) print(float_num) # Output: 5.0
Converting between strings and numeric types:
Convert a string to an int: Use the int()
function to convert a string to an integer. The string must contain a valid integer value, or a ValueError
will be raised.
int_string = "42" int_num = int(int_string) print(int_num) # Output: 42
Convert a string to a float: Use the float()
function to convert a string to a float. The string must contain a valid float value, or a ValueError
will be raised.
float_string = "5.7" float_num = float(float_string) print(float_num) # Output: 5.7
Convert an int or a float to a string: Use the str()
function to convert an integer or a float to a string.
int_num = 42 float_num = 5.7 int_string = str(int_num) float_string = str(float_num) print(int_string) # Output: "42" print(float_string) # Output: "5.7"
Converting between lists, tuples, and sets:
Convert a list to a tuple: Use the tuple()
function to convert a list to a tuple.
my_list = [1, 2, 3] my_tuple = tuple(my_list) print(my_tuple) # Output: (1, 2, 3)
Convert a list to a set: Use the set()
function to convert a list to a set. This will remove duplicate items and order the elements arbitrarily.
my_list = [1, 2, 3, 2, 1] my_set = set(my_list) print(my_set) # Output: {1, 2, 3}
Convert a tuple or a set to a list: Use the list()
function to convert a tuple or a set to a list.
my_tuple = (1, 2, 3) my_set = {4, 5, 6} my_list_from_tuple = list(my_tuple) my_list_from_set = list(my_set) print(my_list_from_tuple) # Output: [1, 2, 3] print(my_list_from_set) # Output: [4, 5, 6] (order may vary)
In summary, Python provides various functions to convert data between different types, including numeric types, strings, and collection.
Type casting in Python:
float_number = 3.14 int_number = int(float_number)
Implicit vs explicit type conversion in Python:
int()
, float()
, etc.implicit_conversion = 5 + 3.14 # Implicit conversion to float explicit_conversion = int(3.14)
Converting between different data types in Python:
int()
, float()
, str()
, etc., for converting between different data types.str_to_int = int("42") int_to_str = str(42)
String to int conversion in Python:
int()
function to convert a string containing digits to an integer.string_number = "42" integer_number = int(string_number)
Float to int conversion in Python:
int()
function to convert a float to an integer. This truncates the decimal part.float_number = 3.14 int_number = int(float_number)
Converting lists and tuples in Python:
list()
and tuple()
.my_list = [1, 2, 3] my_tuple = tuple(my_list)
Handling errors in data type conversion in Python:
try: invalid_conversion = int("abc") except ValueError as e: print(f"Error: {e}")
Type conversion functions in Python:
int()
, float()
, str()
, list()
, and tuple()
are commonly used for type conversion.str_to_int = int("42") float_to_str = str(3.14)