zyx青岛实训day24 8/7

一.Python环境准备

1.查看有没有python3

yum list installed |grep python

yum list |grep python3

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

2.下载安装python3

yum -y install python3

3.查看版本

[root@python ~]# python3 --version

Python 3.6.8

4.进入编辑

[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

  • 变量和数据类型

三大类数据类型

字符(str)    字符串

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

逻辑        True,Flase

>>> a=3

>>> b="abc"

>>> type(a)

<class 'int'>

>>> type(b)

<class 'str'>

>>> flag=True

>>> print(flag);

True

>>> quit()

[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

  • 数据集合

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

1.列表---[]

   使用最广泛的一个数据集合工具

Help(lista) //通过上下方向,enter,space键来翻阅信息,使用q退出查看

创建列表

Lista=[]

Listc=[1,2,3]

追加元素

Lista.append(item)  //在所以元素之后添加

插入元素

Listb.insert(pos,item)  //在pos序列号之前插入item

删除元素

List.pop()  //删除list中的最后一个元素

List.remove(list[index])  删除list中序号为index元素

修改元素

List[[index]]=’’

>>> lista=["张三","李四","王五"]

>>> type(lista)

<class 'list'>

>>> lista

['张三', '李四', '王五']

>>> listb=["tom","jerry"]

>>> listb

['tom', 'jerry']

>>> listb.append("tomcat");

>>> listb

['tom', 'jerry', 'tomcat']

>>> listb.insert(1,"laozhang")

>>> listb

['tom', 'laozhang', 'jerry', 'tomcat']

>>> listb.remove('laozhang')

>>> listb

['tom', 'jerry', 'tomcat']

>>> listb.remove(listb[2])

>>> listb

['tom', 'jerry']

['tom', 'jerry']

>>> listb[1]

'jerry'

>>> listb[0]

'tom'

>>> listb[2]

Traceback (most recent call last):

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

IndexError: list index out of range    //下标越界

>>> listb[0]='sunnai'

>>> listb

['sunnai', 'jerry']

2.字典---{}

   Dict

   Dirctionary

   Key-valuen 键值对

   {“name”:”家人”,”age”:”34”,”gender”:”male”,”height”:167”,”weight”:”110”}

   键值

{

  “from”:”me”

“to”:”you”

“message”:”你在哪”,

“time”:”2024-8-8 9:00”,

}

3.元组----()

不可以修改,修改的话要添加到列表再修改,可以查看

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

>>> tupl0

(1, 2, 3, 4)

>>> tupl0[0]

1

>>> tupl0[0]=1111

Traceback (most recent call last):

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

TypeError: 'tuple' object does not support item assignment

>>> list(tupl0)

[1, 2, 3, 4]

>>> aa=list(tupl0)

>>> aa

[1, 2, 3, 4]

List()可把dict的key生成一个列表

list可以把tupl变成一个列表

Tupl可以把dict和lits变成元组

功能             指令                    说明

创建列表         []                       符号本身就是列表

                 List(元组)                 将元组转成列表

                 List(字典)                 提取字典的key转成列表

                 字典.keys()                字典中的key返回一个列表

                 字典.values()              字典中的value组成的列表

              字典.items()  字典中的每一个k-v组成元组,这些元组组成一个新的列表

修改列表      l.inster(index,value)  在索引值为index的元素之前插入一个元素

  1. append(value)     在所有元素之后添加一个元素

L[index]=value       将索引为index元素的值修改为value

  • 选择语句和循环语句
  1. 选择语句
  1. 缩进是必须的
  2. if

        if condition0:

          statement0;

        slse:

          statement1;

  1. swith插槽

>>> if True:

...    print("i'm true")

... else:

...    print("i'm false")

...

i'm true

>>> quit()

[root@python ~]# vim py001.py

if True:

   print("i am true" )

else:

   print("i am false")

[root@python ~]# python3 py001.py

i am true

>>> n=58

>>> if n>90:

...     print("优秀")

... elif n>80:

...     print("良好")

... elif n>70:

...     print("中等")

... elif n>60:

...     print("及格")

... else:

...     print("不及格")

...

不及格

[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

[root@python ~]# python3 py002.py

随即分数为: 26

不及格

[root@python ~]# python3 py002.py

随即分数为: 61

及格

[root@python ~]# 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@python ~]# python3 py003.py

  1. 循环语句

     for循环

        for var in list:

          print(var)

        for i in range(101):  //0-100

          n=n+i

        print(n)         //1-100数字累加

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

(1)在列表中循环

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

(2)在字典中循环

>>> d={"id":1001,"name":"张三","age":"19","gender":"女"}

>>> d

{'id': 1001, 'name': '张三', 'age': '19', 'gender': '女'}

>>> for var in d:

...     print(var)

...

id

name

age

gender

>>> for var in d:

...     print(var,"---",d[var])   //根据key返回对应的value值

...

id --- 1001

name --- 张三

age --- 19

gender --- 女

>>> for var in d.values():

...     print(var)

...

1001

张三

19

(3)元组中的遍历

tupl0=(“a”,”b”,”c”)

for var in tupl0:

   print(var)

>>> 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整除

while    

   while condition:

     blockak

     #continue(退出当前循环,进入下次循环),break(退出循环);  

>>> n=0

>>> i=1

>>> while i<101:

...     n+=i

...     i+=1

...

>>> n

5050

>>> i=1

>>> n=0

>>> while True:

...     print("abc")

...     break

...

abc

>>> i=1

>>> while True:

...     if i==3:

...             continue

...     print(i)

...     i+=1

...

1

2

生成随机数

import random

n=random.randint(0,10)

创建目录

import os

os.mkdir(“/opt/aaa”)     

  • 常用工具api
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值