8.8-配置python3环境+python语法的使用

1.环境 python2 ,python3

[root@python ~]# yum list installed|grep python
​
​
[root@python ~]# yum list installed|grep epel
epel-release.noarch                   7-11                             @extras 
​
#安装python3
[root@python ~]# yum -y install python3
​
#查看python版本
[root@python ~]# python3 --version
Python 3.6.8
​
#最新安装3.12 可以使用源码安装

2.进入到python的编辑状态

#直接输入python,也会进入到python2中
[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)
<class 'int'>
>>> type(b)
<class 'str'>
>>> quit()

3.使用 pip3 安装软件包时指定清华源

[root@python ~]# 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

4.变量和数据类型

三大类数据类型

(1)字符:字符串

str

>>> b='haha';
>>> b
'haha'
>>> type(b)
<type 'str'>
(2)数值:整数,浮点

int float

>>> c=3
>>> c
3
>>> type(c)
<type 'int'>
>>> d=3.14
>>> d
3.14
>>> type(d)
<type 'float'>
(3)逻辑:True,False
>>> flag=True
>>> print(flag);
True
>>> print(1==1);
True
>>> print(1!=1);
False

5.数据集合

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

1.列表

有序

列表使用最为广泛的一个数据集合工具,是java中数组和list的综合体,当有多个数据需要管理,可以定义一个列表

管理列表
(1)创建列表
# 创建列表
​
#格式
lista=[]
listc=[1,2,3]
​
#练习
>>> lista=["张三","李四","王五","赵六"]
>>> type(lista)
<type 'list'>
>>> lista
['\xe5\xbc\xa0\xe4\xb8\x89', 
'\xe6\x9d\x8e\xe5\x9b\x9b', 
'\xe7\x8e\x8b\xe4\xba\x94', 
'\xe8\xb5\xb5\xe5\x85\xad']
​
>>> listb=["tom","jerry"]
>>> listb
['tom', 'jerry']
(2)帮助命令
#python为开发提供了丰富的使用手册
​
help(lista)  #通过上下方向键,enter,空格键来翻阅信息,使用q退出查看
(3)修改列表
# 修改列表
​
# 追加元素
lista.append(item) # 在所有元素之后添加元素
​
#练习
>>> listb.append("tomcat")
>>> listb
['tom', 'jerry', 'tomcat']
​
​
# 插入元素
listb.insert(pos,item) #在pos序列号之前插入item
​
#练习
>>> listb.insert(1,"haha")
>>> listb
['tom', 'haha', 'jerry', 'tomcat']
​
(4)删除元素
# 删除元素remove和pop
​
#格式
list.pop() #删除list中的最后一个元素
​
#练习
>>> listb=["tom","jerry"]
>>> listb
['tom', 'jerry']
>>> listb.pop()
'jerry'
>>> listb
['tom']
>>> listc=listb.pop() #表示删掉listb中的tom
>>> listc 
'tom'
>>> #当在列表中删除或者修改一个元素的时候,列表会返回新的列表
... 
>>> listb
[]
​
#格式
list.remove(list[index]) #删除序号为index的元素
​
#练习
>>> listb.append("lllll")
>>> listb.append("lisi")
>>> listb
['lllll', 'lisi']
>>> listb.remove('lisi')
>>> listb
['lllll']
>>> listb.append("lllll")
>>> listb.append("lisi")
>>> listb
['lllll', 'lllll', 'lisi']
>>> listb[0]
'lllll'
>>> listb[1]
'lllll'
>>> listb[2]
'lisi'
>>> listb[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> listb.remove(listb[0])
>>> listb
['lllll', 'lisi']
>>> listb.remove(listb[0])
>>> listb
['lisi']
(5)修改元素
# 修改元素
​
# 格式
list[index]=newvalue
​
# 练习
>>> listb.append("jerry")
>>> print(listb)
['lisi', 'jerry']
>>> listb[0]
'lisi'
>>> listb[0]='tom'
>>> listb
['tom', 'jerry']
>>> listb[0]
'tom'
>>> listb[1]
'jerry'
>>> listb[1]='tom'
>>> listb[1]
'tom'
>>> listb
['tom', 'tom']
​
​
(6)删除列表
# 删除列表
​
#格式
del list
​
#练习
>>> del listb
>>> listb  #listb中的数据全都被删掉了
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'listb' is not defined

2.字典

无序

1.dict

2.dictionary

3.key-value 键值对

4{”name“:"haha","age":"18","gender":"male","height:"145"}

5.键:值

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

3.元组

有序,不能修改,可以查看

查看:tuple[index]

修改:将元组改为列表 list(tuple)

tup10=(1,2,3,4)

tup10

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

4.总结

功能指令说明
创建列表[ ]符号本身就是列表
list(元组)将元组转成列表
list(字典)提取字典的key转成列表
字典.keys()字典中的key返回一个列表
字典.values()字典中的value组成列表
字典.Items()字典中的每个键值对组成员组,每个元组组成一个新的列表
修改列表L.insert(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,v0),(k2,v2)])[ ]中的每个()中都有两个值,一个是key,一个是value,自动解析为一个字典了
元组(),(1,2,3,4)创建空元组,创建有初始值的元组
也可以从dict中提取,也可以将列表直接转成元组

