1.知识树
2.基础类型
int float str boolean
complex 1+12j
list [1,”abc”]
tuple(1,“abc”)
set {a,”bc”,123}
dict {“abc”:1,”c”:2}
type查看当前变量类型
name, age, gender = user
print(name)
print(age)
print(gender)
a, b, c = (1, 2, 3)
print(a)
print(b)
print(c)
3.python中的工厂函数举例如下:
1》int(),long(),float(),complex(),bool()
>>> a=int(9.9)
>>> a
9
>>> b=long(45)
>>> b
45L
>>> f=float(8)
>>> f
8.0
>>> c=complex(8)
>>> c
(8+0j)
>>> b1=bool(7.9)
>>> b1
True
>>> b2=bool(0.0)
>>> b2
False
>>> b3=bool([])
>>> b2
False
>>> b4=bool((34,5))
>>> b4
True
2》str()
>>> s=str(9.9)
>>> s
'9.9'
3》list(),tuple():生成列表或者元组
>>> l=list('python')
>>> l
['p', 'y', 't', 'h', 'o', 'n']
>>> t=tuple('python')
>>> t
('p', 'y', 't', 'h', 'o', 'n')
4》type():查看类型
>>> type(6)
<type 'int'>
>>> type('python')
<type 'str'>
>>> type(u'love')
<type 'unicode'>
>>> class A():
... pass
...
>>> a=A()
>>> type(a)
<type 'instance'>
>>> type(A)
<type 'classobj'>
5》dict():生成一个字典
>>> dict()
{}
>>> dict(one=1,two=2)
{'two': 2, 'one': 1}
>>> dict(zip(('one','two'),(1,2)))
{'two': 2, 'one': 1}
>>> dict([('one',1),('two',2)])
{'two': 2, 'one': 1}
>>> dict([['one',1],['two',2]])
{'two': 2, 'one': 1}
>>> dict((('one',1),('two',2)))
{'two': 2, 'one': 1}
>>> dict((['one',1],['two',2]))
{'two': 2, 'one': 1}
6》set(): 生产可变集合
>>> s=set('python')
>>> s
{'n', 'p', 'o', 'y', 't', 'h'}
>>> s.add(523)#可变集合
>>> s
{'y', 't', 523, 'n', 'h', 'o', 'p'}
zodiac_day = filter(lambda x: x>=(month, day), zodiac_days)
print(zodiac_day)
print(list(zodiac_day) )
fdict = dict(x=1,y=2) # factory mode
print(fdict)
fdict = dict([['x', 1], ['y', 2]])
print(fdict)
fdict = dict((['x', 1], ['y', 2]))
print(fdict)
fdict = dict([('x', 1), ('y', 2)])
print(fdict)
fdict = dict((('x', 1), ('y', 2)))
print(fdict)
3.输入语句
x = int(input("please input an number"))
4.list常用方法 append appendleft pop insert remove sort count
value = [1,2 ,3,2,2,2]
for element in value:
print(element)
print(value.count(2))
value.sort()
print(value)
print(id(value))
要按排序后的顺序循环序列的话,使用 sorted() 函数,它不改动原序列,而是生成一个新的已排序的序列:
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> for f in sorted(set(basket)):
... print(f)
...
apple
banana
orange
pear
5.序列的切片 索引 连接 重复 in not in
string = 'xiaoyi' # get value by index
print(type(string))
# copy = string[0] + string[1] + string[2:6] # note: [2:6] means [2 5] or[2 6)
# print(copy)
# copy = string[:4] # start from 1
# print(copy)
# copy = string[2:] # to end
# print(copy)
# copy = string[::1] # step is 1, from start to end
# print(copy)
# copy = string[::2] # step is 2
# print(copy)
# copy = string[-1] # means 'i', the last one
# print(copy)
copy = string[-4:-2:-1] # means 'yoa', -1 step controls direction
print(copy)
6.分支语句
if elif else分支 再if/elif/else后加冒号:后面括号内语句要缩进4个空格
i = int(input("please input a number"))
if i <2:
print("small")
elif i==2:
print("medium")
elif i==3:
print("bigger")
else:
print("largest")
if not True:
print(" if option")
else:
print("else option")
while False:
print("hello")
x = int(input("please input an number"))
if x == 1:
break
else:
print("enter while else option")
7.for语句
value = [1,2 ,3,2,2,2]
for element in value:
print(element)
zodiac_name = (u'摩羯座', u'水瓶座', u'双鱼座', u'白羊座', u'金牛座', u'双子座',
u'巨蟹座', u'狮子座', u'处女座', u'天秤座', u'天蝎座', u'射手座')
zodiac_days = ((1, 20), (2, 19), (3, 21), (4, 21), (5, 21), (6, 22),
(7, 23), (8, 23), (9, 23), (10, 23), (11, 23), (12, 23))
for element in zodiac_name:
print(element)
for element in zodiac_days:
print(element)
for i in range(12):
print("月份%s月 是%s" %(i, zodiac_name[i%12]))
range
- # range(start, end, step), if not set step, default is 1,
- # if not set start, default is 0, should be noted that it is [start, end), not [start, end]
- range(5) # [0, 1, 2, 3, 4]
- range(1, 5) # [1, 2, 3, 4]
- range(1, 10, 2) # [1, 3, 5, 7, 9]
- for i in range(1, 100, 1):
- print i
- dic.items() # return [(1, 111), (2, 222), (5, 555)]
- for key,value in dic.items(): # because we can: a,b=[1,2]
- print key, ': ', value
- else:
- print 'ending'
8.使用字典 字典的方法items keys vaules
直接添加键值对 [''] = xxx
取值 ['']
取所有键 keys()
>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'sape': 4139, 'guido': 4127, 'jack': 4098}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'guido': 4127, 'irv': 4127, 'jack': 4098}
>>> list(tel.keys())
['irv', 'guido', 'jack']
在字典中循环时,关键字和对应的值可以使用 items()
方法同时解读出来:
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.items():
... print(k, v)
...
gallahad the pure
robin the brave
9.列表推导式
列表推导式由包含一个表达式的括号组成,表达式后面跟随一个 for 子句,之后可以有零或多个 for 或 if 子句。结果是一个列表,由表达式依据其后面的 for 和 if 子句上下文计算而来的结果构成。
>>> squares = []
>>> for x in range(10):
... squares.append(x**2)
...
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
等价于
squares = [x**2 for x in range(10)]
ddict = {}
blist = [1,2,3]
clist = [3,1,4]
for x in blist
for y in clist
if x!= y:
ddict.append((x, y)
print(ddict)
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]