Home > Backend Development > Python Tutorial > Introducing common Python data type conversion functions

Introducing common Python data type conversion functions

王林
Release: 2024-01-20 09:04:06
Original
1362 people have browsed it

Introducing common Python data type conversion functions

Introduction to commonly used data type conversion functions in Python

In the Python programming language, data type conversion is a common and important operation. Through data type conversion, we can convert a data object from one type to another, allowing us to process different types of data more flexibly. This article will introduce commonly used data type conversion functions in Python and provide specific code examples.

  1. int() function
    int() function is used to convert a given data object to an integer (int) data type. It can handle different types of data such as strings and floating point numbers, and convert them into corresponding integer types.

Sample code:

num1 = "10"
num2 = 3.5
num3 = 7.8

result1 = int(num1)
result2 = int(num2)
result3 = int(num3)

print(result1)  # 输出结果为 10
print(result2)  # 输出结果为 3
print(result3)  # 输出结果为 7
Copy after login
  1. float() function
    float() function is used to convert a given data object to floating point (float) data type. It can handle different types of data such as integers and strings, and convert them into corresponding floating point types.

Sample code:

num1 = 10
num2 = "3.5"
num3 = "7.8"

result1 = float(num1)
result2 = float(num2)
result3 = float(num3)

print(result1)  # 输出结果为 10.0
print(result2)  # 输出结果为 3.5
print(result3)  # 输出结果为 7.8
Copy after login
  1. str() function
    str() function is used to convert the given data object to the string (str) data type . It can handle different types of data such as integer and floating point types, and convert them into corresponding strings.

Sample code:

num1 = 10
num2 = 3.5

result1 = str(num1)
result2 = str(num2)

print(result1)  # 输出结果为 "10"
print(result2)  # 输出结果为 "3.5"
Copy after login
  1. list() function
    list() function is used to convert a given data object to a list (list) data type. It can handle different types of data such as strings, tuples, and sets, and convert them into corresponding lists.

Sample code:

str1 = "Hello, World!"
tuple1 = (1, 2, 3, 4, 5)
set1 = {1, 2, 3, 4, 5}

result1 = list(str1)
result2 = list(tuple1)
result3 = list(set1)

print(result1)  # 输出结果为 ['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']
print(result2)  # 输出结果为 [1, 2, 3, 4, 5]
print(result3)  # 输出结果为 [1, 2, 3, 4, 5]
Copy after login
  1. tuple() function
    tuple() function is used to convert a given data object to a tuple data type . It can process different types of data such as strings, lists, sets, etc., and convert them into corresponding tuples.

Sample code:

str1 = "Hello, World!"
list1 = [1, 2, 3, 4, 5]
set1 = {1, 2, 3, 4, 5}

result1 = tuple(str1)
result2 = tuple(list1)
result3 = tuple(set1)

print(result1)  # 输出结果为 ('H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!')
print(result2)  # 输出结果为 (1, 2, 3, 4, 5)
print(result3)  # 输出结果为 (1, 2, 3, 4, 5)
Copy after login

Summary:
This article introduces commonly used data type conversion functions in Python and their specific code examples. By using these functions, we can flexibly convert between different types of data and adapt the data to the data type we need. Mastering the use of these functions is of great help in daily programming work.

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template