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

Python关键函数

2016-06-17 15:37 615 查看
1.字符串函数
str1='string' 
str1.upper()  #全部转换为大写
str1.isalpha() #是否全部是字母
str1.lower() #全部转换为小写
len(str1) 长度
str(str1) 将任意字符转换为字符串
2.数学函数The 
abs()
 function
returns the absolute
value
 of the number it takes as an
argument—that is, that number's distance
from 
0
 on
an imagined number line. For
instance, 
3
 and
-3
 both
have the same absolute value: 
3
.
The
abs()
 function
always returns a positive value, and
unlike 
max()
 and 
min()
,
it only takes a single number.Finally, the 
type()
 function
returns the typeof
the data it receives as an argument. If you ask Python to do the
following:
[code]print type(42)
print type(4.2)
print type('spam')

[/code]Python will output:
[code]'int'>
'float'>
'str'>

[/code]
[code]3.序列函数:

[/code]
[code]append()

[/code]
animals = ["ant", "bat", "cat"]
print animals.index("bat")



This exercise will expand on ways to remove items from a list. You actually have a few options. For a list called[code]n
:
[/code]
n.pop(index)
 will
remove the item at
index
 from
the list and return it to you:
[code]n = [

[/code]
n.remove(item)
 will remove the
actual
item
 if
it finds it:
[code]n.remove(

[/code]
del(n[1])
 is
like 
.pop
 in
that it will remove the item at the given index, but it won't
return it:
[code]

[/code]


nts " d = {"foo" : "bar"}for key in d:
print d[key] # prints
ar" 

序列求和可直接用内建的sum(序列名称)函数enumerate函数choices = ['pizza', 'pasta', 'salad', 'nachos']print 'Your choices are:'for index, item in enumerate(choices):       print
index+1, item zip()函数:该函数可以将两个序列配对。例如list_a = [3, 9, 17, 15, 19]list_b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90],那么print
zip(list_a, list_b)之后的结果为[(3,2),(9,4),(17,8),(15,10)(19,30)]


[code]d = {

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