zoukankan      html  css  js  c++  java
  • python学习2—python3特性与各种运算符

    python学习2—python3特性与各种运算符

    python3与python2相比具有的新特性

    1. 在python2中可以使用__future__模块调用python3的特性
    2. print()函数必须带括号
    3. 整数除法,写错了也不会出发SyntaxError
    4. Unicode字符串和UTF-8字符串、两个字节类:bytes与bytearrays
    5. range()函数代替xrange()函数
    6. 只能使用next()函数而不是.next()方法
    7. for循环中的变量不会泄漏到全局命名空间中
    8. 若比较无序类型,则会触发TypeError
    9. 使用input()获取的内容总是被存储为str字符串类型

    用户名密码处理方式:

    import getpass
    
    name = input('Please input your name:')
    pwd = getpass.getpass('Please input your password:')
    
    if name == "alex" and pwd == "cmd":
        print('Welcome, Alex!')
    else:
        print('User name or password is wrong! Please retry.')

    输出1-9,不输出7的另一个方法:

    # test <continue>
    count = 1
    while count < 10:
        if count == 7:
            count += 1
            continue
        print(count)
        count += 1

    代码中,continue结束当前循环,进入下一次循环。

    与此对比,break则结束全部循环,进入循环后面的代码。

    测试break

    # test <break>
    count = 1
    while count < 10:
        if count == 7:
            count += 1
            break
        print(count)
        count += 1

    上面程序输出1-6,当count=7时,则退出循环。

    用户登录(三次尝试机会)

    # user login with three trying times
    import getpass
    
    count = 0
    while count < 3:
        name = input('Please input your name:')
        pwd = getpass.getpass('Please input your password:')
        if name == "alex" and pwd == "cmd":
            print('Welcome, Alex!')
            break
        else:
            print('User name or password is wrong! Please retry.')
        count += 1
    print('next options...')

    成员操作符

    成员操作符:in 与 not in命令,判断一个字符串是否为另一个字符串的子集。

    # in and not in
    name = "alexprone"
    if "alx" in name:
        print('OK')
    elif "alx" not in name:
        print('good')
    else:
        print('Error')

    布尔值

    布尔值一共有两个值:

    True:真

    False:假

    运算符

    算术运算与赋值运算:

    +, -, *, /, %, **, //;

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

    比较运算、逻辑运算与成员运算:

    ==, >, <, >=, <=, !=(不等于), <>(不等于),

    and(与运算符),or(或运算符),not(取反操作符);

    in, not in

    位运算符

    &  按位与运算

    |  按位或运算

    ^  按位异或运算

    ~  按位取反运算

    <<  左移运算符:a << 2

    >>  右移运算符:a >> 2

    优先级运算顺序:

    先计算括号内,从前往后计算,分类讨论:

    True or ==> True

    True and ==> go on

    False or ==> go on

    False and==> False

  • 相关阅读:
    APP测试-流量测试
    APP测试-流畅度测试
    APP测试-耗电分析
    工具安装-Homebrew
    工具安装-go for Mac
    APP测试-耗电量测试
    APP测试-CPU测试
    APP测试-内存测试
    APP测试-monkey
    APP测试-adb命令
  • 原文地址:https://www.cnblogs.com/yangjingxuan/p/11666760.html
Copyright © 2011-2022 走看看