zoukankan      html  css  js  c++  java
  • python基础之注意事项

    一、python2与python3的区别


      1.  2.x 默认编码:ASSIC 不支持中文

          python2若想使用中文:在文件开头加上  #!-*- coding:utf-8 -*-

          #!-*- coding:utf-8 -*-   或者#coding:utf-8 

             msg = u"我爱中国!" 

             print msg

             print type(msg)

        3.x 默认编码:UNICODE 支持中文

               编码:ASSIC->GB2312->GBK1.0->GB18030

          ASSIC->unicode->utf-8 /utf-16

      2.  3.x 不兼容2.x

      3.  核心语法调整,更易学

      4. 新特性只有3.x支持

    二、 python变量名命名规则


      1. 变量名要具有描述性

      2.变量名只能以字母、数字、下划线_组成,不可以是空格或特殊字符,名字一般用下划线分割

      3.不要用中文

      4.变量名不能以数字开头

      5.保留字符是不能被使用的:如,and,as,assert,break,class,continue,print.........

        如何查保留字符:

          form keyword import kwlist

            for i in kwlist:

              print(i)

        常量:不变的量  PIE = 3.141592653...

        在Python里所有的变量都是可变的,所有用全部大写的变量名来代表次变量为常量

    三、注释


      1.单行注释:#

      2.多行注释:'''或"""    eg:'''被注释的内容'''

    四、逻辑语句


      1.if条件语句

        if 判断条件:

          pass

        elif 判断条件:

          pass

        else:

          pass

      2.while循环

        (1)条件循环

          while 条件:

            pass

       (2)无限循环

          while True:

            pass

         (3)其它格式

          while 条件:

            语句

          else:-这里的else语句,只有循环正常结束的时候才会执行,就算执行break时else也不会执行

            pass

         (4)实例      

        num = 1
    
        while num<=10:
            print(num)
            num += 1
            age=50
            flag=true
        while flag;
            user_age=int(input("age is:"))
            if user_age == age:
                    print ("yes")
                flag=false
            elif user_age > age:
            print("is bigger")
            else:
            print("is smaller")
           print("end")
              
        //中止循环:break,不再有循环操作
        //结束当前循环语句执行下次循环:continue
            num=1
            while num <= 5
                num+=1
            if num == 3
            continue
        print(num)
        //结果:2 4 5 6---结果没有3的原因是num ==3 时不执行print(num)就进入下次循环
    
        //换行符:
            //python用
    做换行符
        //windows里的notepad++//苹果换行为
     
    View Code

       3.for循环

        for i in 序列:(多个元素值:eg元组,列表,range(),字典)-range(5)为[0,1,2,3,4]默认从0开始,是一个列表,下面语句可以对序列可以做处理

          语句

         eg: 

            product_list=[('mac',9000),('kindle',800),('tesla',900000),('python book',105),('bike',2000)]

            for i in product_list:

    五、运算符


      1.算术运算符

        +   -   *   /

        //:商    9//2---4

        % :余数     9%5----1

        **:指数      2**10----1024

         表达式:

            num += 1----num= num + 1

            num -= 1-----num= num - 1

            num *= 2---num= num * 2

            num /= 2 ---num= num / 2 --符点数

            num //= 2 ---num=num // 2--整除

            num %= 2---num=num % 2

            num **= 2-num=num ** 2

      2.赋值运算符

        =   +=   -=   *=   /=    //=   **=    %=

      3.比较运算符

        >   <   >=    <=    !=      ==

      4. 逻辑运算符

         条件1 and 条件2(且)---只有二个条件同时为真时,结果才为真

           >>> 5>3 and  6>2----true 

         条件1 or 条件2 -----只有一个为真结果就为真

           >>> 5>3 or 6<2---true

         not 条件1------结果与条件相反,条件为真结果为假

      5.优先级()

      6.身份运算符:is    not is

        num = []

        if type(num) is int

          print("flase ")

      7.成员运算符:in    not in

        

         

  • 相关阅读:
    spring读取配置文件内容并自动注入
    xshell免费下载安装使用
    cas sso原理
    sql两列相除,保留n位小数
    mysql 报zone什么的错误
    mysql union出错: "Every derived table must have its own alias"
    mysql jdbc操作
    sql 对某列取值进行if判断
    Python深入:02浅拷贝深拷贝
    Python基础:22__slots__类属性
  • 原文地址:https://www.cnblogs.com/Dana-xiong/p/14292069.html
Copyright © 2011-2022 走看看