Python之——遇到的小知识点总结

学习过程中,难免会遇到一些冷门的小知识点,熟悉这些小知识可以在工作中达到事半功倍的效果,尽力消除自己的知识盲区。总之当时的自己花了不少功夫去解决这些问题,因此觉得有必要单独记录下来,以后也许会再遇到,温故而知新。

PS:此文不断更新。。

-----------------------------------------------------------------------------------------------------------------------------------

1.python多版本共存(Windows系统下)

    由于本地已经安装了python3.6.4,故再安装python2.7及3.7.1版本共存。

    安装完毕后,3.7.1版本安装目录下文件如图,2.7同理。

    

    配置环境变量,将两个版本安装目录及安装目录下的script目录加入环境变量,如图所示。(环境变量中位置靠上的python版本即为本机的python解释器的默认版本)

    

    此时,3个版本中均包含名为python.exe的解释器应用程序,因此在任务管理器中调用python解释器功能时,只能使用默认版本的python解释器。此时,到新安装的3.7.1及2.7版本的python目录下,将python.exe复制,改名为python37.exe(版本号,2.7同理),将script下pip.exe复制,改名为  pip37.exe(版本号,2.7同理),如图:

    

     改名成功后测试,成功。

    

2.python2版本和python3版本区别

    2.1 规范性

      1)、在大的环境下,Python2含有PHP、Java、C等语言的规范陋习。(Python是一门开源的语言,任何人都可以贡献代码,但是每个人上传的代码规范都不相同。)

      2)、Python2里面重复的代码特别多。

      3)、Python3编码规范、清晰、简单,符合Python的宗旨。

    2.2 编码

      1)、Python2默认编码是ASCII,只能显示英文,显示中文会报错。想让Python2显示中文,就需在首行添加“# -*- encoding:utf-8 -*-”。

      2)、Python3的默认编码就是utf-8,中文和英文都能支持。

    2.3 语法

      1)、用户交互:Python2的语法是“ raw_input”,而Python3的语法是“input”。

    2.4 数据类型

      1)、Python2里既有 int 类型又有 long int 类型,而Python3里只有 int 类型。

3. eval()  

  官方文档里面给出来的功能解释是:将字符串string对象转化为有效的表达式参与求值运算返回计算结果

  背景:在做一个程序,语义分析的,把一个运算符( > , < , = )存在了变量里,之后想用该变量里的运算符去做逻辑运算,故用到了eval。

a = 5
b = 6
c = '>'
if eval('%s%s%s' % (a, c, b)):
    print ('Impossible')
else:
    print('666')

  运行结果:

4. for ... else ...  

在for执行完成且中间没有被break的情况下,才执行else( while ... else ...同理)

for i in range(3):
    print(i)
else:
    print('else执行!')

  运行结果:

5. python中关键字

['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else','except', 'exec', 'finally', 'for', 'from',
'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print','raise', 'return', 'try', 'while', 'with', 'yield']

6. 针对逻辑运算的进一步研究

    1,在没有()的情况下not 优先级高于 and,and优先级高于or,即优先级关系为( )>not>and>or,同一优先级从左往右计算。

    2 ,  x or y , x为真,值就是x,x为假,值是y,x和y都为假,值是y;x and y, x为真,值是y,x为假,值是x。

    例:求出下列逻辑语句的值。

      8 or 4      结果:8
      0 and 3    结果:0
      0 or 4 and 3 or 7 or 9 and 6     结果:3
1判断下列逻辑语句的True,False.
1)1    > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
F or T or F and T and T or F -> True
2)not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 
        F and T or F and T and T or F  -> F or F or F -> False

2.求出下列逻辑语句的值。
1),8 or 3 and 4 or 2 and 0 or 9 and 7
    8 or 4 or 0 or 7  -> 8
2),0 or 2 and 3 and 4 or 6 and 0 or 3
        0 or 4 or 0 or 3    -> 4

3.下列结果是什么?
1)、6 or 2 > 1    6
2)、3 or 2 > 1    3
3)、0 or 5 < 4    False 
4)、5 < 4 or 3    3
5)、2 > 1 or 6    True
6)、3 and 2 > 1    True
7)、0 and 3 > 1    0
8)、2 > 1 and 3    3
9)、3 > 1 and 0    0
10)、3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2  
        T and 2 or T and 3 and 4 or T  ->  2 or 4 or T -> 2
一些小练习

 7. 元组的类型(一道面试题)

  当元组中只含有一个元素时,元组的类型是该元素的类型。

>>print(type((1)))
int
>>print(type(("str")))
str
原文地址:https://www.cnblogs.com/JackLi07/p/8657605.html