Home  >  Article  >  Backend Development  >  Data type conversion in Python

Data type conversion in Python

王林
王林Original
2023-06-10 10:30:072591browse

Python is a very flexible programming language that supports a variety of data types, such as integers, floating point numbers, strings, etc. During the development process, it is often necessary to convert different types of data in order to perform different calculations or operations. This article will introduce data type conversion methods in Python.

  1. Integer conversion

In Python, other types of data can be converted to integer types through the int function. For example, you can convert a string type number to an integer type through the following code:

num_str = '123'
num_int = int(num_str)
print(num_int)

The output result is:

123
  1. Floating point number conversion

Similar to integer types, Python also provides the float function to convert other types of data into floating point types. For example, we can convert a string type number to a floating point type:

num_str = '3.1415926'
num_float = float(num_str)
print(num_float)

The output result is:

3.1415926
  1. String conversion

In In Python, you can use the str function to convert other types of data into string types. For example, we can convert a number of integer type or floating point type to a string type:

num_int = 123
num_float = 3.1415926
num_str1 = str(num_int)
num_str2 = str(num_float)
print(num_str1, num_str2)

The output result is:

123 3.1415926
  1. Conversion of lists, tuples and dictionaries

In addition to converting basic data types, Python also provides ways to convert list, tuple, and dictionary type data.

Convert the list into a tuple:

list1 = [1, 2, 3]
tuple1 = tuple(list1)
print(tuple1)

The output result is:

(1, 2, 3)

Convert the tuple into a list:

tuple2 = (4, 5, 6)
list2 = list(tuple2)
print(list2)

The output result is:

[4, 5, 6]

Convert dictionary keys and values ​​into lists:

dict1 = {'a': 1, 'b': 2, 'c': 3}
list_keys = list(dict1.keys())
list_values = list(dict1.values())
print(list_keys, list_values)

The output result is:

['a', 'b', 'c'] [1, 2, 3]
  1. Boolean type conversion

In Python, other types of data can be converted into Boolean types through the bool function. Any non-zero number or non-empty object will be converted to True, and 0 or empty object will be converted to False.

For example, we can convert any number into a Boolean type:

num1 = 123
num2 = 0
bool1 = bool(num1)
bool2 = bool(num2)
print(bool1, bool2)

The output result is:

True False

Summary

Data type conversion in Python programming Very common. This article introduces common data type conversion methods in Python, covering various types such as integers, floating point numbers, strings, lists, tuples, and dictionaries. Mastering these conversion methods can make it easier for us to perform different types of data processing and operations.

The above is the detailed content of Data type conversion in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn