安装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)     //查看数据类型
<type 'bool'>

>>> a=3          //定义变量
>>> b="abc"
>>> type(a)       //查看数据类型
<class 'int'>
>>> type(b)
<class 'str'>

数据集合:

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值