zoukankan      html  css  js  c++  java
  • Python的基本语法2

    一.运算符

    # 算术运算符, +,  -,  *,  /,  //,  %,  **,  注意//为整除
    # 赋值运算符, =, +=, -=, *=, /=, //= ,%=, **=
    # 比较运算符, ==, !=, <>, >, >=, <, <=
    # 逻辑运算符, and, or, not
    # 成员运算符, in, not in
    # 身份运算符, is, not is
    # 位运算符,   &, |, >>, <<, ^, ~
    
    # 优先级  算术>比较>逻辑>赋值
    
    a = 3
    b = 5
    print(b // a)  # 1
    a *= 3  # 9
    print(a > b)  # True,9>5
    print(3>4 and 4>2)  # False

    二.判断语句

    (1)if单分支

    if 条件表达式:

      command1

      ...

    执行顺序:如果条件表达式为真,就进行command1...

    # 单分支
    if 3 > 5:
        print("oh,my gold,it's impossible.")

    (2)if双分支

    if 条件表达式1:

      command1

      ...

    else:

      command2

      ...

    执行顺序:如果条件表达式1为真,就执行command1...;条件表达式1为假,就执行command2...

    # 双分支
    if 3 > 5:
        print("hehe~")
    else:
        print("3<5 is true")

    (3)多分支

    if 条件表达式1:

      command1

      ...

    elif 条件表达式2:

      command2

      ...

    else:

      command n

      ...

    执行顺序:条件表达式1为真,则执行command1,否则判断条件表达式2,条件表达式2为真,则执行command2...;如果都为假,则执行else下的command n

    # 多分支及嵌套
    age = input("pls type your age:")
    sex = input("pls type your sex:")
    if int(age) < 0:
        print("you type wrong age.pls input again.")
    elif int(age) < 18:
        print("it's a boy.")
    else:
        if sex == "man" or sex == "male":    # or为或者的意思
            print("you are %s years old, is a %s" % (age, sex))
        elif sex == "woman" or sex == "female":
            print("you are {} years old ,is a {}".format(age, sex))
        else:
            print("you type wrong sex,pls input again")

    三.while循环

    while 条件表达式:

      command1

      ...

    执行顺序:

    条件表达式为真,就执行循环体中的命令,一轮循环结束后,再次判断条件表达式,如果为真,就再次进入循环,直到条件表达式为假,就退出循环

    循环控制语句

    continue 结束本次循环,开始下一轮循环

    break 跳出循环(如果有循环嵌套,要注意break的位置,是跳出内循环还是外循环)

    exit() 退出python程序,可以指定返回值

     示例:

    # 猜数字小游戏
    import random
    number = random.randint(1, 100)  # 获取1~100的随机数,包括1和100
    
    while True:
        guess_num = int(input("pls type a number(1-100):"))
        if guess_num > number:
            print("you guess big.pls guesss again.")
            continue
        elif guess_num < number:
            print("you guess small.pls guess again")
            continue
        else:
            print("success,it's {}.".format(guess_num), "you will get a prize")
            flag = input("do you want play again?(y/n):")
            if flag == "y":
                number = random.randint(1, 100)
                continue
            else:
                break
    # while practice
    
    # 打印100以内的偶数之和
    
    # 方法一
    i = 0
    sum = 0
    while i <= 100:
        sum += i
        i += 2
    print("sum = %d" % sum)
    
    # 方法二
    i = 0
    sum = 0
    while i <= 100:
        if i%2 == 0:
            sum += i
        i += 1
    print("sum = %d" % sum)
    
    # 方法三
    i = 0
    sum = 0
    while i <= 100:
        if i%2 == 1:
            i += 1
        else:
            sum += i
            i += 1
    print("sum = %d" % sum)
  • 相关阅读:
    linux command mktemp
    易经中时常会想起的句子
    判断NumLock键和CapsLock键是否被锁定
    获得虚拟键键码
    获取功能键
    捕获组合键
    模拟鼠标操作
    利用鼠标绘图
    使用鼠标拖放复制文本
    双击窗体是模拟键盘上的Tab键
  • 原文地址:https://www.cnblogs.com/golinux/p/10764951.html
Copyright © 2011-2022 走看看