读廖雪峰python教程
1、四个空格
2、
自己运行的部分代码行
>>> s4=r'''hello,
... lisa!'''
>>> print(s4)
hello,
lisa!
>>>
3、unicode
ASCII
可变长编码UTF-8
>>> s1=72
>>> s2=85
>>> r=s2/s1
>>> print('xiaomingchengjitishengle%.2f'%(r))
xiaomingchengjitishengle1.18
>>> '中文'.encode('gb2312')
b'\xd6\xd0\xce\xc4'
4、list
>>> classmates = ['xumin','suman']
>>> classmates
['xumin', 'suman']
>>> s1 = 1
>>> s1
1
>>> len(classmates)
2
>>> classmates90]
File "<stdin>", line 1
classmates90]
^
SyntaxError: invalid syntax
>>> classmates[0]
'xumin'
>>> classmates[-1]
'suman'
>>> classmates.append('tanfeng')
>>> classmates
['xumin', 'suman', 'tanfeng']
>>> classmates.inset(1,'zhongyichen')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'inset'
>>> classmates.insert(1,'zhongyichen')
>>> classmates
['xumin', 'zhongyichen', 'suman', 'tanfeng']
>>>
>>> classmates.pop()
'tanfeng'
>>> classmates
['xumin', 'zhongyichen', 'suman']
>>> classmates[1] = 'tanfeng'
>>> classmates
['xumin', 'tanfeng', 'suman']
>>>
>>>
>>> list = ['a',123,True]
>>> list
['a', 123, True]
>>> s = ['python','java',['a','php'],'scheme']
>>> len(s)
4
>>> s
['python', 'java', ['a', 'php'], 'scheme']
>>> list = []
>>> len(list)
0
>>> list
[]
5、tuple
元素不可变
6、if语句
>>> age = 20
>>> if age >= 18:
... print('your age is ',age)
File "<stdin>", line 2
print('your age is ',age)
^
IndentationError: expected an indented block
7、循环
#依次打印姓名
>>> for a in name:
... print(a)
...
name1
name2
name3
>>> name
['name1', 'name2', 'name3']#打印奇数
>>> while n <10:
... n = n+1
... if n % 2 ==0:
... continue
... print(n)
...
1
3
5
7
9
8、dict
类map
>>> d = {'xumin':95,'suman':100}
>>> d
{'xumin': 95, 'suman': 100}
>>> d['xumin']
95
>>> d['tanfeng'] = 99
>>> d['tanfeng']
99
>>> 'tanfeng' in d
True
>>> d.get('xumin')
95
>>> d.get('xuminmin',-1)
-1
>>> d
{'xumin': 95, 'suman': 100, 'tanfeng': 99}
>>> d.pop('suman')
100
>>> d
{'xumin': 95, 'tanfeng': 99}
>>>
key value
9、set
类dict,与list连用
>>> s = set([1,1,2,3,3])
File "<stdin>", line 1
s = set([1,1,2,3,3])
^
SyntaxError: invalid character in identifier
>>> s = set([1,1,2,3,3])
>>> s
{1, 2, 3}
>>> s.add(4)
>>> s
{1, 2, 3, 4}
>>> s.remove(4)
>>> s
{1, 2, 3}
>>> s1 = set([1,2,3])
>>> s1
{1, 2, 3}
>>> s2 = set([2,3,4])
>>> s1 & s2
{2, 3}
>>> s1 | s2
{1, 2, 3, 4}
10、
ing