zoukankan      html  css  js  c++  java
  • 002-pythn基础-循环、编码

    1. 循环

    while 条件:
    代码块(循环体)
    else:
    当上面的条件为假. 才会执行

    执行顺序:
    判断条件是否为真. 如果真. 执行循环体. 然后再次判断条件....直到循环条件为假. 程序退出

    2. break和continue
    break: 停止当前本层循环
    continue: 停止当前本次循环. 继续执行下一次循环

    # 死循环
    count = 1
    while count <= 5: print("喷死你..") count = count + 1
    # 数数 1-100 count = 1 while count < 101: print(count) count = count + 2 # 让用户一直去输入内容, 并打印. 直到用户输入q的时候退出程序 while True: content = input("请输入一句话,(输入q退出程序):") if content == 'q': break # 打断. 终止当前本层循环 print(content) flag = True while flag: content = input("请输入一句话,(输入q退出程序):") if content == 'q': flag = False # 打断. 终止当前本层循环 print(content) else: print("123") while True: content = input("请输入一句话,(输入q退出程序):") if content == 'q': continue # 停止当前本次循环. 继续执行下一次循环 print(content) # break和continue的区别: break是彻底的停止掉当前层循环. continue停止当前本次循环,继续执行下一次循环 count = 1 while count <= 10: if count == 4: count = count + 1 continue # 用来排除一些内容 print(count) count = count + 1 # 必须要写
    count = 1 while count <= 20: if count == 10: break # 不会触发else的执行, while...else...是一个整体. break的时候彻底的停止这个整体 print(count) count = count + 1 else: # 当上面的条件不成立的时候执行这个else中的代码 print("数完了")

    3. 格式化输出
    %s 占位字符串
    %d 占位数字

    name="alex"
    age = 38
    hobby = "浪"
    location = "湖边"
    print(age+"岁的"+name+"在"+location+"喜欢"+hobby) #
    # 格式化
    # %s 占位. 占位的是字符串, 全能的. 什么都能接
    # %d 占位. 占位的是数字
    print("%s岁的%s在%s喜欢%s" % (age, name, location, hobby))
    
    name = input("请输入名字:")
    age = input("请输入年龄:")
    job = input("请输入你的工作:")
    hobby = input("请输入你的爱好:")
    
    s = '''------------ info of %s ----------- Name : %s Age : %s job : %s Hobbie: %s ------------- end -----------------''' % (name, name, age, job, hobby) # print(s) name = 'sylar' # 如果你的字符串中出现了%s这样的格式化的内容. 后面的%都认为是格式化.如果想要使用%. 需要转义 %% print("我叫%s, 我已经学习了2%%的python了" % (name)) print("我叫周润发. 我已经活了50%了")

    4. 运算符
    and: 并且, 两端同时为真. 结果才能是真
    or: 或者, 有一个是真. 结果就是真
    not: 非真既假, 非假既真

    顺序: () => not => and => or

    x or y:
    如果x是零, 输出y
    如果x是非零, 输出x

    True: 非零
    False: 零

    print(1+1)
    print(1-1)
    print(1*2)
    print(1/2)
    
    print(10%3) # 计算余数  10/3=3......1
    
    n = 49
    if n % 2 == 1:
        print("奇数")
    else:
        print("偶数")
    
    print(10//3) # 整除. 地板除. 计算商
    
    print(5**3) # 5的2次幂  m**n  m的n次幂
    
    a = 10
    b = 20
    print(a == b) # 等于
    print(a != b)   # 不等于
    
    a = 1
    b = 2
    a += b # a = 3    a+=b  => a = a + b
    # a *= b  =>  a = a * b
    print(a)
    print(b)
    
    # 逻辑运算符
    # 1. and 并且的含义. 左右两端同时为真. 结果才能是真.
    # 2. or 或者的含义. 左右两端有一个是真. 结果就是真. 所有的条件都是假. 结果才是假
    # 3. not 取反 非真既假, 非假既真
    # 顺序: () => not => and => or  相同的运算. 从左往右算
    
    print(1>2 and 4<6 or 5>7)
    print(1 > 2 or 3 > 4)
    print(5>3 or 4<6)
    print(5>3 or 4>6)
    
    print(3>4 or 4<3  and  1==1) # False
    print(1 < 2  and  3 < 4 or 1>2 ) # True
    print(2 > 1  and  3 < 4 or 4 > 5 and  2 < 1) # True
    print(1 > 2  and  3 < 4 or 4 > 5 and  2 > 1  or 9 < 8) # False
    print(1 > 1  and  3 < 4 or 4 > 5 and  2 > 1  and  9 > 8 or 7 < 6) # False
    print(not  2 > 1  and 3 < 4  or 4 > 5  and 2 > 1  and 9 > 8  or 7 < 6) # False
    
    # x or y 如果x是0 返回y, 如果x是非零, 返回x
    print(1 or 2) # 1
    print(1 or 0) # 1
    print(0 or 1) # 1
    print(0 or 2) # 2
    print(0 or 1 or 2 or 3)
    print(3 or 0 or 1 or 0 or 2)
    
    # and和or相反. 不要去总结and.  记住or
    print(1 and 2) # 2
    print(0 and 2) # 0
    print(1 and 0) # 0
    print(0 and 1) # 0
    
    print(1 and 2 or 3)
    print(1 or 2 and 3)
    
    # False: 0, True: 1(非零)
    print(1 and 2>3)
    print(2>3 and 1)
    print(1 > 2 or 0 and 3 < 6 or 5) # 先算and 后算or
    print(2**32)
    

    5. 编码
    1. ascii. 最早的编码. 至今还在使用. 8位一个字节(字符)
    2. GBK. 国标码. 16位2个字节.
    3. unicode. 万国码. 32位4个字节
    4. UTF-8. 可变长度的unicode.
    英文: 8位. 1个字节
    欧洲文字:16位. 2个字节
    汉字. 24位. 3个字节

    8bit = 1byte
    1024byte = 1KB
    1024KB = 1MB
    1024MB = 1GB
    1024GB = 1TB

    2. bool. 类型转换的问题=>
    3. str(重点).

  • 相关阅读:
    用命令创建MySQL数据库
    Linux下安装mysql
    MySQL字符集及校对规则的理解
    Mybatis 高级结果映射 ResultMap Association Collection
    查看linux系统版本命令
    hdu 1217 Arbitrage (最小生成树)
    hdu 2544 最短路(两点间最短路径)
    hdu 3371 Connect the Cities(最小生成树)
    hdu 1301 Jungle Roads (最小生成树)
    hdu 1875 畅通工程再续(prim方法求得最小生成树)
  • 原文地址:https://www.cnblogs.com/David-domain/p/10807774.html
Copyright © 2011-2022 走看看