当前位置:   article > 正文

Python的安装环境以及应用

Python的安装环境以及应用

1.环境python2,Python

最新安装3.12可以使用源码安装

查看安装包

[root@python001 ~]# yum list installed | grep epel
3[root@python001 ~]# yum list installed | grep python

[root@python001 ~]# yum -y install python3        安装python3

查看版本

[root@python001 ~]# python3 --version
Python 3.6.8
[root@python001 ~]# python3        进入编辑页面,也会进入到python2中
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

判断其变量类型

>>> a=3
>>> b="abc"
>>> type(a)
<class 'int'>
>>> type(b)
<class 'str'>

>>> quit()        退出

[root@python001 ~]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ some-package        修改pip镜像为清华

2.变量和数据类型

1.三大数据类型

  1.       字符        字符串
  2. >>> b='zhangin'
  3. >>> b
  4. 'zhangin'
  5. >>> type (b)
  6. <class 'str'>
  7.         数值        整数,浮点
  8. >>> c=3
  9. >>> c
  10. 3
  11. >>> type(c)
  12. <class 'int'>
  13. >>> d=3.14
  14. >>> d
  15. 3.14
  16. >>> type(d)
  17. <class 'float'>
  18.         逻辑        true,false
  19. >>> flag=True
  20. >>> print(flag);
  21. True
  22. >>> print(1==1);
  23. True
  24. >>> print(1!=1)
  25. False
  1. >>> name1="张三"
  2. >>> name2="李四"
  3. >>> name3="王五"
  4. >>> print(name1,name2,name3)
  5. 张三 李四 王五

最终的计算是在python内存中计算的,必须要有指定内存空间保存数据,这些内存空间其实就是变量

使用数据集合批量管理数据,管理内存空间

3.数据集合

        1.列表

                1.使用最为广泛的数据集合工具

                2.是java中数组和list的综合体

                3.list

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

  1. >>> lista=["张三","李四","王五","赵六"]
  2. >>> type(lista)
  3. <class 'list'>
>>> help(list)    //查看list相关命令
  1. >>> listb.insert(1,"tomcat")
  2. >>> listb
  3. ['tom', 'tomcat', 'jerry']
  4. >>> listb.append("tomcat");
  5. >>> listb
  6. ['tom', 'tomcat', 'jerry', 'tomcat']
  7. >>> listb.shift("jerry")
  8. Traceback (most recent call last):
  9. File "<stdin>", line 1, in <module>
  10. AttributeError: 'list' object has no attribute 'shift'
  11. >>> listb.pop("jerry")
  12. Traceback (most recent call last):
  13. File "<stdin>", line 1, in <module>
  14. TypeError: 'str' object cannot be interpreted as an integer
  15. >>> listb.pop()
  16. 'tomcat'
  17. >>> listb.pop()
  18. 'jerry'
  19. >>> listb
  20. ['tom', 'tomcat']
  21. >>> listb.pop()
  22. 'tomcat'
  23. >>> listb.pop()
  24. 'tom'
  25. >>> listb
  26. []
  27. >>> listc=listb.pop()
  28. Traceback (most recent call last):
  29. File "<stdin>", line 1, in <module>
  30. IndexError: pop from empty list
  31. >>> listb=["job"]
  32. >>> listc=listb.pop()
  33. >>> listc
  34. 'job'
  35. >>>
  1. >>> listb
  2. []
  3. >>> listb.append("zz")
  4. >>> listb
  5. ['zz']
  6. >>> listb.append("bb")
  7. >>> listb.append("bb")
  8. >>> listb
  9. ['zz', 'bb', 'bb']
  10. >>> listb.remove("bb")
  11. >>> listb
  12. ['zz', 'bb']
  13. >>> listb.remove(listb[0])
  14. >>> listb
  15. ['bb']

当在列表中删除或者修改一个元素的时候,列表会返回新的列表

                5.管理列表

                      

  1. #python为开发提供了丰富的使用感手册
  2. help(lista) #通过上下方向,enter,space健来翻阅信息,使用q来退出查看 more less
  3. #创建列表
  4. lista=[]
  5. listc[1,2,3]
  6. #修改元素
  7. #追加元素
  8. lista.appendd(item) #在所有元素之后添加元素
  9. #插入元素
  10. listb.insert(pos,item) #在pos序号之前插入item
  11. 删除元素 remove和pop
  12. list.pop() #删除list中最后一个元素
  13. list.remove(liast[index]) #删除序号为index的元素
  14. #修改元素
  15. list[index]=newvalue
  16. #del list 删除表
  17. #

         2.字典

              1.dict

                2.dictionary

                3.key-value 键值对

                4.{"name":"陈","age":"20","gender":"male"}

                5.键值

  1. {
  2. "from":"me",
  3. "to":"you",
  4. "message":"你吃饭了吗?"
  5. "time":"2024-7-8 9:00:32",
  6. "user":{
  7. "username":"abc",
  8. "password":"abc"
  9. }
  10. }

                6.{}

        3.元组(不能修改,可以查看)

        1.没有修改,只可以查看

        2.Tuple[index]

        3.list(tuple)

        4.Tuple(list)

