Home Backend Development Python Tutorial python编程开发之类型转换convert实例分析

python编程开发之类型转换convert实例分析

Jun 10, 2016 pm 03:07 PM
convert python type conversion

本文实例讲述了python编程开发之类型转换convert。分享给大家供大家参考,具体如下:

在python的开发过程中,难免会遇到类型转换,这里给出常见的类型转换demo:

   int(x [,base ])         将x转换为一个整数
   long(x [,base ])        将x转换为一个长整数
   float(x )               将x转换到一个浮点数
   complex(real [,imag ])  创建一个复数
   str(x )                 将对象 x 转换为字符串
   repr(x )                将对象 x 转换为表达式字符串
   eval(str )              用来计算在字符串中的有效Python表达式,并返回一个对象
   tuple(s )               将序列 s 转换为一个元组
   list(s )                将序列 s 转换为一个列表
   chr(x )                 将一个整数转换为一个字符
   unichr(x )              将一个整数转换为Unicode字符
   ord(x )                 将一个字符转换为它的整数值
   hex(x )                 将一个整数转换为一个十六进制字符串
   oct(x )                 将一个整数转换为一个八进制字符串

下面是我做的demo:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

#类型转换

#convert

#convert to int

print('int()默认情况下为:', int())

print('str字符型转换为int:', int('010'))

print('float浮点型转换为int:', int(234.23))

#十进制数10,对应的2进制,8进制,10进制,16进制分别是:1010,12,10,0xa

print('int(\'0xa\', 16) = ', int('0xa', 16))

print('int(\'10\', 10) = ', int('10', 10))

print('int(\'12\', 8) = ', int('12', 8))

print('int(\'1010\', 2) = ', int('1010', 2))

#convert to long

print('int浮点型转换为int:', int(23))

#convert to float

print('float()默认情况下为:', float())

print('str字符型转换为float:', float('123.01'))

print('int浮点型转换为float:', float(32))

#covert to complex

print('创建一个复数(实部+虚部):', complex(12, 43))

print('创建一个复数(实部+虚部):', complex(12))

#convert to str

print('str()默认情况下为:', str())

print('float字符型转换为str:', str(232.33))

print('int浮点型转换为str:', str(32))

lists = ['a', 'b', 'e', 'c', 'd', 'a']

print('列表list转换为str:', ''.join(lists))

#covert to list

strs = 'hongten'

print('序列strs转换为list:', list(strs))

#covert to tuple

print('列表list转换为tuple:', tuple(lists))

#字符和整数之间的转换

#char coverted to int

print('整数转换为字符chr:', chr(67))

print('字符chr转换为整数:', ord('C'))

print('整数转16进制数:', hex(12))

print('整数转8进制数:', oct(12))

Copy after login

运行效果:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32

Type "copyright", "credits" or "license()" for more information.

>>> ================================ RESTART ================================

>>>

int()默认情况下为: 0

str字符型转换为int: 10

float浮点型转换为int: 234

int('0xa', 16) = 10

int('10', 10) = 10

int('12', 8) = 10

int('1010', 2) = 10

int浮点型转换为int: 23

float()默认情况下为: 0.0

str字符型转换为float: 123.01

int浮点型转换为float: 32.0

创建一个复数(实部+虚部): (12+43j)

创建一个复数(实部+虚部): (12+0j)

str()默认情况下为:

float字符型转换为str: 232.33

int浮点型转换为str: 32

列表list转换为str: abecda

序列strs转换为list: ['h', 'o', 'n', 'g', 't', 'e', 'n']

列表list转换为tuple: ('a', 'b', 'e', 'c', 'd', 'a')

整数转换为字符chr: C

字符chr转换为整数: 67

整数转16进制数: 0xc

整数转8进制数: 0o14

>>>

Copy after login

希望本文所述对大家Python程序设计有所帮助。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the function of C language sum? What is the function of C language sum? Apr 03, 2025 pm 02:21 PM

There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Is distinctIdistinguish related? Is distinctIdistinguish related? Apr 03, 2025 pm 10:30 PM

Although distinct and distinct are related to distinction, they are used differently: distinct (adjective) describes the uniqueness of things themselves and is used to emphasize differences between things; distinct (verb) represents the distinction behavior or ability, and is used to describe the discrimination process. In programming, distinct is often used to represent the uniqueness of elements in a collection, such as deduplication operations; distinct is reflected in the design of algorithms or functions, such as distinguishing odd and even numbers. When optimizing, the distinct operation should select the appropriate algorithm and data structure, while the distinct operation should optimize the distinction between logical efficiency and pay attention to writing clear and readable code.

How to understand !x in C? How to understand !x in C? Apr 03, 2025 pm 02:33 PM

!x Understanding !x is a logical non-operator in C language. It booleans the value of x, that is, true changes to false, false changes to true. But be aware that truth and falsehood in C are represented by numerical values ​​rather than boolean types, non-zero is regarded as true, and only 0 is regarded as false. Therefore, !x deals with negative numbers the same as positive numbers and is considered true.

Can C language user identifiers contain spaces? Can C language user identifiers contain spaces? Apr 03, 2025 pm 01:51 PM

C language identifiers cannot contain spaces because they can cause confusion and difficulty in maintaining. The specific rules are as follows: they must start with letters or underscores. Can contain letters, numbers, or underscores. Cannot contain illegal characters (such as special symbols).

What does sum mean in C language? What does sum mean in C language? Apr 03, 2025 pm 02:36 PM

There is no built-in sum function in C for sum, but it can be implemented by: using a loop to accumulate elements one by one; using a pointer to access and accumulate elements one by one; for large data volumes, consider parallel calculations.

How to apply snake nomenclature in C language? How to apply snake nomenclature in C language? Apr 03, 2025 pm 01:03 PM

In C language, snake nomenclature is a coding style convention, which uses underscores to connect multiple words to form variable names or function names to enhance readability. Although it won't affect compilation and operation, lengthy naming, IDE support issues, and historical baggage need to be considered.

Does H5 page production require continuous maintenance? Does H5 page production require continuous maintenance? Apr 05, 2025 pm 11:27 PM

The H5 page needs to be maintained continuously, because of factors such as code vulnerabilities, browser compatibility, performance optimization, security updates and user experience improvements. Effective maintenance methods include establishing a complete testing system, using version control tools, regularly monitoring page performance, collecting user feedback and formulating maintenance plans.

See all articles