while & 字符串 & 运算符 & 初识编码

while—死循环

While:
while 条件:
	循环体
while True:
	print("卡路里")
	print("好运来")
print(2)    #该行代码永远不会被执行,因为前面是死循环

运行中的循环程序不能点击“X”关闭,要先点击停止再点击关闭按钮

print(bool(0))

数字中非0的都是True

# 正序输出25-57
count = 25
while count <= 57:
  print(count)
  count = count + 1

# 倒叙输出57-25
count = 57
while count >= 25:
  print(count)
  count = count -1
break
while True:        # 需注意,该地方True首字母要大写
	print(123)
	print(234)
	break          # 终止当前循环,break下方的代码不会被执行
	print("不会被执行")
print("不会被执行")   # 由于上面的循环不会终止,所以这行循环体外的代码也不会被执行
while False:        # 当False的时候,不执行循环体
	print(123)
	print(234)
	break          
	print("不会被执行")
print("会被执行")   # 循环未执行,所以能走到这一步
continue-------形成一个闭环
while True:
    print(123)
    print(234)
    continue
    #continue 伪装成循环体中的最后一行代码(跳出当前循环继续下次循环)
    #continue 下面的代码不会执行
    print(345)
print(1111)
While True,循环体后面的代码行不会被执行
false,循环体后面的代码行会被执行
死循环
含break 终止循环,循环体后面的代码会被执行 终止循环
含continue 终止循环,循环体后面的代码不会被执行,但循环体会一直执行 终止本次循环
while else
# while esle
while True:
    print(111)
    break
else:
    print(222)

总结

打断循环的方式:

  1. 自己修改条件
  2. break

break —— 打破当前循环(终止当前循环)

continue —— 伪装成循环体中最后一条代码,跳出本次循环,继续下次循环

while else —— while条件成立的时候就不执行了,条件不成立的就是就执行 else

字符串

字符串格式化的方法

%是占位符,占的是字符串的位置

%d是占数字类型的位置

%S:%是占位,S是占什么类型的数据
%% :转译,表示普通的百分号

f(“字符串”),py3.6以上才能用

按照位置顺序传参数,占位和补位必须要一一对应

a = "name:"
b = "age:"
c = "job:"
name = input("name")
age = input("age")
job = input("job")
print(a + name + "
" + b + age + "
" + c+ job + "
")
# 使用格式化字符串
T = '''    #T是变量,它的值是个字符串
name: %s
age: %s
job: %s
'''
name = input("name")
age = int(input("age"))
job = input("job")
print(T%(name,age,job))   #输出T的值,
# num = input('学习进度:')
# s11 = "大哥黑的学习进度为:%s %%"
# print(s11%(num))

# s = f"今天下雨了{input('>>>')}"
# print(s)

# s11 = "大哥黑的学习进度为:%s"
# print(s11%("不错"))

# s = f"{1}{2}{3}"
# print(s)

运算符

算术运算符

+加

-减

*乘

/除 python2获取的是整数,python3获取的是浮点数

//整除也叫地板除

**幂

%模(取余)print(5%2) 运行结果为1

比较运算符

>, <, ==, =!, >=, =<

赋值运算符

= 赋值
+= 自加      a+=1---->a=a+1
-= 自减      a-=1---->a=a-1
*= 自乘
/= 
//=
**/
%=

逻辑运算符

and 与
or 或
not 非
# and 都为真的时候取and后边的值(and:真取右,假取左)
# and 都为假的时候取and前面的值
# and 一真一假取假的(同真才是真,一假即假)
print(3 and 5 and 9 and 0 and False)
print(5 and False and 9 and 0)
print(1 and 2 and 5 and 9 and 6)

# or 都为真的时候取or前边的值(or:真取左,假取右)
# or 都为假的时候取or后面的值
# or 一真一假取真的(同假才是假,一真即真)
print(1 or 0)
print(1 or 2)
print(0 or False)

# () > not > and > or
# 从左向右执行
# print(9 and 1 or not False and 8 or 0 and 7 and False)

成员运算符(in & not in)
# s = "alexdsb"
# if "sb" not in s:
#     print(True)
# else:
#     print(False)

编码初识

编码集

ASCII (美)不支持中文

GBK (国标)英文8位,中文16位

unicode (万国码)英文16位2字节,中文32位4个字节

utf-8(可变长的编码)英文8位1字节,欧洲文16位3个字节,亚洲24位2个字节

Linux utf-8

Mac utf-8

Windows  GBK

单位转换:
1字节 = 8位
1Bytes = 8 bit

1024Bytes = 1KB

1024KB = 1MB

1024MB = 1GB

1024GB = 1TB #TB已经够用了

1024TB = 1PB

1024PB = 1EB

原文地址:https://www.cnblogs.com/yangduoduo/p/11142204.html