4.[]列表,{}字典,()元组

5.List()可以吧dict的key生成一个列表

6.list可以吧tupl变成列表

7.tupl可以吧dic和list变成元组

  1. >>> tupl0
  2. (1, 2, 3, 4)
  3. >>> tupl0[0]
  4. 1
  5. >>> tupl0[1]
  6. 2
  7. >>> tupl0[1]=666
  8. Traceback (most recent call last):
  9. File "<stdin>", line 1, in <module>
  10. TypeError: 'tuple' object does not support item assignment
  11. >>> aa=list(tupl0)
  12. >>> aa
  13. [1, 2, 3, 4]
  14. >>> dict1={"a":1,"b":2,"c":3}
  15. >>> dict1.keys
  16. <built-in method keys of dict object at 0x7fe9aa9f9558>
  17. >>> dict1.keys()
  18. dict_keys(['a', 'b', 'c'])
  19. >>> dict1.items()
  20. dict_items([('a', 1), ('b', 2), ('c', 3)])
  21. >>> dict([("a",1),("b",2)])
  22. {'a': 1, 'b': 2}

4.选择语句和循环语句

1.选择语句

        1.if

                1.缩进是必须的

  1. if condition0:
  2. statement0;
  3. if condition:
  4. block1;
  5. else:
  6. block2;
  7. else:
  8. statement1;

       

  1. [root@python001 ~]# cat py003.py
  2. import random
  3. n=random.randint(50,100)
  4. print("随机数值为",n)
  5. if n>90:
  6. print("优秀")
  7. else:
  8. if n>80:
  9. print("良好")
  10. else:
  11. if n>70:
  12. print("中等")
  13. else:
  14. if n>59:
  15. print("及格")
  16. else:
  17. print("不及格")

[root@python001 ~]# python3 py003.py 
随机数值为 87
良好
[root@python001 ~]# python3 py003.py 
随机数值为 53
不及格
[root@python001 ~]# python3 py003.py 
随机数值为 70
及格

     

        2.swith插槽

2.循环语句

        1.for

  1. >>> range(9)
  2. range(0, 9)
  3. >>> list(range(9))
  4. [0, 1, 2, 3, 4, 5, 6, 7, 8]
  5. >>> for i
  6. id( import input( is issubclass(
  7. if in int( isinstance( iter(
  8. >>> for i in range(9):
  9. ... print(i)
  10. ...
  11. 0
  12. 1
  13. 2
  14. 3
  15. 4
  16. 5
  17. 6
  18. 7
  19. 8
  20. for i in range(101): #0-100
  21. n=n+i
  22. print(n) #1-100数字累加
  23. #在列表中循环
  24. for var in ["a","b","c"]:
  25. print(var)
  26. #在字典中遍历
  27. d={"id":1001,"name":"zhangsan","gender":"女","age":18}
  28. for var in d:
  29. print(var) #将这个字典中的额key都输出的
  30. print(d[var]) #根据key返回对应的value值
  31. for var in d.keys():
  32. print(var)
  33. print(d[var])
  34. #在元组中的遍历
  35. tupl0=("a","b","c")
  36. for var in tupl0:
  37. print(var)

案例

  1. >>> b=list(range(101))
  2. >>> b
  3. [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]
  4. >>> for i in b:
  5. ... if i%7==0:
  6. ... print(i,"可以被七整除")
  7. ...
  8. 0 可以被七整除
  9. 7 可以被七整除
  10. 14 可以被七整除
  11. 21 可以被七整除
  12. 28 可以被七整除
  13. 35 可以被七整除
  14. 42 可以被七整除
  15. 49 可以被七整除
  16. 56 可以被七整除
  17. 63 可以被七整除
  18. 70 可以被七整除
  19. 77 可以被七整除
  20. 84 可以被七整除
  21. 91 可以被七整除
  22. 98 可以被七整除

        2.while

  1. while condition:
  2. block
  3. continue,break;
  4. # 指令
  5. vim 001.py
  6. # 执⾏py脚本
  7. python3 001.py
  8. # 调试py脚本
  9. python3 -m pdb 001.py
  10. # 输⼊n按回⻋执⾏下⼀⾏代码
  11. # 输⼊q退出调试
  12. 5.常⽤的⼯具api
  13. # ⽣成随机
  14. import random
  15. n=random.randint(0,10)
  16. # 创建⽬录
  17. import os
  18. os.mkdir("/opt/aaa/")
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/977622
推荐阅读
相关标签
  

闽ICP备14008679号