pyhton学习
1.环境安装
[root@localhost ~]# yum list installed |grep python (查看是否安装python以及版本)
安装python3版本
[root@localhost ~]# yum list installed |grep epel (查看是否安装epel )
[root@localhost ~]# yum list |grep python3
[root@localhost ~]# yum -y install python3(安装python3)[root@localhost ~]# python3 --version (安装成功)
Python 3.6.8[root@localhost ~]# python3
>>> print("hello world")
hello world
>>> quit() (退出)[root@localhost ~]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ some-package
语言学习步骤:
2.变量和数据类型字符 字符串(str)
数值 整数(int),浮点(float)
逻辑 True,False
3.数据集合
(1)列表
1)使用最为广泛的一个数据集合工具
2)是java中数组和list的综合体
3)list
4)当有多个数据需要管理,可以定义一个列表
>>> help(lista)帮助命令
5)管理列表
#python为开发提供了丰富的使用手册
help(lista) #通过上下方向键,enter, space键来翻看信息 使用q退出查看
#创建列表
lsta=[ ]
lista=[1,2,3]
#修改列表
#追加元素
lista.append(item) #在所有元素之后添加元素
#插入元素
listb.insert(pos,lite) #在pos序列号之前插入item
#删除元素remov 和pop
list.pop() #删除list中的最后一个元素
list.remove(list[index]) #删除序列号为index的元素
#修改元素
list[index]=newvalue
#del list
(2)字典
1)dict
2)dictionary
3)key-value 键值对
4){"name":"张三","age":"20","gender":"male",}
5)键:值
{
"from" : "me",
"to" : "you",
"message":"你吃了吗?"
"time":"2024-8-8 9:00.:32",
"user":{
"username":"abc",
"password":"abc"
}
}
(3)元组
1)不可以修改,只可以查看
2)tuple[index]
3) list(tuple)
4) tuple(list)
(4)[]列表,{}字典,()
4选择语句和循环语句
(1)选择语句
1)缩进是必须的
2)if
if condition0:
statement0;
if condition1:
block1;
else:
biock2:
else:
statement1;
3)swith
(2)循环语句
vim py002.py (生成0~100之间的随机整数)
练习:
[root@3 ~]# python3 -m pdb py003.py
> /root/py003.py(1)<module>()
-> import random
(Pdb) n
> /root/py003.py(2)<module>()
-> n=random.randint(50,100)
(Pdb) n
> /root/py003.py(3)<module>()
-> print("随机数值为:",n)
(Pdb) n
随机数值为: 92
......
(Pdb) q
>>> import random
>>> n=random.randint(0,10)
>>> n
10
>>> n=random.randint(0,10)
>>> n
4
>>> import os
>>> os.mkdir("/opt/aaa")
>>> quit()
[root@3 ~]# ls /opt
aaa