您的位置:首页 > 科技 > 能源 > 安装python+python的基础语法

安装python+python的基础语法

2024/10/5 17:27:26 来源:https://blog.csdn.net/weixin_70752007/article/details/141017574  浏览:    关键词:安装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:statement0if condition1:block1else: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/")

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com