您的位置:首页 > 编程语言 > Python开发

python 常用函数笔记

2013-07-31 01:15 357 查看
linux

幂运算:

>>>2**3 或 >>>pow(2,3)

绝对值::

>>>abs(-10)

四舍五入:

>>>round(4.5)

向下取整:

>>>import math

>>>math.floor(32.9)

向上取整:

>>>import math

>>>from math import ceil

>>>math.import(43.3)

转化为整数:

>>>int(32.0)

求平方根:

>>>from math import sqrt

>>>sqrt(9)

求负数的平方根:

>>>import cmath (这里不能用 from math import sqrt) 否则会无法求负数的平方根

>>>cmath.sqrt(-1)

1j

复数的使用:>>>(1+3j)*(9+4j)

打印pi:

>>>from math import pi

>>>pi

将python值转化为字符串:

>>>repr(42) repr是一种函数

>>>str(42) str与int、long一样 是一种类型

>>>`42`

打印一个包含数字的句子:

>>>temp=42

>>>print "The temperature is" + `temp`

input(只能输入数字):

>>>input("Input Enter a number:")

raw_input(输入字符串)

>>>raw_input("Input Enter a string:")

输出长字符串 并且需要换行(用三个单引号或三个单引号,其中输出符号不需要用转义):

>>>print '''This is a long string.

It continues here

And it's not over yet.'''

换行符可直接加在内容中

>>>print 'Hello,\nworld'

输出原始字符串(不用在文中对符号进行转义)

pinrt r'C:\Windows\system32'

list(将字符串列表):

>>>list('hello')

列表删除元素:

>>>names=['Alice','Beth','Cecil','Dee-Dee','Earl']

>>>del names[2]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: