Python的安装环境以及应用

1.环境python2,Python

最新安装3.12可以使用源码安装

查看安装包

root@python001 \~# yum list installed | grep epel

3root@python001 \~# yum list installed | grep python

root@python001 \~# yum -y install python3 安装python3

查看版本

root@python001 \~# python3 --version

Python 3.6.8

root@python001 \~# python3 进入编辑页面,也会进入到python2中

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)

<class 'int'>

>>> type(b)

<class 'str'>

>>> quit() 退出

root@python001 \~# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ some-package 修改pip镜像为清华

2.变量和数据类型

1.三大数据类型

python 复制代码
      字符        字符串

>>> b='zhangin'
>>> b
'zhangin'
>>> type (b)
<class 'str'>

        数值        整数,浮点

>>> c=3
>>> c
3
>>> type(c)
<class 'int'>

>>> d=3.14
>>> d
3.14
>>> type(d)
<class 'float'>


        逻辑        true,false

>>> flag=True
>>> print(flag);
True
>>> print(1==1);
True
>>> print(1!=1)
False
python 复制代码
>>> name1="张三"
>>> name2="李四"
>>> name3="王五"
>>> print(name1,name2,name3)
张三 李四 王五

最终的计算是在python内存中计算的,必须要有指定内存空间保存数据,这些内存空间其实就是变量

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

3.数据集合

1.列表

1.使用最为广泛的数据集合工具

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

3.list

4.当有多个数据需要管理,可以定义一个列表

python 复制代码
>>> lista=["张三","李四","王五","赵六"]
>>> type(lista)
<class 'list'>
python 复制代码
>>> help(list)    //查看list相关命令
python 复制代码
>>> listb.insert(1,"tomcat")
>>> listb
['tom', 'tomcat', 'jerry']
>>> listb.append("tomcat");
>>> listb
['tom', 'tomcat', 'jerry', 'tomcat']
>>> listb.shift("jerry")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'shift'
>>> listb.pop("jerry")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an integer
>>> listb.pop()
'tomcat'
>>> listb.pop()
'jerry'
>>> listb
['tom', 'tomcat']
>>> listb.pop()
'tomcat'
>>> listb.pop()
'tom'
>>> listb
[]
>>> listc=listb.pop()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop from empty list
>>> listb=["job"]
>>> listc=listb.pop()
>>> listc
'job'
>>> 
python 复制代码
>>> listb
[]
>>> listb.append("zz")
>>> listb
['zz']
>>> listb.append("bb")
>>> listb.append("bb")
>>> listb
['zz', 'bb', 'bb']
>>> listb.remove("bb")
>>> listb
['zz', 'bb']
>>> listb.remove(listb[0])
>>> listb
['bb']

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

5.管理列表

python 复制代码
#python为开发提供了丰富的使用感手册
help(lista)    #通过上下方向,enter,space健来翻阅信息,使用q来退出查看 more less
#创建列表
lista=[]
listc[1,2,3]
#修改元素
#追加元素
lista.appendd(item)    #在所有元素之后添加元素
#插入元素
listb.insert(pos,item)    #在pos序号之前插入item



删除元素 remove和pop
list.pop()    #删除list中最后一个元素
list.remove(liast[index])    #删除序号为index的元素


#修改元素
list[index]=newvalue


#del list 删除表



#

2.字典

1.dict

2.dictionary

3.key-value 键值对

4.{"name":"陈","age":"20","gender":"male"}

5.键值

python 复制代码
{
    "from":"me",
    "to":"you",
    "message":"你吃饭了吗?"
    "time":"2024-7-8 9:00:32",
    "user":{
        "username":"abc",
        "password":"abc"    
    }
}

6.{}

3.元组(不能修改,可以查看)

1.没有修改,只可以查看

2.Tupleindex

3.list(tuple)

4.Tuple(list)

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

5.List()可以吧dict的key生成一个列表

6.list可以吧tupl变成列表

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

python 复制代码
>>> tupl0
(1, 2, 3, 4)
>>> tupl0[0]
1
>>> tupl0[1]
2
>>> tupl0[1]=666
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> aa=list(tupl0)
>>> aa
[1, 2, 3, 4]



>>> dict1={"a":1,"b":2,"c":3}
>>> dict1.keys
<built-in method keys of dict object at 0x7fe9aa9f9558>
>>> dict1.keys()
dict_keys(['a', 'b', 'c'])
>>> dict1.items()
dict_items([('a', 1), ('b', 2), ('c', 3)])
>>> dict([("a",1),("b",2)])
{'a': 1, 'b': 2}

4.选择语句和循环语句

1.选择语句

1.if

1.缩进是必须的

python 复制代码
if condition0:
    statement0;
    if condition:
        block1;
    else:
        block2;
else:
    statement1;
python 复制代码
[root@python001 ~]# cat 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@python001 \~# python3 py003.py

随机数值为 87

良好

root@python001 \~# python3 py003.py

随机数值为 53

不及格

root@python001 \~# python3 py003.py

随机数值为 70

及格

2.swith插槽

2.循环语句

1.for

python 复制代码
>>> range(9)
range(0, 9)
>>> list(range(9))
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> for i
id(          import       input(       is           issubclass(  
if           in           int(         isinstance(  iter(        
>>> for i in range(9):
...     print(i)
... 
0
1
2
3
4
5
6
7
8

for i in range(101): #0-100
    n=n+i
print(n) #1-100数字累加


#在列表中循环
for var in ["a","b","c"]:
    print(var)


#在字典中遍历
    d={"id":1001,"name":"zhangsan","gender":"女","age":18}
    for var in d:
        print(var)    #将这个字典中的额key都输出的
        print(d[var])    #根据key返回对应的value值
    for var in d.keys():
        print(var)
        print(d[var])

#在元组中的遍历
tupl0=("a","b","c")
for var in tupl0:
    print(var)

案例

python 复制代码
>>> 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,"可以被七整除")
... 
0 可以被七整除
7 可以被七整除
14 可以被七整除
21 可以被七整除
28 可以被七整除
35 可以被七整除
42 可以被七整除
49 可以被七整除
56 可以被七整除
63 可以被七整除
70 可以被七整除
77 可以被七整除
84 可以被七整除
91 可以被七整除
98 可以被七整除

2.while

python 复制代码
while condition:
    block
    continue,break;



# 指令
vim 001.py
# 执⾏py脚本
python3 001.py
# 调试py脚本
python3 -m pdb 001.py
# 输⼊n按回⻋执⾏下⼀⾏代码
# 输⼊q退出调试
5.常⽤的⼯具api
 
# ⽣成随机
import random
n=random.randint(0,10)
# 创建⽬录
import os
os.mkdir("/opt/aaa/")
相关推荐
Henry-SAP40 分钟前
SAP BOM项目类别详解——L/T/R/N各类目的业务含义解析
运维
天空属于哈夫克31 小时前
企业微信二次开发:解锁更多自动化应用场景
运维·自动化·企业微信
EmbeddedChen2 小时前
Linux 怎么查看 IP 地址和网络状态?常用网络命令入门
linux·网络·ip·route·ping
Diligently_2 小时前
第 08 篇|单机部署:本地安装与 Docker 最佳实践
运维·spring boot·docker
大今野2 小时前
2026 专转本计算机|区块链典型框架:以太坊与超级账本 Fabric 考点全解(附例题)
运维·区块链·fabric·专转本
Z21-X2 小时前
云原生运维学习笔记:Kubernetes 自动扩缩容与资源管控实战
运维·笔记·学习·云原生·kubernetes
爱和冰阔落3 小时前
【Linux网络】GET 和 POST 真的只是参数位置不同吗?从 HTTP 请求方法、抓包分析到长连接与路由实战
linux·c++·http·网络编程·get与post
无足鸟ICT3 小时前
【RHCA+】_proc_pid_attr_exec
linux·运维·服务器
牙牙7053 小时前
接口一直超时,后端却查不出问题:TIME_WAIT 把本地端口耗尽了
linux·网络·tcp·连接池·故障排查·time_wait
Decay__3 小时前
PostgreSQL 内存与锁调优 —— shared_buffers、work_mem、MVCC 与锁等待排查
运维·数据库·sql·postgresql