安装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/")

相关推荐
林姜泽樾24 分钟前
linux入门第五章,mkdir、touch详解
linux·运维·服务器
木子欢儿35 分钟前
在 Debian 13(以及 12)上安装和配置 tightvncserver 并让普通用户使
运维·前端·debian
ol木子李lo40 分钟前
Linux 命令备忘录
linux·运维·服务器·windows·编辑器·ssh·bash
SakitamaX1 小时前
Nginx安装与实验
服务器·前端·nginx
2501_918126911 小时前
stm32能刷什么程序?
linux·stm32·单片机·嵌入式硬件·学习
第七序章1 小时前
【Linux学习笔记】git三板斧
linux·运维·服务器·笔记·git·学习
礼拜天没时间.1 小时前
Node.js运维部署实战:从0到1开始搭建Node.js运行环境
linux·运维·后端·centos·node.js·sre
一只自律的鸡1 小时前
【Linux系统编程】文件IO 标准IO
linux·运维·服务器
郝学胜-神的一滴1 小时前
Python中的del语句与垃圾回收机制深度解析
服务器·开发语言·网络·python·算法
EverydayJoy^v^1 小时前
Linux Shell 高级编程(2)——sed
linux·运维·服务器