while循环语句、格式化输出、常用运算符、字符编码

1.while循环

while 空格 条件 冒号

缩进 循环体

num=1
while num<11:
    print(num)
    num=num+1

变量都是先执行等号右边的,然后执行等号左边的。

break 终止循环,break以下代码都不执行

while 3<4:
    print(1)
    break
    print(2)
print(3)

输出结果:1 3

num=0
while True:
    if num>50:
        break
    print(num)
    num=num+1

输出结果:0到50

continue 临时见底,跳出本次循环,继续下次循环

num=1
while num<11:
    if num==9:
        num=num+1
        continue
    print(num)
    num=num+1

输出结果:1 2 3 4 5 6 7 8 10

num=0
while True:
    if num>9:
        break
    num=num+1
    if num==9:
        continue
    print(num)

输出结果:1 2 3 4 5 6 7 8 10

while else

age=int(input("请输入年龄:"))
while age>18:
    if 18<=age<=22:
        print("你可以嗨了")
        break
    elif age>22:
        print('你可以结婚了')
        break
else:
    print('你不能去网吧')

条件语句可以控制while循环

2.格式化输出

%s 字符串类型,%d 、%i 数字类型

name=input("姓名:")
age=input("年龄:")
msg='姓名:%s,年龄:%d'%(name,int(age))
print(msg)

python36以上版本可以使用下面语句

name=input("姓名:")
age=input("年龄:")
msg=f'姓名:{name},年龄:{age}'
print(msg)

%% 转义

num=input('>>>')
s='目前学习进度:%s%%'%num
print(s)

结果:

>>>80

目前学习进度:80%

3.常用运算符

  1. 算术运算

    运算符 描述
    +
    -
    *
    /
    % 取模(取商的余数)
    **
    // 取整(取商的整数)
  2. 比较运算

    运算符 描述
    == 等于
    != 不等于
    > 大于
    < 小于
    >= 大于等于
    <= 小于等于
  3. 赋值运算

    运算符 描述
    = 简单赋值
    += c+=a等效于c=c+a
    -= c-=a等效于c=c-a
    *= c*=a等效于c=c*a
    /= c/=a等效于c=c/a
    %= c%=a等效于c=c%a
    **= c**=a等效于c=c**a
    //= c//a等效于c=c//a
  4. 逻辑运算

    运算符 描述
    and 布尔“与”
    or 布尔“或”
    not 布尔“非”

4.字符编码

  1. ASCII码 美国

    1个字节 8位

  2. GBK码 国标

    汉字 2个字节

    英文 1个字节

  3. Unicode 万国码

    2个字节

    4个字节

  4. utf-8 可变编程

    美国 1个字节

    欧洲 2个字节

    亚洲 3个字节

  5. 单位转化

    8bit=1byte

    1024byte=1KB

    1024KB=1MB

    1024MB=1GB

    1024GB=1TB

原文地址:https://www.cnblogs.com/lav3nder/p/11123408.html