Day 20
基础语法
1、环境:python2内置,安装并使用python3,最新版3.12版可以使用源码安装
# 查看python版本号
root@python \~\]#yum list installed\|grep python
\[root@python \~\]#yum list installed\|grep epel
\[root@python \~\]# yum -y install python3
\[root@python \~\]# python3 --version
Python 3.6.8
****# 进入python3的编辑状态****
\[root@python \~\]# python3
Python 3.6.8 (default, Nov 14 2023, 16:29:52)
\[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)\] on linux
Type "help", "copyright", "credits" or "license" for more information.
\>\>\> print('hello world')
hello world
\>\>\> a=3
\>\>\> b='abc'
\>\>\> type(a)
\
>>> type(lista)
<class 'list'>
# 插入元素insert;追加元素append
>>> listb.insert(1, 'huajuan') //在下标为1的位置插入元素
>>> listb
'tom', 'huajuan', 'jerry'
>>> listb.append('tomcat') //在所有元素之后添加
>>> listb
'tom', 'huajuan', 'jerry', 'tomcat'
>>> listb
# 删除使用pop(),默认从尾部删除
'tom', 'huajuan', 'jerry', 'tomcat'
>>> listb.pop()
'tomcat'
>>> listb
'tom', 'huajuan', 'jerry'
'tom', 'huajuan', 'jerry'
>>> listb.pop()
'jerry'
>>> listb
# 使用remoe移除指定元素
>>> listb.remove('tom')
>>> listb
'huajuan'
# 使用下标移除元素
>>> listb.remove(listb[0])
>>> listb
# 修改元素
>>> listb[2]
'jerry'
>>> listb[2]='张三'
>>> listb[2]
'张三'
字典:使用key-value记录信息
# 新建
>>> dic={'name': 'huajuan', 'age': '21', 'gender': ' 女' 'phone': '1888'}
>>> dic
{'name': 'huajuan', 'age': '21', 'gender': '女', 'phone': '1888'}
# 新增
>>> dic['realname']='tangpin'
>>> dic
{'id': 1, 'username': 'abc', 'password': '123', 'realname': 'tangpin'}
>>> a=[1, 2, 4, 3, 5]
>>> dic['other']=a
>>> dic
{'username': 'abc', 'password': 'abc$_epl', 'realname': 'tangpin', 'other': [1, 2, 4, 3, 5]}
>>> a=[1, 2, 3]
>>> b={'username': 'abc', 'password': '123'}
>>> a.append(b)
>>> b['a']=a
>>> a
1, 2, 3, {'username': 'abc', 'password': '123', 'a': \[...\]}
>>> b
{'username': 'abc', 'password': '123', 'a': [1, 2, 3, {...}]}
# 修改
>>> dic['password']='abc$_epl'
>>> dic
{'username': 'abc', 'password': 'abc$_epl', 'realname': 'tangpin'}
# 移除
>>> dic.pop('id')
1
>>> dic
{'username': 'abc', 'password': '123', 'realname': 'tangpin'}
>>>
# 取出所有的键
>>> dic.keys()
dict_keys(['username', 'password', 'realname', 'other'])
# 取出所有的值
>>> dic.values()
dict_values(['abc', 'abc$_epl', 'tangpin', [1, 2, 4, 3, 5]])
# 取出所有元素
>>> dic.items()
dict_items([('username', 'abc'), ('password', 'abc$_epl'), ('realname', 'tangpin'), ('other', [1, 2, 4, 3, 5])])
元组:有序,使用下标查看但不可修改,要修改将其强转为列表
>>> tup=(4, 5, 6, 7)
>>> tup[1]
5
>>> tup[1]=888
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> list(tup)
4, 5, 6, 7
list()看将dic的key生成一个列表;可把tuple()强转为列表;tuple可将dic和list变成元组
# 总结
|------------|-----------------------------------|--------------------------------------------|
| 功能 | 指令 | 说明 |
| 列表的创建 | [] | 符号本身就是列表 |
| 列表的创建 | list(元组) | 将元组转成列表 |
| 列表的创建 | list(字典) | 提取字典的key转成列表 |
| 字典取值 | 字典.keys() | 字典中的key返回⼀个列表 |
| 字典取值 | 字典.values() | 字典中的value组成的列表 |
| 字典取值 | 字典.items() | 字典中的每个k-v组成元组,这些元组组成一个新的列表 |
| 列表管理(增删该查) | list.insert(insex, value) | 在索引值为index的元素之前差 ⼈⼀个元素 |
| 列表管理(增删该查) | list.append(value) | 在所有元素之后添加⼀个元素 |
| 列表管理(增删该查) | list.[index]=value | 将索引为index元素的值修改为 value |
| 列表管理(增删该查) | list.pop() | 删除最后⼀个元素 |
| 列表管理(增删该查) | del dist | 释放list内存 |
| 查看列表 | list | 显示列表中的所有数据 |
| 查看列表 | list[index] | 返回索引值为index的元素 |
| 字典的创建 | {} | 代表一个空字典 |
| 字典的创建 | {k0: v0, k1: v1...} | 这是有初始值的列表 |
| 字典的创建 | dic{(k0, v0), (k1, v1), (k2, v2)} | []中的每个()中都有2个值,一个是key,一个是value,自动解析为一个字典 |
| 元组 | (),(1,2,3,4) | 创建空元组,创建有初始值的元组,也可以从dict中提 取,也可以将列表直接转成元组 |
# 编写py文件
vim test01.py
# 执 行 py脚本
python3 001.py
# 调试py脚本 ,输入n回车执行下一行代码,输入q退出
python3 -m pdb test01.py
生成0~9内随机数
import random
n=random.randint(0, 10)
创建目录
import os
os.mkdir("/opt/aaa/")
4、选择语句和循环语句
1)选择语句
=======if...else...=====
>>> if True:
... print('i am true')
... else:
... print('i am false')
...
i am true
root@python \~\]# vim py001.py \[root@python \~\]# python3 py001.py i am true if True: print('i am true') else: print('i am false') Ctrl + Z退出 \[root@python \~\]# fg //切换窗口 python3 ****=======单分支语句======**** \>\>\> n=58 \>\>\> if n\>90: ... print('优秀') ... elif n\>80: ... print('良好') ... elif n\>70: ... print('中等') ... elif n\>60: ... print('及格') ... else: ... print('不及格') ... 不及格 \[root@python \~\]# vim test.py import random n=random.randint(0, 100) print('随机分数为:', n) if n\>90: print('优秀') elif n\>80: print('良好') elif n\>70: print('中等') elif n\>60: print('及格') else: print('不及格') \[root@python \~\]# python3 test.py 随机分数为: 57 不及格 \[root@python \~\]# python3 test.py 随机分数为: 40 不及格 \[root@python \~\]# python3 test.py 随机分数为: 11 不及格 ****=======多分支语句======**** import random n=random.ranint(50, 100) print('rand:', n) if n\>90: print('yx') else: if n\>80: print('lh') else: if n\>70: print('zd') else: if n\>59: print('jg') else: print('bjg') 2)循环语句 ****======for语句=======**** \>\>\> list(range(0, 9)) \[0, 1, 2, 3, 4, 5, 6, 7, 8
>>> for i in range(9):
... print(i)
...
0
1
2
3
4
5
6
7
8
>>> n=0
>>> for i in range(1, 101): //不包含末尾值
... n=n+i
...
>>> print(n)
5050
root@python \~\]# vim test02.py n=0 for i in range(101): n=n+i print(n) \[root@python \~\]# python3 test02.py 5050 ****# 在列表中循环**** \>\>\> for var in \['a', 'b', 'c'\]: ... print(var) ... a b c \>\>\> a=\['e', 'f', 'g', 'h'
>>> for var in a:
... print(var)
...
e
f
g
H
# 在字典中循环,在字典中循环,此时 仅输出key
>>> d={'id': 1001, 'name': '花卷', 'age': 21, 'gender': '女'}
>>> for var in d:
... print(var)
...
id
name
age
gender
>>> for var in d:
... print(var, '-', d[var])
...
id - 1001
name - 花卷
age - 21
gender - 女
>>> for var in d.values():
... print(var)
...
1001
花卷
21
女
# 在元组中循环
>>> tupel0=('a', 'c', 'e')
>>> for var in tuple0:
... print(var)
=====案例=====
>>> b=list(range(101))
>>> 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整除
=======while语句=========
# 1~100的累加
>>> n=0
>>> i=1
>>> while i<101:
... n=n+i
... i=i+1
>>> n
5050
# break用法
>>> while True:
... print('aaa')
... break
...
aaa
# continue用法
>>> i=1
>>> while True:
... if i==3:
... continue
... print(i)
... i+=1
...
1
2 //只输出1、2然后卡住