python基础

一、Python3

1、装包

root@zhu \~\]# yum list installed \| grep python \[root@zhu \~\]# yum -y install python3 \[root@zhu \~\]# python3 --version Python 3.12.4 2、进入python测试 \[root@zhu \~\]# python3 Python 3.12.4 (main, Jul 16 2024, 13:09:55) \[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)\] on linux Type "help", "copyright", "credits" or "license" for more information. \>\>\> print("hello world") hello world \>\>\> a=3 \>\>\> b="abc" \>\>\> type(a) //整型 \ \>\>\> type(b) //字符串类型 \ \>\>\> quit() //退出 \[root@zhu \~\]# 1. 变量和数据类型 1. 三大数据类型 1)字符 字符串 str \>\>\> type (b) \ \>\>\> b='daning'; \>\>\> b 'daning' \>\>\> 2)数值 整数(int) 浮点(float) \>\>\> type (c) \ \>\>\> d=3.14 \>\>\> d 3.14 \>\>\> type(d) \ \>\>\> c=3 \>\>\> 3)逻辑 True,Flase \>\>\> flag=True \>\>\> print(flag); True \>\>\> print(1==1); True \>\>\> print(1!=1); False \>\>\> 1. 数据集合 1. 列表 list(lista=\[\]) 使用最为广泛的一个数据集合工具;是java钟数组和list的综合体;当有多个数据需要管理,可以定义一个列表;管理列表 help(lista) //通过上下,enter,space键来阅读信息,使用q退出,more less 1. 创建列表 \>\>\> listb=\["tom","jerry"

>>> listb

'tom', 'jerry'

2)追加元素(appedd)

>>> listb.append("tomcat")

>>> listb

'tom', 'jerry', 'tomcat'

3)插入元素(insert)

>>> listb.insert(1,"laowang") //在第一个元素之后插入laowang

>>> listb

'tom', 'laowang', 'jerry', 'tomcat'

4)删除元素(pop,remove)

>>> listb.pop()

'tomcat'

>>> listb

'tom', 'laowang', 'jerry'

>>> listb.remove('laowang') //删除指定元素

>>> listb

'tom', 'jerry'

>>> listb[0]

'tom'

>>> listb[1]

'jerry'

>>> 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'

>>>

  1. 修改元素

>>> listb.append("lisi")

>>> listb

'jerry', 'lisi'

>>> listb[0]

'jerry'

>>> listb[0]="tom"

>>> listb

'tom', 'lisi'

>>>

  1. del listb

  2. 字典 dict(dict0={})

key-value:键值对;键:值

  1. 创建

>>> dict0={

... "id":1,

... "username":"abc",

... "pssword":"123"

... }

>>> dict0

{'id': 1, 'username': 'abc', 'pssword': '123'}

  1. 新增

>>> dict0["other"]=a

>>> dict0

{'id': 1, 'username': 'abc', 'pssword': '123', 'password': '123456', 'other': [1, 3, 5, 2, 4, 6, {...}]}

>>>

>>> a.append(dict0)

>>> a

1, 3, 5, 2, 4, 6, {'id': 1, 'username': 'abc', 'pssword': '123', 'password': '123456'}

>>>

  1. 元组 tuple(tupl0=())

>>> tupl0=(1,2,3,4)

>>> tupl0

(1, 2, 3, 4)

>>> tupl0[2]=666 //元组不支持修改

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

TypeError: 'tuple' object does not support item assignment

>>>

  1. 转换
  1. tuple[index]
  2. List(tuple)
  3. Tuple(list)

|-------|-----------------------------------|-------------------------------------------|
| 功能 | 指令 | 说明 |
| 创建列表 | [ ] | 符号本身就是列表 |
| 创建列表 | List(元组) | 将元组转换成列表 |
| 创建列表 | List(字典) | 提取字典的key转成列表 |
| 创建列表 | 字典.keys() | 字典中的key返回一个列表 |
| 创建列表 | 字典.values() | 字典中的values组成的列表 |
| 创建列表 | 字典.items() | 字典中的每个k-v组成元组,这些元组再组成一个新的列表 |
| 修改列表 | L.inster(index,value) | 在索引值为index的元素之前插入一个元素 |
| 修改列表 | L.append(value) | 在所有元素之后添加一个元素 |
| 修改列表 | L[index]=value | 将索引为index元素的值修改为value |
| 修改列表 | L.pop | 删除最后一个元素 |
| 修改列表 | del L | 释放L内存 |
| 查看列表 | L | 显示列表中所有数据 |
| 查看列表 | L[index] | 返回索引值为index的元素 |
| 字典的创建 | {} | 代表一个空字典 |
| 字典的创建 | {k0:v0,k1:v1......} | 这是有初始值的列表 |
| 字典的创建 | dict([(k0,v0),(k1,v1),(k2,v2)]) | []中每个()都有2个值,一个是Key,一个是value自动泛解析为一个字典了 |
| 元组 | (),(1,2,3,4) | 创建空元组,创建初始元组 |
| 元组 | 可以从dict中提取,也可以将列表直接转换成元组 | |

  1. 选择语句
  1. if语句

