python 常用函数笔记

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]





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值