美文网首页
Control structures(二)

Control structures(二)

作者: sssrx | 来源:发表于2018-01-17 15:58 被阅读0次
    1. Booleans布尔运算:布尔运算仅有两个布尔值True和False。
    • 两个等于号==表示“是否相等”,例如:

    1==2
    False

    又如:

    "hello" == "hello"
    True

    • 叹号加等于号!=表示不等于,例如:

    1 != 1
    False
    "eleven" != "seven"
    True
    2 != 10
    True

    • 大于等于和小于等于表示为:> = 和< =
    1. if条件语句
    • if,例如:

    num = 12
    if num > 5:
    print("Bigger than 5")
    if num <=47:
    print("Between 5 and 47")
    结果输出为:
    Bigger than 5
    Between 5 and 47

    • if...else,例如:

    x = 4
    if x == 5:
    print("Yes")
    else:
    print("No")
    输出结果为:
    No

    • if...elif,其中elif表示else if,例如:

    num = 7
    if num == 5:
    print("Number is 5")
    elif num == 11:
    print("Number is 11")
    elif num == 7:
    print("Number is 7")
    else:
    print("Number isn't 5, 11 or 7")
    输出结果为:
    Number is 7

    1. Boolean Logic布尔逻辑,and、or、not
    • and,两者都满足时才为True,例如:

    1 == 1 and 2 == 2
    True
    1 == 1 and 2 == 3
    False
    1 != 1 and 2 == 2
    False
    2 < 1 and 3 > 6
    False

    • or,有一个满足时就为True,例如:

    1 == 1 or 2 == 2
    True
    1 == 1 or 2 == 3
    True
    1 != 1 or 2 == 2
    True
    2 < 1 or 3 > 6
    False

    • not,取反,例如:

    not 1 == 1
    False
    not 1 > 7
    True

    1. 运算符优先级
    • ==高于or,例如:

    False == False or True
    True
    False == (False or True)
    False
    (False == False) or True
    True

    • python中运算优先级(从上到下):


      python运算优先级
    1. while循环
    • 如果满足条件,则只执行一次if后面的语句,而while语句则是满足条件时,循环执行while后面的语句,直至不满足条件,例如:

    i = 1
    while i <=5:
    print(i)
    i = i + 1
    print("Finished!")

    输出结果为:

    1
    2
    3
    4
    5
    Finished!

    • infinite loop无限循环,程序始终循环运行,输入Ctrl + C或关闭程序才能终止,例如:

    while 1==1:
    print("In the loop")

    这一程序会一直输出In the loop

    • 终止循环(要嵌套在循环内)break,程序在满足条件处终止,跳出循环,不在执行下面的语句,例如:

    i = 0
    while 1==1:
    print(i)
    i = i + 1
    if i >= 5:
    print("Breaking")
    break
    print("Finished")

    如果不添加if语句,该循环将一直进行下去,1==1为开启循环,结果输出为:

    0
    1
    2
    3

    4
    Breaking
    Finished

    • 继续循环continue,满足条件时,程序不再执行下面的语句,返回到循环的开头,开始下一次循环,例如:

    i = 0
    while True:
    i = i +1
    if i == 2:
    print("Skipping 2")
    continue
    if i == 5:
    print("Breaking")
    break
    print(i)
    print("Finished")

    输出结果为:

    1
    Skipping 2
    3
    4
    Breaking
    Finished

    1. List列表,使用方括号,并用逗号隔开项[ , , ,],最后一项后面加不加逗号都可以,调用其中的项时也用方括号,例如:

    words = ["Hello", "world", "!"]
    print(words[0])
    print(words[1])
    print(words[2])

    输出结果为:

    Hello
    world

    空列表使用一对里面没有内容的方括号[]表示,例如:

    empty_list = []
    print(empty_list)
    []

    列表内的项可以是不同类型的,如整数、小数、字符串、变量等,例如:

    number = 3
    things = ["string", 0, [1, 2, number], 4.56]
    print(things[1])
    print(things[2])
    print(things[2][2])

    输出结果为:

    0
    [1, 2, 3]
    3
    索引除了可以调用列表内的项,还可调用字符串中的项,但整数不可以,例如:
    str = "Hello world!"
    print(str[6])

    输出结果为:

    w

    对列表中的某一项进行更改,例如:

    nums = [7, 7, 7, 7, 7]
    nums[2] = 5
    print(nums)

    输出结果为:

    [7, 7, 5, 7, 7]

    列表也可以像字符串一样进行加、乘运算,例如:

    nums = [1, 2, 3]
    print(nums + [4, 5, 6])
    print(nums * 3)

    输出结果为:

    [1, 2, 3, 4, 5, 6]
    [1, 2, 3, 1, 2, 3, 1, 2, 3]

    使用in来检查某一项是否在列表中,例如:

    words = ["spam", "egg", "spam", "sausage"]
    print("spam" in words)
    print("egg" in words)
    print("tomato" in words)

    输出结果为:

    True
    True
    False

    其中in还可用于检查一个字符串是否是另一个字符串的子集。
    使用not来检查某一项是否不在列表中,例如:

    nums = [1, 2, 3]
    print(not 4 in nums)
    print(4 not in nums)
    print(not 3 in nums)
    print(3 not in nums)

    输出结果为:

    True
    True

    False
    False

    若要在列表末尾加上一项,可在列表变量名后加上.append(),例如:

    nums = [1, 2, 3]
    nums.append(4)
    print(nums)

    输出结果为:

    [1,2,3,4]

    所要获得列表中项的个数,可用len函数,例如:

    nums = [1, 3, 5, 2, 4]
    print(len(nums))

    输出结果为:

    5

    若要在列表的任意位置插入一项,可在列表变量名后加上.insert(),用index指示插入位置,例如:

    words = ["Python", "fun"]
    index = 1
    words.insert(index, "is")
    print(words)

    输出结果为:

    ['Python', 'is', 'fun']

    若要输出列表中某一项是第几项,可在变量名后加上.index,若该项不在列表中则报错,例如:

    letters = ['p', 'q', 'r', 's', 'p', 'u']
    print(letters.index('r'))
    print(letters.index('p'))
    print(letters.index('z'))

    输出结果为:

    2
    0
    ValueError: 'z' is not in list

    1. range函数
    • 如果range()函数的括号中仅指定一个数,那么则生成从0开始至小于指定数的最大整数的连续整数,例如:

    numbers = list(range(10))
    print(numbers)

    输出结果为:

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    • 如果range()函数的括号中指定有两个数,那么则生成从小数到大数的连续整数,不包括大数,例如:

    numbers = list(range(3, 8))
    print(numbers)
    print(range(20) == range(0, 20))

    输出结果为:

    [3, 4, 5, 6, 7]
    True

    • 如果range()函数的括号中指定有三个数,则第一个和第二个数表示范围,第三个数表示间隔且必须为整数,例如:

    numbers = list(range(5, 20, 2))
    print(numbers)

    输出结果为:

    [5, 7, 9, 11, 13, 15, 17, 19]

    1. 列表中项的迭代循环
    • 使用while循环实现,例如:

    words = ["hello", "world", "spam", "eggs"]
    counter = 0
    max_index = len(words) - 1
    while counter <= max_index:
    word = words[counter]
    print(word + "!")
    counter = counter + 1

    输出结果为:

    hello!
    world!
    spam!
    eggs!

    • 使用更简洁的for循环实现,例如:

    words = ["hello", "world", "spam", "eggs"]
    for word in words:
    print(word + "!")

    输出结果为:

    hello!
    world!
    spam!
    eggs!

    • for循环通畅用于确定次数的循环迭代,例如:

    for i in range(5):
    print("hello!")

    输出结果为:

    hello!
    hello!
    hello!
    hello!
    hello!

    相关文章

      网友评论

          本文标题:Control structures(二)

          本文链接:https://www.haomeiwen.com/subject/tkjuoxtx.html