安装python+python的基础语法

安装python

python2为内置,安装python3----3.6.8

最新安装3.12使用源码安装

1.查看yum源,epel

root@python01 \~\]# yum list installed \|grep epel 2.安装python3 \[root@python01 \~\]# yum -y install python3 3.查看版本 \[root@python01 \~\]# python3 --version Python 3.6.8 4.修改pip镜像为清华 \[root@python01 \~\]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ some-package 5.进入到python的编辑状态 python3 如果直接输入python,会进入到python2中 ## 变量和数据类型 ###### 三大数据类型: 字符 字符串 str 数值 整数,浮点 int,float 逻辑 True , False \>\>\> c=False \>\>\> c False \>\>\> type(c) //查看数据类型 \ \>\>\> a=3 //定义变量 \>\>\> b="abc" \>\>\> type(a) //查看数据类型 \ \>\>\> type(b) \ ![](https://i-blog.csdnimg.cn/direct/9d3269c8c8cc44f0b267eb170ce58361.png) ###### 数据集合: 1.列表:有序 \[

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

是java中数组和list的综合体

list

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

管理列表

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

>>> listb

'tom', 'jerry'

>>> listb.append("tomcat")

>>> listb

'tom', 'jerry', 'tomcat'

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

>>> listb

'tom', 'laozhang', 'jerry', 'tomcat'

>>> listb.pop()

'tomcat'

>>> listb

'tom', 'laozhang', 'jerry'

>>> listb.remove('laozhang')

>>> listb

'tom', 'jerry'

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

>>> listb

'jerry'

>>> listb.append("haha")

>>> listb

'jerry', 'haha'

>>> listb[1]="xiaowang"

>>> listb

'jerry', 'xiaowang'

>>> del listb

>>> listb

Traceback (most recent call last):

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

NameError: name 'listb' is not defined

2.字典:无序 {}

dict

dirctionary

key-value 键值对

{"name":"xiaowang","age":"20"}

键:值

3.元组: ()

没有修改,只可以查看

查看 tuple[index]

list(tuple)

tuple(list)

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

>>> tupl0

(1, 2, 3, 4)

>>> tupl0[1]

2

>>> tuplo0[1]=66 //元组不能修改

Traceback (most recent call last):

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

NameError: name 'tuplo0' is not defined

总结:

选择语句

在python中一定要缩进

1.if
复制代码
if condition0:
    statement0
    if condition1:
        block1
    else:
        block2
else:
    statement1

>>> if True:

... print("i am true")

... else:

... print("i am false")

...

i am true

if多分支:

if condition0:

block0

elif condition1:

block1

...

else:

blockn

2.switch

循环语句

1.for

for var in list:

print(var)

1-100数字累加:

n=0

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

n=n+i

print(n)

在列表中循环:

>>> for var in ["a","b","c"]:

... print(var)

...

a

b

c

在字典中循环:

>>> d={"id":1001,"name":"zhangsaN","age":"18"}

>>> d

{'id': 1001, 'name': 'zhangsaN', 'age': '18'}

>>> for var in d:

... print(var)

...

id //只得到key

name

age

>>> for var in d:

... print(var,"-",d[var])

...

id - 1001 //得到key,values

name - zhangsaN

age - 18

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

... print(var)

...

1001 //只输出values

zhangsaN

18

在元组中循环:

>>> tupl0=("a","b","c")

>>> for var in tupl0:

... print(var)

...

a

b

c

2.while

while condition:

block

1-100的累加

i=0

n=0

while i<101:

n=+i

i+=1

print n

break,continue:也可以应用用for语句

>>> while True:

... print("abc")

... break //退出整个循环

...

abc

>>> while True:

... print("abc")

... continue

会一循环

>>> i=1

>>> while True:

... if i==3:

... continue //结束本次循环,进行下一次循环

... print(i)

... i+=1

...

1

2 //循环没有退出,只是不输出i

指令 :vim 001.py

执行脚本:python3 001.py

调试py脚本:python3 -m pdb 001.py

输入n按回车执行下一行代码

输入q退出调试

生成随机数:

import random

n=random.randint(0,10)

创建目录:

import os

os.mkdir("/opt/aaa/")

相关推荐
鸿蒙布道师4 分钟前
宇树科技安全漏洞揭示智能机器人行业隐忧
运维·网络·科技·安全·机器学习·计算机视觉·机器人
带鱼吃猫9 分钟前
Linux系统:文件系统前言,详解CHS&LBA地址
linux·运维·服务器
香饽饽~、16 分钟前
函数式方法的实现(JDK8+)
java·服务器
默默提升实验室26 分钟前
Linux 系统如何挂载U盘
linux·运维·服务器
mahuifa1 小时前
python实现usb热插拔检测(linux)
linux·服务器·python
Lw老王要学习1 小时前
Linux架构篇、第五章git2.49.0部署与使用
linux·运维·git·云计算·it
showmethetime1 小时前
搭建高可用及负载均衡的Redis
运维·redis·负载均衡
啊吧怪不啊吧2 小时前
Linux之初见进程
linux·centos
上天_去_做颗惺星 EVE_BLUE2 小时前
Docker入门教程:常用命令与基础概念
linux·运维·macos·docker·容器·bash
π大星星️5 小时前
基于LNMP架构的个人博客系统部署
服务器·架构