• python 基础


    1. 后缀名 :导入模块时,后缀名必须是 py  

    2. 2种执行方式 

        2.1  python 解释器  py文件路径

        2.2 python 进入交互模式  # windows cmd 终端默认是 gbk编码

    3.  #!/usr/bin/env python

        因为linunx 下   ./abc.py  上面这句会指明 . 申明一下的解释器名称

    4. # -*- coding:utf8 -*-     # 2.3 必须在文件顶部,才可以生效

        在python3 已可以不加,读任何编码文件,内部会自动转换为unicode,当输出时,其他编码对应unicode也可正常输出

        在python2 必须加

        在python2 不加,输出中文会报错。因为它默认使用ascii编码,无法解释中文。

        加上这句时,指使用utf-8 读取文件

    4+  编码: ascii   00000000  8位

                      unicode  至少16位 (浪费位数)万国码

                      utf-8   能用几位表示就用几位表示 (会节约内存)  中文 用3个字节,相当于对unicode的一种压缩

                      gbk     由unicode 转换来,主要表示中文。 用2个字节16位表示中文 

    5. 注释   单行用 #  多行用 “”“      ”“”   三引号

    6.条件分支  

        if  1 == 1 :

                if 2 == 2:

                         print("hello")

                else :

                         print("hi") 

       elif b :

                pass  # 关键字 表示什么都不执行

       else :

                pass

    7. 行首缩进

        在逻辑行首的空白(空格和制表符)用来决定逻辑行的缩进层次,从而用来决定语句的分组。
        这意味着同一层次的语句必须有相同的缩进。每一组这样的语句称为一个块。

        缺点:1.粘贴代码的时候修改缩进不方便

                   2.html嵌套py代码的时候,要同时维护两套缩进,html的和py的。这个太困难了

                   3.python的代码一旦缩进乱了,是无法重新格式化的

    8. 基本数据类型

        8.1 字符串 name = "jgmorA"

                          name = 'jgmorA'

                          name = '''jgmorA'''

                          name = """jgmorA"""           # 引号必须成对出现

              a = "hello"  

              b = "world" 

              a + b = "helloworld"

              a * 2 = “hellohello"   # 字符串乘法  

      8.2 数字

              c = 2**4   # 2的4次方

              d = 10 % 8  # 取余  2

              e = 10 // 8  # 取商  1

               f = 10 /8  # 1.125

              g = "10"

              h = int(g)  # 字符串转数字

    9. 循环

           while  1==1 :

                  printf('ok')

  • 相关阅读:
    JS放在head和放在body中的区别
    模板模式(Template Pattern)
    原型模式
    Linux下的头文件搜索路径
    How to Change the Default Theme Appearance [editing with no theme]
    版本控制
    What is libacl.so.1 ?
    交叉编译器 arm-linux-gnueabi 和 arm-linux-gnueabihf 的区别
    mount --bind 的妙用
    mount的bind选项
  • 原文地址:https://www.cnblogs.com/jgmor/p/9779248.html
Copyright © 2020-2023  润新知