zoukankan      html  css  js  c++  java
  • 2018.7.27笔记(while循环,格式化输出,运算符)

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

    执行顺序:
    判断条件是否为真. 如果真. 执行循环体. 然后再次判断条件....直到循环条件为假. 程序退出
    当条件为True时,这是个死循环.
    例题1.数数 1-100
    count = 1
     while count < =100:
    print(count)
    count = count + 1
    2. break和continue
    break: 停止当前本层循环
    continue: 停止当前本次循环. 继续执行下一次循环(用来排除一些内容)
    例题2.
    让用户一直去输入内容, 并打印. 直到用户输入q的时候退出程序
    (1)while True:
    content = input("请输入一句话,(输入q退出程序):")
    if content == 'q':
    break # 打断. 终止当前本层循环
    print(content)
    flag = True
    (2)while flag:
    content = input("请输入一句话,(输入q退出程序):")
    if content == 'q':
    flag = False # 打断. 终止当前本层循环
    print(content)
    (3)while True:
    content = input("请输入一句话,(输入q退出程序):")
    if content == 'q':
    continue # 停止当前本次循环. 继续执行下一次循环
    print(content)
    (4)count = 1
    while count <= 20:
    if count == 10:
    break # 不会触发else的执行, while...else...是一个整体. break的时候彻底的停止这个整体
    print(count)
    count = count + 1
    else: # 当上面的条件不成立的时候执行这个else中的代码
    print("数完了")
    二. 格式化输出
    %s 占位字符串,全能的 什么都能接
    %d 占位数字
    例题3.
    name="alex"
    age = 38
    hobby = "浪"
    location = "湖边"
    print(age+"岁的"+name+"在"+location+"喜欢"+hobby)
    print("%s岁的%s在%s喜欢%s" % (age, name, location, hobby))
    例题4.
    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)
    例题5.
    如果你的字符串中出现了%s这样的格式化的内容. 后面的%都认为是格式化.如果想要使用%. 需要转义 %%
    name = 'sylar'
    print("我叫%s, 我已经学习了2%%的python了" % (name))
    print("我叫周润发. 我已经活了50%了")


    三. 运算符
    1.+ - * / % //
    %:判断奇偶数,判断是不是某个数或某个数的倍数
    //: (整除,地板除,计算商)
    **:(次幂)5**2=25
    例题
    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次幂
    2.比较运算符
    ==(等于) !=(不等于) <>(不等于) <= >= < >
    例题
    a = 10
    b = 20
    print(a == b) # 等于
    print(a != b) # 不等于
    3.赋值运算
    += -= *= /= %= //=
    例题
    a = 1
    b = 2
    a += b # a = 3 a+=b => a = a + b
    # a *= b => a = a * b
    print(a)
    print(b)
    4.逻辑运算
    1. and 并且的含义. 左右两端同时为真. 结果才能是真.
    2. or 或者的含义. 左右两端有一个是真. 结果就是真. 所有的条件都是假. 结果才是假
    3. not 取反 非真既假, 非假既真
    顺序: () => not => and => or 相同的运算. 从左往右算
    print(1>2 and 4<6 or 5>7)#False
    print(1 > 2 or 3 > 4)#false
    print(5>3 or 4<6)#True
    print(5>3 or 4>6)#True
    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
    四. 编码
    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
    五.in 和 not in
    in:在里面
    not in:不在里面
    例题
    content = input("请输入你的评论:")
    if "马化腾" not in content:
    print("你的言论不和谐")
    else:
    print(content)
     
  • 相关阅读:
    DRF
    DRF
    DRF
    DRF
    DRF
    DRF
    DRF
    Mongo错误记录:MongoClient opened before fork. Create MongoClient
    Hive默认分隔符和默认NULL值
    hdfs文件格式比较
  • 原文地址:https://www.cnblogs.com/Bug66/p/9377646.html
Copyright © 2011-2022 走看看