24.python基础(8.8)

python基础

1.搭建环境python3

1.查看是否有安装python
[root@python1 ~]# yum list installed |grep python
2.安装python3
[root@python1 ~]#yum -y install python3
[root@python1 ~]#python --version       #查看版本信息
最新安装3.12,可以使用源码安装

开发工具

1.安装自带的ide

2.pycharm

3.anaconda

3.进入python编辑状态

修改pip镜像为清华

[root@python1 ~]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ some-package
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting some-package
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/e7/a2/d318a685319c3801db1ae0002fc8e095663a55546c62a6e30d9d0fc3289b/some-package-0.1.zip
Installing collected packages: some-package
  Running setup.py install for some-package ... done
Successfully installed some-package-0.1
​
[root@python1 ~]# python3
>>> print("hello world")
hello world
>>> a=3
>>> b="abc"
>>> type(a)   #type:字符串类型
<class 'int'>
>>> type(b)
<class 'str'>
>>>quit()  #退出python
​

2.变量和数据类型

三大类数据类型

1.字符

字符串(str)

>>> b='yjj';
>>> b
'yjj'
>>> type(b)
<class 'str'>
2.数值

整数(int),浮点型( float)

>>> c=3
>>> c
3
>>> type(c)
<class 'int'>
>>> d=3.14
>>> d
3.14
>>> type(d)
<class 'float'>
3.逻辑

True,False

>>> flag=True
>>> print(flag);
True
>>> print(1==1);
True
>>> print(1!=1);
False
>>> name1="张三"
>>> name2="李四"
>>> name3="王五"
>>> print(name1,name2,name3)
张三 李四 王五
>>> name1
'张三'
>>> name2
'李四'
>>> name3
'王五'

3.数据集合

1.使用数据集合批量管理内存空间

2.[]列表,{}字典,()元组

3.list()可以吧dict的key生成一个列表

4.list可以吧tupl变成列表

5.tupl可以吧dic和list变成元组

{
​
           "method":"post",
           "username":"abc",
           "password":"123",
           "params":[1,2,3],
           "cntroller":"login"
​
}
1.列表

1.使用最广泛的一个集合工具

2.是java中数组和list的综合体

3.list #有序

4.当有多个数据需要

5.管理列表

#python为开发提供丰富的使用感手册
help(lista)   #通过上下方向,enter,space键来翻阅信息,使用q退出查看  more less
#创建列表
lista=[]
listc=[1,2,3]
#修改列表
#追加元素
lista.append(item)    #在所有元素之后添加元素
#插入元素
liatb.insert(pos,item)#这pos序列号之前插入item
#删除元素remove和pop
list.pop() #删除list中的最后一个元素
list.remove(list[index])  #删除学号为index的元素
​
#修改元素
list[index]=newvalue
​
#del list
​
>>> listb.append("jerry")
>>> listb
['lisi', 'jerry']
>>> print(listb)
['lisi', 'jerry']
>>> listb[0]
'lisi'
>>> listb[1]
'jerry'
>>> listb[0]='tom'
>>> listb
['tom', 'jerry']
​
>>> a=[1,3,5,6,8]
>>> a
[1, 3, 5, 6, 8]
>>> a[0]
1
>>> print(a[0])
1
#插入元素
>>> a
[1, 3, 5, 6, 8]
>>> a.remove(a[3])
>>> a
[1, 3, 5, 8]
>>> a.append(12)
>>> a
[1, 3, 5, 8, 12]
2.字典

1.dict

2.dirctionary

3.key-value,键值对

4.{“name”:“袁佳佳”,“age”,“21”,“gender”,“male”}

5.键:值

{
    "from":"me"
    "to":"you"
    "message":"你吃饭了嘛:"
    "time":"2024-8-8 10:45"
    "user":{
        "username":"abc"
        "password":"abc"
    }
}
>>> dict0={           #无序的
... "id":1,
... "username":"abc",
... "password":"123"
... }
#删除数据
>>> dict0["realname"]="yjj"
>>> dict0
{'id': 1, 'username': 'abc', 'password': '123', 'realname': 'yjj'}
>>> dict0.pop("id")
1
>>> dict0    #在原先的中修改
{'username': 'abc', 'password': '123', 'realname': 'yjj'}
#修改数据
>>> dict0["password"]="123456"
>>> dict0
{'username': 'abc', 'password': '123456', 'realname': 'yjj'}
>>> a
[1, 3, 5, 8, 12]
>>> a.append(dict0)
>>> a
[1, 3, 5, 8, 12, {'username': 'abc', 'password': '123456', 'realname': 'yjj'}]
​
>>> dict0["other"]=a
>>> dict0
{'username': 'abc', 'password': '123456', 'realname': 'yjj', 'other': [1, 3, 5, 8, 12, {...}]}
​
>>> a=[1,2,3]
>>> a
[1, 2, 3]
>>> b={"username":"abc","password":"123"}
>>> b
{'username': 'abc', 'password': '123'}
>>> a.append(b)
SyntaxError: invalid syntax
>>> b["a"]=a
>>> a
[1, 2, 3, {'username': 'abc', 'password': '123', 'a': [...]}]
>>> b
{'username': 'abc', 'password': '123', 'a': [1, 2, 3, {...}]}
​
#列表所有元素
>>> dict0.items()
dict_items([('username', 'abc'), ('password', '123456'), ('realname', 'yjj'), ('other', [1, 3, 5, 8, 12, {'username': 'abc', 'password': '123456', 'realname': 'yjj', 'other': [...]}])])
>>> 
3.元组

当在列表中删除或修改一个元素使,列表会返回新的列表

元组不能更改,只能查看

>>> tupl0=(1,2,3,4)
>>> tupl0   #查看
(1, 2, 3, 4)
>>> tupl0[0]   #查看某一个
1
>>> tupl0[1]
2
#将元组写入list列表
>>> list(tupl0)
[1, 2, 3, 4]
>>> aa=list(tupl0)
>>> aa
[1, 2, 3, 4]
>>> 

4.选择语句和循环语句

1.选择语句

1.必须缩进

[root@python1 ~]# vim py001.py
if True:
        print("i am true")
else:
        print("i am false")              
[root@python1 ~]# python3 py001.py 
i am true

2.if

if condition0:
  statement0;
  if condtion1:
    block1;
  else:
    block2;
else:
  statement1;
>>> n=58
>>> if n>90:
...     print("优秀")
... elif n>80:
...     print("良好")
... elif n>70:
...      print("中等")
... elif n>60:
...      print("及格")
... else:
...      print("不及格")
... 
不及格
​
[root@python1 ~]# vim py002.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@python1 ~]# python3 py002.py 
随机分数为: 56
不及格
[root@python1 ~]# python3 py002.py 
随机分数为: 68
及格
[root@python1 ~]# python3 py002.py 
随机分数为: 32
不及格
[root@python1 ~]# python3 py002.py 
随机分数为: 61
​
[root@python1 ~]# vim py003.py 
import random
n=random.randint(50,100);
​
print("随机数为",n)
​
if n>90:
 print("优秀")
else:
 if n>80:
     print("良好")
 else:
     if n>70:
        print("中的")
     else:
        if n>59:
           print("及格")
        else:
           print("不及格")
[root@python1 ~]# python3 py003.py 
随机数为 98
优秀
2.循环语句

for

for var in list:
 print (var)
>>> range(9)
range(0, 9)
>>> list(range(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
​
[root@python1 ~]# vim py004.py 
n=0
for i in range(101):
 n=n+1
print(n)
[root@python1 ~]# python3 py004.py 
101
[root@python1 ~]# vim py004.py 
n=0
for i in range(101): #0-100,101取不到
 n=n+i
print(n)
[root@python1 ~]# python3 py004.py 
5050
3.在列表中遍历循环
>>> 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
​
4.在字典中遍历
>>> d={"id":1001,"name":"张三","age":18,"gender":"女"}
>>> d
{'id': 1001, 'name': '张三', 'age': 18, 'gender': '女'}
>>> for var in d:
...  print(var)#根据可以返回对应的value值
... 
id
name
age
gender
 print(d)#将d这个字典中的key都输出
#只输出值
>>> for var in d:
...  print(var,"-",d[var])
... 
id - 1001
name - 张三
age - 18
gender - 女
​
5.元组中的遍历
>>> 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整除
​
6.while循环
while condition:
 blocak
 #comtinue,break;
#1-100的累加
>>> n=0
>>> i=1
>>> while i<101:
...  n+=i
...  i+=1
... 
>>> n
5050
​
7.break,continue也可以应用与for
while Ture:
 print("abc")
 break
while Ture:
 if i==3:
    continue
 i+=1
#指令
vim 001.py
#执行python脚本
python3 001.py
​
#调试py脚本
python3 -m pdb 001.py
#输入n按回车执行下一行代码
#输入q推出调试
​
#生产随机数python
import random
n=random.randint(1,10)
​
#创建目录
import os
os.mkdir("/opt/aaa/")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值