while 循环,运算符,字符串的格式化

1.while 关键字 (死循环)

while 条件:
     循环体
    
    条件:只要条件是 Ture就可以循环.  

while 空格 条件 冒号
缩进 循环体

while else
while 空格 条件 冒号
缩进 循环体
else 冒号
缩进 结果

条件都会转化为布尔值,只有 Ture 或者 false 其中包含如果是数字的话,除了 0 是 false 其他都是 Ture.
只要是字符串,除了啥也不填""是 false 其他都是 Ture.

具体程序

while True:
    print("我")
    print("是")
    print("一")
    print("颗")
    print("小")
    print("小")
    print("的")
    print("石")
    print("头")
  1. 数字是"1" ,可以执行
while 1:
    print("我")
    print("是")
    print("一")
    print("颗")
    print("小")
    print("小")
    print("的")
    print("石")
    print("头")
  1. 数字是 0 ,不可以执行
while 0:
    print("我")
    print("是")
    print("一")
    print("颗")
    print("小")
    print("小")
    print("的")
    print("石")
    print("头")
# 程序不执行
  1. 随便的字符串,可以执行,就算是空格也能够执行
s = "A"
while s:
    print("我")
    print("是")
    print("一")
    print("颗")
    print("小")
    print("小")
    print("的")
    print("石")
    print("头")
  1. 如果什么都不写,程序不执行
s = ""
while s:
    print("我")
    print("是")
    print("一")
    print("颗")
    print("小")
    print("小")
    print("的")
    print("石")
    print("头")
# 程序不执行
  • while 中几种常见的形式

第一种:下面的代码中,在输出 1 之后,while 循环一直被执行,进入死循环,后面的 print(2),一直不执行

print(1)
while 1:
    print("我")
    print("是")
    print("一")
    print("颗")
    print("小")
    print("小")
    print("的")
    print("石")
    print("头")
print(2)
1
我
是
一
颗
小
小
的
石
头
我
是

第二种: 要是想要输出 2 只能是条件为假的时候

falg = "suibian"
while falg:
    print(1)
print(2)


当条件为真时,就执行 1111111 一直下去
若是下面的 false 那就输出 2

falg = ""
while falg:
    print(1)
print(2)

练习

输出 1 2 3 4 5的正确排序

count = 1
while count <= 5:
    print(count)
    count = count + 1

将上面题目,用倒叙的手法

count = 5
while count >= 1:
    print(count)
    count = count - 1

2.break , continue

定义: 终止当前循环,break 下面的 代码不会进行执行(1 只是被当成 Ture的条件)

while 1:
    print(123)
    print(456)
    break
    print(789)
print(111)
    
    
    #123
     456
     111

continue: 直接伪装成循环体中的最后一行代码 continue下面的代码不会执行(跳出当前循环,继续下次循环,比如跑步,突然 continue 出现在半路跑道,那么就要从头开始继续跑半截 再半截 以此循环)

while 1:
    print(123)
    print(456)
    continue
    print(789)

#   
123
456
123
456
123
456

一直循环

**break:打破当前循环,终止当前循环. **

continue 跳出当前准备循环,继续下次循环

相同之处 : 他们以下的代码都不执行

字符串的格式化

我们在遇到制作名片的问题时,一般的过程如下

换行

a = "--------ofo---------"
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 + "
" )

但是这样的过程过于繁琐,于是有了下面的字符串格式化

格式化的优点:能够对多个子字符串自由排列 组合 转换格式

s = '''--------ofo-------
name:%s
age:%s
job:%s
-------end------'''
name = input("name")
age = input("age")
job = input("job")
print(s%(name,age,job))

#
namezh
ages
jobx
--------ofo-------
name:zh
age:s
job:x
-------end------

这里对于我来说有些难理解

# num = input("学习进度:")
# s11 = "黑哥的学习进度:%s %%"
# print(s11%('234'))
s = f"今天下雨了{input('>>>')}"
print(s)
s11 = "大哥黑的学习进度为:%s"
 print(s11%("不错"))
s = f"{1}{2}{3}"
 print(s)
# %s 是占的字符串类型的位置

# %d 是占的数字类型的位置

# %% 转换成普通的%号

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

while else

while True:
    print(E)
else:
    print(F)

while True:
    print(A)
    break
print(B)

运算符

算数运算符

/ (python2 中获取的值是整数 , python3 中获取的是浮点数)

// 整除—地板除

print(5//2)

% 模 取余

**幂(次方)

比较运算符

<

>

== 等于

!= 不等于

>= 大于等于

<= 小于等于

赋值运算符

= 赋值

+= 自加

a = 10  
a += 1  (相当于 a = a + 1)
print(a) 

-= 自减

*= 自乘

逻辑运算符

and (与/和) or (或) not (非)

and : 都为真的时候取 and 后面的值

​ 都为假的时候取 and 前面的值

​ 一真一假取假

print(3 and 4)
print(0 and False)
print(0 and 4)
print(3 and 9 and 5 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 后面的值

​ 一真一假取真

print(1 or 0)

print(1 or 2)

print(0 or False)
print(1 or 9 or 4 or 0 or 9)

#1

**级别: () > not > and > or **

从左往右执行 : (如果你愿意一层一层的剥开我的心~~~)

print(9 and 1 or not False and 8 or 0 and 7 and False)

# 1

成员运算符

in 存在

not in 不存在

s = "alexdsb"
if "sb" not in s:
     print(True)
else:
     print(False)

总结

    运算顺序:() > not > and > or 从左向右执行
    算数运算符 : + - * / // ** %
    比较运算符: > < >= <= == !=
    赋值运算符: = += -= *= /= //= **= %=
    逻辑运算符: and or not () > not > and > or
    成员运算符: in not in

编码初始

理解

今 0101
天 0110
晚 0010
上 0001
去 1001
便 1000
利 0100
店 1111

00000101  00000110  0010000110011001

linux -- utf-8
mac -- utf-8
windows -- gbk

四种(重要)

ascii (老美)不支持中文
gbk    (国标) 英文 8位  中文16位
unicode (万国码)英文16 位 中文 32位
utf-8   (可变长的编码) 英文8位 欧洲文 16位 亚洲24位

单位转换

1字节 = 8位
1Bytes = 8bit ***
1024byte = 1KB
1024KB = 1MB
1024MB = 1GB
1024GB = 1TB
1024TB = 1PB
1024PB = 1EB
1024EB = 1ZB
1024ZB = 1YB
1024YB = 1NB
1024NB = 1DB
原文地址:https://www.cnblogs.com/hualibokeyuan/p/11140087.html