1)格式:if (条件):

Print()

Else:

Print()

>>> if True:

... print("true")

... else:

... print("false")

...

true

>>>

root@zhu \~\]# vim 001.py if True: print("i'm true") else: print("i'm false") \[root@zhu \~\]# python3 001.py i'm true \[root@zhu \~\]# 2)格式: if (条件1): Print() Elif (条件2): Print() ...... Else: Print() \>\>\> n=58 \>\>\> if n\>90: ... print("优秀") ... elif n\>80: ... print("良好") ... elif n\>70: ... print("中等") ... elif n\>60: ... print("及格") ... else: ... print("不及格") ... 不及格 \>\>\> \[root@zhu \~\]# vim 002.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@zhu \~\]# python3 002.py 随机分数为: 29 不及格 \[root@zhu \~\]# python3 002.py 随机分数为: 95 优秀 \[root@zhu \~\]# 1. Switch \>\>\> print("请输入您的选择") 请输入您的选择 \>\>\> print("1.创建master,2.创建slave") 1.创建master,2.创建slave \>\>\> input("---:") ---:1 '1' \>\>\> n=input("---:") ---:2 \>\>\> n '2' \>\>\> 1. 循环语句 1. for循环 \>\>\> n=0 \>\>\> for i in range(100): ... n=n+i ... \>\>\> n 4950 \>\>\> \[root@zhu \~\]# vim 003.py n=0 for i in range(101): //0-100 n=n+i print(n) \[root@zhu \~\]# python3 003.py 5050 \[root@zhu \~\]# \>\>\> for var in \["a","b","c"\]: //列表中遍历循环 ... print(var) ... a b c \>\>\> \>\>\> d={"a":1,"b":2,"c":3} //字典中遍历循环 \>\>\> d {'a': 1, 'b': 2, 'c': 3} \>\>\> for var in d: ... print(var) ... a b c \>\>\> for var in d: ... print(var,"-",d\[var\]) ... a - 1 b - 2 c - 3 \>\>\> tupl0=("a","b","c") //元组中遍历循环 \>\>\> for var in tupl0: ... print(var) ... a b c \>\>\> 案例:打印1-100中可以被7整除的数 \>\>\> b=list(range(1,101)) \>\>\> for i in b: ... if i%7==0: ... print(i) ... 7 14 21 28 35 42 49 56 63 70 77 84 91 98 \>\>\> 1. While循环 \>\>\> while i\<101: ... n+=i ... i+=1 ... \>\>\> n 5050 \>\>\> \>\>\> i=1 \>\>\> while i\<=5: ... print(i) ... i+=1 ... 1 2 3 4 5 \>\>\> continue:退出当前循环,继续执行下一次循环 \>\>\> i=0 \>\>\> while i\< 10: ... i+=1 ... if i%2!=0: ... continue ... print(i) ... 2 4 6 8 10 \>\>\> break:直接退出循环 \>\>\> c=0 \>\>\> while True: ... print(c) ... c+=1 ... if c==5: ... break ... 0 1 2 3 4 \>\>\> 1. 常用的工具api

相关推荐
蓝婷儿3 小时前
前端面试每日三题 - Day 32
前端·面试·职场和发展
星空寻流年4 小时前
CSS3(BFC)
前端·microsoft·css3
九月TTS4 小时前
开源分享:TTS-Web-Vue系列:Vue3实现固定顶部与吸顶模式组件
前端·vue.js·开源
CodeCraft Studio4 小时前
数据透视表控件DHTMLX Pivot v2.1发布,新增HTML 模板、增强样式等多个功能
前端·javascript·ui·甘特图
一把年纪学编程4 小时前
【牛马技巧】word统计每一段的字数接近“字数统计”
前端·数据库·word
llc的足迹4 小时前
el-menu 折叠后小箭头不会消失
前端·javascript·vue.js
九月TTS5 小时前
TTS-Web-Vue系列:移动端侧边栏与响应式布局深度优化
前端·javascript·vue.js
Johnstons5 小时前
AnaTraf:深度解析网络性能分析(NPM)
前端·网络·安全·web安全·npm·网络流量监控·网络流量分析
whatever who cares5 小时前
CSS3 伪元素(Pseudo-elements)大全
前端·css·css3
若愚67925 小时前
前端取经路——性能优化:唐僧的九道心经
前端·性能优化