while循环,格式化输出,运算符及编码初识

一.while循环

1.基本循环(死循环)

while 条件:
    循环体

2.使用while计数

count = 0               # 数字里面非零的都为True
while True:
    count = count + 1
    print(count)

3.控制while循环的次数

count = 0
while count < 100:
    count = count + 1
    print(count)
    
# 打断循环的方式
1.自己修改条件
2.使用break关键字

4.break关键字

break--终止当前循环,下方的代码不会执行.

num = 1
while num < 6:
    print(num)
    num = num + 1
    break
    print("end")

5.continue关键字

continue--临时当作循环体中的最后一行代码(跳出当次循环继续下次循环),下面的代码不会执行.

num = 1
while num < 6:
    print(num)
    num = num + 1
    countinue
    print("end")
    
# break和continue的相同之处:他们以下的代码都不会被执行

6.while else

# 循环一
while True:
    if 3 > 2:
        print("你好")
        break
else:
    print("不好")
    
# 循环二
while True:
    if 3 > 2:
        print("你好")
print("不好")

二.格式化输出

字符串格式化要按照位置顺序传递,占位和补位必须要一一对应.

%---占位
%s---是占的字符串类型的位置
%d(%i)---是占的数字类型的位置
%%---转换成普通的%



a = "------------- info -------------"
b = "name:"
c = "age:"
d = "job:"
e = "-------------- end -------------"
name = input("name")
age = input("age")
job = input("job")
print(a + "
" + b + name + "
" + c + age + "
"+ d + job + "
" +e)

# %d 数字类型
num = input('学习进度:')
s11 = "大哥黑的学习进度为:%s %%"
print(s11%(num)

# %s 字符串类型
s = """ ------------- info -------------
name:%s
age:%s
job:%s
-------------- end -------------
"""
name = input("name")
age = int(input("age"))
job = input("job")
print(s%(name,age,job))



# 其他方式      
s = f"今天下雨了{input('>>>')}"
print(s)
      
s = f"{1}{2}{3}"
print(s)     

三.运算符

1.算数运算符

假设 a= 10, b = 20

运算符 描述 实例
+ a+b #30
- a-b #-10
* a*b # 200
/ b/a #2
% 取模(求余数) b%a #0
** a**b #10000000000000000
// 整除(地板除) 20//10 #2.0

2.比较运算符

运算符 描述
== 等于
!= 不等于
> 大于
< 小于
>= 大于等于
<= 小于等于

3.赋值运算符

运算符 描述
= 简单的赋值运算符
+= 加法赋值运算符
-= 减法赋值运算符
*= 乘法赋值运算符
/= 除法赋值运算符
%= 取模赋值运算符
**= 幂赋值运算符
//= 取整除赋值运算符

4.逻辑运算符

运算符 描述
and 与/和
or
not
逻辑运算的顺序排列:从左往右开始执行
    ()  >  not  >  and   > or
               and             or             一真一假
 都为真:      取后面的          取前面的           取假的
 都为假:      取前面的          去后面的           取真的
    
not True:  False
not False:  True

5.成员运算符

in     --  存在
not in --  不存在

四.初识编码(编码集)

1.ASCII(美国)

不支持中文

2.GBK(国标)

英文--8位1字节

中文--16位2字节

3.Unicode(万国码)

英文--16位2字节

中文--32位4字节

4.Utf-8

英文--8位1字节

欧洲文字--16位2字节

亚洲--24位3字节

5.单位转化

#1字节=8位(1byte=8bit)

8bit = 1byte
1024byte = 1KB
1024KB = 1MB
1024MB = 1GB
1024GB = 1TB
1024TB = 1PB
1024TB = 1EB
1024EB = 1ZB
1024ZB = 1YB
1024YB = 1NB
1024NB = 1DB
原文地址:https://www.cnblogs.com/tutougold/p/11141620.html