zoukankan      html  css  js  c++  java
  • while循环

    一.while循环

    定义:判断条件为真时,程序进入循环体,循环体代码执行到底部,重新返回,再次判断条件。

    应用场景:1.用户重复输入密码;2.需要无限次显示页面,或无限次输入

    1.无限循环:

    a = '牵丝线'
    b = '别离歌'
    c = '相思泪'
    d = '脑太残'
    while True:
    print(a)
    print(b)
    print(c)
    print(d)
    2.跳出循环:
    1:标志类
    a = '牵丝线'
    b = '别离歌'
    c = '相思泪'
    d = '脑太残'
    flag = True
    while flag:
    print(a)
    print(b)
    print(c)
    print(d)
    flag = False
    2:break跳出死循环:
    知识点1:while循环中,遇到break,立刻跳出循环;while循环中。
    flag = True
    while flag:
    print('牵丝线')
    print('别离歌')
    print('相思泪')
    print('脑太残')
    break

       知识点2:

    若被break打断,则不执行else代码块.
    count = 1
    while count < 10:
    print('牵丝线')
    print('别离歌')
    print('相思泪')
    print('脑太残')
    count += 1
    if count == 5:
    break
    else:
    print('太平洋')

    3.continue

    知识点:while循环中,遇到continue,结束本次循环,重新进行条件判断,为True则开始执行循环体.

    flag = True
    count = 0
    while flag:
    print('牵丝线')
    print('别离歌')
    count += 1
    if count > 3:
    flag = False
    continue
    print('相思泪')
    print('脑太残')

    4.while循环到某次数,跳出循环:

    count = 1
    while count < 3:
    print('牵丝线')
    print('别离歌')
    print('相思泪')
    print('脑太残')
    count += 1

    二.格式化输出:

    知识点:制作一个输入模板,某些位置的参数是动态的,这是就需要使用到格式化输出:字符串的动态替换。

    1.字典
    name = input('你的名字:')
    age = int(input('你的年龄:'))
    sex = input('你的性别:')
    msg = '你的名字是:%s,你的年龄是:%d,你的性别是:%s' % (name,age,sex)
    print(msg)

      2.元组

    name = input('你的名字:')
    age = int(input('你的年龄:'))
    sex = input('你的性别:')
    msg = '你的名字是:%(name1)s,你的年龄是:%(age1)d,你的性别是:%(sex1)s' % {'name1':name,'age1':age,'sex1':sex}
    print(msg)
    3.格式化输出中,若只是单纯的想表达百分后,需要再加一个百分号进行转义。
    name = input('你的名字:')
    age = int(input('你的年龄:'))
    msg = '你的名字是:%(name1)s,你的年龄是:%(age1)d,你的性别100%%是男' % {'name1':name,'age1':age}
    print(msg)


    三。运算符。
    1.
    ==:比较两边的值是否相等
    = :赋值运算符
    !=:不等于
    +=:i += 1 等于 i = i + 1
    -+:i -= 1 等于 i = i - 1

    2.前后条件为比较运算:and ,not ,or
    优先级:()> not > and > or

    3.前后条件为数值比较:
    x or y if x is True,return x

    补充:0 对应的bool值为False ,其他数值都是对应True.

    四,编码。
    1.ASCII码,8位1字节,包含英文,数字,特殊字符。
    2.unicode万国码,最初版,1字符,16位,两字节表示。改版后1字符32位,四字节表示。
    3.utf-8,包含中文,英文,数字,特殊字符。。。
    4.gbk国标,只包含中文,英文,数字,特殊字符。
    5.换算:8bit == 1bytes
    1024bytes == 1kb
    1024kb == 1mb
    1024mb == 1gb
    1024gb == 1tb

  • 相关阅读:
    LeetCode 1245. Tree Diameter
    LeetCode 1152. Analyze User Website Visit Pattern
    LeetCode 1223. Dice Roll Simulation
    LeetCode 912. Sort an Array
    LeetCode 993. Cousins in Binary Tree
    LeetCode 1047. Remove All Adjacent Duplicates In String
    LeetCode 390. Elimination Game
    LeetCode 1209. Remove All Adjacent Duplicates in String II
    LeetCode 797. All Paths From Source to Target
    LeetCode 1029. Two City Scheduling
  • 原文地址:https://www.cnblogs.com/lijinming110/p/9415907.html
Copyright © 2011-2022 走看看