6.循环语句和选择语句

1.选择语句

>>> if True:
...     print("i'm true")
... else:
...     print("i'am false")
... 
i'm true

缩进是必须的

缩进前:

[root@python ~]# vim py001.py
​
if True:        
print("i'm true")
else:
print("i'm false")
​
[root@python ~]# python3 py001.py 
  File "py001.py", line 2
    print("i'm true")
        ^
IndentationError: expected an indented block

缩进后:

[root@python ~]# vim py001.py
​
if True:
        print("i'm true")
else:
        print("i'm false")
​
[root@python ~]# python3 py001.py 
i'm true

if

嵌套

# 语法
if condition0:
    statement0;
    if condition1:
        block1;
     else:
        block2;
 else:
    statement1;

多分支

# 语法
if condition0:
    block0
elif condition1:
    block1
elif condition2:
    block2
...
else:
    blockn

练习

在vim中写

[root@python ~]# 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@python ~]# python3 py002.py 
随机分数为: 65
及格
[root@python ~]# python3 py002.py 
随机分数为: 88
良好

在python3环境中写

>>> n=58
>>> if n>90:
...     print("优秀")
... elif n>80:
...     print("良好")
... elif n>70:
...     print("中等")
... elif n>60:
...     print("及格")
... else:
...     print("不及格")
... 
不及格
>>> quit()

2.循环语句

1.for
for var in list:
    print(var)

[root@python ~]# vim for.py
n=0
for i in range(101): # 0-100
        n=n+i
 print(n)  # 1-100数字累加
[root@python ~]# python3 for.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

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

#元组中的遍历
>>> tupl0=("a","c","e")
​
>>> for var in tupl0:
...     print(var)
... 
a
c
e

随机数的循环

>>> 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整除
2.while
# 语法
while condition:
    block
    # continue,break;

1.1-100的累加

#1-100的累加
>>> n=0
>>> i=1
>>> while i<101:
...     n+=i;
...     i+=1
... 
>>> n
5050

break和continue也可以使用于for

break:结束循环

>>> while True:
...     print("abc")
...     break
... 
abc
#只循环一次

continue:结束本次循环

#死循环
>>> while True:
...     print("abc")
...     continue

>>> while True:
...     if i==3:
...             continue # 当i=3时,下面的语句不执行
...     print(i)
...     i+=1 
... 
1
2
​
​
#一直循环

在vim环境下写python的指令

#指令
vim 001.py
#执行python脚本
python3 001.py
​
​
#调试py脚本
python3 -m pdb 001.py
# 输入n按回车执行下一条代码
# 输出q推出调试

在python3环境中生成随机数和创建目录

# 生成随机数
import random
n=random.randint(0,10)
​
#创建目录
import os
os.mkdir("/opt/aaa/")

5.选择语句和循环语句

6.常用的工具aip

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值