一、检查并配置python环境(python2内置)
1、检测是否安装
2、安装python3
3、查看安装版本
4、开发工具
5、修改pip镜像为清华
二、数据类型和变量
三大数据类型
三、数据集合
1、列表 list
(1)管理列表
python为开发提供了丰富的使用手册
help(lista) 通过上下方向键,enter,space键来翻阅信息,使用q退出查看,more less
创建列表
lista=[]
listc=[1,2,3]
修改列表
追加元素
lista.append(item) 在所有元素之后添加元素
插入元素
listb.insert(pos,item)在pos之前插入item
删除元素remove pop
list.pop()删除list中的最后一个元素
list.remove(list[index])删除序号为index的元素
修改元素
list[index]=newvalue
del list
>>> name1="张三"
>>> name2="李四"
>>> name3="王五"
>>> print(name1,name2,name3)
张三 李四 王五
>>> lista=["张三","包丽婷","包丽婷的男朋友是我嘿嘿"]
>>> type(lista)
<class 'list'>
>>> print(lista)
['张三', '包丽婷', '包丽婷的男朋友是我嘿嘿']
>>> listb=["tom","jerry"]
>>> listb
['tom', 'jerry']
>>> listb.append("tomcat")
>>> listb
['tom', 'jerry', 'tomcat']
>>> listb.insert(1,"cat")
>>> listb
['tom', 'cat', 'jerry', 'tomcat']
>>> listb.pop()
'tomcat'
>>> listb
['tom', 'cat', 'jerry']
>>> listb.remove('cat')
>>> listb
['tom', 'jerry']
>>> listb[0]
'tom'
>>> listb[2]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> listb.remove(listb[0])
>>> listb
['jerry']
2、 字典
3、 元组
>>> tup10=(1,2,3,4)
>>> tup10
(1, 2, 3, 4)
>>> tup10[0]
1
>>> tup10[3]
4
>>> tup10[1]=666
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> list(tup10)
[1, 2, 3, 4]
>>> aa=list(tup10)
>>> aa
[1, 2, 3, 4]
>>> tuple(aa)
(1, 2, 3, 4)
>>> dict={"a":1,"b":2,"c":3}
>>> dict
{'a': 1, 'b': 2, 'c': 3}
>>> dict.keys()
dict_keys(['a', 'b', 'c'])
>>> dict.items()
dict_items([('a', 1), ('b', 2), ('c', 3)])
>>> list(dict)
['a', 'b', 'c']
四、选择语句和循环语句
1、选择语句
(1)必须缩进
(2)if
>>> a=3
>>> b=4
>>>
[1]+ 已停止 python3
[root@localhost ~]
python3
>>> a
3
>>> b
4
if condition0:
statement0
if condition1:
block1
else:
block2
else:
statement1
(3)if多分枝
if condition0:
block0
elif condition1:
block1
elif condition2:
block1
...
else:
block2
(4)switch
2、循环语句
(1)for
for var in list:
print(var)
for i in range(101):
n=n+i
print(n)
在列表中循环
for var in ["a","b","c"]:
print(var)
在字典中遍历
d={"id":1001,"name":"张三","age":19}
for var in d:
print(d)
print(d[var])
for var in d.values():
print(var)
print(d[var])
for var in d.keys():
print(var)
在元组中遍历
tup10=("a","b","v")
for var in tup10:
print(var)
1.1在列表中遍历
>>> for var in ["a","b","c"]:
... print(var)
...
a
b
c
>>> a=["e","f","g"]
>>> for var in a:
... print(var)
...
e
f
g
1.2在字典中循环遍历
>>> d={"id":1001,"name":"张三","age":18,"gender":"男"}
>>> for var in d:
... print (var)
...
id
name
age
gender
>>> for var in d:
... print (var,"-",d[var])
...
id - 1001
name - 张三
age - 18
gender - 男
>>> for var in d.values():
... print(var)
...
1001
张三
18
男
1.3在元组里面遍历
>>> tup10=("a","b","e")
>>> for var in tup10:
... print (var)
...
a
b
e
3、 案例(0-100之间可以被7整除的数)
>>> b=list(range(101))
>>> b
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
>>> for i in b:
... if i%7==0:
... print (i,"可以被7整除")
...
...
0 可以被7整除
7 可以被7整除
14 可以被7整除
21 可以被7整除
28 可以被7整除
35 可以被7整除
42 可以被7整除
49 可以被7整除
56 可以被7整除
63 可以被7整除
70 可以被7整除
77 可以被7整除
84 可以被7整除
91 可以被7整除
98 可以被7整除
(2)while
while condition:
block
>>> n=0
>>> i=1
>>> while i<101:
... n+=i
... i+=1
...
>>> n
5050
>>> i=1
>>> n=0
>>> while True:
... print (i)
>>> while True:
... print("abc")
... break
...
abc
>>> while True:
... print("abc")
... continue
>>> i=1
>>> while True:
... if i==3:
... continue
... print(i)
... i+=1
...
1
2
五、常用的工具api