1、算数运算在python2.7 / 运算不够精确 需要引用 _future_ 类库
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from _future_ import division
val = 9/2
print(val)
2、range 与 xrange
【python2.7】 xrange做循环的性能比range好,尤其是返回很大的时候。尽量用xrange吧,除非你是要返回一个列表。
在python3.5中只有range 与xrange功能一样!
#!/usr/bin/env python
# -*- conding:utf-8 -*-
s1 = range(1,10)
s2 = xrange(1,5)
print(s1)
print(s2)
for s in s2:
print s
3、判断 in 或 not in 的使用
#!/usr/bin/env python
# -*- conding:utf-8 -*-
li = "Alex SB"
ret = "Al" in li
print(ret) # => True
li = ["alex","eric","rain"]
ret = "al" in li
print(ret) # => False
4、类 和 对象
str 是几个字符的集合 list 是几个元素的集合
tuple和list是一样的,只不过list可以增删改而tuple不行
dict字典的每个元素,键值对
1、type
temp = "alex"
t = type(temp) #查看temp对象的类
print(t)
# str, ctrl+鼠标左,找到 str类,内部所有的方法
2、dir
temp = "alex"
b = dir(temp)
3、help.type
help(type(temp))
4、直接点击
temp = "alex"
temp.upper()
#鼠标放在upper() 上 ctrl+左键,自动定位到upper功能处
5、索引和切片
取单个元素用索引,取多个元素用切片
#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Bin = b'\xe6\xb2\x88\xe7\x8e\x89\xe5\x8a\xa0'
s = "hello {0}, age {1}"
print(s)
new1 = s.format("alex",23)
print(new1)
s1 = ["join","tom","jack"]
print(s1[0])
print(s1[0:2])
转载于:https://blog.51cto.com/shenyujia/1904831