青岛实训day24(8/8)

一.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
相关推荐
zxctsclrjjjcph15 分钟前
【C语言】常见的C语言概念
c语言·开发语言
小灰灰爱代码20 分钟前
C++——求3个数中最大的数(分别考虑整数、双精度数、长整数的情况),用函数模板来实现。
开发语言·c++·算法
Eiceblue27 分钟前
Python 复制Excel 中的行、列、单元格
开发语言·python·excel
项目題供诗31 分钟前
尚品汇-秒杀商品存入缓存、Redis发布订阅实现状态位(五十一)
开发语言·php
m0_7145902639 分钟前
汇编(实现C语言程序的调用)
c语言·开发语言·汇编
NLP工程化42 分钟前
对 Python 中 GIL 的理解
python·gil
做技术的Pandaer42 分钟前
Go 第二期
开发语言·golang
新知图书43 分钟前
Rust编程的作用域与所有权
开发语言·后端·rust
极客代码1 小时前
OpenCV Python 深度指南
开发语言·人工智能·python·opencv·计算机视觉
liO_Oil1 小时前
(2024.9.19)在Python的虚拟环境中安装GDAL
开发语言·python·gdal安装