• python-05 条件、循环及其他语句


    # 条件、 循环 及其他语句
    # 5.1 再谈 print 和 import
    # 5.1.1 打印多个参数
    # print('Age:', 42)
    #
    # name = 'Gumby'
    # salutation = 'Mr.'
    # greeting = 'Hello'
    # print(greeting, salutation, name)
    # print("I", "wish", "to", "register", "a", "complaint", sep="_")
    # end 结尾符 seq 分隔符
    # 5.1.2 导入时重命名
    # import math # 1、
    # from math import *
    # from math import tan
    # from math import tan, sin

    # 重命名
    # import math as foobar
    # print(foobar.sqrt(4))
    #
    # from math import sqrt as foo
    # print(foo(4))

    # 5.2 赋值魔法
    #5.2.1 序列解包
    # x, y, z = 1, 2, 3
    # print(x, y)

    # 用 * 号来收集多余的值,数据结果是元组
    # a, b, *rest = [1, 2, 3, 4]
    # print(rest)
    # a, *rest, b = [1, 2, 3, 4]
    # print(rest)
    # print(b)

    # 5.2.2 链式赋值
    # x = y = [1, 2, 3]
    # print(x is y)
    # b = [1, 2, 3]
    # a = [1, 2, 3]
    # print(a is b)

    # 5.2.3 增强赋值 += -= *=

    # 5.3 代码块:缩进的乐趣

    # 5.4 条件和条件语句

    #5.4.1 这正是布尔值的用武之地
    # 假值 False None 0 '' () [] {}
    # 真值:除了假值之外

    # print(True)
    # print(bool(1))

    # 5.4.2 有条件的执行和if 语句
    """
    name = input('What is your name?')
    if name.endswith('Gumby'):
    print('Hello, Mr.Gumby')
    """
    # 5.4.3 else 子句

    # name = input('What is your name?: ')
    # status = "friend" if name.endswith("Gumby") else "stranger"
    # if name.endswith('Gumby'):
    # print('Hello, Mr.Gumby')
    # else:
    # print('Hello, stranger')
    # print(status)

    # 5.4.4 elif 子句 else if 缩写

    # 5.4.5 代码块嵌套

    # 5.4.6 更复杂的条件

    # 1、比较运算符 != (<>) 没有<> == 相等 is 相同
    # print(1 != 2)

    # 2.布尔运算符 and or

    # 5.4.7 断言

    # 5.5 循环
    # 5.5.1 while 循环
    # x = 1
    # while x <= 100:
    # print(x)
    # x += 1

    # 5.5.2 for 循环
    # words = ['this', 'is', 'an', 'ex', 'parrot']
    # for word in words:
    # print(word)
    #
    # for number in range(101):
    # print(number)

    # 5.5.3 迭代字典
    # d = {'x': 1, 'y':2, 'z':3}
    # for key in d:
    # print(key, 'corresponds to', d[key])

    # 5.5..4 一些迭代工具
    # 1.并行迭代
    # names = ['anne', 'beth', 'george', 'damon']
    # ages = [12, 45, 32, 102]
    # for i in range(len(names)):
    # print(names[i], 'is', ages[i], 'years old')
    # print('{} is {} years old'.format(names[i], ages[i]))
    # print(list(zip(names, ages))) # zip 整合两个序列

    # print( '11'.join(reversed('Hello, world!')))
    # 2 迭代时获取索引 enumerate
    # 3 反向迭代和排序后再迭代 sorted(排序), reversed(首尾互换) 都是返回一个列表

    # 5.5.5 跳出循环
    # 1 break 结束循环
    # 2 continue 结束迭代
    # 3 while True/ break
    # while True:
    # word = input('Please enter a word:')
    # if not word:
    # break
    # print('This word was ', word)

    # 5.5.6 循环中的else 子句 循环进行完之后才会执行else的语句

    # 5.6 简单推导
    # print([x * x for x in range(10)])
    #
    # girls = ['alice', 'bernice', 'clarice']
    # boys = ['chris', 'arnold', 'bob']
    # letterGirls = {}
    # for girl in girls:
    # letterGirls.setdefault(girl[0], []).append(girl)
    # print(letterGirls)
    # print([b + '+' + g for b in boys for g in letterGirls[b[0]]])
    # [] 列表
    # ()生成器
    # 字典推导

    #5.7 三人行 pass del exec(eval)
    # 5.7.1 pass 什么都不用做
    # 5.7.2 del 删除
    # 5.7.3 使用exec 和 eval 执行字符串及计算结果
    # exec 将字符串代码执行
    from math import sqrt
    scope = {}
    exec('sqrt = 9', scope)
    print(scope['sqrt'])
    print(scope.keys())
    print(sqrt(4))

    # 2.eval 算用字符串表示的Python表达式的值,并返回结果



    '''
    x == y x 等于y
    x < y x小于y
    x > y x大于y
    x >= y x大于或等于y
    x <= y x小于或等于y
    x != y x不等于y
    x is y x和y是同一个对象
    x is not y x和y是不同的对象
    x in y x是容器(如序列)y的成员
    x not in y x不是容器(如序列)y的成员

    chr(n) 返回一个字符串,其中只包含一个字符,这个字符对应于传入的顺序值n(0 ≤n < 256)
    eval(source[,globals[,locals]]) 计算并返回字符串表示的表达式的结果
    exec(source[, globals[, locals]]) 将字符串作为语句执行
    enumerate(seq) 生成可迭代的索引值对
    ord(c) 接受一个只包含一个字符的字符串,并返回这个字符的顺序值(一个整数)
    range([start,] stop[, step]) 创建一个由整数组成的列表
    reversed(seq) 按相反的顺序返回seq中的值,以便用于迭代
    sorted(seq[,cmp][,key][,reverse]) 返回一个列表,其中包含seq中的所有值且这些值是经过排序的
    xrange([start,] stop[, step]) 创建一个用于迭代的xrange对象
    zip(seq1, seq2,...) 创建一个适合用于并行迭代的新序列
    '''


  • 相关阅读:
    Vasya and Endless Credits CodeForces
    Dreamoon and Strings CodeForces
    Online Meeting CodeForces
    数塔取数 基础dp
    1001 数组中和等于K的数对 1090 3个数和为0
    1091 线段的重叠
    51nod 最小周长
    走格子 51nod
    1289 大鱼吃小鱼
    POJ 1979 Red and Black
  • 原文地址:https://www.cnblogs.com/fuyouqiang/p/11844626.html
Copyright © 2020-2023  润新知